Module: Chingu::Traits::Velocity

Defined in:
lib/chingu/traits/velocity.rb

Overview

A chingu trait providing velocity and acceleration logic.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#acceleration_xObject

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_yObject

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_velocityObject

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_xObject

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_yObject

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(options)
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(options)
  @velocity_options = {:debug => false}.merge(options)        
  puts "Velocity#setup"   if @velocity_options[:debug]
  
  @velocity_x = options[:velocity_x] || 0
  @velocity_y = options[:velocity_y] || 0
  @acceleration_x = options[:acceleration_x] || 0
  @acceleration_y = options[:acceleration_y] || 0
  @max_velocity = options[:max_velocity] || 1000
  super
end

#stopObject



62
63
64
# File 'lib/chingu/traits/velocity.rb', line 62

def stop
  @acceleration_y = @acceleration_x = @velocity_y = @acceleration_y = 0
end

#update_traitObject

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