3 import pygame.locals as pgl
6 import pymunk.pygame_util
8 from .base import BaseScene
9 from ..lights import LightManager
10 from ..obstacles import ObstacleManager
11 from ..events import SceneChangeEvent
12 from ..utils import debug_timer
14 from ..constants import SCREEN_SIZE
15 from ..widgets import ImageButton
16 from ..turnip import Turnip, TurnipInvalidPosition
19 class DayScene(BaseScene):
20 def enter(self, gamestate):
21 self._space = pymunk.Space()
22 self._obstacles = ObstacleManager(self._space, gamestate)
23 self._lights = LightManager(self._space, gamestate)
25 self._seeds = gamestate.seeds
26 self._harvested = gamestate.harvested
29 for turnip_data in gamestate.turnips:
30 turnip = Turnip(space=self._space, **turnip_data)
31 # Turnips grow at dawn
37 self._turnips.append(turnip)
38 print 'Seeds', self._seeds
41 ImageButton('32', 'seed.png', name='seed',
42 pos=(50, SCREEN_SIZE[1] - 40)),
43 ImageButton('32', 'default_cursor.png', name='reset tool',
44 pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
47 def exit(self, gamestate):
49 gamestate.seeds = self._seeds
50 gamestate.harvested = self._harvested
51 turnip_data = [turnip.serialize() for turnip in self._turnips]
52 gamestate.turnips = turnip_data
54 @debug_timer("day.render")
55 def render(self, surface, gamestate):
56 surface.fill((0, 0, 155))
57 self._lights.render_light(surface)
58 self._obstacles.render(surface)
59 self._lights.render_fittings(surface)
60 for turnip in self._turnips:
61 turnip.render(surface)
62 for tool in self._tools:
64 self._draw_cursor(surface)
66 def event(self, ev, gamestate):
67 if ev.type == pgl.KEYDOWN:
68 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
69 from .menu import MenuScene
70 SceneChangeEvent.post(scene=MenuScene())
72 from .night import NightScene
73 SceneChangeEvent.post(scene=NightScene())
74 if ev.key == pgl.K_SPACE:
75 self._paused = not self._paused
76 elif ev.type == pgl.MOUSEBUTTONDOWN:
79 for tool in self._tools:
81 if tool.name == 'reset tool':
85 self._tool = tool.name
86 self._set_cursor(tool.name)
88 if self._tool == "seed":
91 # We don't want top-left to equal the mouse position,
92 # since that looks weird, but we don't want to center
93 # the turnip under the mouse either, since that
94 # causes issues as well, so we compromise
95 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
97 turnip = Turnip(age=0, pos=pos, space=self._space)
98 self._turnips.append(turnip)
100 self._update_toolbar()
101 except TurnipInvalidPosition as e:
102 # TODO: Add error sound or something
105 # Not tool, so check lights
106 self._lights.toggle_nearest(ev.pos, surfpos=True)
107 print self._lights.lit_by(ev.pos, surfpos=True)
109 @debug_timer("day.tick")
110 def tick(self, gamestate):
114 def _update_toolbar(self):