1. Arne Babenhauserheide
  2. hexbattle


Arne Babenhauserheide  committed dd65077

refactoring: split char.update into visible and model version.

  • Participants
  • Parent commits 82d6b90
  • Branches default

Comments (0)

Files changed (3)

File hexbattle_gui.py

View file
     def __init__(self, logic, *args, **kwds): 
         """Initialize the scene with a core object for basic functions."""
+        # set the background color to white
+        gl.glClearColor(255, 255, 255, 0)
         #: The Game logic.
         self.logic = logic
         #: the core provides data persistence between scenes.
         self.core = core
         #: The logic tells us what should be visible
         self.visible = self.logic.model.visible
         #: Overlay sprites. They are above all other sprites (included for convenience, since most games need an overlay of sorts)
         self.overlay = []
         #: We still need the hexmap overlay here. The logic does not have to know how an overlay looks.
         self.hexmap_overlay = {}
-        # set the background color to white
-        gl.glClearColor(255, 255, 255, 0)
         # the background
-        self.terrain = self.logic.model.terrain
+        self.terrain = self.logic.model.terrain        
+        self.possible_commands = []
         # a hexgrid
         for i in range(-1, 30):
     def update(self, dt=0): 
         """Update the stats of all scene objects. 
-Don't blit them, though. That's done by the Game itself.
+Don't blit them, though. That's done by the Presentation.
 To show something, add it to the self.visible list. 
 To add a collider, add it to the self.colliding list. 
 To add an overlay sprite, add it to the self.overlay list. 
+        # Update the visible representation of the char.
         for char in self.logic.model.chars:
-            char.update()
+            char.update_visible()
+        # Update the model.
+        # TODO: Just execute a step action, when the model tells us
+        # that we can do so.
+        # TODO: Add an inactive state: There’s nothing we can do
+        # (action already scheduled).
+        self.logic.update()
         # do the computer actions. As soon as it’s the players turn, computer_turn() changes the state.
         if self.logic.model.phase["player"] == "setup":
             schedule_once(self.logic.model.setup_step, 0.01)

File hexbattle_logic.py

View file
         self.phase["player"] = "wait"
         # TODO: return the result of the action instead. Or at least a list of changed characters. Best a list of all characters who changed state and how they changed state.
         return Result("step", commands=[Command(self.switch_to_cpu_turn, "continue")])
 class Logic:
     def __init__(self):
         self.model = Model()
         #: The scene to switch to at the next possible step.
         self.switch = False 
+    def update(self):
+        # Update the visible representation of the char.
+        for char in self.model.chars:
+            char.update_model()

File hexbattle_units.py

View file
 class Character(Hexfield, Char):
     """A character on the Hexfield. It knows the level as hexmap.
-    TODO: multi-step actions: action = (identifier, time (from time()))
+    TODO: 1. Split methods which mix model and GUI.
+    TODO: 2. Add GUI methods from the GUI: The selected GUI gives the chars the capabilities they need. Drawback: Can’t change the UI without reloading.
     >>> # Use the Character without model and with fake hexmap
     >>> # → nonfunctional ⇒ only for testing. 
             if target is not None:
-    def update(self):
-        """Make sure we’re alive."""
+    def update_model(self):
+        """Update the model representation."""
         if not self.alive:
+    def update_visible(self):
+        """Update the visible elements, but don’t change the model
+        representation (hexmay stuff)."""
         for action in self.timed_actions:
             if action[0] == "tumble":
         super(Character, self).draw()
         for sprite in self.status_sprites:
 if __name__ == "__main__":
     # there’s only one reason for calling this file: doctests