Skip to content

Commit

Permalink
Implemented current phase asset
Browse files Browse the repository at this point in the history
  • Loading branch information
frgorp committed Apr 2, 2013
1 parent 56af672 commit ccd34a0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
31 changes: 20 additions & 11 deletions risk/game_master.py
Expand Up @@ -12,10 +12,10 @@
from risk.errors.battle import *
from risk.player import HumonRiskPlayer

REINFORCE_PHASE = 0
ATTACK_PHASE = 1
FORTIFY_PHASE = 2
UNDEFINED_PHASE = 3
REINFORCE = 'reinforce'
ATTACK = 'attack'
FORTIFY = 'fortify'
UNDEFINED = 'undefined'

class GameMaster(object):
_RISK_RULE_STARTING_RESERVES = 40
Expand All @@ -29,7 +29,7 @@ def __init__(self, board, settings, num_players=6):
num_players)

self.board = board
self.phase = UNDEFINED_PHASE
self.phase = UNDEFINED
# need to setup with settings later
self.ended = False
self.end_turn_callbacks = []
Expand All @@ -43,6 +43,7 @@ def __init__(self, board, settings, num_players=6):
'start_turn': [],
'end_action': [],
'end_turn': [],
'end_phase': [],
'end_game': [],
}
self.add_end_action_callback(GameMaster.check_player_elimination)
Expand Down Expand Up @@ -98,6 +99,9 @@ def add_start_turn_callback(self, callback):
def add_end_action_callback(self, callback):
self.callbacks['end_action'].append(callback)

def add_end_phase_callback(self, callback):
self.callbacks['end_phase'].append(callback)

def generate_players(self, number_of_human_players, cli=False):
risk.logger.debug("Generating %s human players" % \
number_of_human_players)
Expand Down Expand Up @@ -170,7 +174,6 @@ def check_player_elimination(self, function, result, args):
self._recalculate_current_player_index(
args[_PLAYER_ARG_INDEX])

# TODO fix select next player bug
def eliminate_player(self, player):
self.players.remove(player)
for _ in xrange(10):
Expand All @@ -181,6 +184,12 @@ def eliminate_player(self, player):
print "%s WINS!!!!" % self.current_player().name
self.end_game()

def set_phase(self, new_phase):
previous = self.phase
self.phase = new_phase
for callback in self.callbacks['end_phase']:
callback(self, previous, new_phase)

###########################################################################
## Game state queries
#
Expand Down Expand Up @@ -211,16 +220,16 @@ def _recalculate_current_player_index(self, current_player):
#@event_action
def player_take_turn(self):
self.call_start_turn_callbacks()
self.phase = UNDEFINED_PHASE
self.set_phase(UNDEFINED)
player = self._get_player_with_index(self._current_player)
player.reserves += len(self.player_territories(player))
self.phase = REINFORCE_PHASE
self.set_phase(REINFORCE)
player.reinforce(self)
self.phase = ATTACK_PHASE
self.set_phase(ATTACK)
player.attack(self)
self.phase = FORTIFY_PHASE
self.set_phase(FORTIFY)
player.fortify(self)
self.phase = UNDEFINED_PHASE
self.set_phase(UNDEFINED)

#@event_action
def player_territories(self, player):
Expand Down
21 changes: 21 additions & 0 deletions risk/graphics/assets/image.py
Expand Up @@ -3,6 +3,9 @@
import risk
from risk.graphics.assets.base import PicassoAsset

ON=True
OFF=False

class ImageAsset(PicassoAsset):
def __init__(self, x, y, path, scale_x=1.0, scale_y=1.0):
self.path = path
Expand All @@ -12,3 +15,21 @@ def __init__(self, x, y, path, scale_x=1.0, scale_y=1.0):
(int(self.surface.get_width() * scale_x),
int(self.surface.get_height() * scale_y)))
PicassoAsset.__init__(self, self.surface, x, y)

class ToggleImageAsset(ImageAsset):
def __init__(self, x, y, path, start_state=ON):
ImageAsset.__init__(self, x, y, path)
self.state = start_state
self.blank = pygame.Surface((0, 0), pygame.SRCALPHA, 32)

def draw(self):
if self.state == ON:
return self.surface
else:
return self.blank

def toggle(self):
self.state = not self.state

def set_state(self, state):
self.state = state
23 changes: 22 additions & 1 deletion risk/graphics/graphics.py
Expand Up @@ -15,6 +15,7 @@
import risk.graphics.assets.gameplay

from risk.logger import *
from risk.game_master import UNDEFINED, REINFORCE, ATTACK, FORTIFY
from risk.graphics import assets
from risk.graphics.event import wait_for_event, get_events
from risk.graphics.datastore import Datastore
Expand Down Expand Up @@ -119,6 +120,7 @@ def add_graphic_hooks(game_master):
game_master.add_end_action_callback(release_control)
game_master.add_start_turn_callback(update_game_info_panel)
game_master.add_end_action_callback(update_game_info_panel)
game_master.add_end_phase_callback(update_current_phase)
#game_master.add_end_action_callback(delay)

def initialize_territories(picasso, game_master):
Expand Down Expand Up @@ -148,7 +150,22 @@ def initialize_other_graphic_assets(picasso, game_master):
game_info_asset = assets.gameplay.PlayersAsset(30, 550, game_master)
picasso.add_asset('999_ontop', game_info_asset)
datastore.add_entry('game_info', game_info_asset)

add_state_indicators(picasso, game_master)

def add_state_indicators(picasso, game_master):
datastore = Datastore()
pos_x = 867
state_indicators = {
'reinforce': (pos_x, 548),
'attack': (pos_x, 600),
'fortify': (pos_x, 652),
}
for state, coordinate in state_indicators.iteritems():
asset = assets.image.ToggleImageAsset(coordinate[0], coordinate[1],
"assets/art/gui/button_%s_highlight.png" % state)
datastore.add_entry(state, asset, 'states')
picasso.add_asset('999_ontop', asset)

def add_buttons(picasso):
datastore = Datastore()
#next_button = assets.clickable.ClickableAsset(
Expand Down Expand Up @@ -202,6 +219,10 @@ def check_gui_quit_event(game_master):
def update_game_info_panel(*args):
Datastore().get_entry('game_info').update()

def update_current_phase(game_master, previous, current):
for state, asset in Datastore().get_storage('states').iteritems():
asset.set_state(state == current)

def release_control(game_master, *args):
# release CPU for faster screen update
time.sleep(0)
Expand Down

0 comments on commit ccd34a0

Please sign in to comment.