kroz-rs/src/input/player.rs

95 lines
3.5 KiB
Rust

use std::time::{Duration, Instant};
use bracket_lib::prelude::*;
use specs::prelude::*;
use crate::{
components::monster::*, components::*, constants::*, resources::*, systems::TimeSystem,
};
pub fn try_move(delta_x: i32, delta_y: i32, world: &World) {
let entities = world.entities();
let mut positions = world.write_storage::<Position>();
let mut players = world.write_storage::<Player>();
let monsters = world.write_storage::<Monster>();
let mut map = world.write_resource::<Map>();
let mut stats = world.write_resource::<Stats>();
let mut sound_output = world.write_resource::<SoundOutput>();
let wants_to_whips = world.read_storage::<WantsToWhip>();
let mut clock = world.write_resource::<Clock>();
for (player_entity, player, pos) in (&entities, &mut players, &mut positions).join() {
// The player shouldn't be able to move while whipping
if let Some(_wants_to_whip) = wants_to_whips.get(player_entity) {
continue;
}
let now = Instant::now();
if now - player.last_moved > Duration::from_secs_f32(PLAYER_STEP_PERIOD) {
let destination = Point {
x: pos.x + delta_x,
y: pos.y + delta_y,
};
let mut sound_effects = world.fetch_mut::<SoundEffects>();
if map.in_bounds(destination) {
if map.is_solid(destination) {
sound_output.play_sound(sound_effects.blocked.clone());
} else {
if let Some(e) = map.get_tile_content_at(destination) {
if let Some(monster) = monsters.get(e) {
stats.add_score(damage_for_kind(monster.kind));
stats.take_gems(damage_for_kind(monster.kind));
sound_output
.play_sound(sound_effect_for_kind(monster.kind, &sound_effects));
let _ = entities.delete(e);
}
}
map.clear_tile_content_at(Point::from(*pos));
pos.x = destination.x;
pos.y = destination.y;
map.set_tile_content_at(destination, player_entity);
TimeSystem::force_tick(&mut clock);
sound_output.play_sound(sound_effects.step.clone());
}
} else {
let static_sound = sound_effects.get_new_static_effect(&sound_output);
sound_output.play_sound(static_sound);
}
player.last_moved = now;
}
}
}
pub fn whip(world: &World) {
let entities = world.entities();
let players = world.read_storage::<Player>();
let positions = world.read_storage::<Position>();
let mut wants_to_whips = world.write_storage::<WantsToWhip>();
let mut stats = world.write_resource::<Stats>();
let mut sound_output = world.write_resource::<SoundOutput>();
let sound_effects = world.fetch::<SoundEffects>();
for (entity, _player, _position) in (&entities, &players, &positions).join() {
if wants_to_whips.get(entity).is_none() && stats.whips > 0 {
let _ = wants_to_whips.insert(
entity,
WantsToWhip {
frame: 0,
last_frame: Instant::now(),
sound: Some(sound_output.play_sound(sound_effects.whipping.clone())),
},
);
stats.whips -= 1;
}
}
}