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 components::{LeftMover, Player, Position, Renderable};
|
||||
use constants::{MAP_HEIGHT, MAP_WIDTH, MAP_X, MAP_Y};
|
||||
use map::{Map, TileType};
|
||||
use constants::{MAP_X, MAP_Y};
|
||||
use map::Map;
|
||||
use resources::LevelNumber;
|
||||
use sound::SoundSystem;
|
||||
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() {
|
||||
let now = Instant::now();
|
||||
if now - player.last_moved > Duration::from_secs_f32(0.15) {
|
||||
let destination_x = pos.x + delta_x;
|
||||
let destination_y = pos.y + delta_y;
|
||||
let destination = Point {
|
||||
x: pos.x + delta_x,
|
||||
y: pos.y + delta_y,
|
||||
};
|
||||
|
||||
let mut sound_effects = ecs.fetch_mut::<SoundEffects>();
|
||||
|
||||
if destination_x < 0
|
||||
|| (destination_x as usize) >= MAP_WIDTH
|
||||
|| destination_y < 0
|
||||
|| (destination_y as usize) >= MAP_HEIGHT
|
||||
{
|
||||
if map.in_bounds(destination) {
|
||||
if map.can_player_enter(destination) {
|
||||
pos.x = destination.x;
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -98,16 +92,14 @@ fn player_input(gs: &mut State, ctx: &mut BTerm) {
|
|||
}
|
||||
VirtualKeyCode::S => {
|
||||
let sound_effects = gs.ecs.fetch::<SoundEffects>();
|
||||
let pickup_sound = sound_effects.pickup.clone();
|
||||
gs.sound_system.play_sound(pickup_sound);
|
||||
gs.sound_system.play_sound(sound_effects.pickup.clone());
|
||||
}
|
||||
VirtualKeyCode::Escape => {
|
||||
ctx.quit();
|
||||
}
|
||||
_ => {
|
||||
let sound_effects = gs.ecs.fetch::<SoundEffects>();
|
||||
let bad_key_sound = sound_effects.bad_key.clone();
|
||||
gs.sound_system.play_sound(bad_key_sound);
|
||||
gs.sound_system.play_sound(sound_effects.bad_key.clone());
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
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},
|
||||
vga_color as vga,
|
||||
};
|
||||
use bracket_lib::{
|
||||
prelude::{to_cp437, BTerm, Rect, RGB},
|
||||
random::RandomNumberGenerator,
|
||||
};
|
||||
use bracket_lib::{prelude::*, random::RandomNumberGenerator};
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
pub enum TileType {
|
||||
|
@ -19,6 +16,14 @@ pub struct Map {
|
|||
border_bg: RGB,
|
||||
}
|
||||
|
||||
impl BaseMap for Map {}
|
||||
|
||||
impl Algorithm2D for Map {
|
||||
fn dimensions(&self) -> Point {
|
||||
Point::new(MAP_WIDTH, MAP_HEIGHT)
|
||||
}
|
||||
}
|
||||
|
||||
impl Map {
|
||||
pub fn new() -> Self {
|
||||
let mut tiles = vec![TileType::Floor; MAP_SIZE];
|
||||
|
@ -50,8 +55,8 @@ impl Map {
|
|||
);
|
||||
|
||||
for (i, tile) in self.tiles.iter().enumerate() {
|
||||
let x = i % MAP_WIDTH;
|
||||
let y = (i - x) / MAP_WIDTH;
|
||||
let point = self.index_to_point2d(i);
|
||||
let (x, y) = (point.x as usize, point.y as usize);
|
||||
|
||||
match tile {
|
||||
TileType::Floor => {
|
||||
|
@ -76,7 +81,11 @@ impl Map {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn tile_at(&self, x: usize, y: usize) -> TileType {
|
||||
self.tiles[(y * MAP_WIDTH) + x]
|
||||
pub fn tile_at(&self, point: Point) -> TileType {
|
||||
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