added pausing to night and moved reset tool after other tools
[tabakrolletjie.git] / tabakrolletjie / scenes / night.py
1 """ In the night, the mould attacks. """
2
3 import pygame.surface
4 import pygame.locals as pgl
5
6 import pymunk
7
8 from .base import BaseScene
9 from ..lights import LightManager
10 from ..obstacles import ObstacleManager
11 from ..enemies import Boyd
12 from ..events import SceneChangeEvent
13 from ..utils import debug_timer, shadowed_text
14 from ..loader import loader
15 from ..transforms import Overlay
16 from ..turnip import Turnip
17 from ..widgets import ImageButton
18 from ..constants import NIGHT_LENGTH, DEBUG, FONTS, SCREEN_SIZE
19
20
21 class NightScene(BaseScene):
22
23     DARKNESS = Overlay(colour=(0, 0, 0, 150))
24
25     def enter(self, gamestate):
26         self._space = pymunk.Space()
27         self._obstacles = ObstacleManager(self._space, gamestate)
28         self._lights = LightManager(self._space, gamestate)
29         self._mould = Boyd(gamestate, self._space)
30         self._turnips = []
31         for turnip_data in gamestate.turnips:
32             turnip = Turnip(space=self._space, **turnip_data)
33             self._turnips.append(turnip)
34         self._soil = loader.load_image(
35             "textures", "soil.png", transform=self.DARKNESS)
36         self._tools = self.create_tools(gamestate)
37         self._total_ticks = 0
38         self._do_ticks = True
39         self._paused = False
40         self._eaten_tonight = 0
41         self._night_over_text = []
42
43     def create_tools(self, gamestate):
44         tools = []
45         tools.append(ImageButton(
46             '32', 'pause.png', name='pause play', pos=(SCREEN_SIZE[0] - 150, SCREEN_SIZE[1] - 40)))
47         return tools
48
49     @debug_timer("night.render")
50     def render(self, surface, gamestate):
51         surface.blit(self._soil, (0, 0))
52
53         self._mould.render(surface)
54
55         for turnip in self._turnips[:]:
56             if turnip.eaten:
57                 self._turnips.remove(turnip)
58                 turnip.remove()
59                 gamestate.eaten += 1
60                 self._eaten_tonight += 1
61             else:
62                 turnip.render(surface)
63
64         self._lights.render_light(surface)
65         self._obstacles.render(surface)
66         self._lights.render_fittings(surface)
67
68         for tool in self._tools:
69             tool.render(surface)
70
71         for text, text_pos in self._night_over_text:
72             surface.blit(text, text_pos, None)
73
74     def event(self, ev, gamestate):
75         if ev.type == pgl.KEYDOWN:
76             if not self._do_ticks:
77                 # Any keypress exits
78                 self._to_day()
79             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
80                 from .menu import MenuScene
81                 SceneChangeEvent.post(scene=MenuScene())
82             elif ev.key == pgl.K_e and DEBUG:
83                 self._end_night()
84             elif ev.key == pgl.K_SPACE:
85                 self.toggle_pause()
86         elif ev.type == pgl.MOUSEBUTTONDOWN:
87             if not self._do_ticks:
88                 # Any mouse press exits
89                 self._to_day()
90             if ev.button == 1:
91                 self._lights.toggle_nearest(ev.pos, surfpos=True)
92                 print self._lights.lit_by(ev.pos, surfpos=True)
93
94                 # Check tools
95                 for tool in self._tools:
96                     if tool.pressed(ev):
97                         if tool.name == 'pause play':
98                             self.toggle_pause()
99
100     def toggle_pause(self):
101         self._paused = not self._paused
102         pause_img = "play.png" if self._paused else "pause.png"
103         for tool in self._tools:
104             if tool.name == 'pause play':
105                 tool.update_image("32", pause_img)
106
107     def _to_day(self):
108         # End the night
109         from .day import DayScene
110         SceneChangeEvent.post(scene=DayScene())
111
112     def _end_night(self):
113         self._do_ticks = False
114         self._night_over_text = []
115         overlay = pygame.surface.Surface(
116             (SCREEN_SIZE[0], 240), pgl.SWSURFACE).convert_alpha()
117         overlay.fill((0, 0, 0, 172))
118         self._night_over_text.append((overlay, (0, 40)))
119         self._night_over_text.append(
120             (shadowed_text("The Night is Over", FONTS["bold"], 48), (300, 50)))
121         self._night_over_text.append(
122             (shadowed_text("Turnips eaten tonight: %d" % self._eaten_tonight,
123                            FONTS["sans"], 32), (300, 130)))
124         self._night_over_text.append(
125             (shadowed_text("Surviving turnips: %d" % len(self._turnips),
126                            FONTS["sans"], 32), (300, 170)))
127         self._night_over_text.append(
128             (shadowed_text("Press any key to continue", FONTS["sans"], 24),
129              (350, 240)))
130
131     @debug_timer("night.tick")
132     def tick(self, gamestate):
133         if self._do_ticks and not self._paused:
134             if self._total_ticks < NIGHT_LENGTH:
135                 self._mould.tick(gamestate, self._space, self._lights)
136                 self._lights.tick()
137                 print "Power usage: ", self._lights.total_power_usage()
138             else:
139                 self._end_night()
140             if not self._mould.alive():
141                 self._end_night()
142
143     def exit(self, gamestate):
144         turnip_data = [turnip.serialize() for turnip in self._turnips]
145         gamestate.turnips = turnip_data
146         gamestate.days += 1