Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Entity
- attr_accessor :components, :server_id
- def initialize(server_id)
- @server_id = server_id
- @components ||= {}
- end
- def acts_as(component_sym, *args)
- # TODO maybe use symbols here
- @components[component_sym] = ObjectSpace.const_get(camelize(component_sym)).new *args
- end
- def acts_as?(component_sym)
- @components.has_key? component_sym
- end
- def use_as(component_sym)
- @components[component_sym]
- end
- def update(passed_time)
- @components.each_value{|v|v.update(passed_time)}
- end
- def behaviors
- @components.keys
- end
- private
- # Ganked this from Inflector:
- def camelize(lower_case_and_underscored_word)
- lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
- end
- # Ganked this from Inflector:
- def underscore(camel_cased_word)
- camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
- end
- end
- class Component
- def update(passed_time);end
- end
- require 'component'
- class Movable < Component
- attr_accessor :destination, :moving, :direction
- def moving?()
- @moving
- end
- alias in_motion? moving?
- end
Add Comment
Please, Sign In to add comment