Class: Termfront::Projectile
- Inherits:
-
Object
- Object
- Termfront::Projectile
- Defined in:
- lib/termfront/projectile.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
Returns the value of attribute type.
-
#vx ⇒ Object
Returns the value of attribute vx.
-
#vy ⇒ Object
Returns the value of attribute vy.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #hit_player?(px, py, radius = Config::PROJ_RADIUS) ⇒ Boolean
- #hit_wall?(map) ⇒ Boolean
-
#initialize(x:, y:, vx:, vy:, type:) ⇒ Projectile
constructor
A new instance of Projectile.
- #update(dt) ⇒ Object
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
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/termfront/projectile.rb', line 5 def type @type end |
#vx ⇒ Object
Returns the value of attribute vx.
5 6 7 |
# File 'lib/termfront/projectile.rb', line 5 def vx @vx end |
#vy ⇒ Object
Returns the value of attribute vy.
5 6 7 |
# File 'lib/termfront/projectile.rb', line 5 def vy @vy end |
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/termfront/projectile.rb', line 5 def x @x end |
#y ⇒ Object
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
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
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 |