Leverage Algorithm2D
This commit is contained in:
parent
a5e260621b
commit
71a3eb3135
2 changed files with 35 additions and 34 deletions
44
src/main.rs
44
src/main.rs
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
use bracket_lib::prelude::*;
|
use bracket_lib::prelude::*;
|
||||||
use components::{LeftMover, Player, Position, Renderable};
|
use components::{LeftMover, Player, Position, Renderable};
|
||||||
use constants::{MAP_HEIGHT, MAP_WIDTH, MAP_X, MAP_Y};
|
use constants::{MAP_X, MAP_Y};
|
||||||
use map::{Map, TileType};
|
use map::Map;
|
||||||
use resources::LevelNumber;
|
use resources::LevelNumber;
|
||||||
use sound::SoundSystem;
|
use sound::SoundSystem;
|
||||||
use sound_effects::SoundEffects;
|
use sound_effects::SoundEffects;
|
||||||
|
@ -35,33 +35,27 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World, sound_system: &m
|
||||||
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();
|
||||||
if now - player.last_moved > Duration::from_secs_f32(0.15) {
|
if now - player.last_moved > Duration::from_secs_f32(0.15) {
|
||||||
let destination_x = pos.x + delta_x;
|
let destination = Point {
|
||||||
let destination_y = pos.y + delta_y;
|
x: pos.x + delta_x,
|
||||||
|
y: pos.y + delta_y,
|
||||||
|
};
|
||||||
|
|
||||||
let mut sound_effects = ecs.fetch_mut::<SoundEffects>();
|
let mut sound_effects = ecs.fetch_mut::<SoundEffects>();
|
||||||
|
|
||||||
if destination_x < 0
|
if map.in_bounds(destination) {
|
||||||
|| (destination_x as usize) >= MAP_WIDTH
|
if map.can_player_enter(destination) {
|
||||||
|| destination_y < 0
|
pos.x = destination.x;
|
||||||
|| (destination_y as usize) >= MAP_HEIGHT
|
pos.y = destination.y;
|
||||||
{
|
|
||||||
|
sound_system.play_sound(sound_effects.step.clone());
|
||||||
|
} else {
|
||||||
|
sound_system.play_sound(sound_effects.blocked.clone());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
let static_sound = sound_effects.get_new_static_effect(sound_system);
|
let static_sound = sound_effects.get_new_static_effect(sound_system);
|
||||||
sound_system.play_sound(static_sound);
|
sound_system.play_sound(static_sound);
|
||||||
player.last_moved = now;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let destination_tile = map.tile_at(destination_x as usize, destination_y as usize);
|
|
||||||
if destination_tile != TileType::Wall {
|
|
||||||
pos.x = destination_x;
|
|
||||||
pos.y = destination_y;
|
|
||||||
|
|
||||||
let step_sound = sound_effects.step.clone();
|
|
||||||
sound_system.play_sound(step_sound);
|
|
||||||
} else {
|
|
||||||
let blocked_sound = sound_effects.blocked.clone();
|
|
||||||
sound_system.play_sound(blocked_sound);
|
|
||||||
}
|
|
||||||
player.last_moved = now;
|
player.last_moved = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,16 +92,14 @@ fn player_input(gs: &mut State, ctx: &mut BTerm) {
|
||||||
}
|
}
|
||||||
VirtualKeyCode::S => {
|
VirtualKeyCode::S => {
|
||||||
let sound_effects = gs.ecs.fetch::<SoundEffects>();
|
let sound_effects = gs.ecs.fetch::<SoundEffects>();
|
||||||
let pickup_sound = sound_effects.pickup.clone();
|
gs.sound_system.play_sound(sound_effects.pickup.clone());
|
||||||
gs.sound_system.play_sound(pickup_sound);
|
|
||||||
}
|
}
|
||||||
VirtualKeyCode::Escape => {
|
VirtualKeyCode::Escape => {
|
||||||
ctx.quit();
|
ctx.quit();
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let sound_effects = gs.ecs.fetch::<SoundEffects>();
|
let sound_effects = gs.ecs.fetch::<SoundEffects>();
|
||||||
let bad_key_sound = sound_effects.bad_key.clone();
|
gs.sound_system.play_sound(sound_effects.bad_key.clone());
|
||||||
gs.sound_system.play_sound(bad_key_sound);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
25
src/map.rs
25
src/map.rs
|
@ -2,10 +2,7 @@ use crate::{
|
||||||
constants::{MAP_HEIGHT, MAP_SIZE, MAP_WIDTH, MAP_X, MAP_Y},
|
constants::{MAP_HEIGHT, MAP_SIZE, MAP_WIDTH, MAP_X, MAP_Y},
|
||||||
vga_color as vga,
|
vga_color as vga,
|
||||||
};
|
};
|
||||||
use bracket_lib::{
|
use bracket_lib::{prelude::*, random::RandomNumberGenerator};
|
||||||
prelude::{to_cp437, BTerm, Rect, RGB},
|
|
||||||
random::RandomNumberGenerator,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
pub enum TileType {
|
pub enum TileType {
|
||||||
|
@ -19,6 +16,14 @@ pub struct Map {
|
||||||
border_bg: RGB,
|
border_bg: RGB,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl BaseMap for Map {}
|
||||||
|
|
||||||
|
impl Algorithm2D for Map {
|
||||||
|
fn dimensions(&self) -> Point {
|
||||||
|
Point::new(MAP_WIDTH, MAP_HEIGHT)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut tiles = vec![TileType::Floor; MAP_SIZE];
|
let mut tiles = vec![TileType::Floor; MAP_SIZE];
|
||||||
|
@ -50,8 +55,8 @@ impl Map {
|
||||||
);
|
);
|
||||||
|
|
||||||
for (i, tile) in self.tiles.iter().enumerate() {
|
for (i, tile) in self.tiles.iter().enumerate() {
|
||||||
let x = i % MAP_WIDTH;
|
let point = self.index_to_point2d(i);
|
||||||
let y = (i - x) / MAP_WIDTH;
|
let (x, y) = (point.x as usize, point.y as usize);
|
||||||
|
|
||||||
match tile {
|
match tile {
|
||||||
TileType::Floor => {
|
TileType::Floor => {
|
||||||
|
@ -76,7 +81,11 @@ impl Map {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tile_at(&self, x: usize, y: usize) -> TileType {
|
pub fn tile_at(&self, point: Point) -> TileType {
|
||||||
self.tiles[(y * MAP_WIDTH) + x]
|
self.tiles[self.point2d_to_index(point)]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn can_player_enter(&self, point: Point) -> bool {
|
||||||
|
self.tile_at(point) != TileType::Wall
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue