Add pause button.
[tabakrolletjie.git] / tabakrolletjie / scenes / day.py
1 """ Be prepared. """
2
3 import pygame.locals as pgl
4
5 import pymunk
6 import pymunk.pygame_util
7
8 from .base import BaseScene
9 from ..lights import LightManager
10 from ..obstacles import ObstacleManager
11 from ..events import SceneChangeEvent
12 from ..utils import debug_timer
13
14 from ..constants import SCREEN_SIZE
15 from ..widgets import ImageButton
16 from ..turnip import Turnip, TurnipInvalidPosition
17
18
19 class DayScene(BaseScene):
20     def enter(self, gamestate):
21         self._space = pymunk.Space()
22         self._obstacles = ObstacleManager(self._space, gamestate)
23         self._lights = LightManager(self._space, gamestate)
24         self._turnips = []
25         self._seeds = gamestate.seeds
26         self._harvested = gamestate.harvested
27         self._paused = False
28         self._tool = None
29         for turnip_data in gamestate.turnips:
30             turnip = Turnip(space=self._space, **turnip_data)
31             # Turnips grow at dawn
32             seeds = turnip.grow()
33             if seeds:
34                 self._seeds += seeds
35                 self._harvested += 1
36             else:
37                 self._turnips.append(turnip)
38         print 'Seeds', self._seeds
39         # Toolbar
40         self._tools = [
41             ImageButton('32', 'seed.png', name='seed',
42                         pos=(50, SCREEN_SIZE[1] - 40)),
43             ImageButton('32', 'default_cursor.png', name='reset tool',
44                         pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
45         ]
46
47     def exit(self, gamestate):
48         self._unset_cursor()
49         gamestate.seeds = self._seeds
50         gamestate.harvested = self._harvested
51         turnip_data = [turnip.serialize() for turnip in self._turnips]
52         gamestate.turnips = turnip_data
53
54     @debug_timer("day.render")
55     def render(self, surface, gamestate):
56         surface.fill((0, 0, 155))
57         self._lights.render_light(surface)
58         self._obstacles.render(surface)
59         self._lights.render_fittings(surface)
60         for turnip in self._turnips:
61             turnip.render(surface)
62         for tool in self._tools:
63             tool.render(surface)
64         self._draw_cursor(surface)
65
66     def event(self, ev, gamestate):
67         if ev.type == pgl.KEYDOWN:
68             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
69                 from .menu import MenuScene
70                 SceneChangeEvent.post(scene=MenuScene())
71             if ev.key == pgl.K_e:
72                 from .night import NightScene
73                 SceneChangeEvent.post(scene=NightScene())
74             if ev.key == pgl.K_SPACE:
75                 self._paused = not self._paused
76         elif ev.type == pgl.MOUSEBUTTONDOWN:
77             if ev.button == 1:
78                 # Check tools
79                 for tool in self._tools:
80                     if tool.pressed(ev):
81                         if tool.name == 'reset tool':
82                             self._unset_cursor()
83                             self._tool = None
84                         else:
85                             self._tool = tool.name
86                             self._set_cursor(tool.name)
87                         return
88                 if self._tool == "seed":
89                     if self._seeds > 0:
90                         # plant seed
91                         # We don't want top-left to equal the mouse position,
92                         # since that looks weird, but we don't want to center
93                         # the turnip under the mouse either, since that
94                         # causes issues as well, so we compromise
95                         pos = (ev.pos[0] - 8, ev.pos[1] - 8)
96                         try:
97                             turnip = Turnip(age=0, pos=pos, space=self._space)
98                             self._turnips.append(turnip)
99                             self._seeds -= 1
100                             self._update_toolbar()
101                         except TurnipInvalidPosition as e:
102                             # TODO: Add error sound or something
103                             pass
104                 else:
105                     # Not tool, so check lights
106                     self._lights.toggle_nearest(ev.pos, surfpos=True)
107                     print self._lights.lit_by(ev.pos, surfpos=True)
108
109     @debug_timer("day.tick")
110     def tick(self, gamestate):
111         if not self._paused:
112             self._lights.tick()
113
114     def _update_toolbar(self):
115         pass