Multi-level light toolbar
[tabakrolletjie.git] / tabakrolletjie / scenes / day.py
1 """ Be prepared. """
2
3 import pygame.display
4 import pygame.locals as pgl
5
6 import pymunk
7 import pymunk.pygame_util
8
9 from .base import BaseScene
10 from ..lights import LightManager
11 from ..obstacles import ObstacleManager
12 from ..events import SceneChangeEvent
13 from ..utils import debug_timer
14 from ..loader import loader
15 from ..transforms import Overlay, Multiply, Alpha
16
17 from ..constants import SCREEN_SIZE, FONTS, COLOURS
18 from ..widgets import ImageButton
19 from ..turnip import Turnip, TurnipInvalidPosition
20
21
22 class DayScene(BaseScene):
23
24     BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
25
26     def enter(self, gamestate):
27         self._space = pymunk.Space()
28         self._toolbar_font = loader.load_font(FONTS['sans'], size=20)
29         self._obstacles = ObstacleManager(self._space, gamestate)
30         self._lights = LightManager(self._space, gamestate)
31         self._turnips = []
32         self._seeds = gamestate.seeds
33         self._harvested = gamestate.harvested
34         self._paused = False
35         self._tool = None
36         self._light_color = None
37         for turnip_data in gamestate.turnips:
38             turnip = Turnip(space=self._space, **turnip_data)
39             # Turnips grow at dawn
40             seeds = turnip.grow()
41             if seeds:
42                 self._seeds += seeds
43                 self._harvested += 1
44             else:
45                 self._turnips.append(turnip)
46         # Tools
47         self._light_toolbar = []
48         self._tools = [
49             ImageButton('32', 'seed.png', name='seed',
50                         pos=(50, SCREEN_SIZE[1] - 40)),
51             ImageButton('32', 'spotlight.png', name='spotlight',
52                         pos=(100, SCREEN_SIZE[1] - 40)),
53             ImageButton('32', 'lamp.png', name='lamp',
54                         pos=(150, SCREEN_SIZE[1] - 40)),
55             ImageButton('32', 'default_cursor.png', name='reset tool',
56                         pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
57         ]
58         self._update_toolbar(gamestate)
59         # Background
60         self._soil = loader.load_image(
61             "textures", "soil.png", transform=self.BRIGHTNESS)
62
63     def exit(self, gamestate):
64         self._unset_cursor()
65         gamestate.seeds = self._seeds
66         gamestate.harvested = self._harvested
67         turnip_data = [turnip.serialize() for turnip in self._turnips]
68         gamestate.turnips = turnip_data
69
70     @debug_timer("day.render")
71     def render(self, surface, gamestate):
72         surface.blit(self._soil, (0, 0))
73
74         for turnip in self._turnips:
75             turnip.render(surface)
76         self._lights.render_light(surface)
77         self._obstacles.render(surface)
78         self._lights.render_fittings(surface)
79         surface.blit(self._toolbar, (120, 10), None)
80         for tool in self._tools:
81             tool.render(surface)
82         for light_tool in self._light_toolbar:
83             light_tool.render(surface)
84         self._draw_cursor(surface)
85
86     def _draw_light_toolbar(self, light_type, x):
87         self._light_toolbar = []
88         height = SCREEN_SIZE[1] - 80
89         for color in sorted(COLOURS.keys()):
90             light_tool = ImageButton('32', light_type + '.png',
91                                      pos=(x, height), name=color,
92                                      transform=Multiply(colour=COLOURS[color]))
93             self._light_toolbar.append(light_tool)
94             x += 40
95
96     def _clear_light_toolbar(self):
97         self._light_toolbar = []
98
99     def _place_seed(self, gamestate, ev):
100         if self._seeds > 0:
101             # plant seed
102             # We don't want top-left to equal the mouse position,
103             # since that looks weird, but we don't want to center
104             # the turnip under the mouse either, since that
105             # causes issues as well, so we compromise
106             pos = (ev.pos[0] - 8, ev.pos[1] - 8)
107             try:
108                 turnip = Turnip(age=0, pos=pos, space=self._space)
109                 self._turnips.append(turnip)
110                 self._seeds -= 1
111                 self._update_toolbar(gamestate)
112             except TurnipInvalidPosition as e:
113                 # TODO: Add error sound or something
114                 pass
115
116     def _place_spotlight(self, gamestate, colour, ev):
117         if self._seeds > 5:
118             pos = pymunk.pygame_util.from_pygame(ev.pos,
119                                                  pygame.display.get_surface())
120             # Bail if we're too close to an existing light
121             if self._lights.nearest(pos, max_distance=25):
122                 return
123             self._seeds -= 5
124             self._update_toolbar(gamestate)
125             cfg = {
126                 "type": "spotlight",
127                 "colour": colour,
128                 "position": pos,
129                 "angle_limits": [0, 90],
130                 "intensity": 0.5,
131                 "radius_limits": [0, 100],
132             }
133             gamestate.station["lights"].append(cfg)
134             self._lights.add_light(cfg)
135
136     def _place_lamp(self, gamestate, colour, ev):
137         if self._seeds > 3:
138             pos = pymunk.pygame_util.from_pygame(ev.pos,
139                                                  pygame.display.get_surface())
140             # Bail if we're too close to an existing light
141             if self._lights.nearest(pos, max_distance=25):
142                 return
143             self._seeds -= 3
144             self._update_toolbar(gamestate)
145             cfg = {
146                 "type": "lamp",
147                 "colour": colour,
148                 "position": pos,
149                 "intensity": 0.5,
150             }
151             gamestate.station["lights"].append(cfg)
152             self._lights.add_light(cfg)
153
154     def event(self, ev, gamestate):
155         if ev.type == pgl.KEYDOWN:
156             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
157                 from .menu import MenuScene
158                 SceneChangeEvent.post(scene=MenuScene())
159             if ev.key == pgl.K_e:
160                 from .night import NightScene
161                 SceneChangeEvent.post(scene=NightScene())
162             if ev.key == pgl.K_SPACE:
163                 self._paused = not self._paused
164         elif ev.type == pgl.MOUSEBUTTONDOWN:
165             if ev.button == 1:
166                 # Check tools
167                 for tool in self._tools:
168                     if tool.pressed(ev):
169                         self._color = None
170                         if tool.name == 'reset tool':
171                             self._unset_cursor()
172                             self._tool = None
173                             self._clear_light_toolbar()
174                         else:
175                             self._tool = tool.name
176                             if self._tool == 'seed':
177                                 self._set_cursor(
178                                     'seed', transform=Alpha(alpha=172))
179                                 self._clear_light_toolbar()
180                             elif self._tool == 'spotlight':
181                                 self._unset_cursor()
182                                 self._draw_light_toolbar('spotlight', 100)
183                             elif self._tool == 'lamp':
184                                 self._unset_cursor()
185                                 self._draw_light_toolbar('lamp', 150)
186                         return
187                 # Check light toolbar
188                 for light_tool in self._light_toolbar:
189                     if light_tool.pressed(ev):
190                         self._set_cursor(
191                             self._tool,
192                             transform=Multiply(
193                                 colour=COLOURS[light_tool.name] + (172,)))
194                         self._light_color = light_tool.name
195                         return
196                 if self._tool == "seed":
197                     self._place_seed(gamestate, ev)
198                 elif self._tool == 'spotlight' and self._light_color:
199                     self._place_spotlight(gamestate, self._light_color, ev)
200                 elif self._tool == 'lamp' and self._light_color:
201                     self._place_lamp(gamestate, self._light_color, ev)
202                 else:
203                     # Not tool, so check lights
204                     self._lights.toggle_nearest(ev.pos, surfpos=True)
205                     print self._lights.lit_by(ev.pos, surfpos=True)
206             elif ev.button == 3 and self._tool:
207                 self._tool = None
208                 self._unset_cursor()
209
210     @debug_timer("day.tick")
211     def tick(self, gamestate):
212         if not self._paused:
213             self._lights.tick()
214
215     def _update_toolbar(self, gamestate):
216         text = ("Turnip Stocks: Seeds: %d. Planted: %d. "
217                 "Harvested: %d. Destroyed: %d" %
218                 (self._seeds, len(self._turnips),
219                  self._harvested, gamestate.eaten))
220         self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))