Add ending flag to avoid many scene change events
authorNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 20:50:47 +0000 (22:50 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 10 Sep 2016 20:51:11 +0000 (22:51 +0200)
tabakrolletjie/scenes/day.py
tabakrolletjie/scenes/night.py

index e2d1e19bcb6634b62366fb83efe1bad7fafbbb94..7f73ad288a17342946d91a928577220da0e86ddb 100644 (file)
@@ -53,6 +53,7 @@ class DayScene(BaseScene):
             self._draw_you_lose(gamestate)
         elif gamestate.harvested >= gamestate.turnip_target:
             self._draw_you_win(gamestate)
+        self._ending = False
 
     def _draw_you_lose(self, gamestate):
         overlay = pygame.surface.Surface(
@@ -128,8 +129,11 @@ class DayScene(BaseScene):
         gamestate.turnips = turnip_data
 
     def end_day(self, gamestate):
+        if self._ending:
+            return
         self._battery.apply_recharge()
         gamestate.update_lights(self._lights)
+        self._ending = True
         from .night import NightScene
         SceneChangeEvent.post(scene=NightScene())
 
@@ -226,12 +230,16 @@ class DayScene(BaseScene):
             self._lights.add_light(cfg)
 
     def event(self, ev, gamestate):
+        if self._ending:
+            return
         if self._game_over_text:
             if ev.type in (pgl.KEYDOWN, pgl.MOUSEBUTTONDOWN):
+                self._ending = True
                 from .menu import MenuScene
                 SceneChangeEvent.post(scene=MenuScene())
         if ev.type == pgl.KEYDOWN:
             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
+                self._ending = True
                 from .menu import MenuScene
                 SceneChangeEvent.post(scene=MenuScene())
             elif ev.key == pgl.K_e:
@@ -251,6 +259,7 @@ class DayScene(BaseScene):
                         elif tool.name == 'start night':
                             self.end_day(gamestate)
                         elif tool.name == 'exit':
+                            self._ending = True
                             from .menu import MenuScene
                             SceneChangeEvent.post(scene=MenuScene())
                         else:
index 9459c0ed68668d6edb358879ca93917ab9c75a76..7813e32baab0ce118682dd04133a2d977d879adf 100644 (file)
@@ -47,6 +47,7 @@ class NightScene(BaseScene):
         self._paused = False
         self._eaten_tonight = 0
         self._night_over_text = []
+        self._ending = False
 
     def create_tools(self, gamestate):
         tools = []
@@ -105,11 +106,14 @@ class NightScene(BaseScene):
             surface.blit(text, text_pos, None)
 
     def event(self, ev, gamestate):
+        if self._ending:
+            return
         if ev.type == pgl.KEYDOWN:
             if not self._do_ticks:
                 # Any keypress exits
                 self._to_day(gamestate)
             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
+                self._ending = True
                 from .menu import MenuScene
                 SceneChangeEvent.post(scene=MenuScene())
             elif ev.key == pgl.K_e and DEBUG:
@@ -129,6 +133,7 @@ class NightScene(BaseScene):
                         if tool.name == 'pause play':
                             self.toggle_pause()
                         elif tool.name == 'exit':
+                            self._ending = True
                             from .menu import MenuScene
                             SceneChangeEvent.post(scene=MenuScene())
                         elif tool.name == 'day':
@@ -143,7 +148,10 @@ class NightScene(BaseScene):
 
     def _to_day(self, gamestate):
         # End the night
+        if self._ending:
+            return
         gamestate.update_lights(self._lights)
+        self._ending = True
         from .day import DayScene
         SceneChangeEvent.post(scene=DayScene())