Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

120 lines
3.3KB

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