7 import pygame.locals as pgl
10 import pymunk.pygame_util
12 from .base import BaseScene
13 from ..lights import LightManager
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, Multiply, Alpha
20 from ..constants import SCREEN_SIZE, FONTS, COLOURS
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_color = 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', pos=(SCREEN_SIZE[0] - 100, y)))
100 tools.append(ImageButton(
101 '32', 'exit.png', name='exit', pos=(SCREEN_SIZE[0] - 50, y)))
104 def exit(self, gamestate):
106 gamestate.seeds = self._seeds
107 gamestate.harvested = self._harvested
108 turnip_data = [turnip.serialize() for turnip in self._turnips]
109 gamestate.turnips = turnip_data
111 @debug_timer("day.render")
112 def render(self, surface, gamestate):
113 surface.blit(self._soil, (0, 0))
115 for turnip in self._turnips:
116 turnip.render(surface)
117 self._lights.render_light(surface)
118 self._obstacles.render(surface)
119 self._lights.render_fittings(surface)
120 surface.blit(self._toolbar, (120, 10), None)
121 for tool in self._tools:
123 for light_tool in self._light_toolbar:
124 light_tool.render(surface)
125 self._draw_cursor(surface)
126 if self._game_over_text:
127 for surf, pos in self._game_over_text:
128 surface.blit(surf, pos)
130 def _draw_light_toolbar(self, light_config, x):
131 self._light_toolbar = []
132 height = SCREEN_SIZE[1] - 80
133 for color in sorted(COLOURS.keys()):
134 light_tool = ImageButton('32', light_config["type"] + '.png',
135 pos=(x, height), name=color,
136 transform=Multiply(colour=COLOURS[color]))
137 self._light_toolbar.append(light_tool)
140 def _clear_light_toolbar(self):
141 self._light_toolbar = []
143 def _place_seed(self, gamestate, ev):
146 # We don't want top-left to equal the mouse position,
147 # since that looks weird, but we don't want to center
148 # the turnip under the mouse either, since that
149 # causes issues as well, so we compromise
150 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
152 turnip = Turnip(age=0, pos=pos, space=self._space)
153 self._turnips.append(turnip)
155 self._update_toolbar(gamestate)
156 except TurnipInvalidPosition:
157 # TODO: Add error sound or something
160 def _update_light_angle(self, pos, gamestate):
161 # Update the angle of the given light
162 pos = pymunk.pygame_util.to_pygame(pos, pygame.display.get_surface())
163 distance = pos - self._dragging.position
164 angle = math.atan2(distance[1], distance[0])
165 # Set light angle to this position
166 self._dragging.ray_manager.direction = math.degrees(angle)
167 # Hackily update gamestate with new angle
168 for light_cfg in gamestate.station["lights"]:
169 light_pos = pymunk.Vec2d(light_cfg["position"])
170 if light_pos.get_dist_sqrd(self._dragging.position) < 5.0:
171 light_cfg["direction"] = math.degrees(angle)
174 def _place_light(self, gamestate, cfg, colours, ev):
176 cost = cfg.pop("cost")
177 if self._seeds > cost:
178 pos = pymunk.pygame_util.from_pygame(
179 ev.pos, pygame.display.get_surface())
180 # Bail if we're too close to an existing light
181 if self._lights.nearest(pos, max_distance=25):
184 self._update_toolbar(gamestate)
185 cfg["position"] = pos
186 cfg["colours"] = colours
187 gamestate.station["lights"].append(cfg)
188 self._lights.add_light(cfg)
190 def event(self, ev, gamestate):
191 if self._game_over_text:
192 if ev.type in (pgl.KEYDOWN, pgl.MOUSEBUTTONDOWN):
193 from .menu import MenuScene
194 SceneChangeEvent.post(scene=MenuScene())
195 if ev.type == pgl.KEYDOWN:
196 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
197 from .menu import MenuScene
198 SceneChangeEvent.post(scene=MenuScene())
199 elif ev.key == pgl.K_e:
200 from .night import NightScene
201 SceneChangeEvent.post(scene=NightScene())
202 elif ev.key == pgl.K_SPACE:
203 self._paused = not self._paused
204 elif ev.type == pgl.MOUSEBUTTONDOWN:
207 for tool in self._tools:
210 if tool.name == 'reset tool':
213 self._clear_light_toolbar()
214 elif tool.name == 'start night':
215 from .night import NightScene
216 SceneChangeEvent.post(scene=NightScene())
217 elif tool.name == 'exit':
218 from .menu import MenuScene
219 SceneChangeEvent.post(scene=MenuScene())
222 if self._tool.name == 'seed':
224 'seed', transform=Alpha(alpha=172))
225 self._clear_light_toolbar()
226 elif self._tool.name == 'light':
228 self._draw_light_toolbar(
229 self._tool.light_config, 100)
231 # Check light toolbar
232 for light_tool in self._light_toolbar:
233 if light_tool.pressed(ev):
235 self._tool.light_config["type"],
237 colour=COLOURS[light_tool.name] + (172,)))
238 self._light_color = light_tool.name
240 if self._tool.name == "seed":
241 self._place_seed(gamestate, ev)
242 elif self._tool.name == "light" and self._light_color:
244 gamestate, self._tool.light_config,
245 [self._light_color], ev)
247 # Not tool, so check lights
248 self._lights.toggle_nearest(ev.pos, surfpos=True)
250 light = self._lights.nearest(ev.pos, surfpos=True,
253 # Start drag to rotate light
254 self._dragging = light
259 elif ev.type == pgl.MOUSEMOTION:
261 # Calculate angle between current position and mouse pos
262 self._update_light_angle(ev.pos, gamestate)
263 elif ev.type == pgl.MOUSEBUTTONUP:
264 self._dragging = None
266 @debug_timer("day.tick")
267 def tick(self, gamestate):
271 def _update_toolbar(self, gamestate):
272 text = ("Day: %d: Turnip Stocks: Seeds: %d. Planted: %d. "
273 "Harvested: %d. Destroyed: %d" %
274 (gamestate.days, self._seeds, len(self._turnips),
275 self._harvested, gamestate.eaten))
276 self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))