Generate orcs and trolls randomly throughout the dungeon

This commit is contained in:
Eryn Wells 2022-05-06 21:16:39 -07:00
parent 08b1841bdf
commit d99c97408c

View file

@ -4,11 +4,12 @@
import logging import logging
import random import random
import tcod import tcod
from .actions import ExitAction, MovePlayerAction, RegenerateRoomsAction from . import monsters
from .events import EventHandler from .events import EventHandler
from .geometry import Direction, Point, Size from .geometry import Direction, Size
from .map import Map from .map import Map
from .object import Entity from .monsters import Monster
from .object import Entity, Hero
from dataclasses import dataclass from dataclasses import dataclass
from typing import MutableSet from typing import MutableSet
@ -25,18 +26,33 @@ class Engine:
self.configuration = configuration self.configuration = configuration
self.rng = tcod.random.Random() self.rng = tcod.random.Random()
self.map = Map(configuration.map_size)
map_size = configuration.map_size
self.map = Map(map_size)
first_room = self.map.generator.rooms[0] first_room = self.map.generator.rooms[0]
player_start_position = first_room.center hero_start_position = first_room.center
self.player = Entity('@', position=player_start_position, fg=tcod.white) self.hero = Hero(position=hero_start_position)
self.entities: MutableSet[Entity] = {self.player} self.entities: MutableSet[Entity] = {self.hero}
for _ in range(self.rng.randint(5, 15)): for room in self.map.rooms:
position = self.map.random_walkable_position() should_spawn_monster_chance = random.random()
self.entities.add(Entity('@', position=position, fg=tcod.yellow)) if should_spawn_monster_chance < 0.4:
continue
floor = list(room.walkable_tiles)
while True:
random_start_position = random.choice(floor)
if not any(ent.position == random_start_position for ent in self.entities):
break
for _ in range(2):
spawn_monster_chance = random.random()
if spawn_monster_chance > 0.8:
monster = Monster(monsters.Troll, position=random_start_position)
else:
monster = Monster(monsters.Orc, position=random_start_position)
LOG.info(f'Spawning monster {monster}')
self.entities.add(monster)
self.update_field_of_view() self.update_field_of_view()
@ -46,13 +62,13 @@ class Engine:
if not action: if not action:
return return
action.perform(self, self.player) action.perform(self, self.hero)
directions = list(Direction.all()) directions = list(Direction.all())
moved_entities: MutableSet[Entity] = {self.player} moved_entities: MutableSet[Entity] = {self.hero}
for ent in self.entities: for ent in self.entities:
if ent == self.player: if ent == self.hero:
continue continue
while True: while True:
@ -78,7 +94,7 @@ class Engine:
'''Compute visible area of the map based on the player's position and point of view.''' '''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.visible[:] = tcod.map.compute_fov(
self.map.tiles['transparent'], self.map.tiles['transparent'],
tuple(self.player.position), tuple(self.hero.position),
radius=8) radius=8)
# Visible tiles should be added to the explored list # Visible tiles should be added to the explored list