Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

156 行
4.4KB

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