Start working toward light placement
[tabakrolletjie.git] / tabakrolletjie / scenes / day.py
1 """ Be prepared. """
2
3 import pygame.locals as pgl
4
5 import pymunk
6 import pymunk.pygame_util
7
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
15
16 from ..constants import SCREEN_SIZE, FONTS
17 from ..widgets import ImageButton
18 from ..turnip import Turnip, TurnipInvalidPosition
19 from ..transforms import Overlay
20
21
22 class DayScene(BaseScene):
23
24     BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
25
26     def enter(self, gamestate):
27         self._space = pymunk.Space()
28         self._toolbar_font = loader.load_font(FONTS['sans'], size=20)
29         self._obstacles = ObstacleManager(self._space, gamestate)
30         self._lights = LightManager(self._space, gamestate)
31         self._turnips = []
32         self._seeds = gamestate.seeds
33         self._harvested = gamestate.harvested
34         self._paused = False
35         self._tool = None
36         for turnip_data in gamestate.turnips:
37             turnip = Turnip(space=self._space, **turnip_data)
38             # Turnips grow at dawn
39             seeds = turnip.grow()
40             if seeds:
41                 self._seeds += seeds
42                 self._harvested += 1
43             else:
44                 self._turnips.append(turnip)
45         # Tools
46         self._tools = [
47             ImageButton('32', 'seed.png', name='seed',
48                         pos=(50, SCREEN_SIZE[1] - 40)),
49             ImageButton('32', 'spotlight.png', name='blue_spotlight',
50                         pos=(100, SCREEN_SIZE[1] - 40),
51                         transform=Overlay(colour=(0, 0, 255, 128))),
52             ImageButton('32', 'spotlight.png', name='red_spotlight',
53                         pos=(150, SCREEN_SIZE[1] - 40),
54                         transform=Overlay(colour=(255, 0, 0, 128))),
55             ImageButton('32', 'default_cursor.png', name='reset tool',
56                         pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)),
57         ]
58         self._update_toolbar(gamestate)
59         # Background
60         self._soil = loader.load_image(
61             "textures", "soil.png", transform=self.BRIGHTNESS)
62
63     def exit(self, gamestate):
64         self._unset_cursor()
65         gamestate.seeds = self._seeds
66         gamestate.harvested = self._harvested
67         turnip_data = [turnip.serialize() for turnip in self._turnips]
68         gamestate.turnips = turnip_data
69
70     @debug_timer("day.render")
71     def render(self, surface, gamestate):
72         surface.blit(self._soil, (0, 0))
73
74         for turnip in self._turnips:
75             turnip.render(surface)
76         self._lights.render_light(surface)
77         self._obstacles.render(surface)
78         self._lights.render_fittings(surface)
79         surface.blit(self._toolbar, (120, 10), None)
80         for tool in self._tools:
81             tool.render(surface)
82         self._draw_cursor(surface)
83
84     def event(self, ev, gamestate):
85         if ev.type == pgl.KEYDOWN:
86             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
87                 from .menu import MenuScene
88                 SceneChangeEvent.post(scene=MenuScene())
89             if ev.key == pgl.K_e:
90                 from .night import NightScene
91                 SceneChangeEvent.post(scene=NightScene())
92             if ev.key == pgl.K_SPACE:
93                 self._paused = not self._paused
94         elif ev.type == pgl.MOUSEBUTTONDOWN:
95             if ev.button == 1:
96                 # Check tools
97                 for tool in self._tools:
98                     if tool.pressed(ev):
99                         if tool.name == 'reset tool':
100                             self._unset_cursor()
101                             self._tool = None
102                         else:
103                             self._tool = tool.name
104                             if self._tool == 'seed':
105                                 self._set_cursor(tool.name)
106                             elif self._tool == 'red_spotlight':
107                                 self._set_cursor(
108                                     'spotlight',
109                                     transform=Overlay(colour=(255, 0, 0, 128)))
110                             elif self._tool == 'blue_spotlight':
111                                 self._set_cursor(
112                                     'spotlight',
113                                     transform=Overlay(colour=(0, 0, 255, 128)))
114                         return
115                 if self._tool == "seed":
116                     if self._seeds > 0:
117                         # plant seed
118                         # We don't want top-left to equal the mouse position,
119                         # since that looks weird, but we don't want to center
120                         # the turnip under the mouse either, since that
121                         # causes issues as well, so we compromise
122                         pos = (ev.pos[0] - 8, ev.pos[1] - 8)
123                         try:
124                             turnip = Turnip(age=0, pos=pos, space=self._space)
125                             self._turnips.append(turnip)
126                             self._seeds -= 1
127                             self._update_toolbar(gamestate)
128                         except TurnipInvalidPosition as e:
129                             # TODO: Add error sound or something
130                             pass
131                 else:
132                     # Not tool, so check lights
133                     self._lights.toggle_nearest(ev.pos, surfpos=True)
134                     print self._lights.lit_by(ev.pos, surfpos=True)
135             elif ev.button == 3 and self._tool:
136                 self._tool = None
137                 self._unset_cursor()
138
139     @debug_timer("day.tick")
140     def tick(self, gamestate):
141         if not self._paused:
142             self._lights.tick()
143
144     def _update_toolbar(self, gamestate):
145         text = ("Turnip Stocks: Seeds: %d. Planted: %d. "
146                 "Harvested: %d. Destroyed: %d" %
147                 (self._seeds, len(self._turnips),
148                  self._harvested, gamestate.eaten))
149         self._toolbar = self._toolbar_font.render(text, True, (255, 255, 255))