Track players and monsters as tiles for collision
This commit is contained in:
parent
9ad3d9de0b
commit
0de96c7593
3 changed files with 63 additions and 15 deletions
22
src/main.rs
22
src/main.rs
|
@ -33,7 +33,7 @@ struct State {
|
||||||
fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World, sound_system: &mut SoundSystem) {
|
fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World, sound_system: &mut SoundSystem) {
|
||||||
let mut positions = ecs.write_storage::<Position>();
|
let mut positions = ecs.write_storage::<Position>();
|
||||||
let mut players = ecs.write_storage::<Player>();
|
let mut players = ecs.write_storage::<Player>();
|
||||||
let map = ecs.fetch::<Map>();
|
let mut map = ecs.write_resource::<Map>();
|
||||||
|
|
||||||
for (player, pos) in (&mut players, &mut positions).join() {
|
for (player, pos) in (&mut players, &mut positions).join() {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
@ -49,6 +49,9 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World, sound_system: &m
|
||||||
if map.is_solid(destination) {
|
if map.is_solid(destination) {
|
||||||
sound_system.play_sound(sound_effects.blocked.clone());
|
sound_system.play_sound(sound_effects.blocked.clone());
|
||||||
} else {
|
} else {
|
||||||
|
map.set_tile_at(Point { x: pos.x, y: pos.y }, TileType::Floor);
|
||||||
|
map.set_tile_at(destination, TileType::Player);
|
||||||
|
|
||||||
pos.x = destination.x;
|
pos.x = destination.x;
|
||||||
pos.y = destination.y;
|
pos.y = destination.y;
|
||||||
|
|
||||||
|
@ -191,7 +194,7 @@ fn main() -> BError {
|
||||||
gs.ecs.register::<Monster>();
|
gs.ecs.register::<Monster>();
|
||||||
gs.ecs.register::<Player>();
|
gs.ecs.register::<Player>();
|
||||||
|
|
||||||
let map = Map::new();
|
let mut map = Map::new();
|
||||||
let mut rng = RandomNumberGenerator::new();
|
let mut rng = RandomNumberGenerator::new();
|
||||||
for (i, tile) in &mut map.get_tiles().iter().enumerate() {
|
for (i, tile) in &mut map.get_tiles().iter().enumerate() {
|
||||||
if rng.roll_dice(1, 16) < 2 && *tile == TileType::Floor {
|
if rng.roll_dice(1, 16) < 2 && *tile == TileType::Floor {
|
||||||
|
@ -215,7 +218,16 @@ fn main() -> BError {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gs.ecs.insert(map);
|
|
||||||
|
{
|
||||||
|
let entities = gs.ecs.entities();
|
||||||
|
let positions = gs.ecs.read_storage::<Position>();
|
||||||
|
let monsters = gs.ecs.read_storage::<Monster>();
|
||||||
|
|
||||||
|
for (entity, _monster, pos) in (&entities, &monsters, &positions).join() {
|
||||||
|
map.set_tile_at(Point { x: pos.x, y: pos.y }, TileType::Monster(entity));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let player_start_pos = Point { x: 40, y: 22 };
|
let player_start_pos = Point { x: 40, y: 22 };
|
||||||
|
|
||||||
|
@ -235,6 +247,10 @@ fn main() -> BError {
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
map.set_tile_at(player_start_pos, TileType::Player);
|
||||||
|
|
||||||
|
gs.ecs.insert(map);
|
||||||
|
|
||||||
gs.ecs
|
gs.ecs
|
||||||
.insert(Point::new(player_start_pos.x, player_start_pos.y));
|
.insert(Point::new(player_start_pos.x, player_start_pos.y));
|
||||||
|
|
||||||
|
|
12
src/map.rs
12
src/map.rs
|
@ -3,9 +3,12 @@ use crate::{
|
||||||
vga_color as vga,
|
vga_color as vga,
|
||||||
};
|
};
|
||||||
use bracket_lib::{prelude::*, random::RandomNumberGenerator};
|
use bracket_lib::{prelude::*, random::RandomNumberGenerator};
|
||||||
|
use specs::Entity;
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
pub enum TileType {
|
pub enum TileType {
|
||||||
|
Player,
|
||||||
|
Monster(Entity),
|
||||||
Wall,
|
Wall,
|
||||||
BreakableWall,
|
BreakableWall,
|
||||||
Floor,
|
Floor,
|
||||||
|
@ -72,6 +75,15 @@ impl Map {
|
||||||
let (x, y) = (point.x as usize, point.y as usize);
|
let (x, y) = (point.x as usize, point.y as usize);
|
||||||
|
|
||||||
match tile {
|
match tile {
|
||||||
|
TileType::Player | TileType::Monster(_) => {
|
||||||
|
ctx.set(
|
||||||
|
x + MAP_X,
|
||||||
|
y + MAP_Y,
|
||||||
|
RGB::named(vga::BLACK),
|
||||||
|
RGB::named(vga::BLACK),
|
||||||
|
to_cp437(' '),
|
||||||
|
);
|
||||||
|
}
|
||||||
TileType::Floor => {
|
TileType::Floor => {
|
||||||
ctx.set(
|
ctx.set(
|
||||||
x + MAP_X,
|
x + MAP_X,
|
||||||
|
|
|
@ -32,14 +32,16 @@ impl<'a> System<'a> for MonsterMotion {
|
||||||
player_pos,
|
player_pos,
|
||||||
mut map,
|
mut map,
|
||||||
mut stats,
|
mut stats,
|
||||||
mut monster,
|
mut monsters,
|
||||||
mut position,
|
mut positions,
|
||||||
mut renderable,
|
mut renderables,
|
||||||
): Self::SystemData,
|
): Self::SystemData,
|
||||||
) {
|
) {
|
||||||
for (entity, monster, position, renderable) in
|
let mut data = (&entities, &mut monsters, &mut positions, &mut renderables)
|
||||||
(&entities, &mut monster, &mut position, &mut renderable).join()
|
.join()
|
||||||
{
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
for (entity, monster, position, renderable) in &mut data {
|
||||||
if clock.has_ticked {
|
if clock.has_ticked {
|
||||||
monster.ticks_until_move -= 1;
|
monster.ticks_until_move -= 1;
|
||||||
if monster.ticks_until_move <= 0 {
|
if monster.ticks_until_move <= 0 {
|
||||||
|
@ -70,20 +72,38 @@ impl<'a> System<'a> for MonsterMotion {
|
||||||
y: position.y + delta_y,
|
y: position.y + delta_y,
|
||||||
};
|
};
|
||||||
|
|
||||||
if destination == *player_pos {
|
//for (entity, monster, position, renderable) in &data {}
|
||||||
// TODO: Sound
|
|
||||||
stats.gems -= 1;
|
|
||||||
let _ = entities.delete(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
match map.get_tile_at(destination) {
|
match map.get_tile_at(destination) {
|
||||||
|
TileType::Player => {
|
||||||
|
// TODO: Sound
|
||||||
|
stats.gems -= 1;
|
||||||
|
map.set_tile_at(
|
||||||
|
Point {
|
||||||
|
x: position.x,
|
||||||
|
y: position.y,
|
||||||
|
},
|
||||||
|
TileType::Floor,
|
||||||
|
);
|
||||||
|
let _ = entities.delete(*entity);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
TileType::Monster(_) => {}
|
||||||
TileType::Wall => {}
|
TileType::Wall => {}
|
||||||
TileType::BreakableWall => {
|
TileType::BreakableWall => {
|
||||||
// TODO: Sound
|
// TODO: Sound
|
||||||
map.set_tile_at(destination, TileType::Floor);
|
map.set_tile_at(destination, TileType::Floor);
|
||||||
let _ = entities.delete(entity);
|
let _ = entities.delete(*entity);
|
||||||
}
|
}
|
||||||
TileType::Floor => {
|
TileType::Floor => {
|
||||||
|
map.set_tile_at(
|
||||||
|
Point {
|
||||||
|
x: position.x,
|
||||||
|
y: position.y,
|
||||||
|
},
|
||||||
|
TileType::Floor,
|
||||||
|
);
|
||||||
|
map.set_tile_at(destination, TileType::Monster(*entity));
|
||||||
position.x = destination.x;
|
position.x = destination.x;
|
||||||
position.y = destination.y;
|
position.y = destination.y;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue