1 """ In the night, the mould attacks. """
4 import pygame.locals as pgl
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 ..constants import NIGHT_LENGTH, DEBUG, FONTS, SCREEN_SIZE
20 class NightScene(BaseScene):
22 DARKNESS = Overlay(colour=(0, 0, 0, 150))
24 def enter(self, gamestate):
25 self._space = pymunk.Space()
26 self._obstacles = ObstacleManager(self._space, gamestate)
27 self._lights = LightManager(self._space, gamestate)
28 self._mould = Boyd(gamestate, self._space)
30 for turnip_data in gamestate.turnips:
31 turnip = Turnip(space=self._space, **turnip_data)
32 self._turnips.append(turnip)
33 self._soil = loader.load_image(
34 "textures", "soil.png", transform=self.DARKNESS)
37 self._eaten_tonight = 0
38 self._night_over_text = []
40 @debug_timer("night.render")
41 def render(self, surface, gamestate):
42 surface.blit(self._soil, (0, 0))
44 self._mould.render(surface)
46 for turnip in self._turnips[:]:
48 self._turnips.remove(turnip)
51 self._eaten_tonight += 1
53 turnip.render(surface)
55 self._lights.render_light(surface)
56 self._obstacles.render(surface)
57 self._lights.render_fittings(surface)
59 for text, text_pos in self._night_over_text:
60 surface.blit(text, text_pos, None)
62 def event(self, ev, gamestate):
63 if ev.type == pgl.KEYDOWN:
64 if not self._do_ticks:
67 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
68 from .menu import MenuScene
69 SceneChangeEvent.post(scene=MenuScene())
70 if ev.key == pgl.K_e and DEBUG:
72 elif ev.type == pgl.MOUSEBUTTONDOWN:
73 if not self._do_ticks:
74 # Any mouse press exits
77 self._lights.toggle_nearest(ev.pos, surfpos=True)
78 print self._lights.lit_by(ev.pos, surfpos=True)
82 from .day import DayScene
83 SceneChangeEvent.post(scene=DayScene())
86 self._do_ticks = False
87 self._night_over_text = []
88 overlay = pygame.surface.Surface(
89 (SCREEN_SIZE[0], 240), pgl.SWSURFACE).convert_alpha()
90 overlay.fill((0, 0, 0, 172))
91 self._night_over_text.append((overlay, (0, 40)))
92 self._night_over_text.append(
93 (shadowed_text("The Night is Over", FONTS["bold"], 48), (300, 50)))
94 self._night_over_text.append(
95 (shadowed_text("Turnips eaten tonight: %d" % self._eaten_tonight,
96 FONTS["sans"], 32), (300, 130)))
97 self._night_over_text.append(
98 (shadowed_text("Surviving turnips: %d" % len(self._turnips),
99 FONTS["sans"], 32), (300, 170)))
100 self._night_over_text.append(
101 (shadowed_text("Press any key to continue", FONTS["sans"], 24),
104 @debug_timer("night.tick")
105 def tick(self, gamestate):
107 if self._total_ticks < NIGHT_LENGTH:
108 self._mould.tick(gamestate, self._space, self._lights)
110 print "Power usage: ", self._lights.total_power_usage()
113 if not self._mould.alive():
116 def exit(self, gamestate):
117 turnip_data = [turnip.serialize() for turnip in self._turnips]
118 gamestate.turnips = turnip_data