Add exmine mode
[naja.git] / naja / widgets / robot.py
1 """Widget to draw the player on the screen"""
2
3 import pygame.locals as pgl
4
5 from naja.constants import (PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS,
6                             EXAMINE, MOVE)
7 from naja.events import finish_event
8 from naja.resources import resources
9 from naja.resources.mutators import EIGHT_BIT
10 from naja.widgets.base import Widget
11
12 IMG_MAP = {
13         BITS.CYAN: 'board/robot_cyan.png',
14         BITS.YELLOW: 'board/robot_yellow.png',
15         BITS.MAGENTA: 'board/robot_magenta.png',
16         }
17
18
19 class RobotWidget(Widget):
20     """Widget which holds a tile on the game board."""
21     def __init__(self, state):
22         pos = (state.player.position[0] * TILE_SIZE[0],
23                state.player.position[1] * TILE_SIZE[1] + BIT_SIZE[1])
24         super(RobotWidget, self).__init__(pos, PLAYER_SIZE)
25         self.state = state
26
27     def prepare(self):
28         # Look up the required bits on the board location
29         self.pos = (self.state.player.position[0] * TILE_SIZE[0],
30                     self.state.player.position[1] * TILE_SIZE[1] + BIT_SIZE[1])
31         self.surface = resources.get_image('board/robot.png',
32                                            transforms=(EIGHT_BIT,)).copy()
33         for bit, img_name in IMG_MAP.iteritems():
34             if self.state.player.bits.check_bit(bit):
35                 bit_img = resources.get_image(img_name,
36                                               transforms=(EIGHT_BIT,))
37                 self.surface.blit(bit_img, (0, 0))
38
39     def draw(self, surface):
40         surface.blit(self.surface, self.rect)
41
42     def handle_event(self, ev):
43         if self.state.gameboard.player_mode in (ACT, EXAMINE):
44             return super(RobotWidget, self).handle_event(ev)
45         if ev.type == pgl.KEYDOWN:
46             if ev.key in KEYS.UP:
47                 if self.state.player.move(BITS.NORTH):
48                     self.state.gameboard.change_mode(ACT)
49                     return finish_event()
50             if ev.key in KEYS.DOWN:
51                 if self.state.player.move(BITS.SOUTH):
52                     self.state.gameboard.change_mode(ACT)
53                     return finish_event()
54             if ev.key in KEYS.LEFT:
55                 if self.state.player.move(BITS.WEST):
56                     self.state.gameboard.change_mode(ACT)
57                     return finish_event()
58             if ev.key in KEYS.RIGHT:
59                 if self.state.player.move(BITS.EAST):
60                     self.state.gameboard.change_mode(ACT)
61                     return finish_event()
62             if ev.key in KEYS.NOMOVE:
63                 self.state.gameboard.change_mode(ACT)
64                 return finish_event()
65             if ev.key in KEYS.SWITCH:
66                 self.state.gameboard.change_mode(EXAMINE)
67                 return finish_event()
68         return super(RobotWidget, self).handle_event(ev)