Add exmine mode
authorNeil <neil@dip.sun.ac.za>
Wed, 14 May 2014 20:04:46 +0000 (22:04 +0200)
committerNeil <neil@dip.sun.ac.za>
Wed, 14 May 2014 20:05:48 +0000 (22:05 +0200)
naja/constants.py
naja/gameboard.py
naja/scenes/game.py
naja/widgets/board.py
naja/widgets/info_area.py
naja/widgets/robot.py
naja/widgets/tile.py

index 6e3973da25b26640ac13b9ffcc14d3403833b476..f723fc24af850e2f6e20b519cf3abf8973d6920a 100644 (file)
@@ -58,13 +58,16 @@ PLAYER_SIZE = (64, 96)
 # Player States
 MOVE = 1
 ACT = 2
+EXAMINE = 3
 
 KEYS = AttrDict({
     'UP': (pgl.K_UP, pgl.K_w, pgl.K_COMMA),
     'DOWN': (pgl.K_DOWN, pgl.K_s, pgl.K_o),
     'LEFT': (pgl.K_LEFT, pgl.K_a),
     'RIGHT': (pgl.K_RIGHT, pgl.K_d, pgl.K_e),
+    'NOMOVE': (pgl.K_SPACE,),
 
     'SELECT': (pgl.K_RETURN, pgl.K_KP_ENTER),
     'QUIT': (pgl.K_ESCAPE, pgl.K_q),
+    'SWITCH': (pgl.K_TAB,),
 })
index f3e6f58567ecd962db71cb9c356c3b34ec32a561..dd0861ea0b4005cafa10e1282bee877208c7546e 100644 (file)
@@ -2,7 +2,7 @@ from random import choice
 
 from naja.constants import(
     BITS, DIRECTION_BITS, CONDITION_BITS, PLAYER_DEFAULTS,
-    MOVE, ACT)
+    MOVE, ACT, EXAMINE)
 from naja.player import Player
 from naja import actions
 
@@ -104,12 +104,13 @@ class GameBoard(object):
         location = LocationCard.new_location(choice(self.locations).copy())
         self.board_locations[position] = location
 
-    def change_mode(self):
+    def change_mode(self, new_mode):
         """Advance to the next mode"""
-        if self.player_mode == MOVE:
-            self.player_mode = ACT
-        elif self.player_mode == ACT:
-            self.player_mode = MOVE
+        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):
+            self.player_mode = new_mode
         else:
             raise RuntimeError("Illegal player mode %s" % self.player_mode)
 
index 0d35b7b24a017b43a7492ed98c84f1a2fa4a5cda..c0f52a8be47b9eff08aa4568da6102ddf123207e 100644 (file)
@@ -22,9 +22,10 @@ class GameScene(Scene):
     def __init__(self, state):
         super(GameScene, self).__init__(state)
         self.add(PlayerBitsWidget((0, 0), state))
-        self.add(BoardWidget((0, 60), state))
+        info = InfoAreaWidget((480, 0), state)
+        self.add(BoardWidget((0, 60), state, info))
         self.add(GameBitsWidget((0, 540), state))
-        self.add(InfoAreaWidget((480, 0), state))
+        self.add(info)
         self.add(RobotWidget(state))
 
     def handle_scene_event(self, ev):
index c5eef2ac50ae00784e120fc2a70fc180fa5a7733..aa608cef8673c8a4df1b19739b471bbf064c4d25 100644 (file)
@@ -1,8 +1,11 @@
 """
 Widget that holds the game tiles.
 """
+import pygame.locals as pgl
+
+from naja.constants import BOARD_SIZE, TILE_SIZE, KEYS, MOVE, ACT
+from naja.events import finish_event
 
-from naja.constants import BOARD_SIZE, TILE_SIZE
 from naja.widgets.base import Widget
 from naja.widgets.tile import TileWidget
 
@@ -11,9 +14,11 @@ class BoardWidget(Widget):
     """
     Widget which holds all the tiles that make up the gameboard.
     """
-    def __init__(self, pos, state):
+    def __init__(self, pos, state, info):
         super(BoardWidget, self).__init__(pos, BOARD_SIZE)
-        # FIXME: Placeholder logic
+        self.info = info
+        self.state = state
+        self.pos = (2, 2)
         self._tiles = []
         for y in range(0, 5):
             for x in range(0, 5):
@@ -23,9 +28,42 @@ class BoardWidget(Widget):
 
     def prepare(self):
         for tile in self._tiles:
+            tile.set_highlight(self.pos)
             tile.prepare()
         self.size = BOARD_SIZE
