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
40 for turnip_data in gamestate.turnips:
41 turnip = Turnip(space=self._space, **turnip_data)
42 # Turnips grow at dawn
48 self._turnips.append(turnip)
50 self._light_toolbar = []
52 ImageButton('32', 'seed.png', name='seed',
53 pos=(50, SCREEN_SIZE[1] - 40)),
54 ImageButton('32', 'spotlight.png', name='spotlight',
55 pos=(100, SCREEN_SIZE[1] - 40)),
56 ImageButton('32', 'lamp.png', name='lamp',
57 pos=(150, SCREEN_SIZE[1] - 40)),
58 ImageButton('32', 'default_cursor.png', name='reset tool',
59 pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
61 self._update_toolbar(gamestate)
63 self._soil = loader.load_image(
64 "textures", "soil.png", transform=self.BRIGHTNESS)
66 def exit(self, gamestate):
68 gamestate.seeds = self._seeds
69 gamestate.harvested = self._harvested
70 turnip_data = [turnip.serialize() for turnip in self._turnips]
71 gamestate.turnips = turnip_data
73 @debug_timer("day.render")
74 def render(self, surface, gamestate):
75 surface.blit(self._soil, (0, 0))
77 for turnip in self._turnips:
78 turnip.render(surface)
79 self._lights.render_light(surface)
80 self._obstacles.render(surface)
81 self._lights.render_fittings(surface)
82 surface.blit(self._toolbar, (120, 10), None)
83 for tool in self._tools:
85 for light_tool in self._light_toolbar:
86 light_tool.render(surface)
87 self._draw_cursor(surface)
89 def _draw_light_toolbar(self, light_type, x):
90 self._light_toolbar = []
91 height = SCREEN_SIZE[1] - 80
92 for color in sorted(COLOURS.keys()):
93 light_tool = ImageButton('32', light_type + '.png',
94 pos=(x, height), name=color,
95 transform=Multiply(colour=COLOURS[color]))
96 self._light_toolbar.append(light_tool)
99 def _clear_light_toolbar(self):
100 self._light_toolbar = []
102 def _place_seed(self, gamestate, ev):
105 # We don't want top-left to equal the mouse position,
106 # since that looks weird, but we don't want to center
107 # the turnip under the mouse either, since that
108 # causes issues as well, so we compromise
109 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
111 turnip = Turnip(age=0, pos=pos, space=self._space)
112 self._turnips.append(turnip)
114 self._update_toolbar(gamestate)
115 except TurnipInvalidPosition as e:
116 # TODO: Add error sound or something
119 def _update_light_angle(self, pos):
120 # Update the angle of the given light
121 pos = pymunk.pygame_util.to_pygame(pos, pygame.display.get_surface())
122 distance = pos - self._dragging.position
123 angle = math.atan2(distance[1], distance[0])
124 # Set light angle to this position
125 self._dragging.ray_manager.direction = math.degrees(angle)
126 # TODO: Update gamestate with new angle
128 def _place_spotlight(self, gamestate, colour, ev):
130 pos = pymunk.pygame_util.from_pygame(ev.pos,
131 pygame.display.get_surface())
132 # Bail if we're too close to an existing light
133 if self._lights.nearest(pos, max_distance=25):
136 self._update_toolbar(gamestate)
144 "radius_limits": [0, 100],
146 gamestate.station["lights"].append(cfg)
147 self._lights.add_light(cfg)
149 def _place_lamp(self, gamestate, colour, ev):
151 pos = pymunk.pygame_util.from_pygame(ev.pos,
152 pygame.display.get_surface())
153 # Bail if we're too close to an existing light
154 if self._lights.nearest(ev.pos, surfpos=True, max_distance=25):
157 self._update_toolbar(gamestate)
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 if ev.key == pgl.K_e:
173 from .night import NightScene
174 SceneChangeEvent.post(scene=NightScene())
175 if 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()
188 self._tool = tool.name
189 if self._tool == 'seed':
191 'seed', transform=Alpha(alpha=172))
192 self._clear_light_toolbar()
193 elif self._tool == 'spotlight':
195 self._draw_light_toolbar('spotlight', 100)
196 elif self._tool == 'lamp':
198 self._draw_light_toolbar('lamp', 150)
200 # Check light toolbar
201 for light_tool in self._light_toolbar:
202 if light_tool.pressed(ev):
206 colour=COLOURS[light_tool.name] + (172,)))
207 self._light_color = light_tool.name
209 if self._tool == "seed":
210 self._place_seed(gamestate, ev)
211 elif self._tool == 'spotlight' and self._light_color:
212 self._place_spotlight(gamestate, self._light_color, ev)
213 elif self._tool == 'lamp' and self._light_color:
214 self._place_lamp(gamestate, self._light_color, ev)
216 # Not tool, so check lights
217 self._lights.toggle_nearest(ev.pos, surfpos=True)
218 print self._lights.lit_by(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)
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 = ("Turnip Stocks: Seeds: %d. Planted: %d. "
243 "Harvested: %d. Destroyed: %d" %
244 (self._seeds, len(self._turnips),
245 self._harvested, gamestate.eaten))
246 self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))