1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
module CP
# Chipmunk Object
# Makes it easier to manage complex objects that reference many primitive Chipmunk objects such as bodies shapes and constraints.
# New composite objects simply need to include CP::Object and call #init_chipmunk_object(*objects) with the
# composite and primitive Chipmunk objects that make up itself.
module Object
# Returns the list of primitive Chipmunk objects (bodies, shapes and constraints)
# that this composite object references directly and indirectly.
def chipmunk_objects
if @chipmunk_objects
return @chipmunk_objects
else
raise "This CP::Object (#{self.class}) did not call #init_chipmunk_object."
end
end
private
# Should be called during initialization of a CP::Object to set what primitive
# and composite Chipmunk objects this object references.
def init_chipmunk_object(*objs)
bad_objs = objs.reject{|obj| obj.is_a?(CP::Object)}
raise(ArgumentError, "The following objects: #{bad_objs.inspect} are not CP::Objects") unless bad_objs.empty?
@chipmunk_objects = objs.inject([]){|sum, obj| sum + obj.chipmunk_objects}.uniq
end
end
class Body
include CP::Object
def chipmunk_objects
[self]
end
def add_to_space(space)
space.add_body(self)
end
def remove_from_space(space)
space.remove_body(self)
end
end
module Shape
include CP::Object
def chipmunk_objects
[self]
end
def add_to_space(space)
space.add_shape(self)
end
def remove_from_space(space)
space.remove_shape(self)
end
end
module Constraint
include CP::Object
def chipmunk_objects
[self]
end
def add_to_space(space)
space.add_constraint(self)
end
def remove_from_space(space)
space.remove_constraint(self)
end
end
class Space
def add_object(obj)
obj.chipmunk_objects.each{|elt| elt.add_to_space(self)}
end
def add_objects(*objs)
objs.each{|obj| add_object(obj)}
end
def remove_object(obj)
obj.chipmunk_objects.each{|elt| elt.remove_from_space(self)}
end
def remove_objects(*objs)
objs.each{|obj| remove_object(obj)}
end
end
end
require 'chipmunk'
# Create derived static objects that know to add themselves as static.
module CP
class StaticBody < Body
def initialize
super(Float::INFINITY, Float::INFINITY)
end
def chipmunk_objects
# return [] instead of [self] so the static body will not be added.
[]
end
end
module StaticShape
include Shape
class Circle < Shape::Circle
include StaticShape
end
class Segment < Shape::Segment
include StaticShape
end
class Poly < Shape::Poly
include StaticShape
end
def add_to_space(space)
space.add_static_shape(self)
end
def remove_from_space(space)
space.remove_static_shape(self)
end
end
end
|