Merge branch 'master' of ctpug.org.za:tabakrolletjie
[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 ..turnip import Turnip
14
15
16 class NightScene(BaseScene):
17     def enter(self, gamestate):
18         self._space = pymunk.Space()
19         self._obstacles = ObstacleManager(self._space, gamestate)
20         self._lights = LightManager(self._space, gamestate)
21         self._mould = Boyd(gamestate, self._space)
22         self._turnips = []
23         for turnip_data in gamestate.turnips:
24             turnip = Turnip(**turnip_data)
25             self._turnips.append(turnip)
26
27
28     @debug_timer("night.render")
29     def render(self, surface, gamestate):
30         surface.fill((0, 0, 155))
31         self._mould.render(surface)
32         for turnip in self._turnips:
33             turnip.render(surface)
34         self._lights.render_light(surface)
35         self._obstacles.render(surface)
36         self._lights.render_fittings(surface)
37
38     def event(self, ev, gamestate):
39         if ev.type == pgl.KEYDOWN:
40             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
41                 from .menu import MenuScene
42                 SceneChangeEvent.post(scene=MenuScene())
43             if ev.key == pgl.K_e:
44                 from .day import DayScene
45                 SceneChangeEvent.post(scene=DayScene())
46         elif ev.type == pgl.MOUSEBUTTONDOWN:
47             if ev.button == 1:
48                 self._lights.toggle_nearest(ev.pos, surfpos=True)
49                 print self._lights.lit_by(ev.pos, surfpos=True)
50
51     @debug_timer("night.tick")
52     def tick(self, gamestate):
53         self._mould.tick(gamestate, self._space, self._lights)
54         self._lights.tick()
55
56     def exit(self, gamestate):
57         turnip_data = [turnip.serialize() for turnip in self._turnips]
58         gamestate.turnips = turnip_data