Hide 'end the night' keypress option behind DEBUG
[tabakrolletjie.git] / tabakrolletjie / scenes / night.py
1 """ In the night, the mould attacks. """
2
3 import pygame.locals as pgl
4
5 import pymunk
6
7 from .base import BaseScene
8 from ..lights import LightManager
9 from ..obstacles import ObstacleManager
10 from ..enemies import Boyd
11 from ..events import SceneChangeEvent
12 from ..utils import debug_timer
13 from ..loader import loader
14 from ..transforms import Overlay
15 from ..turnip import Turnip
16 from ..constants import NIGHT_LENGTH, DEBUG
17
18
19 class NightScene(BaseScene):
20
21     DARKNESS = Overlay(colour=(0, 0, 0, 150))
22
23     def enter(self, gamestate):
24         self._space = pymunk.Space()
25         self._obstacles = ObstacleManager(self._space, gamestate)
26         self._lights = LightManager(self._space, gamestate)
27         self._mould = Boyd(gamestate, self._space)
28         self._turnips = []
29         for turnip_data in gamestate.turnips:
30             turnip = Turnip(space=self._space, **turnip_data)
31             self._turnips.append(turnip)
32         self._soil = loader.load_image(
33             "textures", "soil.png", transform=self.DARKNESS)
34         self._total_ticks = 0
35
36     @debug_timer("night.render")
37     def render(self, surface, gamestate):
38         surface.blit(self._soil, (0, 0))
39
40         self._mould.render(surface)
41
42         for turnip in self._turnips[:]:
43             if turnip.eaten:
44                 self._turnips.remove(turnip)
45                 turnip.remove()
46                 gamestate.eaten += 1
47             else:
48                 turnip.render(surface)
49
50         self._lights.render_light(surface)
51         self._obstacles.render(surface)
52         self._lights.render_fittings(surface)
53
54     def event(self, ev, gamestate):
55         if ev.type == pgl.KEYDOWN:
56             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
57                 from .menu import MenuScene
58                 SceneChangeEvent.post(scene=MenuScene())
59             if ev.key == pgl.K_e and DEBUG:
60                 self._to_day()
61         elif ev.type == pgl.MOUSEBUTTONDOWN:
62             if ev.button == 1:
63                 self._lights.toggle_nearest(ev.pos, surfpos=True)
64                 print self._lights.lit_by(ev.pos, surfpos=True)
65
66     def _to_day(self):
67         # End the night
68         from .day import DayScene
69         SceneChangeEvent.post(scene=DayScene())
70
71     @debug_timer("night.tick")
72     def tick(self, gamestate):
73         if self._total_ticks < NIGHT_LENGTH:
74             self._mould.tick(gamestate, self._space, self._lights)
75             self._lights.tick()
76             print "Power usage: ", self._lights.total_power_usage()
77         else:
78             self._to_day()
79         if not self._mould.alive():
80             self._to_day()
81
82     def exit(self, gamestate):
83         turnip_data = [turnip.serialize() for turnip in self._turnips]
84         gamestate.turnips = turnip_data