Class: Termfront::Projectile

Inherits:
Object
  • Object
show all
Defined in:
lib/termfront/projectile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, vx:, vy:, type:) ⇒ Projectile

Returns a new instance of Projectile.



7
8
9
10
11
12
13
# File 'lib/termfront/projectile.rb', line 7

def initialize(x:, y:, vx:, vy:, type:)
  @x = x
  @y = y
  @vx = vx
  @vy = vy
  @type = type
end

Instance Attribute Details

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/termfront/projectile.rb', line 5

def type
  @type
end

#vxObject

Returns the value of attribute vx.



5
6
7
# File 'lib/termfront/projectile.rb', line 5

def vx
  @vx
end

#vyObject

Returns the value of attribute vy.



5
6
7
# File 'lib/termfront/projectile.rb', line 5

def vy
  @vy
end

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/termfront/projectile.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/termfront/projectile.rb', line 5

def y
  @y
end

Class Method Details

.update_all(projectiles, map, player) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/termfront/projectile.rb', line 28

def self.update_all(projectiles, map, player)
  projectiles.reject! do |p|
    p.update(0) # dt is applied in the caller
    if p.hit_wall?(map)
      true
    elsif p.hit_player?(player.x, player.y)
      enemy_klass = Enemy::Base.registry[p.type]
      dmg = enemy_klass ? enemy_klass.allocate.send(:damage) : 10
      player.apply_damage(dmg)
      true
    else
      false
    end
  end
end

Instance Method Details

#hit_player?(px, py, radius = Config::PROJ_RADIUS) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/termfront/projectile.rb', line 24

def hit_player?(px, py, radius = Config::PROJ_RADIUS)
  (@x - px).abs < radius && (@y - py).abs < radius
end

#hit_wall?(map) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/termfront/projectile.rb', line 20

def hit_wall?(map)
  map.wall_at?(@x, @y)
end

#update(dt) ⇒ Object



15
16
17
18
# File 'lib/termfront/projectile.rb', line 15

def update(dt)
  @x += @vx * dt
  @y += @vy * dt
end