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