| 1 | ########################################################################## | ||
| 2 | package Games::PangZero::Guy; | ||
| 3 | ########################################################################## | ||
| 4 | |||
| 5 | @ISA = qw(Games::PangZero::GameObject); | ||
| 6 | use vars qw(%Guys $GuyId); | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my ($class, $player) = @_; | ||
| 10 | my $self = Games::PangZero::GameObject->new(); | ||
| 11 | my $number = $player->{number}; | ||
| 12 | %{$self} = ( %{$self}, | ||
| 13 | 'player' => $player, | ||
| 14 | 'number' => $number, | ||
| 15 | 'x' => $player->{startX}, | ||
| 16 | 'y' => $Games::PangZero::ScreenHeight - 64, | ||
| 17 | 'w' => 64, | ||
| 18 | 'h' => 64, | ||
| 19 | 'collisionw' => '28', | ||
| 20 | 'collisionh' => '48', | ||
| 21 | 'delay' => 0, | ||
| 22 | 'speedY' => 0, | ||
| 23 | 'speedX' => 0, | ||
| 24 | 'dir' => $number % 2, | ||
| 25 | 'state' => 'idle', | ||
| 26 | 'killed' => 0, | ||
| 27 | 'harpoons' => 0, | ||
| 28 | 'invincible' => 0, | ||
| 29 | 'surface' => $player->{guySurface}, | ||
| 30 | 'whiteSurface' => $player->{whiteGuySurface}, | ||
| 31 | 'weapon' => 'Harpoon', | ||
| 32 | 'bonusDelay' => 0, | ||
| 33 | 'id' => ++$GuyId, | ||
| 34 | ); | ||
| 35 | bless $self, $class; | ||
| 36 | $self->SetupCollisions(); | ||
| 37 | $self->CalculateAnimPhases(); | ||
| 38 | $Guys{$self->{id}} = $self; | ||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub Delete { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | $self->SUPER::Delete; | ||
| 46 | delete $Guys{$self->{id}}; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub CalculateAnimPhases { | ||
| 50 | my $self = shift; | ||
| 51 | |||
| 52 | $self->{animPhases} = $self->{player}->{guySurface}->w / 128, | ||
| 53 | } | ||
| 54 | |||
| 55 | sub DemoMode { | ||
| 56 | my ($self) = shift; | ||
| 57 | $self->{state} = 'demo'; | ||
| 58 | $self->{dir} = 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub Fire { | ||
| 62 | my ($self) = @_; | ||
| 63 | |||
| 64 | if ($self->{harpoons} < $Games::PangZero::DifficultyLevel->{harpoons}) { | ||
| 65 | ++$self->{harpoons}; | ||
| 66 | eval("unshift \@Games::PangZero::GameObjects, (Games::PangZero::$self->{weapon}::Create(\$self));"); | ||
| 67 | $self->{state} = 'shoot'; | ||
| 68 | $self->{delay} = 7; | ||
| 69 | Games::PangZero::Music::PlaySound('shoot'); | ||
| 70 | return 1; | ||
| 71 | } | ||
| 72 | return 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub AdvanceWhileFlying { | ||
| 76 | my $self = shift; | ||
| 77 | |||
| 78 | $self->{speedY} += $Games::PangZero::Ball::Gravity * 2; | ||
| 79 | $self->{y} += $self->{speedY}; | ||
| 80 | $self->{x} += $self->{dir} > 0 ? 1 : -1; | ||
| 81 | if ($self->{x} < -16) { | ||
| 82 | $self->{x} = 0; | ||
| 83 | $self->{dir} = 1; | ||
| 84 | } | ||
| 85 | if ($self->{x} > $Games::PangZero::ScreenWidth - $self->{w} + 16) { | ||
| 86 | $self->{x} = $Games::PangZero::ScreenWidth - $self->{w}; $self->{dir} = 0; | ||
| 87 | } | ||
| 88 | if ($self->{y} >= $Games::PangZero::ScreenHeight - $self->{h}) { | ||
| 89 | $self->{state} = 'idle'; | ||
| 90 | $self->{y} = $Games::PangZero::ScreenHeight - $self->{h}; | ||
| 91 | $self->{speedX} = $self->{dir} ? 1 : -1; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub Advance { | ||
| 96 | my ($self) = @_; | ||
| 97 | my ($slippery, $keys); | ||
| 98 | |||
| 99 | $slippery = $Games::PangZero::Slippery ? 0.0625 : 0; | ||
| 100 | |||
| 101 | return if $self->{killed}; | ||
| 102 | return if $self->{state} eq 'demo'; | ||
| 103 | --$self->{invincible}; | ||
| 104 | |||
| 105 | if ($self->{bonusDelay} > 0) { | ||
| 106 | --$self->{bonusDelay}; | ||
| 107 | $self->{weapon} = 'Harpoon' if $self->{bonusDelay} <= 0; | ||
| 108 | } | ||
| 109 | |||
| 110 | if ($self->{state} eq 'fly') { | ||
| 111 | $self->AdvanceWhileFlying(); | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | if ($self->{delay} > 0) { | ||
| 116 | --$self->{delay}; | ||
| 117 | $keys = [ 0, 0, 0 ]; | ||
| 118 | } else { | ||
| 119 | $keys = $self->{player}->{keys}; | ||
| 120 | } | ||
| 121 | |||
| 122 | $self->{speedX} = 0 unless $slippery; | ||
| 123 | $self->{state} = 'idle'; | ||
| 124 | |||
| 125 | if ( $Games::PangZero::Events{$keys->[2]} ) { | ||
| 126 | return if $self->Fire(); | ||
| 127 | } | ||
| 128 | if ( $Games::PangZero::Keys{$keys->[0]} ) { | ||
| 129 | if ($slippery) { | ||
| 130 | $self->{speedX} -= $slippery * 2 if $self->{speedX} > -3; | ||
| 131 | } else { | ||
| 132 | $self->{speedX} = -3; | ||
| 133 | } | ||
| 134 | $self->{dir} = 0; | ||
| 135 | $self->{state} = 'walk'; | ||
| 136 | } elsif ( $Games::PangZero::Keys{$keys->[1]} ) { | ||
| 137 | if ($slippery) { | ||
| 138 | $self->{speedX} += $slippery * 2 if $self->{speedX} < 3; | ||
| 139 | } else { | ||
| 140 | $self->{speedX} = 3; | ||
| 141 | } | ||
| 142 | $self->{dir} = 1; | ||
| 143 | $self->{state} = 'walk'; | ||
| 144 | } else { | ||
| 145 | if ($slippery) { | ||
| 146 | $self->{speedX} += $slippery if $self->{speedX} < 0; | ||
| 147 | $self->{speedX} -= $slippery if $self->{speedX} > 0; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | $self->{x} += $self->{speedX}; | ||
| 151 | |||
| 152 | if ($self->{x} < -16) { | ||
| 153 | $self->{x} = -16; $self->{speedX} = 0; | ||
| 154 | } | ||
| 155 | if ($self->{x} > $Games::PangZero::ScreenWidth - $self->{w} + 16) { | ||
| 156 | $self->{x} = $Games::PangZero::ScreenWidth - $self->{w} + 16; $self->{speedX} = 0; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | sub Draw { | ||
| 161 | my ($self) = @_; | ||
| 162 | my ($surface, $srcrect, $srcx, $srcy, $srcw, $srch); | ||
| 163 | |||
| 164 | return if ($self->{killed}); | ||
| 165 | $surface = $self->{surface}; | ||
| 166 | $surface = $self->{whiteSurface} if $self->{invincible} > 0 and (int($self->{invincible} / 2) % 3 == 0); | ||
| 167 | |||
| 168 | $srcw = $srch = 64; | ||
| 169 | if ($self->{state} eq 'idle') { | ||
| 170 | $srcx = $self->{dir} * 128; | ||
| 171 | $srcy = 64; | ||
| 172 | } elsif ($self->{state} eq 'walk') { | ||
| 173 | $srcx = $self->{dir} * $self->{animPhases} * 64 + (int($self->{x} / 50) % $self->{animPhases}) * 64; | ||
| 174 | $srcy = 0; | ||
| 175 | } elsif ($self->{state} eq 'demo') { | ||
| 176 | $srcx = $self->{dir} * $self->{animPhases} * 64 + (int($Games::PangZero::Game->{anim} / 16) % $self->{animPhases}) * 64; | ||
| 177 | $srcy = 0; | ||
| 178 | } elsif ($self->{state} eq 'shoot') { | ||
| 179 | $srcx = $self->{dir} * 128 + 64; | ||
| 180 | $srcx -= 64 if ($self->{delay} <= 1); | ||
| 181 | $srcy = 64; | ||
| 182 | } elsif ($self->{state} eq 'fly') { | ||
| 183 | $srcx = ($self->{dir} > 0 ? 0 : 64); | ||
| 184 | $srcy = 128; | ||
| 185 | } | ||
| 186 | $srcrect = SDL::Rect->new($srcx, $srcy, $srcw, $srch ); | ||
| 187 | $self->TransferRect(); | ||
| 188 | SDL::Video::blit_surface($surface, $srcrect, $Games::PangZero::App, $self->{rect}); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub Kill { | ||
| 192 | my ($self) = @_; | ||
| 193 | |||
| 194 | return if $Games::PangZero::Cheat; | ||
| 195 | return if $self->{invincible} > 0; | ||
| 196 | $self->{justkilled} = 1; | ||
| 197 | $Games::PangZero::GameEvents{'kill'} = 1; | ||
| 198 | print "player killed\n" if $ENV{PANGZERO_TEST}; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub Earthquake { | ||
| 202 | my ($self, $amplitude) = @_; | ||
| 203 | |||
| 204 | return if $self->{state} eq 'fly'; | ||
| 205 | $self->{speedY} = -($amplitude->[0]); | ||
| 206 | $self->{dir} = $amplitude->[1] > $self->{x} ? 0 : 1; | ||
| 207 | $self->{state} = 'fly'; | ||
| 208 | $self->{y} -= 3; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub DeleteHarpoons { | ||
| 212 | my ($self) = @_; | ||
| 213 | my (@gameObjects, $harpoon); | ||
| 214 | |||
| 215 | @gameObjects = @Games::PangZero::GameObjects; | ||
| 216 | foreach $harpoon (@gameObjects) { | ||
| 217 | $harpoon->Delete if ($harpoon->{guy} and $harpoon->{guy} eq $self); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | sub GiveScore { | ||
| 222 | my ($self, $score) = @_; | ||
| 223 | |||
| 224 | my $player = $self->{player}; | ||
| 225 | $player->{score} += $score; | ||
| 226 | if ($player->{score} >= $player->{scoreforbonuslife}) { | ||
| 227 | ++$player->{lives}; | ||
| 228 | $player->{scoreforbonuslife} += 200000; | ||
| 229 | Games::PangZero::PlaySound('bonuslife'); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | 1; |