Class: Stalker
- Includes:
- GamespacePersistence
- Defined in:
- lib/prkwars/stalker.rb
Overview
Class representing the Stalker enemy unit which is mobile but unable to shoot and endlessly moves towards the player unit.
Constant Summary collapse
- VELOCITY =
2- HP =
1
Instance Attribute Summary
Attributes inherited from Enemy
Instance Method Summary collapse
-
#initialize(gamespace, options = {}) ⇒ Stalker
constructor
Initialization of the Stalker unit requires the gamespace for collision checks, sets the sprite and HP of the unit to an instance variable.
-
#take_damage ⇒ Object
A common method to enemies, reducing their HP by one.
-
#update ⇒ Object
Every time a Stalker unit gets updated, they move in a constant speed towards the player.
Methods included from GamespacePersistence
Methods inherited from Enemy
Constructor Details
#initialize(gamespace, options = {}) ⇒ Stalker
Initialization of the Stalker unit requires the gamespace for collision checks, sets the sprite and HP of the unit to an instance variable.
18 19 20 21 22 23 24 25 |
# File 'lib/prkwars/stalker.rb', line 18 def initialize(gamespace, = {}) super() @image = Image['./media/stalker.png'] @gamespace = gamespace @hp = HP end |
Instance Method Details
#take_damage ⇒ Object
A common method to enemies, reducing their HP by one.
48 49 50 |
# File 'lib/prkwars/stalker.rb', line 48 def take_damage @hp -= 1 end |
#update ⇒ Object
Every time a Stalker unit gets updated, they move in a constant speed towards the player. Their sprite also gets rotated.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/prkwars/stalker.rb', line 31 def update dist_x = @gamespace.player.x - @x dist_y = @gamespace.player.y - @y rot_to = Math.atan2(dist_y, dist_x) / Math::PI * 180 + 90 dist = Math.sqrt(dist_x * dist_x + dist_y * dist_y) ticks = dist / VELOCITY @velocity_x = dist_x / ticks @velocity_y = dist_y / ticks @angle = rot_to end |