Commits

Anonymous committed 24a9728

refactor to make UiState and Presentation reusable.

  • Participants
  • Parent commits 7365aee

Comments (0)

Files changed (3)

     "mountain-volcano-tile.png"
     ]]
 
-class UiState: 
+class UiState(object): 
     """The Hexbattle Scene.
     
     The UiState has the following roles:
         if self.selected is not None and not self.command_overlay.is_visible:
             self.selected.move(x, y)
         self.hexfield.move(x, y)
+
+    def next_scene(self):
+        # TODO: This should somehow move into the logic. It makes the
+        #       API ugly, because I have to initialize a UiState just
+        #       for the next_scene function.
+        raise MethodNotImplemented(func=self.next_scene, implementation="""
+    def next_scene(self):
+        # prepare the new scene
+        import scene1to2_time
+        for i in self.state.terrain.values():
+            if isinstance(i, Sprite):
+                scene1to2_time.terrain.append(i)
+        # and run it
+        core.win.pop_handlers()
+        schedule_once(scene1to2_time.start, 0.5)
+        """)
         
 
 class Presentation:
         if self.state.selected:
             self.state.selected.draw()
 
+
+
 class Input:
     """Take all User Input."""
     def __init__(self, state):
             self.state.text_overlay.hide()
             self.state.theater_overlay.shrink()
             if self.state.logic.model.finished:
-                # prepare the new scene
-                import scene1to2_time
-                for i in self.state.terrain.values():
-                    if isinstance(i, Sprite):
-                        scene1to2_time.terrain.append(i)
-                # and run it
-                core.win.pop_handlers()
-                schedule_once(scene1to2_time.start, 0.5)
+                self.state.next_scene()
 
         # do not close on escape, except if the game is finished.
         if symbol == key.ESCAPE and not self.state.logic.model.finished:
         if symbol == key.Q and modifiers == key.MOD_CTRL:
             core.win.close()
     
-class Interaction:
-    def __init__(self, logic):
-        self.state = UiState(logic)
+class Interaction(object):
+    def __init__(self, logic, uistate=None):
+        if uistate is None:
+            self.state = UiState(logic)
+        else:
+            self.state = uistate
         self.input = Input(self.state)
         self.presentation = Presentation(self.state)
 
 #### Imports ####
 
-from core import core, run
-# ensure that pyglet is available
-try:
-    from pyglet.clock import schedule_interval, schedule_once
-except ImportError: # pyglet subrepo has pyglet as subfolder
-    import sys
-    sys.path.append("pyglet.hg")
-    from pyglet.clock import schedule_interval, schedule_once
+from core import core, run, Sprite, schedule_interval, schedule_once
 
-from hexbattle_gui import Interaction
+from hexbattle_gui import Interaction, UiState
 from hexbattle_logic import BattleLogic, BattleModel
 import hexbattle_units as units
 import hexgrid
     def __init__(self, hexcenter):
         self.model = Model(hexcenter)
         #: The scene to switch to at the next possible step.
-        self.switch = False 
+        self.switch = False
+
+
+class SceneUiState(UiState):
+    def __init__(self, logic):
+        super(SceneUiState, self).__init__(logic)
+    
+    def next_scene(self):
+        # prepare the new scene
+        import scene1to2_time
+        for i in self.terrain.values():
+            if isinstance(i, Sprite):
+                scene1to2_time.terrain.append(i)
+        # and run it
+        core.win.pop_handlers()
+        schedule_once(scene1to2_time.start, 0.5)
 
 
 #### Basic Game-Scene API ####        
     """start the scene"""
     hexcenter = hexgrid.coordinate_to_hexgrid(core.win.width/2, core.win.height/2)
     logic = Logic(hexcenter=hexcenter)
-    interaction = Interaction(logic)
+    uistate = SceneUiState(logic)
+    interaction = Interaction(logic, uistate)
     @core.win.event
     def on_draw():
         interaction.presentation.draw()

scene1to2_time.py

 from core import MethodNotImplemented
 from core import core, run
 # ensure that pyglet is available
-try:
-    from pyglet.clock import schedule_interval, schedule_once
-except ImportError: # pyglet subrepo has pyglet as subfolder
-    import sys
-    sys.path.append("pyglet.hg")
-    from pyglet.clock import schedule_interval, schedule_once
+from core import schedule_interval, schedule_once
 
 import hexgrid