From 62eae792b0b392cf111dc95aab3ee520ca9c58f1 Mon Sep 17 00:00:00 2001 From: Mateu Batle Date: Tue, 23 Nov 2010 15:33:31 +0000 Subject: Implemented cookie system to avoid creation of many objects --- (limited to 'usmpgames') diff --git a/usmpgames/application.py b/usmpgames/application.py index d492b5c..7871ee8 100755 --- a/usmpgames/application.py +++ b/usmpgames/application.py @@ -28,7 +28,9 @@ class Application(): def __init__(self): self._state_stack = [] + self._cookie_stack = [] self._current_state = None + self._current_cookie = None self._running = True self._fps = 25 if Application._instance is None: @@ -40,12 +42,13 @@ class Application(): def screen(self): return self._screen - def push_state(self, new_state): + def push_state(self, new_state, new_cookie = None): # exit from current state if self._current_state is not None: self._current_state.exiting_state( True ) if new_state is not None: self._state_stack.append(self._current_state) + self._cookie_stack.append(self._current_state.cookie()) # process new state fromStack = False @@ -55,18 +58,20 @@ class Application(): return else : self._current_state = self.pop_state() + self._current_cookie = self._current_state.cookie() fromStack = True else: self._current_state = new_state + self._current_cookie = new_cookie # entering new state if self._current_state is None: self.set_running(False) else: self._current_state.set_screen(self.screen()) - self._current_state.entering_state(fromStack) + self._current_state.entering_state(fromStack, self._current_cookie) - def change_state(self, new_state): + def change_state(self, new_state, new_cookie): # exit from current state if self._current_state is not None: self._current_state.exiting_state(False) @@ -81,11 +86,12 @@ class Application(): return else: self._current_state = new_state + self._current_cookie = new_cookie # entering new state if self._current_state is not None: self._current_state.set_screen(self.screen()) - self._current_state.entering_state( False ) + self._current_state.entering_state( False, self._current_cookie ) else: self.set_running(False) @@ -96,11 +102,13 @@ class Application(): self._current_state.exiting_state( False ) if len(self._state_stack) >= 1: self._current_state = self._state_stack.pop() + self._current_cookie = self._current_state.cookie() else: self._current_state = None + self._current_cookie = None if self._current_state is not None: self._current_state.set_screen(self.screen()) - self._current_state.entering_state( True ) + self._current_state.entering_state( True, self._current_cookie ) def current_state(self): return self._current_state diff --git a/usmpgames/applicationstate.py b/usmpgames/applicationstate.py index 6f6439b..ff8613b 100755 --- a/usmpgames/applicationstate.py +++ b/usmpgames/applicationstate.py @@ -6,12 +6,14 @@ from application import * class ApplicationState(): - def __init__(self, next_state = None, background_name = None): + def __init__(self, next_state = None, background_name = None, next_cookie = None): self._running = True self._screen = None self._next_state = next_state + self._next_cookie = next_cookie self._background_name = background_name self._background = None + self._cookie = None def input(self, ms): pass @@ -56,17 +58,22 @@ class ApplicationState(): self.render(ms) self.post_render(ms) - def entering_state(self, fromStack): + def entering_state(self, fromStack, cookie): print "Entering state ", self self.set_running(True) + self._cookie = cookie def exiting_state(self, toStack): print "Exiting state ", self + #self._cookie = None pass def go_to_next_state(self): - Application.instance().change_state( self._next_state ) + Application.instance().change_state( self._next_state, self._next_cookie ) def next_state(self): return self._next_state + def cookie(self): + return self._cookie + diff --git a/usmpgames/infostate.py b/usmpgames/infostate.py index 9bf46ee..23560b6 100755 --- a/usmpgames/infostate.py +++ b/usmpgames/infostate.py @@ -13,8 +13,8 @@ import pygame.font class InfoState(ApplicationState): - def __init__(self, next_state, background = None): - ApplicationState.__init__(self, next_state, background) + def __init__(self, next_state, background = None, next_cookie = None): + ApplicationState.__init__(self, next_state, background, next_cookie) self._images = [] self._ktexts = [] @@ -67,7 +67,7 @@ class InfoState(ApplicationState): for info in self._ktexts: info.draw(self.screen()) - def entering_state(self, fromStack): - ApplicationState.entering_state(self, fromStack) + def entering_state(self, fromStack, cookie): + ApplicationState.entering_state(self, fromStack, cookie) pygame.time.wait(500) pygame.event.clear() diff --git a/usmpgames/menustate.py b/usmpgames/menustate.py index 325e4a4..fa20ad0 100755 --- a/usmpgames/menustate.py +++ b/usmpgames/menustate.py @@ -7,8 +7,8 @@ from infostate import * class MenuState(InfoState): - def __init__(self, background = None): - InfoState.__init__(self, None, background) + def __init__(self, background = None, cookie = None): + InfoState.__init__(self, None, background, cookie) self._menu_options = [] self._menu_states = [] self.pos = ('center', 'center') @@ -41,5 +41,5 @@ class MenuState(InfoState): def input(self, ms): pass - def entering_state(self, fromStack): - ApplicationState.entering_state(self, fromStack) + def entering_state(self, fromStack, cookie): + ApplicationState.entering_state(self, fromStack, cookie) -- cgit v0.9.1