7 import pygame.locals as pgl
10 import pymunk.pygame_util
12 from .base import BaseScene
13 from ..lights import LightManager, light_fitting_by_type
14 from ..obstacles import ObstacleManager
15 from ..events import SceneChangeEvent
16 from ..utils import debug_timer, shadowed_text
17 from ..loader import loader
18 from ..transforms import Overlay, Alpha, ColourWedges
20 from ..constants import SCREEN_SIZE, FONTS
21 from ..widgets import ImageButton
22 from ..turnip import Turnip, TurnipInvalidPosition
25 class DayScene(BaseScene):
27 BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
29 def enter(self, gamestate):
30 self._space = pymunk.Space()
31 self._toolbar_font = loader.load_font(FONTS['sans'], size=20)
32 self._obstacles = ObstacleManager(self._space, gamestate)
33 self._lights = LightManager(self._space, gamestate)
35 self._seeds = gamestate.seeds
36 self._harvested = gamestate.harvested
39 self._light_colors = None
42 self.grow_turnips(gamestate)
44 self._light_toolbar = []
45 self._tools = self.create_tools(gamestate)
46 self._update_toolbar(gamestate)
48 self._soil = loader.load_image(
49 "textures", "soil.png", transform=self.BRIGHTNESS)
51 self._game_over_text = []
52 if self._seeds == 0 and len(self._turnips) == 0:
53 self._draw_game_over_text()
55 def _draw_game_over_text(self):
56 overlay = pygame.surface.Surface(
57 (SCREEN_SIZE[0], 240), pgl.SWSURFACE).convert_alpha()
58 overlay.fill((0, 0, 0, 128))
59 self._game_over_text.append((overlay, (0, 250)))
60 self._game_over_text.append(
61 (shadowed_text("You Lost", FONTS["bold"], 48), (400, 280)))
62 self._game_over_text.append(
63 (shadowed_text("You have no seeds and no turnips growing",
64 FONTS["sans"], 24), (250, 350)))
65 self._game_over_text.append(
66 (shadowed_text("Press a key to return to the menu",
67 FONTS["sans"], 24), (250, 400)))
69 def grow_turnips(self, gamestate):
70 for turnip_data in gamestate.turnips:
71 turnip = Turnip(space=self._space, **turnip_data)
72 # Turnips grow at dawn
78 self._turnips.append(turnip)
80 def create_tools(self, gamestate):
82 x, y, step = 0, SCREEN_SIZE[1] - 40, 50
84 tools.append(ImageButton(
85 '32', 'default_cursor.png', name='reset tool', pos=(x, y)))
87 tools.append(ImageButton('32', 'seed.png', name='seed', pos=(x, y)))
90 for light_config in gamestate.station["available_lights"]:
92 '32', '%s.png' % light_config["type"], name='light',
94 tool.light_config = light_config
98 tools.append(ImageButton(
99 '32', 'night.png', name='start night',
100 pos=(SCREEN_SIZE[0] - 100, y)))
101 tools.append(ImageButton(
102 '32', 'exit.png', name='exit', pos=(SCREEN_SIZE[0] - 50, y)))
105 def exit(self, gamestate):
107 gamestate.seeds = self._seeds
108 gamestate.harvested = self._harvested
109 turnip_data = [turnip.serialize() for turnip in self._turnips]
110 gamestate.turnips = turnip_data
112 @debug_timer("day.render")
113 def render(self, surface, gamestate):
114 surface.blit(self._soil, (0, 0))
116 for turnip in self._turnips:
117 turnip.render(surface)
118 self._lights.render_light(surface)
119 self._obstacles.render(surface)
120 self._lights.render_fittings(surface)
121 surface.blit(self._toolbar, (120, 10), None)
122 for tool in self._tools:
124 for light_tool in self._light_toolbar:
125 light_tool.render(surface)
126 self._draw_cursor(surface)
127 if self._game_over_text:
128 for surf, pos in self._game_over_text:
129 surface.blit(surf, pos)
131 def _draw_light_toolbar(self, light_config, x):
132 height = SCREEN_SIZE[1] - 80
133 self._light_toolbar = []
134 colour_combos = light_config["available_colours"]
135 for combo in colour_combos:
136 colours = combo.split("/")
137 light_fitting = light_fitting_by_type(light_config["type"])
138 light_tool = ImageButton(
139 "32", light_fitting, transform=ColourWedges(colours=colours),
140 pos=(x, height), name=combo)
141 light_tool.colours = colours
142 self._light_toolbar.append(light_tool)
145 def _clear_light_toolbar(self):
146 self._light_toolbar = []
148 def _place_seed(self, gamestate, ev):
151 # We don't want top-left to equal the mouse position,
152 # since that looks weird, but we don't want to center
153 # the turnip under the mouse either, since that
154 # causes issues as well, so we compromise
155 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
157 turnip = Turnip(age=0, pos=pos, space=self._space)
158 self._turnips.append(turnip)
160 self._update_toolbar(gamestate)
161 except TurnipInvalidPosition:
162 # TODO: Add error sound or something
165 def _update_light_angle(self, pos, gamestate):
166 # Update the angle of the given light
167 pos = pymunk.pygame_util.to_pygame(pos, pygame.display.get_surface())
168 distance = pos - self._dragging.position
169 angle = math.atan2(distance[1], distance[0])
170 # Set light angle to this position
171 self._dragging.ray_manager.direction = math.degrees(angle)
172 # Hackily update gamestate with new angle
173 for light_cfg in gamestate.station["lights"]:
174 light_pos = pymunk.Vec2d(light_cfg["position"])
175 if light_pos.get_dist_sqrd(self._dragging.position) < 5.0:
176 light_cfg["direction"] = math.degrees(angle)
179 def _place_light(self, gamestate, cfg, colours, ev):
181 cost = cfg.pop("cost")
182 cfg.pop("available_colours")
183 if self._seeds > cost:
184 pos = pymunk.pygame_util.from_pygame(
185 ev.pos, pygame.display.get_surface())
186 # Bail if we're too close to an existing light
187 if self._lights.nearest(pos, max_distance=25):
190 self._update_toolbar(gamestate)
191 cfg["position"] = pos
192 cfg["colours"] = colours
193 gamestate.station["lights"].append(cfg)
194 self._lights.add_light(cfg)
196 def event(self, ev, gamestate):
197 if self._game_over_text:
198 if ev.type in (pgl.KEYDOWN, pgl.MOUSEBUTTONDOWN):
199 from .menu import MenuScene
200 SceneChangeEvent.post(scene=MenuScene())
201 if ev.type == pgl.KEYDOWN:
202 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
203 from .menu import MenuScene
204 SceneChangeEvent.post(scene=MenuScene())
205 elif ev.key == pgl.K_e:
206 from .night import NightScene
207 SceneChangeEvent.post(scene=NightScene())
208 elif ev.key == pgl.K_SPACE:
209 self._paused = not self._paused
210 elif ev.type == pgl.MOUSEBUTTONDOWN:
213 for tool in self._tools:
216 if tool.name == 'reset tool':
219 self._clear_light_toolbar()
220 elif tool.name == 'start night':
221 from .night import NightScene
222 SceneChangeEvent.post(scene=NightScene())
223 elif tool.name == 'exit':
224 from .menu import MenuScene
225 SceneChangeEvent.post(scene=MenuScene())
228 if self._tool.name == 'seed':
230 'seed', transform=Alpha(alpha=172))
231 self._clear_light_toolbar()
232 elif self._tool.name == 'light':
234 self._draw_light_toolbar(
235 self._tool.light_config, 100)
237 # Check light toolbar
238 for light_tool in self._light_toolbar:
239 if light_tool.pressed(ev):
240 fitting_image = light_fitting_by_type(
241 self._tool.light_config["type"])
243 fitting_image[:-4], # strip .png
244 transform=ColourWedges(colours=light_tool.colours))
245 # colour=COLOURS[0] + (172,)))
246 self._light_colors = light_tool.colours
249 if self._tool.name == "seed":
250 self._place_seed(gamestate, ev)
251 elif self._tool.name == "light" and self._light_colors:
253 gamestate, self._tool.light_config,
254 self._light_colors, ev)
256 # Not tool, so check lights
257 self._lights.toggle_nearest(ev.pos, surfpos=True)
259 light = self._lights.nearest(ev.pos, surfpos=True,
262 # Start drag to rotate light
263 self._dragging = light
267 self._clear_light_toolbar()
269 elif ev.type == pgl.MOUSEMOTION:
271 # Calculate angle between current position and mouse pos
272 self._update_light_angle(ev.pos, gamestate)
273 elif ev.type == pgl.MOUSEBUTTONUP:
274 self._dragging = None
276 @debug_timer("day.tick")
277 def tick(self, gamestate):
281 def _update_toolbar(self, gamestate):
282 text = ("Day: %d: Turnip Stocks: Seeds: %d. Planted: %d. "
283 "Harvested: %d. Destroyed: %d" %
284 (gamestate.days, self._seeds, len(self._turnips),
285 self._harvested, gamestate.eaten))
286 self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))