Flake8
[tabakrolletjie.git] / tabakrolletjie / scenes / day.py
1 """ Be prepared. """
2
3 import pygame.locals as pgl
4 import pygame.surface
5
6 import pymunk
7 import pymunk.pygame_util
8
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
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     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)
27         self._turnips = []
28         self._seeds = gamestate.seeds
29         self._harvested = gamestate.harvested
30         self._paused = False
31         self._tool = None
32         for turnip_data in gamestate.turnips:
33             turnip = Turnip(space=self._space, **turnip_data)
34             # Turnips grow at dawn
35             seeds = turnip.grow()
36             if seeds:
37                 self._seeds += seeds
38                 self._harvested += 1
39             else:
40                 self._turnips.append(turnip)
41         # Tools
42         self._tools = [
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)),
47         ]
48         self._update_toolbar(gamestate)
49
50     def exit(self, gamestate):
51         self._unset_cursor()
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
56
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))
64
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:
72             tool.render(surface)
73         self._draw_cursor(surface)
74
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())
80             if ev.key == pgl.K_e:
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:
86             if ev.button == 1:
87                 # Check tools
88                 for tool in self._tools:
89                     if tool.pressed(ev):
90                         if tool.name == 'reset tool':
91                             self._unset_cursor()
92                             self._tool = None
93                         else:
94                             self._tool = tool.name
95                             self._set_cursor(tool.name)
96                         return
97                 if self._tool == "seed":
98                     if self._seeds > 0:
99                         # plant 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)
105                         try:
106                             turnip = Turnip(age=0, pos=pos, space=self._space)
107                             self._turnips.append(turnip)
108                             self._seeds -= 1
109                             self._update_toolbar(gamestate)
110                         except TurnipInvalidPosition as e:
111                             # TODO: Add error sound or something
112                             pass
113                 else:
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)
117
118     @debug_timer("day.tick")
119     def tick(self, gamestate):
120         if not self._paused:
121             self._lights.tick()
122
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))