Skip to content

Commit

Permalink
ALien movement
Browse files Browse the repository at this point in the history
The aliens zigzag down.
  • Loading branch information
dvberkel committed Feb 26, 2012
1 parent 9622542 commit 54f6489
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/game.rb
Expand Up @@ -4,17 +4,19 @@

class Game < SpaceInvaders
def start
@strategy = ZigZag.new(Vector.new(3,0), -400, 400, 5)
(-4..4).each do |ix|
(0..2).each do |iy|
x,y = 80 * ix, 500 - 50 * iy
addAlien(Alien.new({:location => Vector.new(x,y)}))
addAlien(Alien.new({:location => Vector.new(x,y), :moveStrategy => @strategy}))
end
end
@gun = Gun.new({:moveStrategy => EventedMoveStrategy.new})
addGun(@gun)
end

def tick
@strategy.determineDirection
move
collisionDetection
end
Expand All @@ -23,3 +25,36 @@ def signal(signal)
@gun.notify(signal)
end
end

class ZigZag < MovementStrategy
def initialize(displacement, minimum, maximum, drop)
@displacement = displacement
@minimum = minimum
@maximum = maximum
@drop = Vector.new(0,-drop)
@direction = 1
@shouldSwitch = false
@shouldDrop = false
end

def move(location)
if (@direction > 0 and location.x > @maximum) or (@direction < 0 and location.x < @minimum)
@shouldSwitch = true
end
if @shouldDrop
location = location.displaceBy(@drop)
end
location.displaceBy(@displacement.times(@direction))
end

def determineDirection
if @shouldDrop
@shouldDrop = false
end
if @shouldSwitch
@direction *= -1
@shouldSwitch = false
@shouldDrop = true
end
end
end

0 comments on commit 54f6489

Please sign in to comment.