ECS
This commit is contained in:
parent
d4e49a04fc
commit
8ab719b569
@ -8,3 +8,6 @@ pub struct Render {
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Player;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Enemy;
|
||||
|
@ -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));
|
||||
|
||||
|
@ -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
|
||||
},
|
||||
},
|
||||
));
|
||||
}
|
||||
|
19
src/systems/collisions.rs
Normal file
19
src/systems/collisions.rs
Normal file
@ -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::<Player>());
|
||||
players.iter(ecs).for_each(|pos| player_pos = *pos);
|
||||
|
||||
let mut enemies = <(Entity, &Point)>::query().filter(component::<Enemy>());
|
||||
enemies
|
||||
.iter(ecs)
|
||||
.filter(|(_, pos)| **pos == player_pos)
|
||||
.for_each(|(entity, _)| {
|
||||
commands.remove(*entity);
|
||||
});
|
||||
}
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user