Merge branch 'master' of ctpug.org.za:tabakrolletjie
[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             self._seeds -= 5
104             self._update_toolbar(gamestate)
105             pos = pymunk.pygame_util.from_pygame(ev.pos,
106                                                  pygame.display.get_surface())
107             cfg = {
108                 "type": "spotlight",
109                 "colour": colour,
110                 "position": pos,
111                 "direction": 45,
112                 "angle_limits": [0, 90],
113                 "intensity": 0.5,
114                 "radius_limits": [0, 100],
115             }
116             gamestate.station["lights"].append(cfg)
117             self._lights.add_light(cfg)
118
119     def event(self, ev, gamestate):
120         if ev.type == pgl.KEYDOWN:
121             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
122                 from .menu import MenuScene
123                 SceneChangeEvent.post(scene=MenuScene())
124             if ev.key == pgl.K_e:
125                 from .night import NightScene
126                 SceneChangeEvent.post(scene=NightScene())
127             if ev.key == pgl.K_SPACE:
128                 self._paused = not self._paused
129         elif ev.type == pgl.MOUSEBUTTONDOWN:
130             if ev.button == 1:
131                 # Check tools
132                 for tool in self._tools:
133                     if tool.pressed(ev):
134                         if tool.name == 'reset tool':
135                             self._unset_cursor()
136                             self._tool = None
137                         else:
138                             self._tool = tool.name
139                             if self._tool == 'seed':
140                                 self._set_cursor('seed', transform=Alpha(alpha=172))
141                             elif self._tool == 'red_spotlight':
142                                 self._set_cursor(
143                                     'spotlight',
144                                     transform=Multiply(
145                                         colour=(255, 0, 0, 172)))
146                             elif self._tool == 'blue_spotlight':
147                                 self._set_cursor(
148                                     'spotlight',
149                                     transform=Multiply(
150                                         colour=(0, 0, 255, 172)))
151                         return
152                 if self._tool == "seed":
153                     self._place_seed(gamestate, ev)
154                 elif self._tool == 'red_spotlight':
155                     self._place_spotlight(gamestate, 'red', ev)
156                 elif self._tool == 'blue_spotlight':
157                     self._place_spotlight(gamestate, 'blue', ev)
158                 else:
159                     # Not tool, so check lights
160                     self._lights.toggle_nearest(ev.pos, surfpos=True)
161                     print self._lights.lit_by(ev.pos, surfpos=True)
162             elif ev.button == 3 and self._tool:
163                 self._tool = None
164                 self._unset_cursor()
165
166     @debug_timer("day.tick")
167     def tick(self, gamestate):
168         if not self._paused:
169             self._lights.tick()
170
171     def _update_toolbar(self, gamestate):
172         text = ("Turnip Stocks: Seeds: %d. Planted: %d. "
173                 "Harvested: %d. Destroyed: %d" %
174                 (self._seeds, len(self._turnips),
175                  self._harvested, gamestate.eaten))
176         self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))