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

Instance Method Summary collapse

Instance Attribute Details

#acceleration_xObject

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_yObject

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_xObject

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_yObject

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_xObject (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_yObject (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_xObject

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_yObject

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

#accelerationObject



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_velocityObject



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

Returns:

  • (Boolean)


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(options)
  @velocity_options = {:debug => false}.merge(options)
  
  @velocity_x = options[:velocity_x] || 0
  @velocity_y = options[:velocity_y] || 0
  self.velocity = options[:velocity] if options[:velocity]
        
  @acceleration_x = options[:acceleration_x] || 0
  @acceleration_y = options[:acceleration_y] || 0
  self.acceleration = options[:acceleration] if options[:acceleration]

  @max_velocity_x = options[:max_velocity_x] || 1000
  @max_velocity_y = options[:max_velocity_y] || 1000
  self.max_velocity = options[:max_velocity] if options[:max_velocity]
  
  super
end

#stopObject

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

Returns:

  • (Boolean)


133
134
135
# File 'lib/chingu/traits/velocity.rb', line 133

def stopped?
  @velocity_x == 0 && @velocity_y == 0
end

#update_traitObject

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 trait_options[:velocity][:apply] == false
  
  super
end

#velocityObject



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