Class: Turret

Inherits:
Enemy
  • Object
show all
Defined in:
lib/prkwars/turret.rb

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

#hp

Instance Method Summary collapse

Methods inherited from Enemy

descendants

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

  @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_damageObject

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

#updateObject

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