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
14 from ..transforms import Overlay, Multiply
16 from ..constants import SCREEN_SIZE, FONTS
17 from ..widgets import ImageButton
18 from ..turnip import Turnip, TurnipInvalidPosition
21 class DayScene(BaseScene):
23 BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
25 def enter(self, gamestate):
26 self._space = pymunk.Space()
27 self._toolbar_font = loader.load_font(FONTS['sans'], size=20)
28 self._obstacles = ObstacleManager(self._space, gamestate)
29 self._lights = LightManager(self._space, gamestate)
31 self._seeds = gamestate.seeds
32 self._harvested = gamestate.harvested
35 for turnip_data in gamestate.turnips:
36 turnip = Turnip(space=self._space, **turnip_data)
37 # Turnips grow at dawn
43 self._turnips.append(turnip)
46 ImageButton('32', 'seed.png', name='seed',
47 pos=(50, SCREEN_SIZE[1] - 40)),
48 ImageButton('32', 'spotlight.png', name='blue_spotlight',
49 pos=(100, SCREEN_SIZE[1] - 40),
50 transform=Multiply(colour=(0, 0, 255, 255))),
51 ImageButton('32', 'spotlight.png', name='red_spotlight',
52 pos=(150, SCREEN_SIZE[1] - 40),
53 transform=Multiply(colour=(255, 0, 0, 255))),
54 ImageButton('32', 'default_cursor.png', name='reset tool',
55 pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
57 self._update_toolbar(gamestate)
59 self._soil = loader.load_image(
60 "textures", "soil.png", transform=self.BRIGHTNESS)
62 def exit(self, gamestate):
64 gamestate.seeds = self._seeds
65 gamestate.harvested = self._harvested
66 turnip_data = [turnip.serialize() for turnip in self._turnips]
67 gamestate.turnips = turnip_data
69 @debug_timer("day.render")
70 def render(self, surface, gamestate):
71 surface.blit(self._soil, (0, 0))
73 for turnip in self._turnips:
74 turnip.render(surface)
75 self._lights.render_light(surface)
76 self._obstacles.render(surface)
77 self._lights.render_fittings(surface)
78 surface.blit(self._toolbar, (120, 10), None)
79 for tool in self._tools:
81 self._draw_cursor(surface)
83 def event(self, ev, gamestate):
84 if ev.type == pgl.KEYDOWN:
85 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
86 from .menu import MenuScene
87 SceneChangeEvent.post(scene=MenuScene())
89 from .night import NightScene
90 SceneChangeEvent.post(scene=NightScene())
91 if ev.key == pgl.K_SPACE:
92 self._paused = not self._paused
93 elif ev.type == pgl.MOUSEBUTTONDOWN:
96 for tool in self._tools:
98 if tool.name == 'reset tool':
102 self._tool = tool.name
103 if self._tool == 'seed':
104 self._set_cursor(tool.name)
105 elif self._tool == 'red_spotlight':
109 colour=(255, 0, 0, 172)))
110 elif self._tool == 'blue_spotlight':
114 colour=(0, 0, 255, 172)))
116 if self._tool == "seed":
119 # We don't want top-left to equal the mouse position,
120 # since that looks weird, but we don't want to center
121 # the turnip under the mouse either, since that
122 # causes issues as well, so we compromise
123 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
125 turnip = Turnip(age=0, pos=pos, space=self._space)
126 self._turnips.append(turnip)
128 self._update_toolbar(gamestate)
129 except TurnipInvalidPosition as e:
130 # TODO: Add error sound or something
133 # Not tool, so check lights
134 self._lights.toggle_nearest(ev.pos, surfpos=True)
135 print self._lights.lit_by(ev.pos, surfpos=True)
136 elif ev.button == 3 and self._tool:
140 @debug_timer("day.tick")
141 def tick(self, gamestate):
145 def _update_toolbar(self, gamestate):
146 text = ("Turnip Stocks: Seeds: %d. Planted: %d. "
147 "Harvested: %d. Destroyed: %d" %
148 (self._seeds, len(self._turnips),
149 self._harvested, gamestate.eaten))
150 self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))