dispense with movement mode
authorNeil <neil@dip.sun.ac.za>
Thu, 15 May 2014 15:23:10 +0000 (17:23 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 15 May 2014 15:23:10 +0000 (17:23 +0200)
naja/constants.py
naja/gameboard.py
naja/player.py
naja/widgets/board.py
naja/widgets/info_area.py
naja/widgets/robot.py
naja/widgets/tile.py

index 34a6fc1dade8bea18ff34b87d186d6e38aec0baf..d93b45cf8a4b652b5d0d27f40eb59ad04752ea17 100644 (file)
@@ -56,9 +56,8 @@ INFO_SIZE = (SCREEN[0] - 5 * TILE_SIZE[0], SCREEN[1])
 PLAYER_SIZE = (64, 96)
 
 # Player States
-MOVE = 1
-ACT = 2
-EXAMINE = 3
+ACT = 1
+EXAMINE = 2
 
 KEYS = AttrDict({
     'UP': (pgl.K_UP, pgl.K_w, pgl.K_COMMA),
index 7e8f27323aa54f4e9fa4a05e3b1dae6cbd82a96a..d62684924a514847634707ff038250de5ed5f33f 100644 (file)
@@ -2,7 +2,7 @@ from random import choice
 
 from naja.constants import(
     BITS, DIRECTION_BITS, CONDITION_BITS, PLAYER_DEFAULTS,
-    MOVE, ACT, EXAMINE)
+    ACT, EXAMINE)
 from naja.player import Player
 from naja import actions
 
@@ -20,7 +20,7 @@ class GameBoard(object):
         self.locations = [item.copy() for item in state['locations']]
         self.player = player
         self.board_locations = board_locations
-        self.player_mode = MOVE
+        self.player_mode = EXAMINE
 
     @classmethod
     def new_game(cls, locations_definition,
@@ -152,7 +152,7 @@ class GameBoard(object):
         if new_mode == self.player_mode:
             raise RuntimeError("Inconsistent state. Setting mode %s to itself"
                                % self.player_mode)
-        elif new_mode in (MOVE, ACT, EXAMINE):
+        elif new_mode in (ACT, EXAMINE):
             self.player_mode = new_mode
         else:
             raise RuntimeError("Illegal player mode %s" % self.player_mode)
index 00e6133ed720486bd19d878136abcdcd5087b86a..f502b640ffd94ddd999022931425e77248794141 100644 (file)
@@ -92,6 +92,12 @@ class Player(object):
             return True
         return False
 
+    def set_position(self, new_position):
+        if new_position in self.legal_moves():
+            self.position = new_position
+            return True
+        return False
+
     def legal_moves(self):
         positions = [self.position]
         for direction in [BITS.NORTH, BITS.SOUTH, BITS.EAST, BITS.WEST]:
index aa608cef8673c8a4df1b19739b471bbf064c4d25..d4d315af86d2e5a241dd87ac56d6d8fc54da2c1b 100644 (file)
@@ -3,7 +3,7 @@ Widget that holds the game tiles.
 """
 import pygame.locals as pgl
 
-from naja.constants import BOARD_SIZE, TILE_SIZE, KEYS, MOVE, ACT
+from naja.constants import BOARD_SIZE, TILE_SIZE, KEYS, EXAMINE, ACT
 from naja.events import finish_event
 
 from naja.widgets.base import Widget
@@ -18,8 +18,9 @@ class BoardWidget(Widget):
         super(BoardWidget, self).__init__(pos, BOARD_SIZE)
         self.info = info
         self.state = state
-        self.pos = (2, 2)
+        self.card_pos = (2, 2)
         self._tiles = []
+        self.legal = False
         for y in range(0, 5):
             for x in range(0, 5):
                 tile_pos = (pos[0] + x * TILE_SIZE[0],
@@ -28,26 +29,27 @@ class BoardWidget(Widget):
 
     def prepare(self):
         for tile in self._tiles:
-            tile.set_highlight(self.pos)
+            tile.set_highlight(self.card_pos)
             tile.prepare()
         self.size = BOARD_SIZE
-        if self.state.gameboard.player_mode in (ACT, MOVE):
-            self.pos = self.state.player.position
+        if self.state.gameboard.player_mode == ACT:
+            self.card_pos = self.state.player.position
 
     def draw(self, surface):
         for tile in self._tiles:
             tile.draw(surface)
 
     def change_pos(self, offset):
-        tpos = (offset[0] + self.pos[0], offset[1] + self.pos[1])
+        tpos = (offset[0] + self.card_pos[0], offset[1] + self.card_pos[1])
         if tpos[0] < 0 or tpos[1] < 0 or tpos[0] > 4 or tpos[1] > 4:
             return False
-        self.pos = tpos
-        self.info.set_position(self.pos)
+        self.card_pos = tpos
+        self.info.set_position(self.card_pos)
+        self.legal = self.card_pos in self.state.player.legal_moves()
         return True
 
     def handle_event(self, ev):
-        if self.state.gameboard.player_mode in (ACT, MOVE):
+        if self.state.gameboard.player_mode == ACT:
             return super(BoardWidget, self).handle_event(ev)
         if ev.type == pgl.KEYDOWN:
             if ev.key in KEYS.UP:
@@ -62,8 +64,9 @@ class BoardWidget(Widget):
             if ev.key in KEYS.RIGHT:
                 if self.change_pos((+1, 0)):
                     return finish_event()
-            if ev.key in KEYS.SWITCH:
-                self.state.gameboard.change_mode(MOVE)
-                self.info.set_position(self.state.player.position)
+            if (ev.key in KEYS.SELECT and
+                    self.state.player.set_position(self.card_pos)):
+                self.state.gameboard.change_mode(ACT)
+                self.info.set_position(self.card_pos)
                 return finish_event()
         return super(BoardWidget, self).handle_event(ev)
index 866fb86ba2bea38049163c6548766907f7f2cffb..b5231b2a5ba946030869d2c603c74da97a89deff 100644 (file)
@@ -4,7 +4,7 @@ Widget for the game board information area.
 import pygame
 import pygame.locals as pgl
 
-from naja.constants import (INFO_SIZE, EIGHT_BIT_SCALE, MOVE, ACT, KEYS,
+from naja.constants import (INFO_SIZE, EIGHT_BIT_SCALE, ACT, KEYS,
                             EXAMINE)
 from naja.events import finish_event
 from naja.resources import resources
@@ -16,16 +16,14 @@ from naja.widgets.text import TextBoxWidget, TextWidget
 
 
 HINTS = {
-        MOVE: "Move using the arrow keys.\nPress SPACE to stay in place\n"
-              "Press TAB to change to Examine mode",
         ACT: "Choose an action using the Up/Down keys.\n"
              "Press Return to execute the action.",
-        EXAMINE: "Select a card to examine using the arrow keys.\n"
-                 "Press TAB to change to Move mode",
+        EXAMINE: "Select a card to examine using the arrow keys."
         }
 
+HINT_LEGAL_MOVE = "\nPress Return to move to this card."
+
 TITLES = {
-        MOVE: "Move the Robot",
         ACT: "Choose an Action",
         EXAMINE: "Select a Card",
         }
@@ -39,10 +37,11 @@ class InfoAreaWidget(Widget):
         super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
         self.state = state
         self.chosen = None
+        self.card_position = state.player.position
         self.set_position(state.player.position)
 
     def prepare(self):
-        if self.state.gameboard.player_mode in (ACT, MOVE):
+        if self.state.gameboard.player_mode == ACT:
             self.set_position(self.state.player.position)
         self.surface = pygame.surface.Surface(INFO_SIZE)
         self.surface.fill((0, 0, 0))
@@ -62,8 +61,12 @@ class InfoAreaWidget(Widget):
         for choice, action in enumerate(self.card.actions):
             y_offset = self.prepare_action(choice, action, y_offset)
         # We cheat horribly for layout reasons
-        hint = TextBoxWidget((4, 0), HINTS[self.state.gameboard.player_mode],
-                             padding=2,
+        hint_text = HINTS[self.state.gameboard.player_mode]
+        if self.state.gameboard.player_mode == EXAMINE:
+            if self.card_position in self.state.player.legal_moves():
+                hint_text += HINT_LEGAL_MOVE
+
+        hint = TextBoxWidget((4, 0), hint_text, padding=2,
                              box_width=(INFO_SIZE[0] - 4) // EIGHT_BIT_SCALE)
         hint.prepare()
         y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
@@ -95,7 +98,8 @@ class InfoAreaWidget(Widget):
         return y_offset + text.surface.get_rect().height + 16
 
     def set_position(self, position):
-        self.card = self.state.board_locations[position]
+        self.card_position = position
+        self.card = self.state.board_locations[self.card_position]
         if self.state.gameboard.player_mode == ACT:
             if self.chosen is None:
                 self.chosen = 0
@@ -106,7 +110,7 @@ class InfoAreaWidget(Widget):
         surface.blit(self.surface, self.pos)
 
     def handle_event(self, ev):
-        if self.state.gameboard.player_mode in (MOVE, EXAMINE):
+        if self.state.gameboard.player_mode == EXAMINE:
             return super(InfoAreaWidget, self).handle_event(ev)
         if ev.type == pgl.KEYDOWN:
             if ev.key in KEYS.SELECT:
@@ -117,7 +121,8 @@ class InfoAreaWidget(Widget):
                 else:
                     action.perform_action(self.state.gameboard, self.card)
                     self.state.gameboard.replace_card(player.position)
-                    self.state.gameboard.change_mode(MOVE)
+                    self.state.gameboard.change_mode(EXAMINE)
+                    self.set_position(player.position)
                 return finish_event()
             if ev.key in KEYS.UP:
                 if self.chosen > 0:
index 292c6bb74e55f7de3990894c62ff106fc5f8ce9e..2eeb1d39cd2d663d514989a17a4e6557e9e7a80b 100644 (file)
@@ -2,8 +2,7 @@
 
 import pygame.locals as pgl
 
-from naja.constants import (PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS,
-                            EXAMINE, MOVE)
+from naja.constants import (PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS)
 from naja.events import finish_event
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
@@ -38,31 +37,3 @@ class RobotWidget(Widget):
 
     def draw(self, surface):
         surface.blit(self.surface, self.rect)
-
-    def handle_event(self, ev):
-        if self.state.gameboard.player_mode in (ACT, EXAMINE):
-            return super(RobotWidget, self).handle_event(ev)
-        if ev.type == pgl.KEYDOWN:
-            if ev.key in KEYS.UP:
-                if self.state.player.move(BITS.NORTH):
-                    self.state.gameboard.change_mode(ACT)
-                    return finish_event()
-            if ev.key in KEYS.DOWN:
-                if self.state.player.move(BITS.SOUTH):
-                    self.state.gameboard.change_mode(ACT)
-                    return finish_event()
-            if ev.key in KEYS.LEFT:
-                if self.state.player.move(BITS.WEST):
-                    self.state.gameboard.change_mode(ACT)
-                    return finish_event()
-            if ev.key in KEYS.RIGHT:
-                if self.state.player.move(BITS.EAST):
-                    self.state.gameboard.change_mode(ACT)
-                    return finish_event()
-            if ev.key in KEYS.NOMOVE:
-                self.state.gameboard.change_mode(ACT)
-                return finish_event()
-            if ev.key in KEYS.SWITCH:
-                self.state.gameboard.change_mode(EXAMINE)
-                return finish_event()
-        return super(RobotWidget, self).handle_event(ev)
index 85f90057ecd35760aaaf858ddf6be72911ce757e..e559d956f57b943295746c239733f4fc1312680b 100644 (file)
@@ -2,7 +2,7 @@
 import pygame
 import pygame.locals as pgl
 
-from naja.constants import TILE_SIZE, BITS, LOCK_HEIGHT, MOVE, EXAMINE
+from naja.constants import TILE_SIZE, BITS, LOCK_HEIGHT, EXAMINE
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
 from naja.widgets.base import Widget
@@ -31,7 +31,7 @@ class TileWidget(Widget):
         # Draw background
         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
         legal_move = (self.board_pos in self.state.player.legal_moves())
-        if self.state.gameboard.player_mode in (MOVE, EXAMINE) and legal_move:
+        if self.state.gameboard.player_mode == EXAMINE and legal_move:
             bg = resources.get_image('board/tile_available.png',
                                      transforms=(EIGHT_BIT,))
         elif (x + y) % 2 == 0: