Use multiply transform
[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, Multiply
15
16 from ..constants import SCREEN_SIZE, FONTS
17 from ..widgets import ImageButton
18 from ..turnip import Turnip, TurnipInvalidPosition
19
20
21 class DayScene(BaseScene):
22
23     BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
24
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)
30         self._turnips = []
31         self._seeds = gamestate.seeds
32         self._harvested = gamestate.harvested
33         self._paused = False
34         self._tool = None
35         for turnip_data in gamestate.turnips:
36             turnip = Turnip(space=self._space, **turnip_data)
37             # Turnips grow at dawn
38             seeds = turnip.grow()
39             if seeds:
40                 self._seeds += seeds
41                 self._harvested += 1
42             else:
43                 self._turnips.append(turnip)
44         # Tools
45         self._tools = [
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)),
56         ]
57         self._update_toolbar(gamestate)
58         # Background
59         self._soil = loader.load_image(
60             "textures", "soil.png", transform=self.BRIGHTNESS)
61
62     def exit(self, gamestate):
63         self._unset_cursor()
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
68
69     @debug_timer("day.render")
70     def render(self, surface, gamestate):
71         surface.blit(self._soil, (0, 0))
72
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:
80             tool.render(surface)
81         self._draw_cursor(surface)
82
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())
88             if ev.key == pgl.K_e:
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:
94             if ev.button == 1:
95                 # Check tools
96                 for tool in self._tools:
97                     if tool.pressed(ev):
98                         if tool.name == 'reset tool':
99                             self._unset_cursor()
100                             self._tool = None
101                         else:
102                             self._tool = tool.name
103                             if self._tool == 'seed':
104                                 self._set_cursor(tool.name)
105                             elif self._tool == 'red_spotlight':
106                                 self._set_cursor(
107                                     'spotlight',
108                                     transform=Multiply(
109                                         colour=(255, 0, 0, 172)))
110                             elif self._tool == 'blue_spotlight':
111                                 self._set_cursor(
112                                     'spotlight',
113                                     transform=Multiply(
114                                         colour=(0, 0, 255, 172)))
115                         return
116                 if self._tool == "seed":
117                     if self._seeds > 0:
118                         # plant 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)
124                         try:
125                             turnip = Turnip(age=0, pos=pos, space=self._space)
126                             self._turnips.append(turnip)
127                             self._seeds -= 1
128                             self._update_toolbar(gamestate)
129                         except TurnipInvalidPosition as e:
130                             # TODO: Add error sound or something
131                             pass
132                 else:
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:
137                 self._tool = None
138                 self._unset_cursor()
139
140     @debug_timer("day.tick")
141     def tick(self, gamestate):
142         if not self._paused:
143             self._lights.tick()
144
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))