|
| 1 | +"""Handle the loading and initialization of game sessions.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Optional |
| 6 | +import copy |
| 7 | +import lzma |
| 8 | +import pickle |
| 9 | +import traceback |
| 10 | + |
| 11 | +from PIL import Image |
| 12 | +from tcod import libtcodpy |
| 13 | +import numpy as np |
| 14 | +import tcod |
| 15 | + |
| 16 | +from game.color import black, menu_text, menu_title, welcome_text |
| 17 | +from game.engine import Engine |
| 18 | +from game.input_handlers import BaseEventHandler, MainGameEventHandler, PopupMessage |
| 19 | +from game.procgen import generate_dungeon |
| 20 | +import game.entity_factories |
| 21 | +import game.game_map |
| 22 | + |
| 23 | +# Load the background image and remove the alpha channel. |
| 24 | +background_image = np.array(Image.open("data/menu_background.png").convert("RGB")) |
| 25 | + |
| 26 | + |
| 27 | +def new_game() -> Engine: |
| 28 | + """Return a brand new game session as an Engine instance.""" |
| 29 | + map_width = 80 |
| 30 | + map_height = 43 |
| 31 | + |
| 32 | + room_max_size = 10 |
| 33 | + room_min_size = 6 |
| 34 | + max_rooms = 30 |
| 35 | + |
| 36 | + max_monsters_per_room = 2 |
| 37 | + max_items_per_room = 2 |
| 38 | + |
| 39 | + player = copy.deepcopy(game.entity_factories.player) |
| 40 | + |
| 41 | + engine = Engine(player=player) |
| 42 | + |
| 43 | + engine.game_map = generate_dungeon( |
| 44 | + max_rooms=max_rooms, |
| 45 | + room_min_size=room_min_size, |
| 46 | + room_max_size=room_max_size, |
| 47 | + map_width=map_width, |
| 48 | + map_height=map_height, |
| 49 | + max_monsters_per_room=max_monsters_per_room, |
| 50 | + max_items_per_room=max_items_per_room, |
| 51 | + engine=engine, |
| 52 | + ) |
| 53 | + engine.update_fov() |
| 54 | + |
| 55 | + engine.message_log.add_message("Hello and welcome, adventurer, to yet another dungeon!", welcome_text) |
| 56 | + return engine |
| 57 | + |
| 58 | + |
| 59 | +def load_game(filename: str) -> Engine: |
| 60 | + """Load an Engine instance from a file.""" |
| 61 | + with open(filename, "rb") as f: |
| 62 | + engine = pickle.loads(lzma.decompress(f.read())) |
| 63 | + assert isinstance(engine, Engine) |
| 64 | + return engine |
| 65 | + |
| 66 | + |
| 67 | +class MainMenu(BaseEventHandler): |
| 68 | + """Handle the main menu rendering and input.""" |
| 69 | + |
| 70 | + def on_render(self, console: tcod.console.Console) -> None: |
| 71 | + """Render the main menu on a background image.""" |
| 72 | + console.draw_semigraphics(background_image, 0, 0) |
| 73 | + |
| 74 | + console.print( |
| 75 | + console.width // 2, |
| 76 | + console.height // 2 - 4, |
| 77 | + "TOMBS OF THE ANCIENT KINGS", |
| 78 | + fg=menu_title, |
| 79 | + alignment=libtcodpy.CENTER, |
| 80 | + ) |
| 81 | + console.print( |
| 82 | + console.width // 2, |
| 83 | + console.height - 2, |
| 84 | + "By (Your name here)", |
| 85 | + fg=menu_title, |
| 86 | + alignment=libtcodpy.CENTER, |
| 87 | + ) |
| 88 | + |
| 89 | + menu_width = 24 |
| 90 | + for i, text in enumerate(["[N] Play a new game", "[C] Continue last game", "[Q] Quit"]): |
| 91 | + console.print( |
| 92 | + console.width // 2, |
| 93 | + console.height // 2 - 2 + i, |
| 94 | + text.ljust(menu_width), |
| 95 | + fg=menu_text, |
| 96 | + bg=black, |
| 97 | + alignment=libtcodpy.CENTER, |
| 98 | + bg_blend=libtcodpy.BKGND_ALPHA(64), |
| 99 | + ) |
| 100 | + |
| 101 | + def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[BaseEventHandler]: |
| 102 | + if event.sym in (tcod.event.KeySym.Q, tcod.event.KeySym.ESCAPE): |
| 103 | + raise SystemExit() |
| 104 | + elif event.sym == tcod.event.KeySym.C: |
| 105 | + try: |
| 106 | + return MainGameEventHandler(load_game("savegame.sav")) |
| 107 | + except FileNotFoundError: |
| 108 | + return PopupMessage(self, "No saved game to load.") |
| 109 | + except Exception as exc: |
| 110 | + traceback.print_exc() # Print to stderr. |
| 111 | + return PopupMessage(self, f"Failed to load save:\n{exc}") |
| 112 | + elif event.sym == tcod.event.KeySym.N: |
| 113 | + return MainGameEventHandler(new_game()) |
| 114 | + |
| 115 | + return None |
0 commit comments