Module: Chingu::Traits::Velocity
- Defined in:
- lib/chingu/traits/velocity.rb
Overview
A chingu trait providing velocity and acceleration logic.
Instance Attribute Summary collapse
-
#acceleration_x ⇒ Object
Returns the value of attribute acceleration_x.
-
#acceleration_y ⇒ Object
Returns the value of attribute acceleration_y.
-
#max_velocity ⇒ Object
Returns the value of attribute max_velocity.
-
#velocity_x ⇒ Object
Returns the value of attribute velocity_x.
-
#velocity_y ⇒ Object
Returns the value of attribute velocity_y.
Instance Method Summary collapse
-
#setup_trait(options) ⇒ Object
def self.initialize_trait(options) @velocity_options = => false.merge(options) puts “Velocity#initialize” if @velocity_options super end.
- #stop ⇒ Object
-
#update_trait ⇒ Object
Modifies X & Y of parent.
Instance Attribute Details
#acceleration_x ⇒ Object
Returns the value of attribute acceleration_x.
29 30 31 |
# File 'lib/chingu/traits/velocity.rb', line 29 def acceleration_x @acceleration_x end |
#acceleration_y ⇒ Object
Returns the value of attribute acceleration_y.
29 30 31 |
# File 'lib/chingu/traits/velocity.rb', line 29 def acceleration_y @acceleration_y end |
#max_velocity ⇒ Object
Returns the value of attribute max_velocity.
29 30 31 |
# File 'lib/chingu/traits/velocity.rb', line 29 def max_velocity @max_velocity end |
#velocity_x ⇒ Object
Returns the value of attribute velocity_x.
29 30 31 |
# File 'lib/chingu/traits/velocity.rb', line 29 def velocity_x @velocity_x end |
#velocity_y ⇒ Object
Returns the value of attribute velocity_y.
29 30 31 |
# File 'lib/chingu/traits/velocity.rb', line 29 def velocity_y @velocity_y end |
Instance Method Details
#setup_trait(options) ⇒ Object
def self.initialize_trait(options)
@velocity_options = {:debug => false}.merge()
puts "Velocity#initialize" if @velocity_options[:debug]
super
end
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/chingu/traits/velocity.rb', line 37 def setup_trait() @velocity_options = {:debug => false}.merge() puts "Velocity#setup" if @velocity_options[:debug] @velocity_x = [:velocity_x] || 0 @velocity_y = [:velocity_y] || 0 @acceleration_x = [:acceleration_x] || 0 @acceleration_y = [:acceleration_y] || 0 @max_velocity = [:max_velocity] || 1000 super end |
#stop ⇒ Object
62 63 64 |
# File 'lib/chingu/traits/velocity.rb', line 62 def stop @acceleration_y = @acceleration_x = @velocity_y = @acceleration_y = 0 end |
#update_trait ⇒ Object
Modifies X & Y of parent
52 53 54 55 56 57 58 59 60 |
# File 'lib/chingu/traits/velocity.rb', line 52 def update_trait puts "Velocity#update" if @velocity_options[:debug] @velocity_y += @acceleration_y if (@velocity_y + @acceleration_y).abs < @max_velocity @velocity_x += @acceleration_x if (@velocity_x + @acceleration_x).abs < @max_velocity @y += @velocity_y @x += @velocity_x super end |