3 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
16 from ..constants import SCREEN_SIZE, FONTS
17 from ..widgets import ImageButton
18 from ..turnip import Turnip, TurnipInvalidPosition
21 class DayScene(BaseScene):
22 def enter(self, gamestate):
23 self._space = pymunk.Space()
24 self._toolbar_font = loader.load_font(FONTS['sans'], size=20)
25 self._obstacles = ObstacleManager(self._space, gamestate)
26 self._lights = LightManager(self._space, gamestate)
28 self._seeds = gamestate.seeds
29 self._harvested = gamestate.harvested
32 for turnip_data in gamestate.turnips:
33 turnip = Turnip(space=self._space, **turnip_data)
34 # Turnips grow at dawn
40 self._turnips.append(turnip)
43 ImageButton('32', 'seed.png', name='seed',
44 pos=(50, SCREEN_SIZE[1] - 40)),
45 ImageButton('32', 'default_cursor.png', name='reset tool',
46 pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
48 self._update_toolbar(gamestate)
50 def exit(self, gamestate):
52 gamestate.seeds = self._seeds
53 gamestate.harvested = self._harvested
54 turnip_data = [turnip.serialize() for turnip in self._turnips]
55 gamestate.turnips = turnip_data
57 @debug_timer("day.render")
58 def render(self, surface, gamestate):
59 surface.blit(loader.load_image("textures", "soil.png"), (0, 0))
60 brightness = pygame.surface.Surface(surface.get_size())
61 brightness = brightness.convert_alpha()
62 brightness.fill((255, 255, 255, 50))
63 surface.blit(brightness, (0, 0))
65 for turnip in self._turnips:
66 turnip.render(surface)
67 self._lights.render_light(surface)
68 self._obstacles.render(surface)
69 self._lights.render_fittings(surface)
70 surface.blit(self._toolbar, (120, 10), None)
71 for tool in self._tools:
73 self._draw_cursor(surface)
75 def event(self, ev, gamestate):
76 if ev.type == pgl.KEYDOWN:
77 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
78 from .menu import MenuScene
79 SceneChangeEvent.post(scene=MenuScene())
81 from .night import NightScene
82 SceneChangeEvent.post(scene=NightScene())
83 if ev.key == pgl.K_SPACE:
84 self._paused = not self._paused
85 elif ev.type == pgl.MOUSEBUTTONDOWN:
88 for tool in self._tools:
90 if tool.name == 'reset tool':
94 self._tool = tool.name
95 self._set_cursor(tool.name)
97 if self._tool == "seed":
100 # We don't want top-left to equal the mouse position,
101 # since that looks weird, but we don't want to center
102 # the turnip under the mouse either, since that
103 # causes issues as well, so we compromise
104 pos = (ev.pos[0] - 8, ev.pos[1] - 8)
106 turnip = Turnip(age=0, pos=pos, space=self._space)
107 self._turnips.append(turnip)
109 self._update_toolbar(gamestate)
110 except TurnipInvalidPosition as e:
111 # TODO: Add error sound or something
114 # Not tool, so check lights
115 self._lights.toggle_nearest(ev.pos, surfpos=True)
116 print self._lights.lit_by(ev.pos, surfpos=True)
118 @debug_timer("day.tick")
119 def tick(self, gamestate):
123 def _update_toolbar(self, gamestate):
124 text = ("Turnip Stocks: Seeds: %d. Planted: %d. "
125 "Harvested: %d. Destroyed: %d" %
126 (self._seeds, len(self._turnips),
127 self._harvested, gamestate.eaten))
128 self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))