Make map setup more DRY, add map generation visualizer, completes section 4.2

This commit is contained in:
Timothy Warren 2021-12-01 14:45:27 -05:00
parent 713c3f6fb8
commit e55218c325
4 changed files with 114 additions and 85 deletions

View File

@ -23,11 +23,10 @@ mod spawner;
mod trigger_system; mod trigger_system;
mod visibility_system; mod visibility_system;
use crate::inventory_system::ItemRemoveSystem;
use components::*; use components::*;
use damage_system::DamageSystem; use damage_system::DamageSystem;
pub use game_log::GameLog; pub use game_log::GameLog;
use inventory_system::{ItemCollectionSystem, ItemDropSystem, ItemUseSystem}; use inventory_system::{ItemCollectionSystem, ItemDropSystem, ItemRemoveSystem, ItemUseSystem};
pub use map::*; pub use map::*;
use map_indexing_system::MapIndexingSystem; use map_indexing_system::MapIndexingSystem;
use melee_combat_system::MeleeCombatSystem; use melee_combat_system::MeleeCombatSystem;
@ -47,6 +46,8 @@ macro_rules! register {
} }
} }
const SHOW_MAPGEN_VISUALIZER: bool = true;
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
pub enum RunState { pub enum RunState {
AwaitingInput, AwaitingInput,
@ -69,15 +70,28 @@ pub enum RunState {
MagicMapReveal { MagicMapReveal {
row: i32, row: i32,
}, },
MapGeneration,
} }
pub struct State { pub struct State {
pub ecs: World, pub ecs: World,
mapgen_next_state: Option<RunState>,
mapgen_history: Vec<Map>,
mapgen_index: usize,
mapgen_timer: f32,
} }
impl State { impl State {
fn new() -> Self { fn new() -> Self {
State { ecs: World::new() } State {
ecs: World::new(),
mapgen_next_state: Some(RunState::MainMenu {
menu_selection: gui::MainMenuSelection::NewGame,
}),
mapgen_index: 0,
mapgen_history: Vec::new(),
mapgen_timer: 0.0,
}
} }
fn run_systems(&mut self) { fn run_systems(&mut self) {
@ -137,7 +151,7 @@ impl GameState for State {
RunState::GameOver { .. } => {} RunState::GameOver { .. } => {}
_ => { _ => {
// Draw the UI // Draw the UI
draw_map(&self.ecs, ctx); draw_map(&self.ecs.fetch::<Map>(), ctx);
{ {
let positions = self.ecs.read_storage::<Position>(); let positions = self.ecs.read_storage::<Position>();
let renderables = self.ecs.read_storage::<Renderable>(); let renderables = self.ecs.read_storage::<Renderable>();
@ -160,6 +174,22 @@ impl GameState for State {
} }
match newrunstate { match newrunstate {
RunState::MapGeneration => {
if !SHOW_MAPGEN_VISUALIZER {
newrunstate = self.mapgen_next_state.unwrap();
}
ctx.cls();
draw_map(&self.mapgen_history[self.mapgen_index], ctx);
self.mapgen_timer += ctx.frame_time_ms;
if self.mapgen_timer > 300.0 {
self.mapgen_timer = 0.0;
self.mapgen_index += 1;
if self.mapgen_index >= self.mapgen_history.len() {
newrunstate = self.mapgen_next_state.unwrap();
}
}
}
RunState::PreRun => { RunState::PreRun => {
self.run_systems(); self.run_systems();
self.ecs.maintain(); self.ecs.maintain();
@ -384,14 +414,60 @@ impl State {
} }
// Build a new map and place the player // Build a new map and place the player
let mut builder;
let current_depth; let current_depth;
{
let worldmap_resource = self.ecs.fetch::<Map>();
current_depth = worldmap_resource.depth;
}
self.generate_world_map(current_depth + 1);
// Notify the player
let player_entity = self.ecs.fetch::<Entity>();
let mut gamelog = self.ecs.fetch_mut::<GameLog>();
gamelog.append("You descend to the next level, and take a moment to heal.");
// Give them some health
let mut player_health_store = self.ecs.write_storage::<CombatStats>();
if let Some(player_health) = player_health_store.get_mut(*player_entity) {
player_health.hp = i32::max(player_health.hp, player_health.max_hp / 2);
}
}
fn game_over_cleanup(&mut self) {
// Delete everything
let mut to_delete = Vec::new();
for e in self.ecs.entities().join() {
to_delete.push(e);
}
for del in to_delete.iter() {
self.ecs
.delete_entity(*del)
.expect("Failed to delete entity");
}
// Spawn a new player
{
let player_entity = spawner::player(&mut self.ecs, 0, 0);
let mut player_entity_writer = self.ecs.write_resource::<Entity>();
*player_entity_writer = player_entity;
}
// Build a new map and place the player
self.generate_world_map(1);
}
fn generate_world_map(&mut self, new_depth: i32) {
self.mapgen_index = 0;
self.mapgen_timer = 0.0;
self.mapgen_history.clear();
let mut builder = map_builders::random_builder(new_depth);
builder.build_map();
self.mapgen_history = builder.get_snapshot_history();
let player_start; let player_start;
{ {
let mut worldmap_resource = self.ecs.write_resource::<Map>(); let mut worldmap_resource = self.ecs.write_resource::<Map>();
current_depth = worldmap_resource.depth;
builder = map_builders::random_builder(current_depth + 1);
builder.build_map();
*worldmap_resource = builder.get_map(); *worldmap_resource = builder.get_map();
player_start = builder.get_starting_position(); player_start = builder.get_starting_position();
} }
@ -415,63 +491,6 @@ impl State {
if let Some(vs) = viewshed_components.get_mut(*player_entity) { if let Some(vs) = viewshed_components.get_mut(*player_entity) {
vs.dirty = true; vs.dirty = true;
} }
// Notify the player and give them some health
let mut gamelog = self.ecs.fetch_mut::<GameLog>();
gamelog
.entries
.push("You descend to the next level, and take a moment to heal.".to_string());
let mut player_health_store = self.ecs.write_storage::<CombatStats>();
let player_health = player_health_store.get_mut(*player_entity);
if let Some(player_health) = player_health {
player_health.hp = i32::max(player_health.hp, player_health.max_hp / 2);
}
}
fn game_over_cleanup(&mut self) {
// Delete everything
let mut to_delete = Vec::new();
for e in self.ecs.entities().join() {
to_delete.push(e);
}
for del in to_delete.iter() {
self.ecs
.delete_entity(*del)
.expect("Failed to delete entity");
}
// Build a new map and place the player
let mut builder = map_builders::random_builder(1);
let player_start;
{
let mut worldmap_resource = self.ecs.write_resource::<Map>();
builder.build_map();
player_start = builder.get_starting_position();
*worldmap_resource = builder.get_map();
}
// Spawn bad guys
builder.spawn_entities(&mut self.ecs);
// Place the player and update resources
let (player_x, player_y) = (player_start.x, player_start.y);
let player_entity = spawner::player(&mut self.ecs, player_x, player_y);
let mut player_position = self.ecs.write_resource::<Point>();
*player_position = Point::new(player_x, player_y);
let mut position_components = self.ecs.write_storage::<Position>();
let mut player_entity_writer = self.ecs.write_resource::<Entity>();
*player_entity_writer = player_entity;
if let Some(player_pos_comp) = position_components.get_mut(player_entity) {
player_pos_comp.x = player_x;
player_pos_comp.y = player_y;
}
// Mark the player's visibility as dirty
let mut viewshed_components = self.ecs.write_storage::<Viewshed>();
let vs = viewshed_components.get_mut(player_entity);
if let Some(vs) = vs {
vs.dirty = true;
}
} }
} }
@ -526,26 +545,19 @@ fn main() -> rltk::BError {
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new()); gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());
let mut builder = map_builders::random_builder(1); gs.ecs.insert(Map::new(1));
builder.build_map(); gs.ecs.insert(Point::new(0, 0));
let player_start = builder.get_starting_position();
let map = builder.get_map();
let (player_x, player_y) = (player_start.x, player_start.y);
let player_entity = spawner::player(&mut gs.ecs, player_x, player_y);
gs.ecs.insert(rltk::RandomNumberGenerator::new()); gs.ecs.insert(rltk::RandomNumberGenerator::new());
builder.spawn_entities(&mut gs.ecs);
gs.ecs.insert(map); let player_entity = spawner::player(&mut gs.ecs, 0, 0);
gs.ecs.insert(Point::new(player_x, player_y));
gs.ecs.insert(player_entity); gs.ecs.insert(player_entity);
gs.ecs.insert(RunState::MainMenu {
menu_selection: gui::MainMenuSelection::NewGame, gs.ecs.insert(RunState::MapGeneration {});
});
gs.ecs.insert(GameLog::new("Welcome to Rusty Roguelike")); gs.ecs.insert(GameLog::new("Welcome to Rusty Roguelike"));
gs.ecs.insert(particle_system::ParticleBuilder::new()); gs.ecs.insert(particle_system::ParticleBuilder::new());
gs.ecs.insert(rex_assets::RexAssets::new()); gs.ecs.insert(rex_assets::RexAssets::new());
gs.generate_world_map(1);
rltk::main_loop(context, gs) rltk::main_loop(context, gs)
} }

View File

@ -130,9 +130,7 @@ impl Algorithm2D for Map {
} }
} }
pub fn draw_map(ecs: &World, ctx: &mut Rltk) { pub fn draw_map(map: &Map, ctx: &mut Rltk) {
let map = ecs.fetch::<Map>();
let mut y = 0; let mut y = 0;
let mut x = 0; let mut x = 0;

View File

@ -11,6 +11,8 @@ pub trait MapBuilder {
fn spawn_entities(&mut self, ecs: &mut World); fn spawn_entities(&mut self, ecs: &mut World);
fn get_map(&self) -> Map; fn get_map(&self) -> Map;
fn get_starting_position(&self) -> Position; fn get_starting_position(&self) -> Position;
fn get_snapshot_history(&self) -> Vec<Map>;
fn take_snapshot(&mut self);
} }
pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> { pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {

View File

@ -1,5 +1,5 @@
use super::{apply_horizontal_tunnel, apply_room_to_map, apply_vertical_tunnel, MapBuilder}; use super::{apply_horizontal_tunnel, apply_room_to_map, apply_vertical_tunnel, MapBuilder};
use crate::spawner; use crate::{spawner, SHOW_MAPGEN_VISUALIZER};
use crate::{Map, Position, Rect, TileType}; use crate::{Map, Position, Rect, TileType};
use rltk::RandomNumberGenerator; use rltk::RandomNumberGenerator;
use specs::prelude::*; use specs::prelude::*;
@ -8,7 +8,8 @@ pub struct SimpleMapBuilder {
map: Map, map: Map,
starting_position: Position, starting_position: Position,
depth: i32, depth: i32,
rooms: Vec<Rect> rooms: Vec<Rect>,
history: Vec<Map>,
} }
impl MapBuilder for SimpleMapBuilder { impl MapBuilder for SimpleMapBuilder {
@ -29,15 +30,30 @@ impl MapBuilder for SimpleMapBuilder {
fn get_starting_position(&self) -> Position { fn get_starting_position(&self) -> Position {
self.starting_position.clone() self.starting_position.clone()
} }
fn get_snapshot_history(&self) -> Vec<Map> {
self.history.clone()
}
fn take_snapshot(&mut self) {
if SHOW_MAPGEN_VISUALIZER {
let mut snapshot = self.map.clone();
for v in snapshot.revealed_tiles.iter_mut() {
*v = true;
}
self.history.push(snapshot);
}
}
} }
impl SimpleMapBuilder { impl SimpleMapBuilder {
pub fn new(new_depth: i32) -> SimpleMapBuilder { pub fn new(new_depth: i32) -> SimpleMapBuilder {
SimpleMapBuilder{ SimpleMapBuilder {
map: Map::new(new_depth), map: Map::new(new_depth),
starting_position: Position { x: 0, y: 0 }, starting_position: Position { x: 0, y: 0 },
depth: new_depth, depth: new_depth,
rooms: Vec::new(), rooms: Vec::new(),
history: Vec::new(),
} }
} }
@ -80,6 +96,7 @@ impl SimpleMapBuilder {
} }
self.rooms.push(new_room); self.rooms.push(new_room);
self.take_snapshot();
} }
} }