Add enemy movement, turns, split systems

This commit is contained in:
Daniel Lynn 2021-07-07 10:41:13 -05:00
parent dcfc284f45
commit 1326400616
5 changed files with 86 additions and 6 deletions

View File

@ -8,3 +8,9 @@ pub struct Render {
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Player;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Enemy;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MovingRandomly;

View File

@ -4,6 +4,7 @@ mod map;
mod map_builder;
mod spawner;
mod systems;
mod turn_state;
mod prelude {
pub use bracket_lib::prelude::*;
@ -20,6 +21,7 @@ mod prelude {
pub use crate::map_builder::*;
pub use crate::spawner::*;
pub use crate::systems::*;
pub use crate::turn_state::*;
}
use prelude::*;
@ -27,7 +29,9 @@ use prelude::*;
struct State {
ecs: World,
resources: Resources,
systems: Schedule,
input_systems: Schedule,
player_systems: Schedule,
monster_systems: Schedule,
}
impl State {
@ -37,15 +41,25 @@ impl State {
let mut rng = RandomNumberGenerator::new();
let map_builder = MapBuilder::new(&mut rng);
spawn_player(&mut ecs, map_builder.player_start);
map_builder
.rooms
.iter()
.skip(1)
.map(|r| r.center())
.for_each(|pos| spawn_monster(&mut ecs, &mut rng, pos));
resources.insert(map_builder.map);
resources.insert(Camera::new(map_builder.player_start));
spawn_player(&mut ecs, map_builder.player_start);
resources.insert(TurnState::AwaitingInput);
Self {
ecs,
resources,
systems: build_scheduler(),
input_systems: build_input_scheduler(),
player_systems: build_player_scheduler(),
monster_systems: build_monster_scheduler(),
}
}
}
@ -56,8 +70,23 @@ impl GameState for State {
ctx.cls();
ctx.set_active_console(1);
ctx.cls();
self.resources.insert(ctx.key);
self.systems.execute(&mut self.ecs, &mut self.resources);
let current_state = *self.resources.get::<TurnState>().unwrap();
match current_state {
TurnState::AwaitingInput => self
.input_systems
.execute(&mut self.ecs, &mut self.resources),
TurnState::PlayerTurn => self
.player_systems
.execute(&mut self.ecs, &mut self.resources),
TurnState::MonsterTurn => self
.monster_systems
.execute(&mut self.ecs, &mut self.resources),
}
render_draw_buffer(ctx).expect("Render error");
}
}

View File

@ -10,3 +10,20 @@ pub fn spawn_player(ecs: &mut World, pos: Point) {
},
));
}
pub fn spawn_monster(ecs: &mut World, rng: &mut RandomNumberGenerator, pos: Point) {
ecs.push((
Enemy,
pos,
Render {
color: ColorPair::new(WHITE, BLACK),
glyph: match rng.range(0, 4) {
0 => to_cp437('E'),
1 => to_cp437('O'),
2 => to_cp437('o'),
_ => to_cp437('g'),
},
},
MovingRandomly,
));
}

View File

@ -1,13 +1,39 @@
mod collisions;
mod end_turn;
mod entity_render;
mod map_render;
mod player_input;
mod random_move;
use crate::prelude::*;
pub fn build_scheduler() -> Schedule {
pub fn build_input_scheduler() -> Schedule {
Schedule::builder()
.add_system(player_input::player_input_system())
.flush()
.add_system(map_render::map_render_system())
.add_system(entity_render::entity_render_system())
.build()
}
pub fn build_player_scheduler() -> Schedule {
Schedule::builder()
.add_system(collisions::collisions_system())
.flush()
.add_system(map_render::map_render_system())
.add_system(entity_render::entity_render_system())
.add_system(end_turn::end_turn_system())
.build()
}
pub fn build_monster_scheduler() -> Schedule {
Schedule::builder()
.add_system(random_move::random_move_system())
.flush()
.add_system(collisions::collisions_system())
.flush()
.add_system(map_render::map_render_system())
.add_system(entity_render::entity_render_system())
.add_system(end_turn::end_turn_system())
.build()
}

View File

@ -8,6 +8,7 @@ pub fn player_input(
#[resource] map: &Map,
#[resource] key: &Option<VirtualKeyCode>,
#[resource] camera: &mut Camera,
#[resource] turn_state: &mut TurnState,
) {
if let Some(key) = key {
let delta = match key {
@ -27,6 +28,7 @@ pub fn player_input(
if map.can_enter_tile(destination) {
*pos = destination;
camera.on_player_move(destination);
*turn_state = TurnState::PlayerTurn;
}
});
}