6 import pygame.locals as pgl
9 import pymunk.pygame_util
11 from .base import BaseScene
12 from ..lights import LightManager
13 from ..obstacles import ObstacleManager
14 from ..events import SceneChangeEvent
15 from ..utils import debug_timer
16 from ..loader import loader
17 from ..transforms import Overlay, Multiply, Alpha
19 from ..constants import SCREEN_SIZE, FONTS, COLOURS
20 from ..widgets import ImageButton
21 from ..turnip import Turnip, TurnipInvalidPosition
24 class DayScene(BaseScene):
26 BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
28 def enter(self, gamestate):
29 self._space = pymunk.Space()
30 self._toolbar_font = loader.load_font(FONTS['sans'], size=20)
31 self._obstacles = ObstacleManager(self._space, gamestate)
32 self._lights = LightManager(self._space, gamestate)
34 self._seeds = gamestate.seeds
35 self._harvested = gamestate.harvested
38 self._light_color = None
41 self.grow_turnips(gamestate)
43 self._light_toolbar = []
44 self._tools = self.create_tools(gamestate)
45 self._update_toolbar(gamestate)
47 self._soil = loader.load_image(
48 "textures", "soil.png", transform=self.BRIGHTNESS)
50 def grow_turnips(self, gamestate):
51 for turnip_data in gamestate.turnips:
52 turnip = Turnip(space=self._space, **turnip_data)
53 # Turnips grow at dawn
59 self._turnips.append(turnip)
61 def create_tools(self, gamestate):
63 x, y, step = 0, SCREEN_SIZE[1] - 40, 50
65 tools.append(ImageButton(
66 '32', 'default_cursor.png', name='reset tool', pos=(x, y)))
68 tools.append(ImageButton('32', 'seed.png', name='seed', pos=(x, y)))
71 for light_config in gamestate.station["available_lights"]:
73 '32', '%s.png' % light_config["type"], name='light',
75 tool.light_config = light_config
79 # TODO: will also add back to menu button
80 tools.append(ImageButton(
81 '32', 'night.png', name='start night', pos=(SCREEN_SIZE[0] - 50, y)))
84 def exit(self, gamestate):
86 gamestate.seeds = self._seeds
87 gamestate.harvested = self._harvested
88 turnip_data = [turnip.serialize() for turnip in self._turnips]
89 gamestate.turnips = turnip_data
91 @debug_timer("day.render")
92 def render(self, surface, gamestate):
93 surface.blit(self._soil, (0, 0))
95 for turnip in self._turnips:
96 turnip.render(surface)
97 self._lights.render_light(surface)
98 self._obstacles.render(surface)
99 self._lights.render_fittings(surface)
100 surface.blit(self._toolbar, (120, 10), None)
101 for tool in self._tools:
103 for light_tool in self._light_toolbar:
104 light_tool.render(surface)
105 self._draw_cursor(surface)
107 def _draw_light_toolbar(self, light_config, x):
108 self._light_toolbar = []
109 height = SCREEN_SIZE[1] - 80
110 for color in sorted(COLOURS.keys()):
111 light_tool = ImageButton('32', light_config["type"] + '.png',
112 pos=(x, height), name=color,
113 transform=Multiply(colour=COLOURS[color]))
114 self._light_toolbar.append(light_tool)
117 def _clear_light_toolbar(self):
118 self._light_toolbar = []
120 def _place_seed(self, gamestate, ev):
123 # We don't want top-left to equal the mouse position,
124 # since that looks weird, but we don't want to center
125 # the turnip under the mouse either, since that
126 # causes issues as well, so we compromise
127 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
129 turnip = Turnip(age=0, pos=pos, space=self._space)
130 self._turnips.append(turnip)
132 self._update_toolbar(gamestate)
133 except TurnipInvalidPosition:
134 # TODO: Add error sound or something
137 def _update_light_angle(self, pos, gamestate):
138 # Update the angle of the given light
139 pos = pymunk.pygame_util.to_pygame(pos, pygame.display.get_surface())
140 distance = pos - self._dragging.position
141 angle = math.atan2(distance[1], distance[0])
142 # Set light angle to this position
143 self._dragging.ray_manager.direction = math.degrees(angle)
144 # Hackily update gamestate with new angle
145 for light_cfg in gamestate.station["lights"]:
146 light_pos = pymunk.Vec2d(light_cfg["position"])
147 if light_pos.get_dist_sqrd(self._dragging.position) < 5.0:
148 light_cfg["direction"] = math.degrees(angle)
151 def _place_light(self, gamestate, cfg, colours, ev):
153 cost = cfg.pop("cost")
154 if self._seeds > cost:
155 pos = pymunk.pygame_util.from_pygame(
156 ev.pos, pygame.display.get_surface())
157 # Bail if we're too close to an existing light
158 if self._lights.nearest(pos, max_distance=25):
161 self._update_toolbar(gamestate)
162 cfg["position"] = pos
163 cfg["colours"] = colours
164 gamestate.station["lights"].append(cfg)
165 self._lights.add_light(cfg)
167 def event(self, ev, gamestate):
168 if ev.type == pgl.KEYDOWN:
169 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
170 from .menu import MenuScene
171 SceneChangeEvent.post(scene=MenuScene())
172 elif ev.key == pgl.K_e:
173 from .night import NightScene
174 SceneChangeEvent.post(scene=NightScene())
175 elif ev.key == pgl.K_SPACE:
176 self._paused = not self._paused
177 elif ev.type == pgl.MOUSEBUTTONDOWN:
180 for tool in self._tools:
183 if tool.name == 'reset tool':
186 self._clear_light_toolbar()
187 elif tool.name == 'start night':
188 from .night import NightScene
189 SceneChangeEvent.post(scene=NightScene())
192 if self._tool.name == 'seed':
194 'seed', transform=Alpha(alpha=172))
195 self._clear_light_toolbar()
196 elif self._tool.name == 'light':
198 self._draw_light_toolbar(
199 self._tool.light_config, 100)
201 # Check light toolbar
202 for light_tool in self._light_toolbar:
203 if light_tool.pressed(ev):
205 self._tool.light_config["type"],
207 colour=COLOURS[light_tool.name] + (172,)))
208 self._light_color = light_tool.name
210 if self._tool.name == "seed":
211 self._place_seed(gamestate, ev)
212 elif self._tool.name == "light" and self._light_color:
214 gamestate, self._tool.light_config,
215 [self._light_color], ev)
217 # Not tool, so check lights
218 self._lights.toggle_nearest(ev.pos, surfpos=True)
220 light = self._lights.nearest(ev.pos, surfpos=True,
223 # Start drag to rotate light
224 self._dragging = light
229 elif ev.type == pgl.MOUSEMOTION:
231 # Calculate angle between current position and mouse pos
232 self._update_light_angle(ev.pos, gamestate)
233 elif ev.type == pgl.MOUSEBUTTONUP:
234 self._dragging = None
236 @debug_timer("day.tick")
237 def tick(self, gamestate):
241 def _update_toolbar(self, gamestate):
242 text = ("Day: %d: Turnip Stocks: Seeds: %d. Planted: %d. "
243 "Harvested: %d. Destroyed: %d" %
244 (gamestate.days, self._seeds, len(self._turnips),
245 self._harvested, gamestate.eaten))
246 self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))