Module: Chingu::Traits::Vectors

Defined in:
lib/vector.rb,
lib/actual_vectors.rb

Defined Under Namespace

Modules: ClassMethods Classes: Vector

Constant Summary collapse

DEFAULT_OPTIONS =
{ :position  => [0, 0],
:speed     => [0, 0],
:hastening => [0, 0] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hasteningObject

Hasteing (acceleration) of game object with [x,y] in pixels per second^2



28
29
30
# File 'lib/actual_vectors.rb', line 28

def hastening
  @hastening
end

#last_positionObject (readonly)

Position of game object in last tick



31
32
33
# File 'lib/actual_vectors.rb', line 31

def last_position
  @last_position
end

#positionObject

Position of game object with [x,y] in pixel



22
23
24
# File 'lib/actual_vectors.rb', line 22

def position
  @position
end

#speedObject

Speed of game object with [x,y] in pixels per second



25
26
27
# File 'lib/actual_vectors.rb', line 25

def speed
  @speed
end

Instance Method Details

#draw_traitObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/actual_vectors.rb', line 85

def draw_trait
  if trait_options[:vectors][:debug]
    x = position.x
    y = position.y
    #draw speed
    $window.draw_line(x, y, Gosu::Color::GREEN, x+speed.x, y+speed.y, Gosu::Color::GREEN, 1)
    #draw hastening
    $window.draw_line(x, y, Gosu::Color::CYAN, x+hastening.x, y+hastening.y, Gosu::Color::CYAN, 1)
  end
  super
end

#moved?Boolean

Checks if game object hast moved since last tick

Returns:

  • (Boolean)


116
117
118
# File 'lib/actual_vectors.rb', line 116

def moved?
  last_position != position
end

#setup_trait(options) ⇒ Object

Sets up the trait. Possible options are position, speed and hastening (with defaults [0,0] respectively)



37
38
39
40
41
42
43
44
# File 'lib/actual_vectors.rb', line 37

def setup_trait options
  puts "setup trait in vectors called"
  trait_options[:vectors] = DEFAULT_OPTIONS.merge(options)
  self.position           = trait_options[:vectors][:position]
  self.speed              = trait_options[:vectors][:speed]
  self.hastening          = trait_options[:vectors][:hastening]
  super options
end

#stopObject

Stopes the game object. Speed = 0 and hastening = 0



100
101
102
103
# File 'lib/actual_vectors.rb', line 100

def stop
  self.speed     = [0, 0]
  self.hastening = [0, 0]
end

#stopped?Boolean

Checks if game object is on move are hastening

Returns:

  • (Boolean)


108
109
110
111
# File 'lib/actual_vectors.rb', line 108

def stopped?
  speed == [0, 0]
  hastening == [0, 0]
end

#update_traitObject

Moves the game object apropriate to the time that passed since last tick



75
76
77
78
79
80
81
82
83
# File 'lib/actual_vectors.rb', line 75

def update_trait
  if trait_options[:vectors][:apply]
    seconds        = $window.milliseconds_since_last_tick / 1000.0
    @last_position = position
    self.position  = position + (speed * seconds)
    self.speed     = speed + (hastening * seconds)
  end
  super
end