7 import pygame.locals as pgl
10 import pymunk.pygame_util
12 from .base import BaseScene
13 from ..battery import BatteryManager
14 from ..lights import LightManager, light_fitting_by_type, check_space_for_light
15 from ..infobar import InfoBar
16 from ..obstacles import ObstacleManager
17 from ..events import SceneChangeEvent
18 from ..utils import debug_timer, shadowed_text
19 from ..loader import loader
20 from ..transforms import Overlay, Alpha, ColourWedges
22 from ..constants import SCREEN_SIZE, FONTS, FPS, NIGHT_HOURS_PER_TICK, DEBUG
23 from ..widgets import ImageButton
24 from ..turnip import Turnip, TurnipInvalidPosition
27 class DayScene(BaseScene):
29 BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
31 def enter(self, gamestate):
32 self._space = pymunk.Space()
33 self._obstacles = ObstacleManager(self._space, gamestate)
34 self._lights = LightManager(self._space, gamestate)
35 self._battery = BatteryManager(gamestate)
36 self._infobar = InfoBar("day", battery=self._battery, scene=self)
40 self._light_colors = None
43 self.grow_turnips(gamestate)
45 self._light_toolbar = []
46 self._tools = self.create_tools(gamestate)
48 self._soil = loader.load_image(
49 "textures", "soil.png", transform=self.BRIGHTNESS)
51 self._game_over_text = []
52 if gamestate.seeds == 0 and len(self._turnips) == 0:
53 self._draw_you_lose(gamestate)
54 elif gamestate.harvested >= gamestate.turnip_target:
55 self._draw_you_win(gamestate)
57 def _draw_you_lose(self, gamestate):
58 overlay = pygame.surface.Surface(
59 (SCREEN_SIZE[0], 240), pgl.SWSURFACE).convert_alpha()
60 overlay.fill((0, 0, 0, 128))
61 self._game_over_text.append((overlay, (0, 250)))
62 self._game_over_text.append(
63 (shadowed_text("You Lost", FONTS["bold"], 48), (400, 280)))
64 self._game_over_text.append(
65 (shadowed_text("You have no seeds and no turnips growing",
66 FONTS["sans"], 24), (300, 350)))
67 self._game_over_text.append(
68 (shadowed_text("Press a key to return to the menu",
69 FONTS["sans"], 24), (350, 400)))
71 def _draw_you_win(self, gamestate):
72 overlay = pygame.surface.Surface(
73 (SCREEN_SIZE[0], 240), pgl.SWSURFACE).convert_alpha()
74 overlay.fill((0, 0, 0, 128))
75 self._game_over_text.append((overlay, (0, 250)))
76 self._game_over_text.append(
77 (shadowed_text("You Win", FONTS["bold"], 48), (400, 280)))
78 self._game_over_text.append(
80 ("You have Successfully Harvested %d turnips" %
84 self._game_over_text.append(
85 (shadowed_text("Press a key to return to the menu",
86 FONTS["sans"], 24), (350, 400)))
88 def grow_turnips(self, gamestate):
89 for turnip_data in gamestate.turnips:
90 turnip = Turnip(space=self._space, **turnip_data)
91 # Turnips grow at dawn
94 gamestate.seeds += seeds
95 gamestate.harvested += 1
97 self._turnips.append(turnip)
99 def create_tools(self, gamestate):
102 x, y, step = 50, SCREEN_SIZE[1] - 40, 50
104 tools.append(ImageButton('32', 'seed.png', name='seed', pos=(x, y)))
107 for light_config in gamestate.station["available_lights"]:
109 '32', '%s.png' % light_config["type"], name='light',
111 tool.light_config = light_config
115 tools.append(ImageButton(
116 '32', 'default_cursor.png', name='reset tool', pos=(x, y)))
118 tools.append(ImageButton(
119 '32', 'night.png', name='start night',
120 pos=(SCREEN_SIZE[0] - 100, y)))
121 tools.append(ImageButton(
122 '32', 'exit.png', name='exit', pos=(SCREEN_SIZE[0] - 50, y)))
125 def exit(self, gamestate):
127 turnip_data = [turnip.serialize() for turnip in self._turnips]
128 gamestate.turnips = turnip_data
130 def end_day(self, gamestate):
131 self._battery.apply_recharge()
132 gamestate.update_lights(self._lights)
133 from .night import NightScene
134 SceneChangeEvent.post(scene=NightScene())
137 def turnip_count(self):
138 return len(self._turnips)
141 def power_usage(self):
142 power = self._lights.total_power_usage()
143 power = power / (FPS * NIGHT_HOURS_PER_TICK)
144 return int(round(power))
146 @debug_timer("day.render")
147 def render(self, surface, gamestate):
148 surface.blit(self._soil, (0, 0))
150 for turnip in self._turnips:
151 turnip.render(surface)
152 self._lights.render_light(surface)
153 self._obstacles.render(surface)
154 self._lights.render_fittings(surface)
155 self._infobar.render(surface, gamestate)
156 for tool in self._tools:
158 for light_tool in self._light_toolbar:
159 light_tool.render(surface)
160 self._draw_cursor(surface)
161 if self._game_over_text:
162 for surf, pos in self._game_over_text:
163 surface.blit(surf, pos)
165 def _draw_light_toolbar(self, light_config, x):
166 height = SCREEN_SIZE[1] - 80
167 self._light_toolbar = []
168 colour_combos = light_config["available_colours"]
169 for combo in colour_combos:
170 colours = combo.split("/")
171 light_fitting = light_fitting_by_type(light_config["type"])
172 light_tool = ImageButton(
173 "32", light_fitting, transform=ColourWedges(colours=colours),
174 pos=(x, height), name=combo)
175 light_tool.colours = colours
176 self._light_toolbar.append(light_tool)
179 def _clear_light_toolbar(self):
180 self._light_toolbar = []
182 def _place_seed(self, gamestate, ev):
183 if gamestate.seeds > 0:
185 # We don't want top-left to equal the mouse position,
186 # since that looks weird, but we don't want to center
187 # the turnip under the mouse either, since that
188 # causes issues as well, so we compromise
189 pos = (ev.pos[0] - 18, ev.pos[1] - 18)
191 turnip = Turnip(age=0, pos=pos, space=self._space)
192 self._turnips.append(turnip)
194 except TurnipInvalidPosition:
195 # TODO: Add error sound or something
198 def _update_light_angle(self, pos, gamestate):
199 # Update the angle of the given light
200 pos = pymunk.pygame_util.to_pygame(pos, pygame.display.get_surface())
201 distance = pos - self._dragging.position
202 angle = math.atan2(distance[1], distance[0])
203 # Set light angle to this position
204 self._dragging.ray_manager.direction = math.degrees(angle)
205 # Hackily update gamestate with new angle
206 for light_cfg in gamestate.station["lights"]:
207 light_pos = pymunk.Vec2d(light_cfg["position"])
208 if light_pos.get_dist_sqrd(self._dragging.position) < 5.0:
209 light_cfg["direction"] = math.degrees(angle)
212 def _place_light(self, gamestate, cfg, colours, ev):
214 cost = cfg.pop("cost")
215 cfg.pop("available_colours")
216 if gamestate.seeds > cost:
217 pos = pymunk.pygame_util.from_pygame(
218 ev.pos, pygame.display.get_surface())
219 # Bail if we're too close to an existing light, obstacle or turnip
220 if check_space_for_light(self._space, pos, max_distance=25):
222 gamestate.seeds -= cost
223 cfg["position"] = pos
224 cfg["colours"] = colours
225 gamestate.station["lights"].append(cfg)
226 self._lights.add_light(cfg)
228 def event(self, ev, gamestate):
229 if self._game_over_text:
230 if ev.type in (pgl.KEYDOWN, pgl.MOUSEBUTTONDOWN):
231 from .menu import MenuScene
232 SceneChangeEvent.post(scene=MenuScene())
233 if ev.type == pgl.KEYDOWN:
234 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
235 from .menu import MenuScene
236 SceneChangeEvent.post(scene=MenuScene())
237 elif ev.key == pgl.K_e:
238 self.end_day(gamestate)
239 elif ev.key == pgl.K_SPACE and DEBUG:
240 self._paused = not self._paused
241 elif ev.type == pgl.MOUSEBUTTONDOWN:
244 for tool in self._tools:
247 if tool.name == 'reset tool':
250 self._clear_light_toolbar()
251 elif tool.name == 'start night':
252 self.end_day(gamestate)
253 elif tool.name == 'exit':
254 from .menu import MenuScene
255 SceneChangeEvent.post(scene=MenuScene())
258 if self._tool.name == 'seed':
260 'seed', transform=Alpha(alpha=172))
261 self._clear_light_toolbar()
262 elif self._tool.name == 'light':
264 self._draw_light_toolbar(
265 self._tool.light_config, 100)
267 # Check light toolbar
268 for light_tool in self._light_toolbar:
269 if light_tool.pressed(ev):
270 fitting_image = light_fitting_by_type(
271 self._tool.light_config["type"])
273 fitting_image[:-4], # strip .png
274 transform=ColourWedges(colours=light_tool.colours))
275 # colour=COLOURS[0] + (172,)))
276 self._light_colors = light_tool.colours
279 if self._tool.name == "seed":
280 self._place_seed(gamestate, ev)
281 elif self._tool.name == "light" and self._light_colors:
283 gamestate, self._tool.light_config,
284 self._light_colors, ev)
286 # Not tool, so check lights
287 self._lights.toggle_nearest(ev.pos, surfpos=True)
289 light = self._lights.nearest(ev.pos, surfpos=True,
292 # Start drag to rotate light
293 self._dragging = light
297 self._clear_light_toolbar()
299 elif ev.type == pgl.MOUSEMOTION:
301 # Calculate angle between current position and mouse pos
302 self._update_light_angle(ev.pos, gamestate)
303 elif ev.type == pgl.MOUSEBUTTONUP:
304 self._dragging = None
306 @debug_timer("day.tick")
307 def tick(self, gamestate):