X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tabakrolletjie%2Fscenes%2Fday.py;h=2a1cc35a18c8c0a12f546d4067c6e7edac7676dc;hb=6f2903d031718b0c61cac95550721818a094238d;hp=4c3cbfe9759c01458be1c1bcb307f4d1ca74221c;hpb=a583e427c608fcae7e73e4fcd175e767fb3994be;p=tabakrolletjie.git diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index 4c3cbfe..2a1cc35 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -11,15 +11,16 @@ import pymunk.pygame_util from .base import BaseScene from ..battery import BatteryManager -from ..lights import LightManager, light_fitting_by_type +from ..lights import LightManager, light_fitting_by_type, check_space_for_light from ..infobar import InfoBar from ..obstacles import ObstacleManager from ..events import SceneChangeEvent -from ..utils import debug_timer, shadowed_text +from ..utils import debug_timer, shadowed_text, write_save_file from ..loader import loader +from ..sound import sound from ..transforms import Overlay, Alpha, ColourWedges -from ..constants import SCREEN_SIZE, FONTS, DEBUG +from ..constants import SCREEN_SIZE, FONTS, FPS, NIGHT_HOURS_PER_TICK, DEBUG from ..widgets import ImageButton from ..turnip import Turnip, TurnipInvalidPosition @@ -39,8 +40,10 @@ class DayScene(BaseScene): self._tool = None self._light_colors = None self._dragging = None - # Turnip - self.grow_turnips(gamestate) + # Create Turnips + for turnip_data in gamestate.turnips: + turnip = Turnip(space=self._space, **turnip_data) + self._turnips.append(turnip) # Tools self._light_toolbar = [] self._tools = self.create_tools(gamestate) @@ -53,6 +56,9 @@ class DayScene(BaseScene): self._draw_you_lose(gamestate) elif gamestate.harvested >= gamestate.turnip_target: self._draw_you_win(gamestate) + else: + write_save_file(gamestate.serialize()) + self._ending = False def _draw_you_lose(self, gamestate): overlay = pygame.surface.Surface( @@ -85,17 +91,6 @@ class DayScene(BaseScene): (shadowed_text("Press a key to return to the menu", FONTS["sans"], 24), (350, 400))) - def grow_turnips(self, gamestate): - for turnip_data in gamestate.turnips: - turnip = Turnip(space=self._space, **turnip_data) - # Turnips grow at dawn - seeds = turnip.grow() - if seeds: - gamestate.seeds += seeds - gamestate.harvested += 1 - else: - self._turnips.append(turnip) - def create_tools(self, gamestate): tools = [] @@ -112,6 +107,10 @@ class DayScene(BaseScene): tools.append(tool) x += step + tools.append(ImageButton( + '32', 'remove.png', name='remove light', pos=(x, y))) + x += step + tools.append(ImageButton( '32', 'default_cursor.png', name='reset tool', pos=(x, y))) @@ -124,11 +123,15 @@ class DayScene(BaseScene): def exit(self, gamestate): self._unset_cursor() - turnip_data = [turnip.serialize() for turnip in self._turnips] - gamestate.turnips = turnip_data def end_day(self, gamestate): + if self._ending: + return self._battery.apply_recharge() + gamestate.update_lights(self._lights) + turnip_data = [turnip.serialize() for turnip in self._turnips] + gamestate.turnips = turnip_data + self._ending = True from .night import NightScene SceneChangeEvent.post(scene=NightScene()) @@ -138,7 +141,9 @@ class DayScene(BaseScene): @property def power_usage(self): - return int(self._lights.total_power_usage()) + power = self._lights.total_power_usage() + power = power / (FPS * NIGHT_HOURS_PER_TICK) + return int(round(power)) @debug_timer("day.render") def render(self, surface, gamestate): @@ -183,14 +188,13 @@ class DayScene(BaseScene): # since that looks weird, but we don't want to center # the turnip under the mouse either, since that # causes issues as well, so we compromise - pos = (ev.pos[0] - 8, ev.pos[1] - 8) + pos = (ev.pos[0] - 18, ev.pos[1] - 18) try: turnip = Turnip(age=0, pos=pos, space=self._space) self._turnips.append(turnip) gamestate.seeds -= 1 except TurnipInvalidPosition: - # TODO: Add error sound or something - pass + sound.play_sound("beep_kind.ogg") def _update_light_angle(self, pos, gamestate): # Update the angle of the given light @@ -213,8 +217,9 @@ class DayScene(BaseScene): if gamestate.seeds > cost: pos = pymunk.pygame_util.from_pygame( ev.pos, pygame.display.get_surface()) - # Bail if we're too close to an existing light - if self._lights.nearest(pos, max_distance=25): + # Bail if we're too close to an existing light, obstacle or turnip + if check_space_for_light(self._space, pos, max_distance=25): + sound.play_sound("beep_kind.ogg") return gamestate.seeds -= cost cfg["position"] = pos @@ -222,13 +227,22 @@ class DayScene(BaseScene): gamestate.station["lights"].append(cfg) self._lights.add_light(cfg) + def _remove_light(self, ev): + light = self._lights.nearest(ev.pos, surfpos=True, max_distance=25.0) + if light: + self._lights.remove_light(light) + def event(self, ev, gamestate): + if self._ending: + return if self._game_over_text: if ev.type in (pgl.KEYDOWN, pgl.MOUSEBUTTONDOWN): + self._ending = True from .menu import MenuScene SceneChangeEvent.post(scene=MenuScene()) if ev.type == pgl.KEYDOWN: if ev.key in (pgl.K_q, pgl.K_ESCAPE): + self._ending = True from .menu import MenuScene SceneChangeEvent.post(scene=MenuScene()) elif ev.key == pgl.K_e: @@ -248,6 +262,7 @@ class DayScene(BaseScene): elif tool.name == 'start night': self.end_day(gamestate) elif tool.name == 'exit': + self._ending = True from .menu import MenuScene SceneChangeEvent.post(scene=MenuScene()) else: @@ -256,6 +271,10 @@ class DayScene(BaseScene): self._set_cursor( 'seed', transform=Alpha(alpha=172)) self._clear_light_toolbar() + elif self._tool.name == 'remove light': + self._set_cursor( + 'remove', transform=Alpha(alpha=172)) + self._clear_light_toolbar() elif self._tool.name == 'light': self._unset_cursor() self._draw_light_toolbar( @@ -275,6 +294,8 @@ class DayScene(BaseScene): if self._tool: if self._tool.name == "seed": self._place_seed(gamestate, ev) + elif self._tool.name == "remove light": + self._remove_light(ev) elif self._tool.name == "light" and self._light_colors: self._place_light( gamestate, self._tool.light_config,