Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

161 lines
4.5KB

  1. '''
  2. Filename nodes.py
  3. Author: Bryan "ObsidianBlk" Miller
  4. Date Created: 8/1/2018
  5. Python Version: 3.7
  6. '''
  7. NODETYPE_NODE = "NODE"
  8. class NodeError(Exception):
  9. pass
  10. class Node:
  11. def __init__(self, name="Node", parent=None):
  12. self._parent = None
  13. self._name = name
  14. self._children = []
  15. if parent is not None:
  16. try:
  17. self.parent = parent
  18. except NodeError as e:
  19. raise e
  20. @property
  21. def type(self):
  22. return NODETYPE_NODE
  23. @property
  24. def parent(self):
  25. return self._parent
  26. @parent.setter
  27. def parent(self, new_parent):
  28. try:
  29. self.parent_to_node(new_parent)
  30. except NodeError as e:
  31. raise e
  32. @property
  33. def root(self):
  34. if self._parent is None:
  35. return self
  36. return self._parent.root
  37. @property
  38. def name(self):
  39. return self._name
  40. @name.setter
  41. def name(self, value):
  42. if self._parent is not None:
  43. if self._parent.get_node(value) is not None:
  44. raise NodeError("Parent already contains node named '{}'.".format(name))
  45. self._name = value
  46. @property
  47. def full_name(self):
  48. if self._parent is None:
  49. return self._name
  50. return self._parent.full_name + "." + self._name
  51. @property
  52. def child_count(self):
  53. return len(this._children)
  54. def parent_to_node(self, parent, allow_reparenting=False):
  55. if not isinstance(value, Node):
  56. raise NodeError("Node may only parent to another Node instance.")
  57. if self._parent is None or self._parent != parent:
  58. if self._parent is not None:
  59. if allow_Reparenting == False:
  60. raise NodeError("Node already assigned a parent Node.")
  61. if self._parent.remove_node(self) != self:
  62. raise NodeError("Failed to remove self from current parent.")
  63. try:
  64. parent.attach_node(self)
  65. except NodeError as e:
  66. raise e
  67. def attach_node(self, node, reparent=False, index=-1):
  68. if node.parent is not None:
  69. if node.parent == self:
  70. return # Nothing to do. Given node already parented to this node.
  71. if reparent == False:
  72. raise NodeError("Node already parented.")
  73. if node.parent.remove_node(node) != node:
  74. raise NodeError("Failed to remove given node from it's current parent.")
  75. if self.get_node(node.name) is not None:
  76. raise NodeError("Node with name '{}' already attached.".format(node.name))
  77. node._parent = self
  78. if index < 0 or index >= len(self._children):
  79. self._children.append(node)
  80. else:
  81. self._children.insert(index, node)
  82. def remove_node(self, node):
  83. if isinstance(node, (str, unicode)):
  84. n = self.get_node(node)
  85. if n is not None:
  86. try:
  87. return self.remove_node(n)
  88. except NodeError as e:
  89. raise e
  90. elif isinstance(node, Node):
  91. if node.parent != self:
  92. if node.parent == None:
  93. raise NodeError("Cannot remove an unparented node.")
  94. try:
  95. return node.parent.remove_node(node)
  96. except NodeError as e:
  97. raise e
  98. if node in self._children:
  99. self._children.remove(node)
  100. node._parent = None
  101. return node
  102. else:
  103. raise NodeError("Expected a Node instance or a string.")
  104. return None
  105. def get_node(self, name):
  106. if len(self._children) <= 0:
  107. return None
  108. subnames = name.split(".")
  109. for c in self._children:
  110. if c.name == subnames[0]:
  111. if len(subnames) > 1:
  112. return c.get_node(".".join(subnames[1:-1]))
  113. return c
  114. return None
  115. def _update(self, dt):
  116. if hasattr(self, "on_update"):
  117. self.on_update(dt)
  118. for c in self._children:
  119. c._update(dt)
  120. def _render(self, surface):
  121. for c in self._children:
  122. c._render(surface)
  123. if hasattr(self, "on_render"):
  124. self.on_render(surface)
  125. class NodeSurface(Node):
  126. def __init__(self, name="NodeSurface", parent=None, display_ref=None):
  127. try:
  128. Node.__init__(self, name, parent)
  129. except NodeError as e:
  130. raise e
  131. self._surface = None