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
28 for turnip_data in gamestate.turnips:
29 turnip = Turnip(space=self._space, **turnip_data)
30 # Turnips grow at dawn
36 self._turnips.append(turnip)
37 print 'Seeds', self._seeds
40 ImageButton('32', 'seed.png', name='seed',
41 pos=(50, SCREEN_SIZE[1] - 40)),
42 ImageButton('32', 'default_cursor.png', name='reset tool',
43 pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
46 def exit(self, gamestate):
48 gamestate.seeds = self._seeds
49 gamestate.harvested = self._harvested
50 turnip_data = [turnip.serialize() for turnip in self._turnips]
51 gamestate.turnips = turnip_data
53 @debug_timer("day.render")
54 def render(self, surface, gamestate):
55 surface.fill((0, 0, 155))
56 self._lights.render_light(surface)
57 self._obstacles.render(surface)
58 self._lights.render_fittings(surface)
59 for turnip in self._turnips:
60 turnip.render(surface)
61 for tool in self._tools:
63 self._draw_cursor(surface)
65 def event(self, ev, gamestate):
66 if ev.type == pgl.KEYDOWN:
67 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
68 from .menu import MenuScene
69 SceneChangeEvent.post(scene=MenuScene())
71 from .night import NightScene
72 SceneChangeEvent.post(scene=NightScene())
73 elif ev.type == pgl.MOUSEBUTTONDOWN:
76 for tool in self._tools:
78 if tool.name == 'reset tool':
82 self._tool = tool.name
83 self._set_cursor(tool.name)
85 if self._tool == "seed":
88 # We don't want top-left to equal the mouse position,
89 # since that looks weird, but we don't want to center
90 # the turnip under the mouse either, since that
91 # causes issues as well, so we compromise
92 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
94 turnip = Turnip(age=0, pos=pos, space=self._space)
95 self._turnips.append(turnip)
97 self._update_toolbar()
98 except TurnipInvalidPosition as e:
99 # TODO: Add error sound or something
102 # Not tool, so check lights
103 self._lights.toggle_nearest(ev.pos, surfpos=True)
104 print self._lights.lit_by(ev.pos, surfpos=True)
106 @debug_timer("day.tick")
107 def tick(self, gamestate):
110 def _update_toolbar(self):