| 1 | ########################################################################## | ||
| 2 | package Games::PangZero::Ball; | ||
| 3 | ########################################################################## | ||
| 4 | |||
| 5 | use Games::PangZero::Globals; | ||
| 6 | |||
| 7 | @ISA = qw(Games::PangZero::GameObject); | ||
| 8 | $Gravity = 0.05; | ||
| 9 | $MagicBallRect = SDL::Rect->new(80, 0, 16, 15); | ||
| 10 | |||
| 11 | for (my $i = 0; $i <= $#Games::PangZero::BallDesc; ++$i) { | ||
| 12 | my $desc = $Games::PangZero::BallDesc[$i]; | ||
| 13 | $desc->{speedY} = 0 unless $desc->{speedY}; | ||
| 14 | $desc->{bounceY} = $desc->{speedY} * $desc->{speedY} / $Games::PangZero::Ball::Gravity / 2 unless $desc->{bounceY}; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub Create { | ||
| 18 | my ($description, $x, $y, $dir) = @_; | ||
| 19 | my ($retval); | ||
| 20 | |||
| 21 | my $code = sprintf('$retval = Games::PangZero::%s->new(@_);', $description->{class}); | ||
| 22 | eval $code; die $@ if $@; | ||
| 23 | return $retval; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub Spawn { | ||
| 27 | my ($description, $x, $dir, $hasBonus) = @_; | ||
| 28 | my ($retval); | ||
| 29 | |||
| 30 | $x = $Games::PangZero::Game->Rand( $Games::PangZero::ScreenWidth - $description->{width} ) if $x < 0; | ||
| 31 | $retval = Games::PangZero::Ball::Create( $description, $x, -$description->{height} - $Games::PangZero::ScreenMargin, $dir ); | ||
| 32 | $retval->GiveMagic() if $retval->{w} > 32; | ||
| 33 | $retval->GiveBonus() if $hasBonus; | ||
| 34 | |||
| 35 | $retval->{spawning} = 1; | ||
| 36 | my $surfaceName = 'dark' . $description->{surface}; | ||
| 37 | $retval->{surface} = $Games::PangZero::BallSurfaces{$surfaceName}; | ||
| 38 | die "No surface: $surfaceName" unless $retval->{surface}; | ||
| 39 | return $retval; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my ($class, $description, $x, $y, $dir) = @_; | ||
| 44 | my $self = Games::PangZero::GameObject->new(); | ||
| 45 | %{$self} = ( %{$self}, | ||
| 46 | 'x' => $x, | ||
| 47 | 'y' => $y, | ||
| 48 | 'w' => $description->{width}, | ||
| 49 | 'h' => $description->{height}, | ||
| 50 | 'surface' => $Games::PangZero::BallSurfaces{$description->{surface}}, | ||
| 51 | 'hexa' => $description->{hexa} ? 1 : 0, | ||
| 52 | 'desc' => $description, | ||
| 53 | 'hasmagic' => 0, # true if one of the ball's descendants is magic | ||
| 54 | 'ismagic' => 0, # true if the ball IS magic | ||
| 55 | 'spawning' => 0, | ||
| 56 | ); | ||
| 57 | $self->{speedX} = $dir > 0 ? 1.3 : -1.3; | ||
| 58 | $self->SetupCollisions(); | ||
| 59 | bless $self, $class; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub NormalAdvance { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | $self->{speedY} += $Games::PangZero::Ball::Gravity * $Games::PangZero::GameSpeed unless ($self->{hexa}); | ||
| 66 | $self->{x} += $self->{speedX} * $Games::PangZero::GameSpeed; | ||
| 67 | $self->{y} += $self->{speedY} * $Games::PangZero::GameSpeed; | ||
| 68 | if ($self->{y} > $Games::PangZero::ScreenHeight - $self->{h}) { | ||
| 69 | $self->{y} = $Games::PangZero::ScreenHeight - $self->{h}; | ||
| 70 | if ($self->{hexa}) { | ||
| 71 | $self->{speedY} = -abs($self->{speedY}); | ||
| 72 | } else { | ||
| 73 | $self->{speedY} = -$self->{desc}->{speedY}; | ||
| 74 | } | ||
| 75 | $self->Bounce; | ||
| 76 | } | ||
| 77 | if ($self->{y} < 0) { | ||
| 78 | $self->{y} = 0; | ||
| 79 | $self->{speedY} = abs($self->{speedY}); | ||
| 80 | } | ||
| 81 | if ($self->{x} < 0) { | ||
| 82 | $self->{x} = 0; | ||
| 83 | $self->{speedX} = abs( $self->{speedX} ); | ||
| 84 | } | ||
| 85 | if ($self->{x} > $Games::PangZero::ScreenWidth - $self->{w}) { | ||
| 86 | $self->{x} = $Games::PangZero::ScreenWidth - $self->{w}; | ||
| 87 | $self->{speedX} = -abs( $self->{speedX} ); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | sub SpawningAdvance { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | $self->{y} += 0.32; | ||
| 95 | if ($self->{y} >= 0) { | ||
| 96 | $self->{spawning} = 0; | ||
| 97 | $self->{surface} = $Games::PangZero::BallSurfaces{$self->{desc}->{surface}}, | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | sub Advance { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | unless( $Games::PangZero::GamePause > 0 ) { | ||
| 105 | if ($self->{spawning}) { | ||
| 106 | $self->SpawningAdvance(); | ||
| 107 | } else { | ||
| 108 | $self->NormalAdvance(); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | $self->CheckCollisions() unless $Games::PangZero::Game->{nocollision} or $self->{spawning}; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub Bounce { | ||
| 116 | } | ||
| 117 | |||
| 118 | sub CheckCollisions { | ||
| 119 | my $self = shift; | ||
| 120 | |||
| 121 | foreach my $harpoon (values %Games::PangZero::Harpoon::Harpoons) { | ||
| 122 | if ($self->Collisions($harpoon)) { | ||
| 123 | $self->Pop($harpoon->{guy}, $harpoon->{popEffect}); | ||
| 124 | $harpoon->Delete(); | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | foreach my $guy (values %Games::PangZero::Guy::Guys) { | ||
| 129 | if ($Games::PangZero::GamePause <= 0 and $self->Collisions($guy)) { | ||
| 130 | $guy->Kill(); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | sub Draw { | ||
| 136 | my ($self) = @_; | ||
| 137 | |||
| 138 | return if $Games::PangZero::GamePause > 0 and $Games::PangZero::GamePause < 100 and (int($Games::PangZero::GamePause / 3) % 4) < 2; | ||
| 139 | |||
| 140 | $self->TransferRect(); | ||
| 141 | if ($self->{ismagic} and int($Games::PangZero::Game->{anim}/4) % 2) { | ||
| 142 | SDL::Video::blit_surface($Games::PangZero::BallSurfaces{ball4}, $Games::PangZero::Ball::MagicBallRect, $Games::PangZero::App, $self->{rect} ); | ||
| 143 | } else { | ||
| 144 | SDL::Video::blit_surface($self->{surface}, $self->{desc}->{rect}, $Games::PangZero::App, $self->{rect} ); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | sub Collisions { | ||
| 149 | my ($self, $other) = @_; | ||
| 150 | |||
| 151 | # Bounding box detection | ||
| 152 | |||
| 153 | return unless $self->SUPER::Collisions($other); | ||
| 154 | |||
| 155 | # Circle vs rectangle collision | ||
| 156 | |||
| 157 | my ($centerX, $centerY, $boxAxisX, $boxAxisY, $boxCenterX, $boxCenterY, $distSquare, $distance); | ||
| 158 | $boxAxisX = ($other->{collisionw} or $other->{w}) / 2; | ||
| 159 | $boxAxisY = ($other->{collisionh} or $other->{h}) / 2; | ||
| 160 | $boxCenterX = $other->{x} + $other->{w} / 2; | ||
| 161 | $boxCenterY = $other->{y} + $other->{h} / 2; | ||
| 162 | $centerX = $self->{x} + $self->{w} / 2; | ||
| 163 | $centerY = $self->{y} + $self->{h} / 2; | ||
| 164 | |||
| 165 | # Translate coordinates to the box center | ||
| 166 | $centerX -= $boxCenterX; | ||
| 167 | $centerY -= $boxCenterY; | ||
| 168 | $centerX = abs($centerX); | ||
| 169 | $centerY = abs($centerY); | ||
| 170 | |||
| 171 | if ($centerX < $boxAxisX) { | ||
| 172 | return 1 if $centerY < $boxAxisY + $self->{h} / 2; | ||
| 173 | return 0; | ||
| 174 | } | ||
| 175 | if ($centerY < $boxAxisY) { | ||
| 176 | return 2 if $centerX < $boxAxisX + $self->{w} / 2; | ||
| 177 | return 0; | ||
| 178 | } | ||
| 179 | $distSquare = ($centerX-$boxAxisX) * ($centerX-$boxAxisX); | ||
| 180 | $distSquare += ($centerY-$boxAxisY) * ($centerY-$boxAxisY); | ||
| 181 | return 3 if $distSquare < $self->{h} * $self->{h} / 4; | ||
| 182 | |||
| 183 | return 0; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub Pop { | ||
| 187 | my ($self, $guy, $popEffect) = @_; | ||
| 188 | |||
| 189 | Carp::confess "no $popEffect" unless defined $popEffect; | ||
| 190 | $Games::PangZero::GameEvents{'pop'} = 1; | ||
| 191 | $Games::PangZero::GameEvents{'magic'} = 1 if ($self->{ismagic}); | ||
| 192 | $guy->GiveScore($self->{desc}->{score}) if $guy; | ||
| 193 | $self->Delete(); | ||
| 194 | |||
| 195 | goto skipChildren if ($popEffect eq 'meltdown'); | ||
| 196 | |||
| 197 | if ($self->{desc}->{nextgen}) { | ||
| 198 | die caller unless $self->{desc}->{nextgen}->{class}; | ||
| 199 | my @children = $self->SpawnChildren(); | ||
| 200 | if (scalar @children) { | ||
| 201 | $self->AdjustChildren(@children); | ||
| 202 | if ($popEffect eq 'HalfCutter') { | ||
| 203 | push @Games::PangZero::GameObjects, ($self->{speedX} > 0 ? $children[1] : $children[0]); | ||
| 204 | } else { | ||
| 205 | push @Games::PangZero::GameObjects, (@children); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | if ($self->{bonus} and $popEffect ne 'superkill') { | ||
| 210 | push @Games::PangZero::GameObjects, Games::PangZero::BonusDrop->new($self); | ||
| 211 | } | ||
| 212 | $Games::PangZero::Game->OnBallPopped(); | ||
| 213 | |||
| 214 | skipChildren: | ||
| 215 | push @Games::PangZero::GameObjects, Games::PangZero::Pop->new($self->{x}, $self->{y}, $self->{desc}->{popIndex}, $self->{surface}); | ||
| 216 | } | ||
| 217 | |||
| 218 | sub SpawnChildren { | ||
| 219 | my $self = shift; | ||
| 220 | my $nextgen = $self->{desc}->{nextgen}; | ||
| 221 | die caller unless $nextgen->{class}; | ||
| 222 | my $x = $self->{x} + $self->{w} / 2; | ||
| 223 | my $y = $self->{y} + ( $self->{h} - $nextgen->{height} ) / 2; | ||
| 224 | my $child1 = Create($nextgen, $self->{x}, $y, 0); | ||
| 225 | my $child2 = Create($nextgen, $self->{x} + $self->{w} - $nextgen->{width}, $y, 1); | ||
| 226 | |||
| 227 | return ($child1, $child2); | ||
| 228 | } | ||
| 229 | |||
| 230 | sub AdjustChildren { | ||
| 231 | my ($self, @children) = @_; | ||
| 232 | my ($nextgen, $speedY, $altitude); | ||
| 233 | |||
| 234 | if ($self->{hasmagic}) { | ||
| 235 | $children[0]->GiveMagic(); | ||
| 236 | } | ||
| 237 | |||
| 238 | $nextgen = $self->{desc}->{nextgen}; | ||
| 239 | $altitude = $Games::PangZero::ScreenHeight - $self->{y} - $self->{h}; | ||
| 240 | $speedY = 1.8; | ||
| 241 | unless ($altitude > $nextgen->{bounceY}) { | ||
| 242 | $speedY = 1.8; | ||
| 243 | while ($speedY * $speedY / $Games::PangZero::Ball::Gravity / 2 + $altitude < $nextgen->{bounceY}) { | ||
| 244 | ++$speedY; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | foreach (@children) { | ||
| 248 | $_->{speedY} = -$speedY; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | sub GiveMagic { | ||
| 253 | my $self = shift; | ||
| 254 | |||
| 255 | $self->{hasmagic} = 1; | ||
| 256 | $self->{ismagic} = 1 unless $self->{desc}->{nextgen}; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub GiveBonus { | ||
| 260 | my $self = shift; | ||
| 261 | |||
| 262 | $self->{bonus} = 1; | ||
| 263 | } | ||
| 264 | |||
| 265 | 1; |