Complete chapter 2.11

This commit is contained in:
Timothy Warren 2021-11-09 15:50:42 -05:00
parent 57adc4bc01
commit f6dc758f12
5 changed files with 146 additions and 5 deletions

3
.gitignore vendored
View File

@ -111,3 +111,6 @@ Cargo.lock
*.pdb *.pdb
# End of https://www.toptal.com/developers/gitignore/api/rust,jetbrains+all # End of https://www.toptal.com/developers/gitignore/api/rust,jetbrains+all
# Ignore save game file
savegame.json

View File

@ -51,6 +51,16 @@ pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
); );
} }
let map = ecs.fetch::<Map>();
let depth = format!("Depth: {}", map.depth);
ctx.print_color(
2,
43,
RGB::named(rltk::YELLOW),
RGB::named(rltk::BLACK),
&depth,
);
// Display logs // Display logs
let log = ecs.fetch::<GameLog>(); let log = ecs.fetch::<GameLog>();
let mut y = 44; let mut y = 44;

View File

@ -59,6 +59,7 @@ pub enum RunState {
menu_selection: gui::MainMenuSelection, menu_selection: gui::MainMenuSelection,
}, },
SaveGame, SaveGame,
NextLevel,
} }
pub struct State { pub struct State {
@ -252,6 +253,10 @@ impl GameState for State {
menu_selection: gui::MainMenuSelection::LoadGame, menu_selection: gui::MainMenuSelection::LoadGame,
} }
} }
RunState::NextLevel => {
self.goto_next_level();
newrunstate = RunState::PreRun;
}
} }
{ {
@ -263,6 +268,95 @@ impl GameState for State {
} }
} }
impl State {
fn entities_to_remove_on_level_change(&mut self) -> Vec<Entity> {
let entities = self.ecs.entities();
let player = self.ecs.read_storage::<Player>();
let backpack = self.ecs.read_storage::<InBackpack>();
let player_entity = self.ecs.fetch::<Entity>();
let mut to_delete: Vec<Entity> = Vec::new();
for entity in entities.join() {
let mut should_delete = true;
// Don't delete the player
let p = player.get(entity);
if let Some(_p) = p {
should_delete = false;
}
// Don't delete the player's equipment
let bp = backpack.get(entity);
if let Some(bp) = bp {
if bp.owner == *player_entity {
should_delete = false;
}
}
if should_delete {
to_delete.push(entity);
}
}
to_delete
}
fn goto_next_level(&mut self) {
// Delete entities that aren't the palyer or their equipment
let to_delete = self.entities_to_remove_on_level_change();
for target in to_delete {
self.ecs
.delete_entity(target)
.expect("failed to delete entity");
}
// Build a new map and place the player
let worldmap;
{
let mut worldmap_resource = self.ecs.write_resource::<Map>();
let current_depth = worldmap_resource.depth;
*worldmap_resource = Map::new_map_rooms_and_corridors(current_depth + 1);
worldmap = worldmap_resource.clone();
}
// Spawn bad guys
for room in worldmap.rooms.iter().skip(1) {
spawner::spawn_room(&mut self.ecs, room);
}
// Place the player and update resources
let (player_x, player_y) = worldmap.rooms[0].center();
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 player_entity = self.ecs.fetch::<Entity>();
let player_pos_comp = position_components.get_mut(*player_entity);
if let Some(player_pos_comp) = player_pos_comp {
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;
}
// 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 main() -> rltk::BError { fn main() -> rltk::BError {
use rltk::RltkBuilder; use rltk::RltkBuilder;
@ -301,7 +395,7 @@ fn main() -> rltk::BError {
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new()); gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());
let map = Map::new_map_rooms_and_corridors(); let map = Map::new_map_rooms_and_corridors(1);
let (player_x, player_y) = map.rooms[0].center(); let (player_x, player_y) = map.rooms[0].center();
let player_entity = spawner::player(&mut gs.ecs, player_x, player_y); let player_entity = spawner::player(&mut gs.ecs, player_x, player_y);

View File

@ -12,6 +12,7 @@ pub const MAP_COUNT: usize = MAP_HEIGHT * MAP_WIDTH;
pub enum TileType { pub enum TileType {
Wall, Wall,
Floor, Floor,
DownStairs,
} }
#[derive(Default, Serialize, Deserialize, Clone)] #[derive(Default, Serialize, Deserialize, Clone)]
@ -23,6 +24,7 @@ pub struct Map {
pub revealed_tiles: Vec<bool>, pub revealed_tiles: Vec<bool>,
pub visible_tiles: Vec<bool>, pub visible_tiles: Vec<bool>,
pub blocked: Vec<bool>, pub blocked: Vec<bool>,
pub depth: i32,
#[serde(skip_serializing)] #[serde(skip_serializing)]
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
@ -65,7 +67,7 @@ impl Map {
/// Makes a new map using the algorithm from http://rogueliketutorials.com/tutorials/tcod/part-3/ /// Makes a new map using the algorithm from http://rogueliketutorials.com/tutorials/tcod/part-3/
/// This gives a handful of random rooms and corridors joining them together /// This gives a handful of random rooms and corridors joining them together
pub fn new_map_rooms_and_corridors() -> Map { pub fn new_map_rooms_and_corridors(new_depth: i32) -> Map {
let mut map = Map { let mut map = Map {
tiles: vec![TileType::Wall; MAP_COUNT], tiles: vec![TileType::Wall; MAP_COUNT],
rooms: Vec::new(), rooms: Vec::new(),
@ -75,6 +77,7 @@ impl Map {
visible_tiles: vec![false; MAP_COUNT], visible_tiles: vec![false; MAP_COUNT],
blocked: vec![false; MAP_COUNT], blocked: vec![false; MAP_COUNT],
tile_content: vec![Vec::new(); MAP_COUNT], tile_content: vec![Vec::new(); MAP_COUNT],
depth: new_depth,
}; };
const MAX_ROOMS: i32 = 30; const MAX_ROOMS: i32 = 30;
@ -118,6 +121,10 @@ impl Map {
} }
} }
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;
map map
} }
@ -222,6 +229,10 @@ pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
glyph = rltk::to_cp437('#'); glyph = rltk::to_cp437('#');
fg = RGB::from_f32(0., 1.0, 0.); fg = RGB::from_f32(0., 1.0, 0.);
} }
TileType::DownStairs => {
glyph = rltk::to_cp437('>');
fg = RGB::from_f32(0., 1.0, 1.0);
}
} }
if !map.visible_tiles[idx] { if !map.visible_tiles[idx] {

View File

@ -1,7 +1,7 @@
use crate::{ use crate::components::{
game_log::GameLog, CombatStats, Item, Map, Player, Position, RunState, State, Viewshed, CombatStats, Item, Player, Position, Viewshed, WantsToMelee, WantsToPickupItem,
WantsToMelee, WantsToPickupItem,
}; };
use crate::{game_log::GameLog, Map, RunState, State, TileType};
use rltk::{Point, Rltk, VirtualKeyCode}; use rltk::{Point, Rltk, VirtualKeyCode};
use specs::prelude::*; use specs::prelude::*;
use std::cmp::{max, min}; use std::cmp::{max, min};
@ -107,6 +107,13 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
// Save and Quit // Save and Quit
VirtualKeyCode::Escape => return RunState::SaveGame, VirtualKeyCode::Escape => return RunState::SaveGame,
// Level changes
VirtualKeyCode::Period => {
if try_next_level(&mut gs.ecs) {
return RunState::NextLevel;
}
}
_ => return RunState::AwaitingInput, _ => return RunState::AwaitingInput,
}, },
} }
@ -147,3 +154,19 @@ fn get_item(ecs: &mut World) {
} }
} }
} }
pub fn try_next_level(ecs: &mut World) -> bool {
let player_pos = ecs.fetch::<Point>();
let map = ecs.fetch::<Map>();
let player_idx = map.xy_idx(player_pos.x, player_pos.y);
if map.tiles[player_idx] == TileType::DownStairs {
true
} else {
let mut gamelog = ecs.fetch_mut::<GameLog>();
gamelog
.entries
.push("There is no way down from here.".to_string());
false
}
}