2022-04-30 23:30:23 -07:00
|
|
|
# Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2022-05-07 08:55:10 -07:00
|
|
|
'''Defines the core game engine.'''
|
|
|
|
|
2022-04-30 23:30:23 -07:00
|
|
|
import logging
|
2022-05-05 08:38:06 -07:00
|
|
|
import random
|
2022-05-07 08:55:10 -07:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import MutableSet
|
|
|
|
|
2022-04-30 23:30:23 -07:00
|
|
|
import tcod
|
2022-05-07 08:55:10 -07:00
|
|
|
|
2022-05-06 21:16:39 -07:00
|
|
|
from . import monsters
|
2022-05-08 09:55:10 -07:00
|
|
|
from .ai import HostileEnemy
|
2022-05-07 12:28:02 -07:00
|
|
|
from .geometry import Size
|
2022-04-30 23:30:23 -07:00
|
|
|
from .map import Map
|
2022-05-08 09:55:10 -07:00
|
|
|
from .object import Entity, Hero, Monster
|
2022-04-30 23:30:23 -07:00
|
|
|
|
|
|
|
LOG = logging.getLogger('engine')
|
|
|
|
|
2022-05-06 14:34:26 -07:00
|
|
|
@dataclass
|
2022-04-30 23:30:23 -07:00
|
|
|
class Configuration:
|
2022-05-06 14:34:26 -07:00
|
|
|
map_size: Size
|
2022-04-30 23:30:23 -07:00
|
|
|
|
|
|
|
class Engine:
|
2022-05-07 11:42:48 -07:00
|
|
|
'''The main game engine.
|
|
|
|
|
|
|
|
This class provides the event handling, map drawing, and maintains the list of entities.
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
configuration : Configuration
|
|
|
|
Defines the basic configuration for the game
|
|
|
|
entities : MutableSet[Entity]
|
|
|
|
A set of all the entities on the current map, including the Hero
|
|
|
|
hero : Hero
|
|
|
|
The hero, the Entity controlled by the player
|
|
|
|
map : Map
|
|
|
|
A map of the current level
|
|
|
|
rng : tcod.random.Random
|
|
|
|
A random number generator
|
|
|
|
'''
|
|
|
|
|
2022-05-07 12:25:46 -07:00
|
|
|
def __init__(self, configuration: Configuration):
|
2022-04-30 23:30:23 -07:00
|
|
|
self.configuration = configuration
|
|
|
|
|
2022-05-06 14:34:26 -07:00
|
|
|
self.rng = tcod.random.Random()
|
2022-05-06 21:16:39 -07:00
|
|
|
self.map = Map(configuration.map_size)
|
2022-04-30 23:30:23 -07:00
|
|
|
|
2022-05-01 10:46:30 -07:00
|
|
|
first_room = self.map.generator.rooms[0]
|
2022-05-06 21:16:39 -07:00
|
|
|
hero_start_position = first_room.center
|
|
|
|
self.hero = Hero(position=hero_start_position)
|
|
|
|
|
|
|
|
self.entities: MutableSet[Entity] = {self.hero}
|
|
|
|
for room in self.map.rooms:
|
|
|
|
should_spawn_monster_chance = random.random()
|
|
|
|
if should_spawn_monster_chance < 0.4:
|
|
|
|
continue
|
|
|
|
|
|
|
|
floor = list(room.walkable_tiles)
|
|
|
|
for _ in range(2):
|
2022-05-11 07:57:28 -07:00
|
|
|
while True:
|
|
|
|
random_start_position = random.choice(floor)
|
|
|
|
if not any(ent.position == random_start_position for ent in self.entities):
|
|
|
|
break
|
|
|
|
|
2022-05-06 21:16:39 -07:00
|
|
|
spawn_monster_chance = random.random()
|
|
|
|
if spawn_monster_chance > 0.8:
|
2022-05-08 09:55:10 -07:00
|
|
|
monster = Monster(monsters.Troll, ai_class=HostileEnemy, position=random_start_position)
|
2022-05-06 21:16:39 -07:00
|
|
|
else:
|
2022-05-08 09:55:10 -07:00
|
|
|
monster = Monster(monsters.Orc, ai_class=HostileEnemy, position=random_start_position)
|
2022-05-01 10:46:30 -07:00
|
|
|
|
2022-05-08 23:42:36 -07:00
|
|
|
LOG.info('Spawning %s', monster)
|
2022-05-06 21:16:39 -07:00
|
|
|
self.entities.add(monster)
|
2022-04-30 23:30:23 -07:00
|
|
|
|
2022-05-04 09:22:40 -07:00
|
|
|
self.update_field_of_view()
|
|
|
|
|
2022-04-30 23:30:23 -07:00
|
|
|
def print_to_console(self, console):
|
2022-05-07 08:55:10 -07:00
|
|
|
'''Print the whole game to the given console.'''
|
2022-04-30 23:30:23 -07:00
|
|
|
self.map.print_to_console(console)
|
|
|
|
|
2022-05-11 07:57:28 -07:00
|
|
|
hp, max_hp = self.hero.fighter.hit_points, self.hero.fighter.maximum_hit_points
|
|
|
|
console.print(x=1, y=47, string=f'HP: {hp}/{max_hp}')
|
|
|
|
|
|
|
|
for ent in sorted(self.entities, key=lambda e: e.render_order.value):
|
2022-05-04 09:22:40 -07:00
|
|
|
# Only print entities that are in the field of view
|
|
|
|
if not self.map.visible[tuple(ent.position)]:
|
|
|
|
continue
|
2022-05-01 09:29:30 -07:00
|
|
|
ent.print_to_console(console)
|
2022-05-04 09:22:40 -07:00
|
|
|
|
|
|
|
def update_field_of_view(self) -> None:
|
|
|
|
'''Compute visible area of the map based on the player's position and point of view.'''
|
|
|
|
self.map.visible[:] = tcod.map.compute_fov(
|
|
|
|
self.map.tiles['transparent'],
|
2022-05-06 21:16:39 -07:00
|
|
|
tuple(self.hero.position),
|
2022-05-04 09:22:40 -07:00
|
|
|
radius=8)
|
|
|
|
|
|
|
|
# Visible tiles should be added to the explored list
|
|
|
|
self.map.explored |= self.map.visible
|