Class: Turret
Overview
Class representing the Turret enemy unit which is stationary and endlessly shoots bullets in the direction of the player.
Constant Summary collapse
- SHOOT_GAP =
40- VELOCITY =
3- HP =
3
Instance Attribute Summary
Attributes inherited from Enemy
Instance Method Summary collapse
-
#initialize(gamespace, options = {}) ⇒ Turret
constructor
Initializing a
Turretbuilds a newChingu::Animationwith the turret sprites. -
#take_damage ⇒ Object
A common
Enemymethod - reduces the unit’s HP by one and changes the sprite. -
#update ⇒ Object
Updating a
Turretonly consists of shooting a bullet if the framecounter got far enough.
Methods inherited from Enemy
Constructor Details
#initialize(gamespace, options = {}) ⇒ Turret
Initializing a Turret builds a new Chingu::Animation with the turret sprites. Each time a Turret gets hit, it changes its sprite to better display the amount of HP. Also sets the gamespace instance variable, HP and a shooting timer.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/prkwars/turret.rb', line 21 def initialize(gamespace, = {}) super() @animation = Chingu::Animation.new(file: 'media/turrets.png', size: 32) @gamespace = gamespace @hp = HP @shoot_timer = 0 @image = @animation[@hp - 1] end |
Instance Method Details
#take_damage ⇒ Object
A common Enemy method - reduces the unit’s HP by one and changes the sprite.
47 48 49 50 |
# File 'lib/prkwars/turret.rb', line 47 def take_damage @hp -= 1 @image = @animation[@hp - 1] end |
#update ⇒ Object
Updating a Turret only consists of shooting a bullet if the framecounter got far enough.
36 37 38 39 40 41 42 |
# File 'lib/prkwars/turret.rb', line 36 def update @shoot_timer += 1 return unless @shoot_timer == SHOOT_GAP shoot_bullet @shoot_timer = 0 end |