+        if self.state.gameboard.player_mode in (ACT, MOVE):
+            self.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])
+        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)
+        return True
+
+    def handle_event(self, ev):
+        if self.state.gameboard.player_mode in (ACT, MOVE):
+            return super(BoardWidget, self).handle_event(ev)
+        if ev.type == pgl.KEYDOWN:
+            if ev.key in KEYS.UP:
+                if self.change_pos((0, -1)):
+                    return finish_event()
+            if ev.key in KEYS.DOWN:
+                if self.change_pos((0, +1)):
+                    return finish_event()
+            if ev.key in KEYS.LEFT:
+                if self.change_pos((-1, 0)):
+                    return finish_event()
+            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)
+                return finish_event()
+        return super(BoardWidget, self).handle_event(ev)
index c46c93da04a4b378e6a0739440034af3aa981915..a2406746dea23cc1f85b0958ce4cc015e0f320d0 100644 (file)
@@ -4,7 +4,8 @@ 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, MOVE, ACT, KEYS,
+                            EXAMINE)
 from naja.events import finish_event
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
@@ -15,14 +16,18 @@ from naja.widgets.text import TextBoxWidget, TextWidget
 
 
 HINTS = {
-        MOVE: "Move using the arrow keys.\nPress SPACE to stay in place",
+        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",
         }
 
 TITLES = {
         MOVE: "Move the Robot",
         ACT: "Choose an Action",
+        EXAMINE: "Select a Card",
         }
 
 
@@ -36,7 +41,8 @@ class InfoAreaWidget(Widget):
         self.set_position(state.player.position)
 
     def prepare(self):
-        self.set_position(self.state.player.position)
+        if self.state.gameboard.player_mode in (ACT, MOVE):
+            self.set_position(self.state.player.position)
         self.surface = pygame.surface.Surface(INFO_SIZE)
         self.surface.fill((0, 0, 0))
         # Extract actions and such from the card
@@ -97,7 +103,7 @@ class InfoAreaWidget(Widget):
         surface.blit(self.surface, self.pos)
 
     def handle_event(self, ev):
-        if self.state.gameboard.player_mode == MOVE:
+        if self.state.gameboard.player_mode in (MOVE, EXAMINE):
             return super(InfoAreaWidget, self).handle_event(ev)
         if ev.type == pgl.KEYDOWN:
             if ev.key in KEYS.SELECT:
@@ -108,7 +114,7 @@ class InfoAreaWidget(Widget):
                 else:
                     action.perform_action(self.state.gameboard, self.card)
                     self.state.gameboard.replace_card(player.position)
-                    self.state.gameboard.change_mode()
+                    self.state.gameboard.change_mode(MOVE)
                 return finish_event()
             if ev.key in KEYS.UP:
                 if self.chosen > 0:
index ddad5808547469f023348bd38dcdd8b0050d5d37..292c6bb74e55f7de3990894c62ff106fc5f8ce9e 100644 (file)
@@ -2,7 +2,8 @@
 
 import pygame.locals as pgl
 
-from naja.constants import PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS
+from naja.constants import (PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS,
+                            EXAMINE, MOVE)
 from naja.events import finish_event
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
@@ -39,26 +40,29 @@ class RobotWidget(Widget):
         surface.blit(self.surface, self.rect)
 
     def handle_event(self, ev):
-        if self.state.gameboard.player_mode == ACT:
+        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()
+                    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()
+                    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()
+                    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()
+                    self.state.gameboard.change_mode(ACT)
                     return finish_event()
-            if ev.key in (pgl.K_SPACE,):
-                self.state.gameboard.change_mode()
+            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 f71cda9b3609e8f2b1a2163159136ec29563ee50..5d083cce710e153913020763983fd94392bc5874 100644 (file)
@@ -2,7 +2,7 @@
 import pygame
 import pygame.locals as pgl
 
-from naja.constants import TILE_SIZE, BITS, LOCK_HEIGHT, MOVE
+from naja.constants import TILE_SIZE, BITS, LOCK_HEIGHT, MOVE, EXAMINE
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
 from naja.widgets.base import Widget
@@ -25,6 +25,7 @@ class TileWidget(Widget):
         super(TileWidget, self).__init__(pos, TILE_SIZE)
         self.state = state
         self.board_pos = board_pos
+        self.highlighted = False
 
     def prepare(self):
         # Draw background
@@ -39,6 +40,9 @@ class TileWidget(Widget):
         else:
             bg = resources.get_image('board/tile_1.png',
                                      transforms=(EIGHT_BIT,))
+        if self.highlighted:
+            bg = resources.get_image('board/tile_selected.png',
+                                     transforms=(EIGHT_BIT,))
         self.surface = pygame.surface.Surface(TILE_SIZE)
         self.surface.blit(bg, (0, 0))
         # Look up the required bits on the board location
@@ -70,5 +74,11 @@ class TileWidget(Widget):
                 self.surface.blit(img, (x_offset, y_offset))
                 y_offset += LOCK_HEIGHT
 
+    def set_highlight(self, pos):
+        self.highlighted = False
+        if (self.state.gameboard.player_mode == EXAMINE and
+                self.board_pos == pos):
+            self.highlighted = True
+
     def draw(self, surface):
         surface.blit(self.surface, self.pos)