4 import pygame.locals as pgl
7 import pymunk.pygame_util
9 from .base import BaseScene
10 from ..lights import LightManager
11 from ..obstacles import ObstacleManager
12 from ..events import SceneChangeEvent
13 from ..utils import debug_timer
14 from ..loader import loader
15 from ..transforms import Overlay, Multiply, Alpha
17 from ..constants import SCREEN_SIZE, FONTS, COLOURS
18 from ..widgets import ImageButton
19 from ..turnip import Turnip, TurnipInvalidPosition
22 class DayScene(BaseScene):
24 BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
26 def enter(self, gamestate):
27 self._space = pymunk.Space()
28 self._toolbar_font = loader.load_font(FONTS['sans'], size=20)
29 self._obstacles = ObstacleManager(self._space, gamestate)
30 self._lights = LightManager(self._space, gamestate)
32 self._seeds = gamestate.seeds
33 self._harvested = gamestate.harvested
36 self._light_color = None
37 for turnip_data in gamestate.turnips:
38 turnip = Turnip(space=self._space, **turnip_data)
39 # Turnips grow at dawn
45 self._turnips.append(turnip)
47 self._light_toolbar = []
49 ImageButton('32', 'seed.png', name='seed',
50 pos=(50, SCREEN_SIZE[1] - 40)),
51 ImageButton('32', 'spotlight.png', name='spotlight',
52 pos=(100, SCREEN_SIZE[1] - 40)),
53 ImageButton('32', 'lamp.png', name='lamp',
54 pos=(150, SCREEN_SIZE[1] - 40)),
55 ImageButton('32', 'default_cursor.png', name='reset tool',
56 pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
58 self._update_toolbar(gamestate)
60 self._soil = loader.load_image(
61 "textures", "soil.png", transform=self.BRIGHTNESS)
63 def exit(self, gamestate):
65 gamestate.seeds = self._seeds
66 gamestate.harvested = self._harvested
67 turnip_data = [turnip.serialize() for turnip in self._turnips]
68 gamestate.turnips = turnip_data
70 @debug_timer("day.render")
71 def render(self, surface, gamestate):
72 surface.blit(self._soil, (0, 0))
74 for turnip in self._turnips:
75 turnip.render(surface)
76 self._lights.render_light(surface)
77 self._obstacles.render(surface)
78 self._lights.render_fittings(surface)
79 surface.blit(self._toolbar, (120, 10), None)
80 for tool in self._tools:
82 for light_tool in self._light_toolbar:
83 light_tool.render(surface)
84 self._draw_cursor(surface)
86 def _draw_light_toolbar(self, light_type, x):
87 self._light_toolbar = []
88 height = SCREEN_SIZE[1] - 80
89 for color in sorted(COLOURS.keys()):
90 light_tool = ImageButton('32', light_type + '.png',
91 pos=(x, height), name=color,
92 transform=Multiply(colour=COLOURS[color]))
93 self._light_toolbar.append(light_tool)
96 def _clear_light_toolbar(self):
97 self._light_toolbar = []
99 def _place_seed(self, gamestate, ev):
102 # We don't want top-left to equal the mouse position,
103 # since that looks weird, but we don't want to center
104 # the turnip under the mouse either, since that
105 # causes issues as well, so we compromise
106 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
108 turnip = Turnip(age=0, pos=pos, space=self._space)
109 self._turnips.append(turnip)
111 self._update_toolbar(gamestate)
112 except TurnipInvalidPosition as e:
113 # TODO: Add error sound or something
116 def _place_spotlight(self, gamestate, colour, ev):
118 pos = pymunk.pygame_util.from_pygame(ev.pos,
119 pygame.display.get_surface())
120 # Bail if we're too close to an existing light
121 if self._lights.nearest(pos, max_distance=25):
124 self._update_toolbar(gamestate)
129 "angle_limits": [0, 90],
131 "radius_limits": [0, 100],
133 gamestate.station["lights"].append(cfg)
134 self._lights.add_light(cfg)
136 def _place_lamp(self, gamestate, colour, ev):
138 pos = pymunk.pygame_util.from_pygame(ev.pos,
139 pygame.display.get_surface())
140 # Bail if we're too close to an existing light
141 if self._lights.nearest(pos, max_distance=25):
144 self._update_toolbar(gamestate)
151 gamestate.station["lights"].append(cfg)
152 self._lights.add_light(cfg)
154 def event(self, ev, gamestate):
155 if ev.type == pgl.KEYDOWN:
156 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
157 from .menu import MenuScene
158 SceneChangeEvent.post(scene=MenuScene())
159 if ev.key == pgl.K_e:
160 from .night import NightScene
161 SceneChangeEvent.post(scene=NightScene())
162 if ev.key == pgl.K_SPACE:
163 self._paused = not self._paused
164 elif ev.type == pgl.MOUSEBUTTONDOWN:
167 for tool in self._tools:
170 if tool.name == 'reset tool':
173 self._clear_light_toolbar()
175 self._tool = tool.name
176 if self._tool == 'seed':
178 'seed', transform=Alpha(alpha=172))
179 self._clear_light_toolbar()
180 elif self._tool == 'spotlight':
182 self._draw_light_toolbar('spotlight', 100)
183 elif self._tool == 'lamp':
185 self._draw_light_toolbar('lamp', 150)
187 # Check light toolbar
188 for light_tool in self._light_toolbar:
189 if light_tool.pressed(ev):
193 colour=COLOURS[light_tool.name] + (172,)))
194 self._light_color = light_tool.name
196 if self._tool == "seed":
197 self._place_seed(gamestate, ev)
198 elif self._tool == 'spotlight' and self._light_color:
199 self._place_spotlight(gamestate, self._light_color, ev)
200 elif self._tool == 'lamp' and self._light_color:
201 self._place_lamp(gamestate, self._light_color, ev)
203 # Not tool, so check lights
204 self._lights.toggle_nearest(ev.pos, surfpos=True)
205 print self._lights.lit_by(ev.pos, surfpos=True)
206 elif ev.button == 3 and self._tool:
210 @debug_timer("day.tick")
211 def tick(self, gamestate):
215 def _update_toolbar(self, gamestate):
216 text = ("Turnip Stocks: Seeds: %d. Planted: %d. "
217 "Harvested: %d. Destroyed: %d" %
218 (self._seeds, len(self._turnips),
219 self._harvested, gamestate.eaten))
220 self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))