Guest User

Untitled

a guest
Apr 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. class Entity
  2.  
  3. attr_accessor :components, :server_id
  4.  
  5. def initialize(server_id)
  6. @server_id = server_id
  7. @components ||= {}
  8. end
  9.  
  10. def acts_as(component_sym, *args)
  11. # TODO maybe use symbols here
  12. @components[component_sym] = ObjectSpace.const_get(camelize(component_sym)).new *args
  13. end
  14.  
  15. def acts_as?(component_sym)
  16. @components.has_key? component_sym
  17. end
  18.  
  19. def use_as(component_sym)
  20. @components[component_sym]
  21. end
  22.  
  23. def update(passed_time)
  24. @components.each_value{|v|v.update(passed_time)}
  25. end
  26.  
  27. def behaviors
  28. @components.keys
  29. end
  30.  
  31. private
  32. # Ganked this from Inflector:
  33. def camelize(lower_case_and_underscored_word)
  34. lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
  35. end
  36. # Ganked this from Inflector:
  37. def underscore(camel_cased_word)
  38. camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
  39. end
  40. end
  41.  
  42.  
  43. class Component
  44. def update(passed_time);end
  45. end
  46.  
  47.  
  48. require 'component'
  49. class Movable < Component
  50. attr_accessor :destination, :moving, :direction
  51.  
  52. def moving?()
  53. @moving
  54. end
  55. alias in_motion? moving?
  56. end
Add Comment
Please, Sign In to add comment