From 8ab719b5698054ca5d6250e5656f91f49eb0cf24 Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Sat, 28 Aug 2021 16:59:12 -0400 Subject: [PATCH] ECS --- src/components.rs | 3 +++ src/main.rs | 8 +++++++- src/spawners.rs | 16 ++++++++++++++++ src/systems/collisions.rs | 19 +++++++++++++++++++ src/systems/mod.rs | 2 ++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/systems/collisions.rs diff --git a/src/components.rs b/src/components.rs index 50dd738..7c00aae 100644 --- a/src/components.rs +++ b/src/components.rs @@ -8,3 +8,6 @@ pub struct Render { #[derive(Clone, Copy, Debug, PartialEq)] pub struct Player; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Enemy; diff --git a/src/main.rs b/src/main.rs index 40e9fb0..2059466 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,8 +34,14 @@ impl State { let mut resources = Resources::default(); let mut rng = RandomNumberGenerator::new(); let map_builder = MapBuilder::new(&mut rng); - spawn_player(&mut ecs, map_builder.player_start); + 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)); diff --git a/src/spawners.rs b/src/spawners.rs index e1f8197..8415a31 100644 --- a/src/spawners.rs +++ b/src/spawners.rs @@ -10,3 +10,19 @@ 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'), // Ettin + 1 => to_cp437('O'), // Ogre + 2 => to_cp437('o'), // Orc + _ => to_cp437('g'), // goblin + }, + }, + )); +} diff --git a/src/systems/collisions.rs b/src/systems/collisions.rs new file mode 100644 index 0000000..d71c46a --- /dev/null +++ b/src/systems/collisions.rs @@ -0,0 +1,19 @@ +use crate::prelude::*; + +#[system] +#[read_component(Point)] +#[read_component(Player)] +#[read_component(Enemy)] +pub fn collisions(ecs: &mut SubWorld, commands: &mut CommandBuffer) { + let mut player_pos = Point::zero(); + let mut players = <&Point>::query().filter(component::()); + players.iter(ecs).for_each(|pos| player_pos = *pos); + + let mut enemies = <(Entity, &Point)>::query().filter(component::()); + enemies + .iter(ecs) + .filter(|(_, pos)| **pos == player_pos) + .for_each(|(entity, _)| { + commands.remove(*entity); + }); +} diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 714d27d..90c680a 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -1,4 +1,5 @@ use crate::prelude::*; +mod collisions; mod entity_render; mod map_render; mod player_input; @@ -6,6 +7,7 @@ mod player_input; pub fn build_scheduler() -> Schedule { Schedule::builder() .add_system(player_input::player_input_system()) + .add_system(collisions::collisions_system()) .add_system(map_render::map_render_system()) .add_system(entity_render::entity_render_system()) .build()