7d028d58f1688ac984a512e190cdd7b8e71ce2b3
[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         y = SCREEN_SIZE[1] - 40
46         tools.append(ImageButton(
47             '32', 'pause.png', name='pause play', pos=(SCREEN_SIZE[0] - 150, y)))
48         tools.append(ImageButton(
49             '32', 'exit.png', name='exit', pos=(SCREEN_SIZE[0] - 50, y)))
50         return tools
51
52     @debug_timer("night.render")
53     def render(self, surface, gamestate):
54         surface.blit(self._soil, (0, 0))
55
56         self._mould.render(surface)
57
58         for turnip in self._turnips[:]:
59             if turnip.eaten:
60                 self._turnips.remove(turnip)
61                 turnip.remove()
62                 gamestate.eaten += 1
63                 self._eaten_tonight += 1
64             else:
65                 turnip.render(surface)
66
67         self._lights.render_light(surface)
68         self._obstacles.render(surface)
69         self._lights.render_fittings(surface)
70
71         for tool in self._tools:
72             tool.render(surface)
73
74         for text, text_pos in self._night_over_text:
75             surface.blit(text, text_pos, None)
76
77     def event(self, ev, gamestate):
78         if ev.type == pgl.KEYDOWN:
79             if not self._do_ticks:
80                 # Any keypress exits
81                 self._to_day()
82             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
83                 from .menu import MenuScene
84                 SceneChangeEvent.post(scene=MenuScene())
85             elif ev.key == pgl.K_e and DEBUG:
86                 self._end_night()
87             elif ev.key == pgl.K_SPACE:
88                 self.toggle_pause()
89         elif ev.type == pgl.MOUSEBUTTONDOWN:
90             if not self._do_ticks:
91                 # Any mouse press exits
92                 self._to_day()
93             if ev.button == 1:
94                 self._lights.toggle_nearest(ev.pos, surfpos=True)
95                 print self._lights.lit_by(ev.pos, surfpos=True)
96
97                 # Check tools
98                 for tool in self._tools:
99                     if tool.pressed(ev):
100                         if tool.name == 'pause play':
101                             self.toggle_pause()
102                         elif tool.name == 'exit':
103                             from .menu import MenuScene
104                             SceneChangeEvent.post(scene=MenuScene())
105
106     def toggle_pause(self):
107         self._paused = not self._paused
108         pause_img = "play.png" if self._paused else "pause.png"
109         for tool in self._tools:
110             if tool.name == 'pause play':
111                 tool.update_image("32", pause_img)
112
113     def _to_day(self):
114         # End the night
115         from .day import DayScene
116         SceneChangeEvent.post(scene=DayScene())
117
118     def _end_night(self):
119         self._do_ticks = False
120         self._night_over_text = []
121         overlay = pygame.surface.Surface(
122             (SCREEN_SIZE[0], 240), pgl.SWSURFACE).convert_alpha()
123         overlay.fill((0, 0, 0, 172))
124         self._night_over_text.append((overlay, (0, 40)))
125         self._night_over_text.append(
126             (shadowed_text("The Night is Over", FONTS["bold"], 48), (300, 50)))
127         self._night_over_text.append(
128             (shadowed_text("Turnips eaten tonight: %d" % self._eaten_tonight,
129                            FONTS["sans"], 32), (300, 130)))
130         self._night_over_text.append(
131             (shadowed_text("Surviving turnips: %d" % len(self._turnips),
132                            FONTS["sans"], 32), (300, 170)))
133         self._night_over_text.append(
134             (shadowed_text("Press any key to continue", FONTS["sans"], 24),
135              (350, 240)))
136
137     @debug_timer("night.tick")
138     def tick(self, gamestate):
139         if self._do_ticks and not self._paused:
140             if self._total_ticks < NIGHT_LENGTH:
141                 self._mould.tick(gamestate, self._space, self._lights)
142                 self._lights.tick()
143                 print "Power usage: ", self._lights.total_power_usage()
144                 self._total_ticks += 1
145             else:
146                 self._end_night()
147             if not self._mould.alive():
148                 self._end_night()
149
150     def exit(self, gamestate):
151         turnip_data = [turnip.serialize() for turnip in self._turnips]
152         gamestate.turnips = turnip_data
153         # TODO: Move this into the end_night function
154         gamestate.days += 1
155         self._mould.update_resistances(gamestate)