Class: Kawaii::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/kawaii/node.rb

Direct Known Subclasses

Entity

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(static = false, position = Vector2.new, velocity = Vector2.new, gravity = Kawaii::Vector2.new(0, Kawaii::GRAVITY)) ⇒ Node

Returns a new instance of Node.



6
7
8
9
10
11
12
13
# File 'lib/kawaii/node.rb', line 6

def initialize static = false, position = Vector2.new, velocity = Vector2.new, gravity = Kawaii::Vector2.new(0, Kawaii::GRAVITY)
  @children = []
  @rotation = 0.0
  @position = position
  @velocity = velocity
  @gravity = gravity
  @static = static
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



4
5
6
# File 'lib/kawaii/node.rb', line 4

def children
  @children
end

#gravityObject

Returns the value of attribute gravity.



4
5
6
# File 'lib/kawaii/node.rb', line 4

def gravity
  @gravity
end

#positionObject

Returns the value of attribute position.



4
5
6
# File 'lib/kawaii/node.rb', line 4

def position
  @position
end

#rotationObject

Returns the value of attribute rotation.



4
5
6
# File 'lib/kawaii/node.rb', line 4

def rotation
  @rotation
end

#staticObject

Returns the value of attribute static.



4
5
6
# File 'lib/kawaii/node.rb', line 4

def static
  @static
end

#velocityObject

Returns the value of attribute velocity.



4
5
6
# File 'lib/kawaii/node.rb', line 4

def velocity
  @velocity
end

Instance Method Details

#add_child(node) ⇒ Object



23
24
25
# File 'lib/kawaii/node.rb', line 23

def add_child node
  @children.push(node)
end

#countObject



15
16
17
18
19
20
21
# File 'lib/kawaii/node.rb', line 15

def count
  sum = 1
  @children.each do |child|
    sum += child.count 
  end
  sum
end

#drawObject



56
57
58
59
60
# File 'lib/kawaii/node.rb', line 56

def draw
  @children.each do |child|
    child.draw
  end
end

#has_children?Boolean

Returns:

  • (Boolean)


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

def has_children?
  @children.size > 0
end

#remove_child(node) ⇒ Object



27
28
29
# File 'lib/kawaii/node.rb', line 27

def remove_child node
  @children.delete(node)
end

#update(dt) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kawaii/node.rb', line 35

def update dt
  if self.class.method_defined? :before_update
    before_update dt
  end

  if !@static
    @velocity.x += @gravity.x * dt
    @velocity.y += @gravity.y * dt
    @position.x += @velocity.x * dt
    @position.y += @velocity.y * dt  
  end
  
  @children.each do |child|
    child.update dt
  end

  if self.class.method_defined? :after_update
    after_update dt
  end
end