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