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
13 from ..loader import loader
15 from ..constants import SCREEN_SIZE, FONTS
16 from ..widgets import ImageButton
17 from ..turnip import Turnip, TurnipInvalidPosition
20 class DayScene(BaseScene):
21 def enter(self, gamestate):
22 self._space = pymunk.Space()
23 self._toolbar_font = loader.load_font(FONTS['sans'], size=20)
24 self._obstacles = ObstacleManager(self._space, gamestate)
25 self._lights = LightManager(self._space, gamestate)
27 self._seeds = gamestate.seeds
28 self._harvested = gamestate.harvested
31 for turnip_data in gamestate.turnips:
32 turnip = Turnip(space=self._space, **turnip_data)
33 # Turnips grow at dawn
39 self._turnips.append(turnip)
42 ImageButton('32', 'seed.png', name='seed',
43 pos=(50, SCREEN_SIZE[1] - 40)),
44 ImageButton('32', 'default_cursor.png', name='reset tool',
45 pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
47 self._update_toolbar(gamestate)
49 def exit(self, gamestate):
51 gamestate.seeds = self._seeds
52 gamestate.harvested = self._harvested
53 turnip_data = [turnip.serialize() for turnip in self._turnips]
54 gamestate.turnips = turnip_data
56 @debug_timer("day.render")
57 def render(self, surface, gamestate):
58 surface.fill((0, 0, 155))
59 for turnip in self._turnips:
60 turnip.render(surface)
61 self._lights.render_light(surface)
62 self._obstacles.render(surface)
63 self._lights.render_fittings(surface)
64 surface.blit(self._toolbar, (120, 10), None)
65 for tool in self._tools:
67 self._draw_cursor(surface)
69 def event(self, ev, gamestate):
70 if ev.type == pgl.KEYDOWN:
71 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
72 from .menu import MenuScene
73 SceneChangeEvent.post(scene=MenuScene())
75 from .night import NightScene
76 SceneChangeEvent.post(scene=NightScene())
77 if ev.key == pgl.K_SPACE:
78 self._paused = not self._paused
79 elif ev.type == pgl.MOUSEBUTTONDOWN:
82 for tool in self._tools:
84 if tool.name == 'reset tool':
88 self._tool = tool.name
89 self._set_cursor(tool.name)
91 if self._tool == "seed":
94 # We don't want top-left to equal the mouse position,
95 # since that looks weird, but we don't want to center
96 # the turnip under the mouse either, since that
97 # causes issues as well, so we compromise
98 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
100 turnip = Turnip(age=0, pos=pos, space=self._space)
101 self._turnips.append(turnip)
103 self._update_toolbar(gamestate)
104 except TurnipInvalidPosition as e:
105 # TODO: Add error sound or something
108 # Not tool, so check lights
109 self._lights.toggle_nearest(ev.pos, surfpos=True)
110 print self._lights.lit_by(ev.pos, surfpos=True)
112 @debug_timer("day.tick")
113 def tick(self, gamestate):
117 def _update_toolbar(self, gamestate):
118 text = ("Turnip Stocks: Seeds: %d. Planted: %d. "
119 "Harvested: %d. Destroyed: %d" %
120 (self._seeds, len(self._turnips),
121 self._harvested, gamestate.eaten))
122 self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))