Properly implement player movement.
authorJeremy Thurgood <firxen@gmail.com>
Tue, 13 May 2014 20:06:18 +0000 (22:06 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Tue, 13 May 2014 20:06:18 +0000 (22:06 +0200)
naja/events.py
naja/widgets/info_area.py
naja/widgets/robot.py

index 1b57ee0dc3244bc0c7dc2077485f7671b0ec6ad5..11adab0f7528cbd68e88adf32275d4ab39ff8be9 100644 (file)
@@ -41,3 +41,8 @@ class InvalidateTheWorld(NajaEvent):
 
 class SelectEvent(NajaEvent):
     TYPE = "SELECT"
 
 class SelectEvent(NajaEvent):
     TYPE = "SELECT"
+
+
+class PlayerMoved(NajaEvent):
+    # This is used to signal to widgets that care that the player has moved.
+    TYPE = "PLAYER_MOVED"
index 4c79f07d330641123285efb5d7e928e32f7fee7b..a2956a0d139a4d1d1cd369fed03cccddf6769228 100644 (file)
@@ -5,7 +5,7 @@ import pygame
 import pygame.locals as pgl
 
 from naja.constants import INFO_SIZE, EIGHT_BIT_SCALE, MOVE, ACT, KEYS
 import pygame.locals as pgl
 
 from naja.constants import INFO_SIZE, EIGHT_BIT_SCALE, MOVE, ACT, KEYS
-from naja.events import InvalidateTheWorld
+from naja.events import InvalidateTheWorld, PlayerMoved
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
 
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
 
@@ -32,15 +32,12 @@ class InfoAreaWidget(Widget):
     """
     def __init__(self, pos, state):
         super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
     """
     def __init__(self, pos, state):
         super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
-        self.card = None
         self.state = state
         self.state = state
-        self.chosen = 0
+        self.set_position(state.player.position)
 
     def prepare(self):
         self.surface = pygame.surface.Surface(INFO_SIZE)
         self.surface.fill((0, 0, 0))
 
     def prepare(self):
         self.surface = pygame.surface.Surface(INFO_SIZE)
         self.surface.fill((0, 0, 0))
-        # Quick hack for testing
-        self.card = self.state.board_locations[self.state.player.position]
         # Extract actions and such from the card
         title = TextWidget((0, 0), TITLES[self.state.gameboard.player_mode],
                            colour=(255, 255, 255))
         # Extract actions and such from the card
         title = TextWidget((0, 0), TITLES[self.state.gameboard.player_mode],
                            colour=(255, 255, 255))
@@ -72,13 +69,18 @@ class InfoAreaWidget(Widget):
         y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
         self.surface.blit(hint.surface, (0, y_offset))
 
         y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
         self.surface.blit(hint.surface, (0, y_offset))
 
-    def set_card(self, card):
-        self.card = card
+    def set_position(self, position):
+        self.card = self.state.board_locations[position]
+        self.chosen = 0
 
     def draw(self, surface):
         surface.blit(self.surface, self.pos)
 
     def handle_event(self, ev):
 
     def draw(self, surface):
         surface.blit(self.surface, self.pos)
 
     def handle_event(self, ev):
+        if PlayerMoved.matches(ev):
+            self.set_position(self.state.player.position)
+            return False
+
         if self.state.gameboard.player_mode == MOVE:
             return super(InfoAreaWidget, self).handle_event(ev)
         if ev.type == pgl.KEYDOWN:
         if self.state.gameboard.player_mode == MOVE:
             return super(InfoAreaWidget, self).handle_event(ev)
         if ev.type == pgl.KEYDOWN:
index 9f3afc69948da5eb959b862dc660ddc88ba20f0d..60c3f406ed9b8f08460b8c968e5b56d42e8eb907 100644 (file)
@@ -3,7 +3,7 @@
 import pygame.locals as pgl
 
 from naja.constants import PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS
 import pygame.locals as pgl
 
 from naja.constants import PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS
-from naja.events import InvalidateTheWorld
+from naja.events import InvalidateTheWorld, PlayerMoved
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
 from naja.widgets.base import Widget
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
 from naja.widgets.base import Widget
@@ -45,21 +45,25 @@ class RobotWidget(Widget):
             if ev.key in KEYS.UP:
                 if self.state.player.move(BITS.NORTH):
                     self.state.gameboard.change_mode()
             if ev.key in KEYS.UP:
                 if self.state.player.move(BITS.NORTH):
                     self.state.gameboard.change_mode()
+                    PlayerMoved.post()
                     InvalidateTheWorld.post()
                     return True
             if ev.key in KEYS.DOWN:
                 if self.state.player.move(BITS.SOUTH):
                     self.state.gameboard.change_mode()
                     InvalidateTheWorld.post()
                     return True
             if ev.key in KEYS.DOWN:
                 if self.state.player.move(BITS.SOUTH):
                     self.state.gameboard.change_mode()
+                    PlayerMoved.post()
                     InvalidateTheWorld.post()
                     return True
             if ev.key in KEYS.LEFT:
                 if self.state.player.move(BITS.WEST):
                     self.state.gameboard.change_mode()
                     InvalidateTheWorld.post()
                     return True
             if ev.key in KEYS.LEFT:
                 if self.state.player.move(BITS.WEST):
                     self.state.gameboard.change_mode()
+                    PlayerMoved.post()
                     InvalidateTheWorld.post()
                     return True
             if ev.key in KEYS.RIGHT:
                 if self.state.player.move(BITS.EAST):
                     self.state.gameboard.change_mode()
                     InvalidateTheWorld.post()
                     return True
             if ev.key in KEYS.RIGHT:
                 if self.state.player.move(BITS.EAST):
                     self.state.gameboard.change_mode()
+                    PlayerMoved.post()
                     InvalidateTheWorld.post()
                     return True
             if ev.key in (pgl.K_SPACE,):
                     InvalidateTheWorld.post()
                     return True
             if ev.key in (pgl.K_SPACE,):