Mercurial > sypikslang
changeset 80:a40a76012bd7
Refactor message handling so we can deal with in the gui
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Wed, 09 May 2012 12:08:44 +0200 |
parents | 8d1cf0cbe5e1 |
children | 59afe9f92383 |
files | gamelib/constants.py gamelib/gamegui.py gamelib/gamestate.py gamelib/missions.py gamelib/tests/repl_game.py |
diffstat | 5 files changed, 41 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/constants.py Wed May 09 00:31:11 2012 +0200 +++ b/gamelib/constants.py Wed May 09 12:08:44 2012 +0200 @@ -10,3 +10,7 @@ BITSIZE = -16 # unsigned 16 bit CHANNELS = 2 BUFFER = 1024 + +# Result codes for UI hints +(MAJOR_SETBACK, FAILURE, SUCCESS, MAJOR_SUCCESS, GAME_WIN, NEW_SCIENCE, + NEW_SCHEMATIC) = range(7)
--- a/gamelib/gamegui.py Wed May 09 00:31:11 2012 +0200 +++ b/gamelib/gamegui.py Wed May 09 12:08:44 2012 +0200 @@ -9,7 +9,8 @@ from gamelib.gui_base import Window, TextLabel, font_small, font_medium from gamelib.gui import BigButton, ImageDrawable from gamelib.engine import PopWindow, AddWindow -from gamelib.constants import WIDTH +from gamelib.constants import (WIDTH, FAILURE, SUCCESS, NEW_SCIENCE, + NEW_SCHEMATIC) from gamelib.gamestate import Game @@ -365,7 +366,17 @@ def end_turn(self): self.game.cur_missions = self.activity.get_mission_list() - self.game.end_turn() + messages = self.game.end_turn() + # FIXME: Hook this up to a report window or something + for msg_type, msg in messages: + if msg_type == NEW_SCIENCE or msg_type == NEW_SCHEMATIC: + print msg + elif msg_type == FAILURE: + print 'Mission failure: %s' % msg + elif msg_type == SUCCESS: + print 'Success: %s' % msg + else: + print 'Unhandled result %d: %s' % (msg_type, msg) self.game.start_turn() self.update_labels() self.update_widgets()
--- a/gamelib/gamestate.py Wed May 09 00:31:11 2012 +0200 +++ b/gamelib/gamestate.py Wed May 09 12:08:44 2012 +0200 @@ -5,6 +5,7 @@ from gamelib import missions, lab from gamelib.game_base import get_subclasses +from gamelib.constants import NEW_SCIENCE, NEW_SCHEMATIC class Game(object): @@ -60,11 +61,20 @@ new_stuff = self.lab.spend_points(self.cur_allocation, self.points) self.points = 0 # Process mission results + messages = [] for result in mission_results: result.apply(self) + messages.append((result.outcome, result.msg)) for science in new_stuff: # FIXME: Update UI better. - print "You learned new stuff:", science.NAME + if science.SCIENCE_TYPE == 'research': + messages.append((NEW_SCIENCE, + "You've started a new line of research in %s" + % science.NAME)) + else: + messages.append((NEW_SCHEMATIC, + "You've developed blueprints for a %s" % science.NAME)) + return messages def save_data(self): """Serialize the game state into a dict"""
--- a/gamelib/missions.py Wed May 09 00:31:11 2012 +0200 +++ b/gamelib/missions.py Wed May 09 12:08:44 2012 +0200 @@ -3,9 +3,8 @@ from random import random -from schematics import HAND_WEAPON, VEHICLE, PATHOGEN, DOOMSDAY_DEVICE - -MAJOR_SETBACK, FAILURE, SUCCESS, MAJOR_SUCCESS, GAME_WIN = range(5) +from gamelib.constants import SUCCESS, FAILURE +from gamelib.schematics import HAND_WEAPON, VEHICLE, PATHOGEN, DOOMSDAY_DEVICE class Result(object): @@ -23,8 +22,6 @@ state.money += self.money state.reputation += self.reputation self.applied = True - # FIXME: Hook up to the UI - print self.message else: raise RuntimeError('attempted to apply result twice')
--- a/gamelib/tests/repl_game.py Wed May 09 00:31:11 2012 +0200 +++ b/gamelib/tests/repl_game.py Wed May 09 12:08:44 2012 +0200 @@ -1,4 +1,5 @@ from gamelib.gamestate import Game +from gamelib.constants import SUCCESS, FAILURE, NEW_SCIENCE, NEW_SCHEMATIC class ReplGame(object): @@ -62,5 +63,14 @@ self.game.cur_allocation.extend(research_list) self.game.cur_missions.extend(missions) print "-" * 10, "RESULTS", "-" * 31 - self.game.end_turn() + messages = self.game.end_turn() + for msg_type, msg in messages: + if msg_type == NEW_SCIENCE or msg_type == NEW_SCHEMATIC: + print msg + elif msg_type == FAILURE: + print 'Mission failure: %s' % msg + elif msg_type == SUCCESS: + print 'Success: %s' % msg + else: + print 'Unhandled result %d: %s' % (msg_type, msg) self._begin_turn()