Module: Chingu::Traits::Velocity
- Defined in:
- lib/chingu/traits/velocity.rb
Overview
A chingu trait providing velocity and acceleration logic. Adds parameters: velocity_x/y, acceleration_x/y and modifies self.x / self.y Also keeps previous_x and previous_y which is the x, y before modification. Can be useful for example collision detection
Defined Under Namespace
Modules: ClassMethods
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_x ⇒ Object
Returns the value of attribute max_velocity_x.
-
#max_velocity_y ⇒ Object
Returns the value of attribute max_velocity_y.
-
#previous_x ⇒ Object
readonly
Returns the value of attribute previous_x.
-
#previous_y ⇒ Object
readonly
Returns the value of attribute previous_y.
-
#velocity_x ⇒ Object
Returns the value of attribute velocity_x.
-
#velocity_y ⇒ Object
Returns the value of attribute velocity_y.
Instance Method Summary collapse
- #acceleration ⇒ Object
-
#acceleration=(value) ⇒ Object
Sets X and Y acceleration with one single call.
- #max_velocity ⇒ Object
-
#max_velocity=(value) ⇒ Object
Sets X and Y acceleration with one single call.
-
#move(x, y) ⇒ Object
Move game object.
-
#moved? ⇒ Boolean
Did game object move last tick.
- #setup_trait(options) ⇒ Object
-
#stop ⇒ Object
Setts velocity_x and velocity_y to 0, stopping the game object Note it doesn’t reset the acceleration!.
-
#stopped? ⇒ Boolean
Returns true if both velocity_x and velocity_y is 0.
-
#update_trait ⇒ Object
Modifies X & Y of parent.
- #velocity ⇒ Object
-
#velocity=(value) ⇒ Object
Sets X and Y velocity with one single call.
Instance Attribute Details
#acceleration_x ⇒ Object
Returns the value of attribute acceleration_x.
31 32 33 |
# File 'lib/chingu/traits/velocity.rb', line 31 def acceleration_x @acceleration_x end |
#acceleration_y ⇒ Object
Returns the value of attribute acceleration_y.
31 32 33 |
# File 'lib/chingu/traits/velocity.rb', line 31 def acceleration_y @acceleration_y end |
#max_velocity_x ⇒ Object
Returns the value of attribute max_velocity_x.
31 32 33 |
# File 'lib/chingu/traits/velocity.rb', line 31 def max_velocity_x @max_velocity_x end |
#max_velocity_y ⇒ Object
Returns the value of attribute max_velocity_y.
31 32 33 |
# File 'lib/chingu/traits/velocity.rb', line 31 def max_velocity_y @max_velocity_y end |
#previous_x ⇒ Object (readonly)
Returns the value of attribute previous_x.
32 33 34 |
# File 'lib/chingu/traits/velocity.rb', line 32 def previous_x @previous_x end |
#previous_y ⇒ Object (readonly)
Returns the value of attribute previous_y.
32 33 34 |
# File 'lib/chingu/traits/velocity.rb', line 32 def previous_y @previous_y end |
#velocity_x ⇒ Object
Returns the value of attribute velocity_x.
31 32 33 |
# File 'lib/chingu/traits/velocity.rb', line 31 def velocity_x @velocity_x end |
#velocity_y ⇒ Object
Returns the value of attribute velocity_y.
31 32 33 |
# File 'lib/chingu/traits/velocity.rb', line 31 def velocity_y @velocity_y end |
Instance Method Details
#acceleration ⇒ Object
82 |
# File 'lib/chingu/traits/velocity.rb', line 82 def acceleration; [@acceleration_x, @acceleration_y]; end |
#acceleration=(value) ⇒ Object
Sets X and Y acceleration with one single call. Takes an Array-argument with 2 values.
74 75 76 77 78 79 80 |
# File 'lib/chingu/traits/velocity.rb', line 74 def acceleration=(value) if value.is_a? Array @acceleration_x, @acceleration_y = value else @acceleration_x, @acceleration_y = value, value end end |
#max_velocity ⇒ Object
92 |
# File 'lib/chingu/traits/velocity.rb', line 92 def max_velocity; [@max_velocity_x, @max_velocity_y]; end |
#max_velocity=(value) ⇒ Object
Sets X and Y acceleration with one single call. Takes an Array-argument with 2 values.
87 88 89 90 |
# File 'lib/chingu/traits/velocity.rb', line 87 def max_velocity=(value) @max_velocity_x, @max_velocity_y = value && return if value.is_a? Array @max_velocity_x, @max_velocity_y = value, value end |
#move(x, y) ⇒ Object
Move game object
115 116 117 118 |
# File 'lib/chingu/traits/velocity.rb', line 115 def move(x, y) self.x += x self.y += y end |
#moved? ⇒ Boolean
Did game object move last tick
140 141 142 |
# File 'lib/chingu/traits/velocity.rb', line 140 def moved? self.x != @previous_x || self.y != @previous_y end |
#setup_trait(options) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/chingu/traits/velocity.rb', line 40 def setup_trait() @velocity_options = {:debug => false}.merge() @velocity_x = [:velocity_x] || 0 @velocity_y = [:velocity_y] || 0 self.velocity = [:velocity] if [:velocity] @acceleration_x = [:acceleration_x] || 0 @acceleration_y = [:acceleration_y] || 0 self.acceleration = [:acceleration] if [:acceleration] @max_velocity_x = [:max_velocity_x] || 1000 @max_velocity_y = [:max_velocity_y] || 1000 self.max_velocity = [:max_velocity] if [:max_velocity] super end |
#stop ⇒ Object
Setts velocity_x and velocity_y to 0, stopping the game object Note it doesn’t reset the acceleration!
124 125 126 127 128 |
# File 'lib/chingu/traits/velocity.rb', line 124 def stop # @acceleration_y = @acceleration_x = 0 @velocity_x = 0 @velocity_y = 0 end |
#stopped? ⇒ Boolean
Returns true if both velocity_x and velocity_y is 0
133 134 135 |
# File 'lib/chingu/traits/velocity.rb', line 133 def stopped? @velocity_x == 0 && @velocity_y == 0 end |
#update_trait ⇒ Object
Modifies X & Y of parent
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/chingu/traits/velocity.rb', line 97 def update_trait @velocity_x += @acceleration_x if (@velocity_x + @acceleration_x).abs < @max_velocity_x @velocity_y += @acceleration_y if (@velocity_y + @acceleration_y).abs < @max_velocity_y @previous_x = self.x @previous_y = self.y # # if option :apply is false, just calculate velocities, don't apply them to x/y # move(@velocity_x, @velocity_y) unless [:velocity][:apply] == false super end |
#velocity ⇒ Object
69 |
# File 'lib/chingu/traits/velocity.rb', line 69 def velocity; [@velocity_x, @velocity_y]; end |
#velocity=(value) ⇒ Object
Sets X and Y velocity with one single call. Takes an Array-argument with 2 values.
61 62 63 64 65 66 67 |
# File 'lib/chingu/traits/velocity.rb', line 61 def velocity=(value) if value.is_a? Array @velocity_x, @velocity_y = value else @velocity_x, @velocity_y = value, value end end |