Class: Chippunk::World

Inherits:
Object
  • Object
show all
Defined in:
lib/chippunk/world.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ World

Returns a new instance of World.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
# File 'lib/chippunk/world.rb', line 6

def initialize(&block)
  @space = CP::Space.new
  @objects = []
  yield(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



12
13
14
# File 'lib/chippunk/world.rb', line 12

def method_missing(method, *args, &block)
  @space.send(method, *args, &block)
end

Instance Attribute Details

#dtObject

Returns the value of attribute dt.



4
5
6
# File 'lib/chippunk/world.rb', line 4

def dt
  @dt
end

#objectsObject (readonly)

Returns the value of attribute objects.



3
4
5
# File 'lib/chippunk/world.rb', line 3

def objects
  @objects
end

#spaceObject (readonly)

Returns the value of attribute space.



3
4
5
# File 'lib/chippunk/world.rb', line 3

def space
  @space
end

#substepsObject

Returns the value of attribute substeps.



4
5
6
# File 'lib/chippunk/world.rb', line 4

def substeps
  @substeps
end

Instance Method Details

#add_object(obj) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/chippunk/world.rb', line 16

def add_object(obj)
  obj.world = self
  obj.shape.obj = obj
  @space.add_shape(obj.shape)
  @space.add_body(obj.shape.body) unless obj.static?
  @objects << obj
end

#add_objects(*objects) ⇒ Object



24
25
26
# File 'lib/chippunk/world.rb', line 24

def add_objects(*objects)
  objects.each{ |obj| self.add_object(obj) }
end

#draw(canvas) ⇒ Object



36
37
38
# File 'lib/chippunk/world.rb', line 36

def draw(canvas)
  @objects.each{ |obj| obj.draw(canvas) }
end

#update(&block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/chippunk/world.rb', line 28

def update(&block)
  @substeps.times do
    @objects.each{ |o| o.body.reset_forces }
    yield(self) if block_given?
    @space.step(@dt)
  end
end