PEP8 fix.
[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
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         for turnip_data in gamestate.turnips:
37             turnip = Turnip(space=self._space, **turnip_data)
38             # Turnips grow at dawn
39             seeds = turnip.grow()
40             if seeds:
41                 self._seeds += seeds
42                 self._harvested += 1
43             else:
44                 self._turnips.append(turnip)
45         # Tools
46         self._tools = [
47             ImageButton('32', 'seed.png', name='seed',
48                         pos=(50, SCREEN_SIZE[1] - 40)),
49             ImageButton('32', 'spotlight.png', name='blue_spotlight',
50                         pos=(100, SCREEN_SIZE[1] - 40),
51                         transform=Multiply(colour=(0, 0, 255, 255))),
52             ImageButton('32', 'spotlight.png', name='red_spotlight',
53                         pos=(150, SCREEN_SIZE[1] - 40),
54                         transform=Multiply(colour=(255, 0, 0, 255))),
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         self._draw_cursor(surface)
83
84     def _place_seed(self, gamestate, ev):
85         if self._seeds > 0:
86             # plant seed
87             # We don't want top-left to equal the mouse position,
88             # since that looks weird, but we don't want to center
89             # the turnip under the mouse either, since that
90             # causes issues as well, so we compromise
91             pos = (ev.pos[0] - 8, ev.pos[1] - 8)
92             try:
93                 turnip = Turnip(age=0, pos=pos, space=self._space)
94                 self._turnips.append(turnip)
95                 self._seeds -= 1
96                 self._update_toolbar(gamestate)
97             except TurnipInvalidPosition as e:
98                 # TODO: Add error sound or something
99                 pass
100
101     def _place_spotlight(self, gamestate, colour, ev):
102         if self._seeds > 5:
103             pos = pymunk.pygame_util.from_pygame(ev.pos,
104                                                  pygame.display.get_surface())
105             # Bail if we're too close to an existing light
106             if self._lights.nearest(pos, max_distance=25):
107                 return
108             self._seeds -= 5
109             self._update_toolbar(gamestate)
110             cfg = {
111                 "type": "spotlight",
112                 "colour": colour,
113                 "position": pos,
114                 "angle_limits": [0, 90],
115                 "intensity": 0.5,
116                 "radius_limits": [0, 100],
117             }
118             gamestate.station["lights"].append(cfg)
119             self._lights.add_light(cfg)
120
121     def event(self, ev, gamestate):
122         if ev.type == pgl.KEYDOWN:
123             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
124                 from .menu import MenuScene
125                 SceneChangeEvent.post(scene=MenuScene())
126             if ev.key == pgl.K_e:
127                 from .night import NightScene
128                 SceneChangeEvent.post(scene=NightScene())
129             if ev.key == pgl.K_SPACE:
130                 self._paused = not self._paused
131         elif ev.type == pgl.MOUSEBUTTONDOWN:
132             if ev.button == 1:
133                 # Check tools
134                 for tool in self._tools:
135                     if tool.pressed(ev):
136                         if tool.name == 'reset tool':
137                             self._unset_cursor()
138                             self._tool = None
139                         else:
140                             self._tool = tool.name
141                             if self._tool == 'seed':
142                                 self._set_cursor(
143                                     'seed', transform=Alpha(alpha=172))
144                             elif self._tool == 'red_spotlight':
145                                 self._set_cursor(
146                                     'spotlight',
147                                     transform=Multiply(
148                                         colour=(255, 0, 0, 172)))
149                             elif self._tool == 'blue_spotlight':
150                                 self._set_cursor(
151                                     'spotlight',
152                                     transform=Multiply(
153                                         colour=(0, 0, 255, 172)))
154                         return
155                 if self._tool == "seed":
156                     self._place_seed(gamestate, ev)
157                 elif self._tool == 'red_spotlight':
158                     self._place_spotlight(gamestate, 'red', ev)
159                 elif self._tool == 'blue_spotlight':
160                     self._place_spotlight(gamestate, 'blue', ev)
161                 else:
162                     # Not tool, so check lights
163                     self._lights.toggle_nearest(ev.pos, surfpos=True)
164                     print self._lights.lit_by(ev.pos, surfpos=True)
165             elif ev.button == 3 and self._tool:
166                 self._tool = None
167                 self._unset_cursor()
168
169     @debug_timer("day.tick")
170     def tick(self, gamestate):
171         if not self._paused:
172             self._lights.tick()
173
174     def _update_toolbar(self, gamestate):
175         text = ("Turnip Stocks: Seeds: %d. Planted: %d. "
176                 "Harvested: %d. Destroyed: %d" %
177                 (self._seeds, len(self._turnips),
178                  self._harvested, gamestate.eaten))
179         self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))