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 ..widgets import ImageButton
18 from ..constants import NIGHT_LENGTH, DEBUG, FONTS, SCREEN_SIZE
21 class NightScene(BaseScene):
23 DARKNESS = Overlay(colour=(0, 0, 0, 150))
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)
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)
40 self._eaten_tonight = 0
41 self._night_over_text = []
43 def create_tools(self, gamestate):
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)))
52 @debug_timer("night.render")
53 def render(self, surface, gamestate):
54 surface.blit(self._soil, (0, 0))
56 self._mould.render(surface)
58 for turnip in self._turnips[:]:
60 self._turnips.remove(turnip)
63 self._eaten_tonight += 1
65 turnip.render(surface)
67 self._lights.render_light(surface)
68 self._obstacles.render(surface)
69 self._lights.render_fittings(surface)
71 for tool in self._tools:
74 for text, text_pos in self._night_over_text:
75 surface.blit(text, text_pos, None)
77 def event(self, ev, gamestate):
78 if ev.type == pgl.KEYDOWN:
79 if not self._do_ticks:
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:
87 elif ev.key == pgl.K_SPACE:
89 elif ev.type == pgl.MOUSEBUTTONDOWN:
90 if not self._do_ticks:
91 # Any mouse press exits
94 self._lights.toggle_nearest(ev.pos, surfpos=True)
95 print self._lights.lit_by(ev.pos, surfpos=True)
98 for tool in self._tools:
100 if tool.name == 'pause play':
102 elif tool.name == 'exit':
103 from .menu import MenuScene
104 SceneChangeEvent.post(scene=MenuScene())
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)
115 from .day import DayScene
116 SceneChangeEvent.post(scene=DayScene())
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),
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)
143 print "Power usage: ", self._lights.total_power_usage()
144 self._total_ticks += 1
147 if not self._mould.alive():
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
155 self._mould.update_resistances(gamestate)