Abandon map_indexing_system
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Alex Page 2022-01-31 19:10:24 -05:00
parent 1c54755039
commit e5d820eded
6 changed files with 36 additions and 36 deletions

View file

@ -2,7 +2,7 @@ use bracket_lib::prelude::*;
use specs::prelude::*;
use specs_derive::Component;
#[derive(Component)]
#[derive(Component, Clone, Copy)]
pub struct Position {
pub x: i32,
pub y: i32,
@ -13,3 +13,21 @@ impl PartialEq<Point> for Position {
self.x == other.x && self.y == other.y
}
}
impl From<Point> for Position {
fn from(point: Point) -> Self {
Self {
x: point.x,
y: point.y,
}
}
}
impl From<Position> for Point {
fn from(position: Position) -> Self {
Self {
x: position.x,
y: position.y,
}
}
}

View file

@ -182,6 +182,10 @@ impl Map {
self.tile_content[index] = Some(entity);
}
pub fn clear_tile_content(&mut self, index: usize) {
self.tile_content[index] = None;
}
pub fn get_tiles(&self) -> &Vec<TileType> {
&self.tiles
}

View file

@ -117,7 +117,7 @@ impl State {
let mut positions = self.ecs.write_storage::<Position>();
let mut players = self.ecs.write_storage::<Player>();
let monsters = self.ecs.write_storage::<Monster>();
let map = self.ecs.read_resource::<Map>();
let mut map = self.ecs.write_resource::<Map>();
let mut stats = self.ecs.write_resource::<Stats>();
let mut sound_system = self.ecs.write_resource::<SoundOutput>();
let wants_to_whips = self.ecs.read_storage::<WantsToWhip>();
@ -148,6 +148,9 @@ impl State {
}
}
let index = map.point2d_to_index(Point::from(*pos));
map.clear_tile_content(index);
pos.x = destination.x;
pos.y = destination.y;
@ -155,6 +158,9 @@ impl State {
player_pos.x = pos.x;
player_pos.y = pos.y;
let index = map.point2d_to_index(destination);
map.set_tile_content(index, player_entity);
self.ecs.write_resource::<Clock>().force_tick();
sound_system.play_sound(sound_effects.step.clone());
@ -217,9 +223,6 @@ impl State {
}
fn run_systems(&mut self) {
let mut map_indexing_system = MapIndexingSystem {};
map_indexing_system.run_now(&self.ecs);
let mut whip_system = WhipSystem {};
whip_system.run_now(&self.ecs);

View file

@ -1,27 +0,0 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use crate::{components::Position, resources::Map};
pub struct MapIndexingSystem {}
impl<'a> System<'a> for MapIndexingSystem {
type SystemData = (
WriteExpect<'a, Map>,
ReadStorage<'a, Position>,
Entities<'a>,
);
fn run(&mut self, data: Self::SystemData) {
let (mut map, position, entities) = data;
map.clear_all_tile_content();
for (entity, position) in (&entities, &position).join() {
let index = map.point2d_to_index(Point {
x: position.x,
y: position.y,
});
map.set_tile_content(index, entity);
}
}
}

View file

@ -1,7 +1,5 @@
pub mod map_indexing_system;
pub mod monster_ai_system;
pub mod whip_system;
pub use map_indexing_system::MapIndexingSystem;
pub use monster_ai_system::MonsterAiSystem;
pub use whip_system::WhipSystem;

View file

@ -17,7 +17,7 @@ impl<'a> System<'a> for MonsterAiSystem {
Entities<'a>,
ReadExpect<'a, Clock>,
ReadExpect<'a, Point>,
ReadExpect<'a, Map>,
WriteExpect<'a, Map>,
WriteExpect<'a, Stats>,
WriteStorage<'a, Monster>,
WriteStorage<'a, Position>,
@ -31,7 +31,7 @@ impl<'a> System<'a> for MonsterAiSystem {
entities,
clock,
player_pos,
map,
mut map,
mut stats,
mut monsters,
mut positions,
@ -88,8 +88,12 @@ impl<'a> System<'a> for MonsterAiSystem {
let _ = entities.delete(*entity);
}
_ => {
let index = map.point2d_to_index(Point::from(**position));
map.clear_tile_content(index);
position.x = destination.x;
position.y = destination.y;
let index = map.point2d_to_index(destination);
map.set_tile_content(index, *entity);
}
}
}