Advertisement
szymski

Untitled

Feb 6th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. module orly.engine.components.component;
  2.  
  3. import orly.engine.gameobjects.gameobject;
  4.  
  5. alias GameObject _GameObject;
  6.  
  7. /*
  8. Component class
  9. */
  10.  
  11. class Component {
  12. private:
  13.  
  14. _GameObject gameObject;
  15.  
  16. public:
  17.  
  18. @property ref _GameObject GameObject() { return gameObject; }
  19.  
  20. }
  21.  
  22. /*
  23. Component factory
  24. */
  25.  
  26. static class ComponentFactory {
  27. private:
  28. static Component delegate()[string] delegates;
  29.  
  30. public:
  31.  
  32. /**
  33. Registers a Component creating delegate
  34. */
  35. static void RegisterComponent(string name, Component delegate() func) {
  36. delegates[name] = func;
  37. std.stdio.writeln("Registered " ~ name ~ " component");
  38. }
  39.  
  40. /**
  41. Creates a new Component object from the specified name
  42. */
  43. static Component CreateNew(string name) {
  44. auto p = name in delegates;
  45.  
  46. if(p is null)
  47. throw new Exception("No such component (" ~ name ~ ") registered");
  48.  
  49. return (*p)();
  50. }
  51. }
  52.  
  53. /*
  54. Registering mixin
  55. */
  56.  
  57. /*
  58. Mixin used to register a component.
  59. You should use it once for each component.
  60. */
  61. mixin template RegisterComponent(T) {
  62. shared static this() {
  63. ComponentFactory.RegisterComponent(__traits(identifier, T), delegate() {
  64. return new T();
  65. });
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement