Module: Nimo::Behavior::Deflector

Defined in:
lib/nimo/behavior/deflector.rb

Instance Method Summary collapse

Instance Method Details

#deflect(projectile) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nimo/behavior/deflector.rb', line 5

def deflect(projectile)
  @collision_timeout ||= 0
  @collision_timeout -= 1 if @collision_timeout > 0
  return unless @collision_timeout.zero? && collide?(projectile)

  case intersection(projectile).collistion_side_for(self)
    when :top
      projectile.velocity.y = -projectile.velocity.y.abs
    when :bottom
      projectile.velocity.y = projectile.velocity.y.abs
    when :left
      projectile.velocity.x = -projectile.velocity.x.abs
    when :right
      projectile.velocity.x = projectile.velocity.x.abs
  end

  projectile.velocity.adjust(deflection_modifier(projectile))
  @deflect_action.call(projectile) if @deflect_action
  @collision_timeout = 5
end

#when_deflect(&action) ⇒ Object



26
27
28
29
# File 'lib/nimo/behavior/deflector.rb', line 26

def when_deflect(&action)
  @deflect_action = action
  self
end