Allow going back a level
This commit is contained in:
parent
ca79653703
commit
dd6a4c26d9
29
src/main.rs
29
src/main.rs
@ -77,6 +77,7 @@ pub enum RunState {
|
|||||||
},
|
},
|
||||||
SaveGame,
|
SaveGame,
|
||||||
NextLevel,
|
NextLevel,
|
||||||
|
PreviousLevel,
|
||||||
ShowRemoveItem,
|
ShowRemoveItem,
|
||||||
GameOver,
|
GameOver,
|
||||||
MagicMapReveal {
|
MagicMapReveal {
|
||||||
@ -345,6 +346,11 @@ impl GameState for State {
|
|||||||
self.goto_next_level();
|
self.goto_next_level();
|
||||||
newrunstate = RunState::PreRun;
|
newrunstate = RunState::PreRun;
|
||||||
}
|
}
|
||||||
|
RunState::PreviousLevel => {
|
||||||
|
self.goto_previous_level();
|
||||||
|
self.mapgen_next_state = Some(RunState::PreRun);
|
||||||
|
newrunstate = RunState::MapGeneration;
|
||||||
|
}
|
||||||
RunState::MagicMapReveal { row } => {
|
RunState::MagicMapReveal { row } => {
|
||||||
let mut map = self.ecs.fetch_mut::<Map>();
|
let mut map = self.ecs.fetch_mut::<Map>();
|
||||||
for x in 0..map.width {
|
for x in 0..map.width {
|
||||||
@ -429,6 +435,29 @@ impl State {
|
|||||||
gamelog.append("You descend to the next level.");
|
gamelog.append("You descend to the next level.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn goto_previous_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
|
||||||
|
#[allow(unused_assignments)]
|
||||||
|
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 mut gamelog = self.ecs.fetch_mut::<GameLog>();
|
||||||
|
gamelog.append("You ascend to the previous level.");
|
||||||
|
}
|
||||||
|
|
||||||
fn game_over_cleanup(&mut self) {
|
fn game_over_cleanup(&mut self) {
|
||||||
// Delete everything
|
// Delete everything
|
||||||
let mut to_delete = Vec::new();
|
let mut to_delete = Vec::new();
|
||||||
|
@ -119,6 +119,22 @@ pub fn try_next_level(ecs: &mut World) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn try_previous_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::UpStairs {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
let mut gamelog = ecs.fetch_mut::<GameLog>();
|
||||||
|
gamelog
|
||||||
|
.entries
|
||||||
|
.push("There is no way up from here.".to_string());
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn get_item(ecs: &mut World) {
|
fn get_item(ecs: &mut World) {
|
||||||
let player_pos = ecs.fetch::<Point>();
|
let player_pos = ecs.fetch::<Point>();
|
||||||
let player_entity = ecs.fetch::<Entity>();
|
let player_entity = ecs.fetch::<Entity>();
|
||||||
@ -316,6 +332,11 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
|||||||
return RunState::NextLevel;
|
return RunState::NextLevel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
VirtualKeyCode::Comma => {
|
||||||
|
if try_previous_level(&mut gs.ecs) {
|
||||||
|
return RunState::PreviousLevel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Item management
|
// Item management
|
||||||
VirtualKeyCode::G => get_item(&mut gs.ecs),
|
VirtualKeyCode::G => get_item(&mut gs.ecs),
|
||||||
|
Loading…
Reference in New Issue
Block a user