| 1 | ########################################################################## | ||
| 2 | package Games::PangZero::PlayableGameBase; | ||
| 3 | ########################################################################## | ||
| 4 | |||
| 5 | @ISA = qw( Games::PangZero::GameBase ); | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class) = @_; | ||
| 12 | my $self = Games::PangZero::GameBase->new(); | ||
| 13 | %{$self} = (%{$self}, | ||
| 14 | 'playersalive' => 0, | ||
| 15 | 'level' => 0, | ||
| 16 | 'backgrounds' => [ qw( desert2.png l1.jpg l2.jpg l3.jpg l4.jpg l5.jpg l6.jpg l7.jpg l8.jpg l9.jpg )], | ||
| 17 | ); | ||
| 18 | bless $self, $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub ResetGame { | ||
| 22 | my $self = shift; | ||
| 23 | $self->SUPER::ResetGame(); | ||
| 24 | $self->{playersalive} = 0; | ||
| 25 | $Games::PangZero::GamePause = 0; | ||
| 26 | |||
| 27 | foreach my $player (@Games::PangZero::Players) { | ||
| 28 | last if $player->{number} >= $Games::PangZero::NumGuys; | ||
| 29 | $self->SpawnPlayer($player); | ||
| 30 | } | ||
| 31 | $self->SetGameLevel(0); | ||
| 32 | $self->LayoutScoreBoard(); | ||
| 33 | push @Games::PangZero::GameObjects, (Games::PangZero::FpsIndicator->new()); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub SetGameSpeed { | ||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | $Games::PangZero::GameSpeed = 0.8 * $Games::PangZero::DifficultyLevel->{speed}; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub SetGameLevel { | ||
| 43 | my ($self, $level) = @_; | ||
| 44 | |||
| 45 | $self->{level} = $level; | ||
| 46 | if (($level % 10) == 9) { | ||
| 47 | $self->SetBackground( int($level / 10) + 1 ); | ||
| 48 | } | ||
| 49 | $self->SetGameSpeed(); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub SpawnPlayer { | ||
| 53 | my ($self, $player) = @_; | ||
| 54 | $player->{score} = 0; | ||
| 55 | $player->{scoreforbonuslife} = 200000; | ||
| 56 | $player->{lives} = 2; | ||
| 57 | $player->{startX} = ($Games::PangZero::ScreenWidth - $Games::PangZero::NumGuys * 60) / 2 + 60 * ($player->{number}+0.5) - 32; | ||
| 58 | $player->{respawn} = -1; | ||
| 59 | my $guy = Games::PangZero::Guy->new($player); | ||
| 60 | push @Games::PangZero::GameObjects, ($guy); | ||
| 61 | ++$self->{playersalive}; | ||
| 62 | return $guy; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub AdvanceGameObjects { | ||
| 66 | my ($self) = @_; | ||
| 67 | |||
| 68 | $self->SUPER::AdvanceGameObjects(); | ||
| 69 | $self->RespawnPlayers(); | ||
| 70 | --$Games::PangZero::GamePause if $Games::PangZero::GamePause > 0; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub RespawnPlayers { | ||
| 74 | my $self = shift; | ||
| 75 | |||
| 76 | foreach my $player (@Games::PangZero::Players) { | ||
| 77 | last if $player->{number} >= $Games::PangZero::NumGuys; | ||
| 78 | if ($player->{respawn} > 0) { | ||
| 79 | --$player->{respawn}; | ||
| 80 | $player->{score} = int($player->{respawn} / 100) if $self->{playersalive}; | ||
| 81 | if ($player->{respawn} <= 0) { | ||
| 82 | my $guy = $self->SpawnPlayer($player); | ||
| 83 | $guy->{invincible} = 500; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub PlayerNextLife { | ||
| 90 | my ($self, $guy) = @_; | ||
| 91 | |||
| 92 | $guy->DeleteHarpoons; | ||
| 93 | if ($guy->{player}->{lives}--) { | ||
| 94 | $guy->{x} = $guy->{player}->{startX}; | ||
| 95 | $guy->{y} = $Games::PangZero::ScreenHeight - $guy->{h}; | ||
| 96 | $guy->{state} = 'idle'; | ||
| 97 | $guy->{speedY} = $guy->{speedX} = 0; | ||
| 98 | $guy->{invincible} = 500; # 0.5s | ||
| 99 | $guy->{killed} = 0; | ||
| 100 | $guy->{justkilled} = 0; | ||
| 101 | $self->{playerspawned} = 1; | ||
| 102 | } else { | ||
| 103 | # One player less | ||
| 104 | Games::PangZero::Highscore::AddHighScore($guy->{player}, $guy->{player}->{score}, $self->{level} + 1); | ||
| 105 | $guy->Delete(); | ||
| 106 | --$self->{playersalive}; | ||
| 107 | $guy->{player}->{respawn} = 6000; # 60s | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | sub PlayerDeathSequence { | ||
| 112 | my $self = shift; | ||
| 113 | my (@killedGuys, @deadGuys, $guy, $i); | ||
| 114 | |||
| 115 | $self->DrawGame(); | ||
| 116 | Games::PangZero::Music::PlaySound('death'); | ||
| 117 | Games::PangZero::Graphics::RenderBorder($Games::PangZero::WhiteBorderSurface, $Games::PangZero::App); | ||
| 118 | $Games::PangZero::App->sync(); | ||
| 119 | $self->Delay(10); | ||
| 120 | Games::PangZero::Graphics::RenderBorder($Games::PangZero::RedBorderSurface, $Games::PangZero::App); | ||
| 121 | Games::PangZero::Graphics::RenderBorder($Games::PangZero::RedBorderSurface, $Games::PangZero::Background); | ||
| 122 | $Games::PangZero::App->sync(); | ||
| 123 | $self->Delay(90); | ||
| 124 | |||
| 125 | @killedGuys = grep { $_->{justkilled}; } @Games::PangZero::GameObjects; | ||
| 126 | foreach $guy (@killedGuys) { | ||
| 127 | $guy->Clear(); | ||
| 128 | $guy->{killed} = 1; | ||
| 129 | push @deadGuys, (Games::PangZero::DeadGuy->new($guy)); | ||
| 130 | } | ||
| 131 | push @Games::PangZero::GameObjects, (@deadGuys); | ||
| 132 | |||
| 133 | for ($i = 0; $i < 300; ++$i) { | ||
| 134 | Games::PangZero::HandleEvents(); | ||
| 135 | return if $self->{abortgame}; | ||
| 136 | my $advance = $self->CalculateAdvances(); | ||
| 137 | while ($advance--) { | ||
| 138 | foreach my $gameObject (@deadGuys) { | ||
| 139 | $gameObject->Advance(); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | $self->DrawGame(); | ||
| 143 | last if $deadGuys[0]->{deleted}; | ||
| 144 | } | ||
| 145 | |||
| 146 | foreach $guy (@killedGuys) { | ||
| 147 | $self->PlayerNextLife($guy); | ||
| 148 | } | ||
| 149 | |||
| 150 | Games::PangZero::Graphics::RenderBorder($Games::PangZero::BorderSurface, $Games::PangZero::App); | ||
| 151 | Games::PangZero::Graphics::RenderBorder($Games::PangZero::BorderSurface, $Games::PangZero::Background); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub SuperKill { | ||
| 155 | my ($self, $guy) = @_; | ||
| 156 | my @gameObjects = @Games::PangZero::GameObjects; | ||
| 157 | my $sound = 0; | ||
| 158 | foreach my $ball (@gameObjects) { | ||
| 159 | next unless $ball->isa("Games::PangZero::Ball"); | ||
| 160 | $ball->Pop($guy, 'superkill'); | ||
| 161 | $sound = 1; | ||
| 162 | } | ||
| 163 | Games::PangZero::Music::PlaySound('pop') if $sound; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub PopEveryBall { | ||
| 167 | my $self = shift; | ||
| 168 | my @guys = (); | ||
| 169 | my @gameObjects = @Games::PangZero::GameObjects; | ||
| 170 | foreach (@gameObjects) { | ||
| 171 | if ($_->isa('Games::PangZero::Ball')) { | ||
| 172 | $_->Pop(undef, 'meltdown'); | ||
| 173 | } elsif ('Games::PangZero::Guy' eq ref $_) { | ||
| 174 | push @guys, $_; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | return @guys; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub DeathballMeltdown { | ||
| 181 | my ($self) = @_; | ||
| 182 | my ($i, $allKilled, @guys, @killedGuys, @deadGuys); | ||
| 183 | $self->{nocollision} = 1; | ||
| 184 | my $meltdown = Games::PangZero::Meltdown->new(); | ||
| 185 | push @Games::PangZero::GameObjects, $meltdown; | ||
| 186 | |||
| 187 | for ($i = 0; $i < 300; ++$i) { | ||
| 188 | %Games::PangZero::Events = (); | ||
| 189 | Games::PangZero::HandleEvents(); | ||
| 190 | return if $self->{abortgame}; | ||
| 191 | my $advance = $self->CalculateAdvances(); | ||
| 192 | while ($advance--) { | ||
| 193 | # TODO REINSTATE THIS IN 1.1!!! $self->PreAdvanceAction(); # Hook for something special | ||
| 194 | $self->SUPER::AdvanceGameObjects(); | ||
| 195 | $Games::PangZero::GamePause = 0; | ||
| 196 | if ($meltdown->{bounce} and not $allKilled) { | ||
| 197 | $allKilled = 1; | ||
| 198 | @guys = $self->PopEveryBall(); | ||
| 199 | foreach (@guys) { | ||
| 200 | $_->{killed} = 1; | ||
| 201 | push @deadGuys, Games::PangZero::DeadGuy->new($_); | ||
| 202 | push @killedGuys, $_; | ||
| 203 | } | ||
| 204 | push @Games::PangZero::GameObjects, (@deadGuys); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | $self->DrawGame(); | ||
| 208 | } | ||
| 209 | |||
| 210 | foreach (@killedGuys) { | ||
| 211 | $self->PlayerNextLife($_); | ||
| 212 | } | ||
| 213 | |||
| 214 | $self->{nocollision} = 0; | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | ########################################################################## | ||
| 219 | # GAME DRAWING | ||
| 220 | ########################################################################## | ||
| 221 | |||
| 222 | sub DrawScoreBoard { | ||
| 223 | my ($self) = @_; | ||
| 224 | |||
| 225 | $self->DrawLevelIndicator( 10, $self->{scoreBoardTop} ); | ||
| 226 | for (my $i = 0; $i < $Games::PangZero::NumGuys; ++$i) { | ||
| 227 | $self->DrawScore( $Games::PangZero::Players[$i], $Games::PangZero::Players[$i]->{scoreX}, $Games::PangZero::Players[$i]->{scoreY} ); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | sub LayoutScoreBoard { | ||
| 232 | my ($self) = @_; | ||
| 233 | my $scoreBoardTop = $Games::PangZero::ScreenHeight + $Games::PangZero::ScreenMargin * 2 + 5; | ||
| 234 | my $scoreBoardHeight = $Games::PangZero::PhysicalScreenHeight - $scoreBoardTop; | ||
| 235 | my $rowHeight = 64; | ||
| 236 | my $leftMargin = 150; | ||
| 237 | my $rows = $Games::PangZero::NumGuys > 4 ? 2 : 1; | ||
| 238 | $rows = 1 if ($scoreBoardTop + $rows * $rowHeight > $Games::PangZero::PhysicalScreenHeight); | ||
| 239 | |||
| 240 | if ($scoreBoardTop + $rows * $rowHeight > $Games::PangZero::PhysicalScreenHeight) { | ||
| 241 | $rowHeight = 32; | ||
| 242 | $scoreBoardTop = $Games::PangZero::PhysicalScreenHeight - 32; | ||
| 243 | } | ||
| 244 | |||
| 245 | my $guysPerRow = int ($Games::PangZero::NumGuys / $rows + 0.5); | ||
| 246 | my $widthPerGuy = ($Games::PangZero::PhysicalScreenWidth - $leftMargin) / $guysPerRow; | ||
| 247 | |||
| 248 | for (my $i = 0; $i < $Games::PangZero::NumGuys; ++$i) { | ||
| 249 | $Games::PangZero::Players[$i]->{scoreX} = $leftMargin + ($i % $guysPerRow) * $widthPerGuy; | ||
| 250 | $Games::PangZero::Players[$i]->{scoreY} = $scoreBoardTop + int ($i / $guysPerRow) * $rowHeight; | ||
| 251 | $Games::PangZero::Players[$i]->{scoreRect} = | ||
| 252 | SDL::Rect->new($Games::PangZero::Players[$i]->{scoreX}, $Games::PangZero::Players[$i]->{scoreY}, 130, $rowHeight); | ||
| 253 | } | ||
| 254 | |||
| 255 | $self->{scoreBoardTop} = $scoreBoardTop; | ||
| 256 | $self->{scoreBoardHeight} = $scoreBoardHeight; | ||
| 257 | $self->{rowHeight} = $rowHeight; | ||
| 258 | |||
| 259 | } | ||
| 260 | |||
| 261 | sub DrawLevelIndicator { | ||
| 262 | my ($self, $x, $y) = @_; | ||
| 263 | $self->{levelIndicatorRect} = SDL::Rect->new($x, $y, 100, 32) unless $self->{levelIndicatorRect}; | ||
| 264 | SDL::Video::fill_rect($Games::PangZero::App, $self->{levelIndicatorRect}, SDL::Color->new(0, 0, 0) ); | ||
| 265 | SDLx::SFont::print_text( $Games::PangZero::App, $x, $y + 3, 'Level ' . ($self->{level}+1) ); | ||
| 266 | |||
| 267 | } | ||
| 268 | |||
| 269 | sub PrintNumber { | ||
| 270 | my ($self, $player, $x, $y, $number) = @_; | ||
| 271 | my $numberText = sprintf("%d", $number); | ||
| 272 | my $srcrect = SDL::Rect->new(0, 160, 16, 16); | ||
| 273 | my $dstrect = SDL::Rect->new($x, $y, 16, 16); | ||
| 274 | |||
| 275 | for (my $i = 0; $i < length($numberText); ++$i) { | ||
| 276 | $srcrect->x(320 + (ord(substr($numberText, $i)) - ord('0')) * 16); | ||
| 277 | $dstrect->x($x + $i * 16); | ||
| 278 | SDL::Video::blit_surface($player->{guySurface}, $srcrect, $Games::PangZero::App, $dstrect ); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | sub DrawScore { | ||
| 283 | my ($self, $player, $x, $y, $livesY) = @_; | ||
| 284 | |||
| 285 | #SDL::Video::fill_rect( $Games::PangZero::App, $player->{scoreRect}, SDL::Color->new(0, 0, 0)); | ||
| 286 | $self->PrintNumber( $player, $x, $y, $player->{score}); | ||
| 287 | |||
| 288 | $livesY = $self->{rowHeight} > 32 ? $y + 24 : $y + 16; | ||
| 289 | my $dstrect = SDL::Rect->new($x, $livesY, 32, 32); | ||
| 290 | my $srcrect = ($self->{rowHeight} <= 32) | ||
| 291 | ? SDL::Rect->new(320, 176, 16, 16) | ||
| 292 | : SDL::Rect->new(320, 128, 32, 32); | ||
| 293 | |||
| 294 | if ($player->{lives} > 3) { | ||
| 295 | SDL::Video::blit_surface($player->{guySurface}, $srcrect, $Games::PangZero::App, $dstrect ); | ||
| 296 | $self->PrintNumber( $player, $x + $srcrect->w() + 8, $livesY + ($srcrect->h() - 16 ) / 2, $player->{lives} ); | ||
| 297 | } else { | ||
| 298 | foreach my $i ( 0 .. $player->{lives}-1 ) { | ||
| 299 | $dstrect->x( $x + $i * ($srcrect->w() + 4) ); | ||
| 300 | SDL::Video::blit_surface($player->{guySurface}, $srcrect, $Games::PangZero::App, $dstrect ); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | sub PreAdvanceAction {} | ||
| 306 | |||
| 307 | sub AdvanceGame { | ||
| 308 | my $self = shift; | ||
| 309 | %Games::PangZero::GameEvents = (); | ||
| 310 | $self->PreAdvanceAction(); # Hook for something special | ||
| 311 | |||
| 312 | if ($self->{superKillCount} > 0) { | ||
| 313 | if (--$self->{superKillDelay} <= 0) { | ||
| 314 | --$self->{superKillCount}; | ||
| 315 | $self->{superKillDelay} = 50; | ||
| 316 | $self->SuperKill($self->{superKillGuy}); | ||
| 317 | } | ||
| 318 | $Games::PangZero::GamePause = 0; | ||
| 319 | } | ||
| 320 | |||
| 321 | $self->AdvanceGameObjects(); | ||
| 322 | if ($Games::PangZero::GameEvents{earthquake}) { | ||
| 323 | Games::PangZero::Music::PlaySound('quake'); | ||
| 324 | foreach my $guy (@Games::PangZero::GameObjects) { | ||
| 325 | $guy->Earthquake($Games::PangZero::GameEvents{earthquake}) if ref $guy eq 'Games::PangZero::Guy'; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | if ($Games::PangZero::GameEvents{'pop'}) { | ||
| 330 | Games::PangZero::Music::PlaySound('pop'); | ||
| 331 | } | ||
| 332 | |||
| 333 | if ($Games::PangZero::GameEvents{meltdown} and $Games::PangZero::DifficultyLevel->{name} ne 'Miki') { | ||
| 334 | $self->DeathballMeltdown(); | ||
| 335 | } elsif ($Games::PangZero::GameEvents{kill} ) { | ||
| 336 | $self->PlayerDeathSequence(); | ||
| 337 | return if $self->{playersalive} <= 0; | ||
| 338 | $Games::PangZero::GamePause = 200 if $Games::PangZero::GamePause < 200; | ||
| 339 | Games::PangZero::GamePause::Show(); | ||
| 340 | } elsif ($Games::PangZero::GameEvents{magic}) { | ||
| 341 | if ($Games::PangZero::GamePause < 200) { | ||
| 342 | $Games::PangZero::GamePause = 200; | ||
| 343 | Games::PangZero::Music::PlaySound('pause'); | ||
| 344 | Games::PangZero::GamePause::Show(); | ||
| 345 | } | ||
| 346 | } elsif ($Games::PangZero::GameEvents{superpause}) { | ||
| 347 | if ($Games::PangZero::GamePause < 800) { | ||
| 348 | $Games::PangZero::GamePause = 800; | ||
| 349 | Games::PangZero::Music::PlaySound('pause'); | ||
| 350 | Games::PangZero::GamePause::Show(); | ||
| 351 | } | ||
| 352 | } elsif ($Games::PangZero::GameEvents{superkill}) { | ||
| 353 | $self->{superKillCount} = 5; | ||
| 354 | $self->{superKillDelay} = 0; | ||
| 355 | $self->{superKillGuy} = $Games::PangZero::GameEvents{superkillguy}; | ||
| 356 | $self->{spawndelay} = 250; | ||
| 357 | $self->{superballdelay} += 1000; # 10 second penalty | ||
| 358 | my @gameObjects = @Games::PangZero::GameObjects; | ||
| 359 | foreach my $spawningBall (@gameObjects) { | ||
| 360 | $spawningBall->Delete if $spawningBall->{spawning}; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | sub Run { | ||
| 366 | my ($self) = shift; | ||
| 367 | |||
| 368 | $self->ResetGame(); | ||
| 369 | Games::PangZero::GameTimer::ResetTimer(); | ||
| 370 | |||
| 371 | $self->{superKillCount} = 0; | ||
| 372 | $self->{superKillDelay} = 0; | ||
| 373 | $self->{superKillGuy} = undef; | ||
| 374 | |||
| 375 | while (1) { | ||
| 376 | |||
| 377 | # Calculate advance (how many game updates to perform) | ||
| 378 | my $advance = $self->CalculateAdvances(); | ||
| 379 | |||
| 380 | # Advance the game | ||
| 381 | |||
| 382 | %Games::PangZero::Events = (); | ||
| 383 | Games::PangZero::HandleEvents(); | ||
| 384 | while ($advance--) { | ||
| 385 | return if $self->{abortgame}; | ||
| 386 | $self->AdvanceGame(); | ||
| 387 | } | ||
| 388 | |||
| 389 | if ($self->{playersalive} <= 0) { | ||
| 390 | my $gameoverSurface = SDL::Image::load("$Games::PangZero::DataDir/gameover.png"); | ||
| 391 | my @gameObjects = @Games::PangZero::GameObjects; | ||
| 392 | foreach (@gameObjects) { $_->Delete() if ('Games::PangZero::DeadGuy' eq ref $_); } | ||
| 393 | $self->DrawGame(); | ||
| 394 | SDL::Video::blit_surface($gameoverSurface, SDL::Rect->new(0, 0, $gameoverSurface->w, $gameoverSurface->h), | ||
| 395 | $Games::PangZero::App, SDL::Rect->new( | ||
| 396 | ($Games::PangZero::PhysicalScreenWidth - $gameoverSurface->w) / 2, $Games::PangZero::PhysicalScreenHeight / 2 - 100, | ||
| 397 | $gameoverSurface->w, $gameoverSurface->h)); | ||
| 398 | $Games::PangZero::App->sync(); | ||
| 399 | SDL::delay(1000); | ||
| 400 | for (my $i=0; $i < 20; ++$i) { | ||
| 401 | SDL::delay(100); | ||
| 402 | %Games::PangZero::Events = (); | ||
| 403 | Games::PangZero::HandleEvents(); | ||
| 404 | last if $self->{abortgame}; | ||
| 405 | last if %Games::PangZero::Events; | ||
| 406 | } | ||
| 407 | last; | ||
| 408 | } | ||
| 409 | $self->DrawGame(); | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | 1; |