From feb7988f5fc138749e1ebe07b7064c42a2c503f1 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 1 Dec 2021 12:03:49 -0500 Subject: [PATCH] Move map builder state into map builder --- src/main.rs | 37 ++++++++--------- src/map_builders/mod.rs | 10 +++-- src/map_builders/simple_map.rs | 72 +++++++++++++++++++++------------- 3 files changed, 66 insertions(+), 53 deletions(-) diff --git a/src/main.rs b/src/main.rs index 40e4cf7..362cb10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -384,24 +384,20 @@ impl State { } // Build a new map and place the player - let mut builder = map_builders::random_builder(); - let mut worldmap; + let mut builder; let current_depth; let player_start; { let mut worldmap_resource = self.ecs.write_resource::(); current_depth = worldmap_resource.depth; - // builder = map_builders::random_builder(current_depth + 1); - - let (newmap, start) = builder.build_map(current_depth + 1); - - *worldmap_resource = newmap; - player_start = start; - worldmap = worldmap_resource.clone(); + builder = map_builders::random_builder(current_depth + 1); + builder.build_map(); + *worldmap_resource = builder.get_map(); + player_start = builder.get_starting_position(); } // Spawn bad guys - builder.spawn_entities(&mut worldmap, &mut self.ecs, current_depth + 1); + builder.spawn_entities(&mut self.ecs); // Place the player and update resources let (player_x, player_y) = (player_start.x, player_start.y); @@ -445,20 +441,17 @@ impl State { } // Build a new map and place the player - let mut builder = map_builders::random_builder(); - let mut worldmap; + let mut builder = map_builders::random_builder(1); let player_start; { let mut worldmap_resource = self.ecs.write_resource::(); - let (newmap, start) = builder.build_map(1); - - *worldmap_resource = newmap; - player_start = start; - worldmap = worldmap_resource.clone(); + builder.build_map(); + player_start = builder.get_starting_position(); + *worldmap_resource = builder.get_map(); } // Spawn bad guys - builder.spawn_entities(&mut worldmap, &mut self.ecs, 1); + builder.spawn_entities(&mut self.ecs); // Place the player and update resources let (player_x, player_y) = (player_start.x, player_start.y); @@ -533,14 +526,16 @@ fn main() -> rltk::BError { gs.ecs.insert(SimpleMarkerAllocator::::new()); - let mut builder = map_builders::random_builder(); - let (mut map, player_start) = builder.build_map(1); + let mut builder = map_builders::random_builder(1); + builder.build_map(); + 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()); - builder.spawn_entities(&mut map, &mut gs.ecs, 1); + builder.spawn_entities(&mut gs.ecs); gs.ecs.insert(map); gs.ecs.insert(Point::new(player_x, player_y)); diff --git a/src/map_builders/mod.rs b/src/map_builders/mod.rs index bbc68c6..f5ab291 100644 --- a/src/map_builders/mod.rs +++ b/src/map_builders/mod.rs @@ -7,11 +7,13 @@ use simple_map::SimpleMapBuilder; use specs::prelude::*; pub trait MapBuilder { - fn build_map(&mut self, new_depth: i32) -> (Map, Position); - fn spawn_entities(&mut self, map: &Map, ecs: &mut World, new_depth: i32); + fn build_map(&mut self); + fn spawn_entities(&mut self, ecs: &mut World); + fn get_map(&self) -> Map; + fn get_starting_position(&self) -> Position; } -pub fn random_builder() -> Box { +pub fn random_builder(new_depth: i32) -> Box { // Note that until we have a second map type, this isn't even slightly random - Box::new(SimpleMapBuilder {}) + Box::new(SimpleMapBuilder::new(new_depth)) } diff --git a/src/map_builders/simple_map.rs b/src/map_builders/simple_map.rs index 010a5a8..aebb606 100644 --- a/src/map_builders/simple_map.rs +++ b/src/map_builders/simple_map.rs @@ -4,25 +4,42 @@ use crate::{Map, Position, Rect, TileType}; use rltk::RandomNumberGenerator; use specs::prelude::*; -pub struct SimpleMapBuilder {} +pub struct SimpleMapBuilder { + map: Map, + starting_position: Position, + depth: i32 +} impl MapBuilder for SimpleMapBuilder { - fn build_map(&mut self, new_depth: i32) -> (Map, Position) { - let mut map = Map::new(new_depth); - let playerpos = SimpleMapBuilder::rooms_and_corridors(&mut map); - - (map, playerpos) + fn build_map(&mut self) { + self.rooms_and_corridors(); } - fn spawn_entities(&mut self, map: &Map, ecs: &mut World, _new_depth: i32) { - for room in map.rooms.iter().skip(1) { - spawner::spawn_room(ecs, room, 1); + fn spawn_entities(&mut self, ecs: &mut World) { + for room in self.map.rooms.iter().skip(1) { + spawner::spawn_room(ecs, room, self.depth); } } + + fn get_map(&self) -> Map { + self.map.clone() + } + + fn get_starting_position(&self) -> Position { + self.starting_position.clone() + } } impl SimpleMapBuilder { - fn rooms_and_corridors(map: &mut Map) -> Position { + pub fn new(new_depth: i32) -> SimpleMapBuilder { + SimpleMapBuilder{ + map: Map::new(new_depth), + starting_position: Position { x: 0, y: 0 }, + depth: new_depth + } + } + + fn rooms_and_corridors(&mut self) { const MAX_ROOMS: i32 = 30; const MIN_SIZE: i32 = 6; const MAX_SIZE: i32 = 10; @@ -32,47 +49,46 @@ impl SimpleMapBuilder { for _ in 0..MAX_ROOMS { let w = rng.range(MIN_SIZE, MAX_SIZE); let h = rng.range(MIN_SIZE, MAX_SIZE); - let x = rng.roll_dice(1, map.width - w - 1) - 1; - let y = rng.roll_dice(1, map.height - h - 1) - 1; + let x = rng.roll_dice(1, self.map.width - w - 1) - 1; + let y = rng.roll_dice(1, self.map.height - h - 1) - 1; let new_room = Rect::new(x, y, w, h); let mut ok = true; - for other_room in map.rooms.iter() { + for other_room in self.map.rooms.iter() { if new_room.intersect(other_room) { ok = false; } } if ok { - apply_room_to_map(map, &new_room); + apply_room_to_map(&mut self.map, &new_room); - if !map.rooms.is_empty() { + if !self.map.rooms.is_empty() { let (new_x, new_y) = new_room.center(); - let (prev_x, prev_y) = map.rooms[map.rooms.len() - 1].center(); + let (prev_x, prev_y) = self.map.rooms[self.map.rooms.len() - 1].center(); if rng.range(0, 2) == 1 { - apply_horizontal_tunnel(map, prev_x, new_x, prev_y); - apply_vertical_tunnel(map, prev_y, new_y, new_x); + apply_horizontal_tunnel(&mut self.map, prev_x, new_x, prev_y); + apply_vertical_tunnel(&mut self.map, prev_y, new_y, new_x); } else { - apply_vertical_tunnel(map, prev_y, new_y, prev_x); - apply_horizontal_tunnel(map, prev_x, new_x, new_y); + apply_vertical_tunnel(&mut self.map, prev_y, new_y, prev_x); + apply_horizontal_tunnel(&mut self.map, prev_x, new_x, new_y); } } - map.rooms.push(new_room); + self.map.rooms.push(new_room); } } - let stairs_position = map.rooms[map.rooms.len() - 1].center(); - let stairs_idx = map.xy_idx(stairs_position.0, stairs_position.1); - map.tiles[stairs_idx] = TileType::DownStairs; + let stairs_position = self.map.rooms[self.map.rooms.len() - 1].center(); + let stairs_idx = self.map.xy_idx(stairs_position.0, stairs_position.1); + self.map.tiles[stairs_idx] = TileType::DownStairs; - let start_pos = map.rooms[0].center(); - - Position { + let start_pos = self.map.rooms[0].center(); + self.starting_position = Position { x: start_pos.0, y: start_pos.1, - } + }; } }