Add turnips to the space and check for collisions when planting them
[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._tool = None
28         for turnip_data in gamestate.turnips:
29             turnip = Turnip(space=self._space, **turnip_data)
30             # Turnips grow at dawn
31             seeds = turnip.grow()
32             if seeds:
33                 self._seeds += seeds
34                 self._harvested += 1
35             else:
36                 self._turnips.append(turnip)
37         print 'Seeds', self._seeds
38         # Toolbar
39         self._tools = [
40             ImageButton('32', 'seed.png', name='seed',
41                         pos=(50, SCREEN_SIZE[1] - 40)),
42             ImageButton('32', 'default_cursor.png', name='reset tool',
43                         pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
44         ]
45
46     def exit(self, gamestate):
47         self._unset_cursor()
48         gamestate.seeds = self._seeds
49         gamestate.harvested = self._harvested
50         turnip_data = [turnip.serialize() for turnip in self._turnips]
51         gamestate.turnips = turnip_data
52
53     @debug_timer("day.render")
54     def render(self, surface, gamestate):
55         surface.fill((0, 0, 155))
56         self._lights.render_light(surface)
57         self._obstacles.render(surface)
58         self._lights.render_fittings(surface)
59         for turnip in self._turnips:
60             turnip.render(surface)
61         for tool in self._tools:
62             tool.render(surface)
63         self._draw_cursor(surface)
64
65     def event(self, ev, gamestate):
66         if ev.type == pgl.KEYDOWN:
67             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
68                 from .menu import MenuScene
69                 SceneChangeEvent.post(scene=MenuScene())
70             if ev.key == pgl.K_e:
71                 from .night import NightScene
72                 SceneChangeEvent.post(scene=NightScene())
73         elif ev.type == pgl.MOUSEBUTTONDOWN:
74             if ev.button == 1:
75                 # Check tools
76                 for tool in self._tools:
77                     if tool.pressed(ev):
78                         if tool.name == 'reset tool':
79                             self._unset_cursor()
80                             self._tool = None
81                         else:
82                             self._tool = tool.name
83                             self._set_cursor(tool.name)
84                         return
85                 if self._tool == "seed":
86                     if self._seeds > 0:
87                         # plant seed
88                         # We don't want top-left to equal the mouse position,
89                         # since that looks weird, but we don't want to center
90                         # the turnip under the mouse either, since that
91                         # causes issues as well, so we compromise
92                         pos = (ev.pos[0] - 8, ev.pos[1] - 8)
93                         try:
94                             turnip = Turnip(age=0, pos=pos, space=self._space)
95                             self._turnips.append(turnip)
96                             self._seeds -= 1
97                             self._update_toolbar()
98                         except TurnipInvalidPosition as e:
99                             # TODO: Add error sound or something
100                             pass
101                 else:
102                     # Not tool, so check lights
103                     self._lights.toggle_nearest(ev.pos, surfpos=True)
104                     print self._lights.lit_by(ev.pos, surfpos=True)
105
106     @debug_timer("day.tick")
107     def tick(self, gamestate):
108         self._lights.tick()
109
110     def _update_toolbar(self):
111         pass