Class: Stalker

Inherits:
Enemy
  • Object
show all
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

#hp

Instance Method Summary collapse

Methods included from GamespacePersistence

#correct_coords, #in_bounds

Methods inherited from Enemy

descendants

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, options = {})
  super(options)

  @image = Image['./media/stalker.png']

  @gamespace = gamespace
  @hp = HP
end

Instance Method Details

#take_damageObject

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

#updateObject

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