| 1 | ########################################################################## | ||
| 2 | package Games::PangZero::PanicGame; | ||
| 3 | ########################################################################## | ||
| 4 | |||
| 5 | @ISA = qw(Games::PangZero::PlayableGameBase); | ||
| 6 | |||
| 7 | sub new { | ||
| 8 | my ($class) = @_; | ||
| 9 | my $self = Games::PangZero::PlayableGameBase->new(); | ||
| 10 | %{$self} = (%{$self}, | ||
| 11 | 'spawndelay' => 0, | ||
| 12 | 'superballdelay' => 0, | ||
| 13 | 'leveladvance' => 0, | ||
| 14 | 'panicleveldesc' => undef, | ||
| 15 | ); | ||
| 16 | bless $self, $class; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub ResetGame { | ||
| 20 | my $self = shift; | ||
| 21 | $self->SUPER::ResetGame(); | ||
| 22 | $self->{spawndelay} = 0; | ||
| 23 | $self->{superballdelay} = 2500 + $self->Rand(2500); # 25sec - 50sec | ||
| 24 | $self->{superballdelay} *= $Games::PangZero::DifficultyLevel->{superball}; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub SetGameSpeed { | ||
| 28 | my ($self) = @_; | ||
| 29 | |||
| 30 | $Games::PangZero::GameSpeed = $self->{leveldesc}->{gamespeed} * 0.8 * $Games::PangZero::DifficultyLevel->{speed}; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub SetGameLevel { | ||
| 34 | my ($self, $level) = @_; | ||
| 35 | my ($levelIndex); | ||
| 36 | |||
| 37 | $levelIndex = ($level > $#Games::PangZero::PanicLevels) ? $#Games::PangZero::PanicLevels : $level; | ||
| 38 | $self->{leveldesc} = $Games::PangZero::PanicLevels[$levelIndex]; | ||
| 39 | die unless $self->{leveldesc}; | ||
| 40 | $self->{leveladvance} = 0; | ||
| 41 | $self->SUPER::SetGameLevel($level); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub AdvanceGame { | ||
| 45 | my ($self) = @_; | ||
| 46 | |||
| 47 | $self->SpawnBalls() if $Games::PangZero::GamePause <= 0; | ||
| 48 | $self->SUPER::AdvanceGame(); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub SpawnBalls { | ||
| 52 | my $self = shift; | ||
| 53 | my ($randmax, $rnd, $ballName, $balldesc, $deathBallCount, $earthquakeBallCount, $hasBonus); | ||
| 54 | |||
| 55 | --$self->{superballdelay}; | ||
| 56 | if ($self->{superballdelay} <= 0) { | ||
| 57 | push @Games::PangZero::GameObjects, ( | ||
| 58 | Games::PangZero::Ball::Spawn($Games::PangZero::BallDesc{sprintf('super%d', $self->Rand(2))}, -1, $self->Rand(40) < 20 ? 0 : 1) ); | ||
| 59 | $self->{superballdelay} = (2500 + $self->Rand(2000)) * $Games::PangZero::DifficultyLevel->{superball}; # 25sec - 45sec | ||
| 60 | } | ||
| 61 | |||
| 62 | --$self->{spawndelay}; | ||
| 63 | return if $self->{spawndelay} > 0; | ||
| 64 | $deathBallCount = $earthquakeBallCount = -1; | ||
| 65 | $randmax = 10000; | ||
| 66 | while ($self->{spawndelay} <= 0) { | ||
| 67 | if ($Games::PangZero::DifficultyLevel->{name} eq 'Miki') { | ||
| 68 | $balldesc = $Games::PangZero::BallDesc{'death'}; | ||
| 69 | last; | ||
| 70 | } | ||
| 71 | $rnd = int($self->Rand($randmax)); | ||
| 72 | $randmax = 0; | ||
| 73 | |||
| 74 | # We try to find the balldesc that falls at $rnd | ||
| 75 | my $ballRoulette = $self->{leveldesc}->{balls}; | ||
| 76 | for (my $i = 0; $i < scalar @{$ballRoulette}; $i+=2) { | ||
| 77 | my $rouletteWeight = $ballRoulette->[$i+1]; | ||
| 78 | $randmax += $rouletteWeight; | ||
| 79 | $rnd -= $rouletteWeight; | ||
| 80 | if ($rnd < 0) { | ||
| 81 | $ballName = $ballRoulette->[$i]; | ||
| 82 | last; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | next unless ($ballName); # $rnd too large.. We'll have a better $randmax this time! | ||
| 86 | |||
| 87 | ($balldesc) = $Games::PangZero::BallDesc{$ballName}; | ||
| 88 | if ($balldesc->{class} eq 'DeathBall') { | ||
| 89 | next unless $Games::PangZero::DeathBallsEnabled; | ||
| 90 | $deathBallCount = Games::PangZero::DeathBall::CountDeathBalls() if $deathBallCount < 0; # Lazy counting | ||
| 91 | next if $deathBallCount >= 2; | ||
| 92 | } | ||
| 93 | if ($balldesc->{class} eq 'EarthquakeBall') { | ||
| 94 | next unless $Games::PangZero::EarthquakeBallsEnabled; | ||
| 95 | $earthquakeBallCount = Games::PangZero::EarthquakeBall::CountEarthquakeBalls if $earthquakeBallCount < 0; | ||
| 96 | next if $earthquakeBallCount >= 1; | ||
| 97 | } | ||
| 98 | if ($balldesc->{class} eq 'WaterBall') { | ||
| 99 | next unless $Games::PangZero::WaterBallsEnabled; | ||
| 100 | } | ||
| 101 | if ($balldesc->{class} eq 'SeekerBall') { | ||
| 102 | next unless $Games::PangZero::SeekerBallsEnabled; | ||
| 103 | } | ||
| 104 | last if $balldesc; | ||
| 105 | } | ||
| 106 | |||
| 107 | $hasBonus = 1 if ($balldesc->{width} >= 32) and ($self->Rand(1) < $Games::PangZero::DifficultyLevel->{bonusprobability}); | ||
| 108 | |||
| 109 | push @Games::PangZero::GameObjects, ( Games::PangZero::Ball::Spawn($balldesc, -1, $self->Rand(40) < 20 ? 0 : 1, $hasBonus) ); | ||
| 110 | $self->{spawndelay} = $self->{leveldesc}->{spawndelay} * $balldesc->{spawndelay} * 50; | ||
| 111 | $self->{spawndelay} /= ($Games::PangZero::NumGuys + 1) / 2; | ||
| 112 | $self->{spawndelay} *= $Games::PangZero::DifficultyLevel->{spawnmultiplier}; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub OnBallPopped { | ||
| 116 | my $self = shift; | ||
| 117 | |||
| 118 | ++$self->{leveladvance}; | ||
| 119 | if ($self->{leveladvance} >= 18) { | ||
| 120 | Games::PangZero::Music::PlaySound('level'); | ||
| 121 | $self->SetGameLevel($self->{level}+1); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | sub DrawLevelIndicator { | ||
| 126 | my ($self, $x, $y) = @_; | ||
| 127 | |||
| 128 | $self->{levelIndicatorRect} = SDL::Rect->new($x, $y, 140, $self->{scoreBoardHeight}) unless $self->{levelIndicatorRect}; | ||
| 129 | #SDL::Video::fill_rect( $Games::PangZero::App, $self->{levelIndicatorRect}, SDL::Color->new(0,0,0) ); | ||
| 130 | SDL::Video::blit_surface($Games::PangZero::LevelIndicatorSurface2, SDL::Rect->new($x, $y, $Games::PangZero::LevelIndicatorSurface2->w, $Games::PangZero::LevelIndicatorSurface2->h), | ||
| 131 | $Games::PangZero::App, SDL::Rect->new($x, $y, 0, 0)); | ||
| 132 | SDL::Video::blit_surface($Games::PangZero::LevelIndicatorSurface, SDL::Rect->new(0, 0, 130 * $self->{leveladvance} / 17, 30), $Games::PangZero::App, SDL::Rect->new($x, $y, 0, 0)); | ||
| 133 | SDLx::SFont::print_text( $Games::PangZero::App, $x + 25, $y + 3, 'Level ' . ($self->{level}+1) ); | ||
| 134 | |||
| 135 | SDLx::SFont::print_text( $Games::PangZero::App, $x, $y + 40, sprintf('spd: %d/%d', $Games::PangZero::GameSpeed * 100, $self->{leveldesc}->{spawndelay}) ) if $self->{scoreBoardHeight} >= 64; | ||
| 136 | |||
| 137 | } | ||
| 138 | |||
| 139 | 1; |