Switch from specs to hecs
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Alex Page 2022-02-03 00:07:12 -05:00
parent c0ce37aa38
commit d9606e8b87
27 changed files with 514 additions and 673 deletions

View file

@ -1,8 +1,5 @@
use std::time::Instant;
use specs::prelude::*;
use specs_derive::Component;
pub mod monster;
pub mod player;
pub mod position;
@ -15,9 +12,10 @@ pub use renderable::Renderable;
use crate::resources::sound_output::SoundEffectHandle;
#[derive(Component)]
pub struct WantsToWhip {
pub frame: u8,
pub last_frame: Instant,
pub sound: Option<SoundEffectHandle>,
}
pub struct Killed {}

View file

@ -3,10 +3,8 @@ use crate::{
resources::{sound_output::SoundSamples, SoundEffects},
};
use bracket_lib::prelude::*;
use specs::prelude::*;
use specs_derive::Component;
#[derive(Component, Debug)]
#[derive(Debug)]
pub struct Monster {
pub kind: MonsterKind,
pub ticks_until_move: i32,

View file

@ -1,9 +1,6 @@
use std::time::Instant;
use specs::prelude::*;
use specs_derive::Component;
#[derive(Component, Debug)]
#[derive(Debug)]
pub struct Player {
pub last_moved: Instant,
}

View file

@ -1,8 +1,6 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use specs_derive::Component;
#[derive(Component, Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub struct Position {
pub x: i32,
pub y: i32,

View file

@ -1,8 +1,6 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use specs_derive::Component;
#[derive(Component)]
#[derive(Debug)]
pub struct Renderable {
pub glyph: FontCharType,
pub fg: RGB,

View file

@ -1,13 +1,10 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use hecs::World;
use crate::{components::*, constants::*};
pub fn draw(world: &World, bterm: &mut BTerm) {
let positions = world.read_storage::<Position>();
let renderables = world.read_storage::<Renderable>();
for (pos, render) in (&positions, &renderables).join() {
for (_, (pos, render)) in &mut world.query::<(&Position, &Renderable)>() {
bterm.set(
pos.x + MAP_X as i32,
pos.y + MAP_Y as i32,

View file

@ -1,9 +1,7 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use crate::resources::*;
pub fn draw(world: &World, bterm: &mut BTerm) {
let map = world.fetch::<Map>();
map.draw(bterm);
pub fn draw(resources: &Resources, bterm: &mut BTerm) {
resources.map.draw(bterm);
}

View file

@ -1,5 +1,7 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use hecs::World;
use crate::resources::Resources;
mod entities;
mod map;
@ -7,10 +9,10 @@ mod sidebar;
pub mod vga_color;
mod whip;
pub fn draw(world: &World, bterm: &mut BTerm) {
pub fn draw(world: &World, resources: &Resources, bterm: &mut BTerm) {
bterm.cls();
map::draw(world, bterm);
map::draw(resources, bterm);
entities::draw(world, bterm);
whip::draw(world, bterm);
sidebar::draw(world, bterm);
whip::draw(world, resources, bterm);
sidebar::draw(resources, bterm);
}

View file

@ -1,10 +1,9 @@
use crate::constants::{SIDEBAR_POS_X, SIDEBAR_POS_Y};
use crate::graphics::vga_color as vga;
use crate::resources::{Clock, LevelNumber, ShowDebugInfo, Stats};
use crate::resources::Resources;
use bracket_lib::prelude::*;
use specs::prelude::*;
pub fn draw(world: &World, bterm: &mut BTerm) {
pub fn draw(resources: &Resources, bterm: &mut BTerm) {
// Blue background
bterm.fill_region(
Rect {
@ -39,14 +38,14 @@ pub fn draw(world: &World, bterm: &mut BTerm) {
bterm.print_centered_at(
SIDEBAR_POS_X + 6,
SIDEBAR_POS_Y + 4,
world.read_resource::<LevelNumber>().0 + 1,
resources.level_number + 1,
);
bterm.print(SIDEBAR_POS_X + 4, SIDEBAR_POS_Y + 6, "Gems");
bterm.print(SIDEBAR_POS_X + 4, SIDEBAR_POS_Y + 9, "Whips");
bterm.print(SIDEBAR_POS_X + 2, SIDEBAR_POS_Y + 12, "Teleports");
bterm.print(SIDEBAR_POS_X + 4, SIDEBAR_POS_Y + 15, "Keys");
let stats = world.read_resource::<Stats>();
let stats = &resources.stats;
bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 1, stats.score);
bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 7, stats.gems);
bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 10, stats.whips);
@ -93,7 +92,7 @@ pub fn draw(world: &World, bterm: &mut BTerm) {
bterm.print(SIDEBAR_POS_X + 3, SIDEBAR_POS_Y + 23, "Save");
bterm.print(SIDEBAR_POS_X + 3, SIDEBAR_POS_Y + 24, "Restore");
if world.read_resource::<ShowDebugInfo>().0 {
if resources.show_debug_info {
bterm.print_color_right(
SIDEBAR_POS_X + 14,
SIDEBAR_POS_Y,
@ -107,7 +106,7 @@ pub fn draw(world: &World, bterm: &mut BTerm) {
0,
RGB::named(vga::YELLOW_BRIGHT),
RGB::named(vga::BLACK),
&format!("{}", world.read_resource::<Clock>().ticks),
&format!("{}", resources.clock.ticks),
);
}
}

View file

@ -1,16 +1,14 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use hecs::World;
use crate::{components::*, constants::*, resources::*};
use super::{vga_color as vga, vga_color::get_by_index};
pub fn draw(world: &World, bterm: &mut BTerm) {
let positions = world.read_storage::<Position>();
let wants_to_whips = world.read_storage::<WantsToWhip>();
let map = world.read_resource::<Map>();
pub fn draw(world: &World, resources: &Resources, bterm: &mut BTerm) {
let mut rng = RandomNumberGenerator::new();
for (position, wants_to_whip) in (&positions, &wants_to_whips).join() {
for (_, (position, wants_to_whip)) in &mut world.query::<(&Position, &WantsToWhip)>() {
let color = RGB::named(get_by_index(rng.range(1, 16)));
let mut rendered_frame = wants_to_whip.frame;
let frame_data = loop {
@ -31,7 +29,7 @@ pub fn draw(world: &World, bterm: &mut BTerm) {
x: position.x + data.0,
y: position.y + data.1,
};
if map.in_bounds(dest) {
if resources.map.in_bounds(dest) {
break frame_data;
}
rendered_frame += 1;

View file

@ -1,49 +1,50 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use hecs::World;
use crate::resources::*;
mod player;
pub fn handle(world: &World, bterm: &mut BTerm) {
pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) {
match bterm.key {
None => {}
Some(key) => match key {
VirtualKeyCode::Left | VirtualKeyCode::J => {
player::try_move(-1, 0, world);
player::try_move(-1, 0, world, resources);
}
VirtualKeyCode::U | VirtualKeyCode::Home => player::try_move(-1, -1, world),
VirtualKeyCode::U | VirtualKeyCode::Home => player::try_move(-1, -1, world, resources),
VirtualKeyCode::Up | VirtualKeyCode::I => {
player::try_move(0, -1, world);
player::try_move(0, -1, world, resources);
}
VirtualKeyCode::O | VirtualKeyCode::PageUp => player::try_move(1, -1, world),
VirtualKeyCode::O | VirtualKeyCode::PageUp => player::try_move(1, -1, world, resources),
VirtualKeyCode::Right | VirtualKeyCode::K => {
player::try_move(1, 0, world);
player::try_move(1, 0, world, resources);
}
VirtualKeyCode::Comma | VirtualKeyCode::PageDown => {
player::try_move(1, 1, world, resources)
}
VirtualKeyCode::Comma | VirtualKeyCode::PageDown => player::try_move(1, 1, world),
VirtualKeyCode::Down | VirtualKeyCode::M => {
player::try_move(0, 1, world);
player::try_move(0, 1, world, resources);
}
VirtualKeyCode::N | VirtualKeyCode::End => player::try_move(-1, 1, world),
VirtualKeyCode::N | VirtualKeyCode::End => player::try_move(-1, 1, world, resources),
VirtualKeyCode::W => {
player::whip(world);
player::whip(world, resources);
}
VirtualKeyCode::S => {
let mut sound_system = world.write_resource::<SoundOutput>();
let sound_effects = world.fetch::<SoundEffects>();
sound_system.play_sound(sound_effects.pickup.clone());
resources
.sound_output
.play_sound(resources.sound_effects.pickup.clone());
}
VirtualKeyCode::D => {
let mut show_debug_info = world.write_resource::<ShowDebugInfo>();
show_debug_info.0 = !show_debug_info.0;
resources.show_debug_info = !resources.show_debug_info;
}
VirtualKeyCode::Escape | VirtualKeyCode::Q => {
bterm.quit();
}
_ => {
let mut sound_system = world.write_resource::<SoundOutput>();
let sound_effects = world.fetch::<SoundEffects>();
sound_system.play_sound(sound_effects.bad_key.clone());
resources
.sound_output
.play_sound(resources.sound_effects.bad_key.clone());
}
},
}

View file

@ -1,26 +1,16 @@
use std::time::{Duration, Instant};
use bracket_lib::prelude::*;
use specs::prelude::*;
use hecs::{Entity, With, Without, World};
use crate::{
components::monster::*, components::*, constants::*, resources::*, systems::TimeSystem,
};
use crate::{components::monster::*, components::*, constants::*, resources::*, systems};
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>();
pub fn try_move(delta_x: i32, delta_y: i32, world: &mut World, resources: &mut Resources) {
let mut to_kill: Vec<Entity> = Vec::new();
for (player_entity, player, pos) in (&entities, &mut players, &mut positions).join() {
for (player_entity, (player, pos)) in &mut world.query::<(&mut Player, &mut Position)>() {
// The player shouldn't be able to move while whipping
if let Some(_wants_to_whip) = wants_to_whips.get(player_entity) {
if let Ok(_wants_to_whip) = world.get::<WantsToWhip>(player_entity) {
continue;
}
@ -31,65 +21,77 @@ pub fn try_move(delta_x: i32, delta_y: i32, world: &World) {
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());
if resources.map.in_bounds(destination) {
if resources.map.is_solid(destination) {
resources
.sound_output
.play_sound(resources.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);
if let Some(monster_entity) = resources.map.get_tile_content_at(destination) {
if let Ok(monster) = world.get::<Monster>(monster_entity) {
resources.stats.add_score(damage_for_kind(monster.kind));
resources.stats.take_gems(damage_for_kind(monster.kind));
resources.sound_output.play_sound(sound_effect_for_kind(
monster.kind,
&resources.sound_effects,
));
to_kill.push(monster_entity);
}
}
map.clear_tile_content_at(Point::from(*pos));
resources.map.clear_tile_content_at(Point::from(*pos));
pos.x = destination.x;
pos.y = destination.y;
map.set_tile_content_at(destination, player_entity);
resources
.map
.set_tile_content_at(destination, player_entity);
TimeSystem::force_tick(&mut clock);
systems::time::force_tick(&mut resources.clock);
sound_output.play_sound(sound_effects.step.clone());
resources
.sound_output
.play_sound(resources.sound_effects.step.clone());
}
} else {
let static_sound = sound_effects.get_new_static_effect(&sound_output);
sound_output.play_sound(static_sound);
let static_sound = resources
.sound_effects
.get_new_static_effect(&resources.sound_output);
resources.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;
}
for e in to_kill {
let _ = world.despawn(e);
}
}
pub fn whip(world: &mut World, resources: &mut Resources) {
let mut to_add: Vec<Entity> = Vec::new();
for (entity, _) in world.query_mut::<With<Player, With<Position, Without<WantsToWhip, ()>>>>() {
if resources.stats.whips > 0 {
to_add.push(entity);
}
}
for e in to_add {
let _ = world.insert_one(
e,
WantsToWhip {
frame: 0,
last_frame: Instant::now(),
sound: Some(
resources
.sound_output
.play_sound(resources.sound_effects.whipping.clone()),
),
},
);
resources.stats.whips -= 1;
}
}

View file

@ -2,7 +2,6 @@
pub mod components;
pub mod constants;
pub mod difficulty;
mod graphics;
pub mod input;
pub mod levels;
@ -11,59 +10,54 @@ mod state;
pub mod systems;
pub mod tile_data;
use bracket_lib::prelude::*;
use components::{Monster, Player, Position, Renderable, WantsToWhip};
use resources::{
Clock, LevelNumber, Map, ShowDebugInfo, SoundEffects, SoundOutput, Stats, StopClock,
};
use specs::prelude::*;
use state::State;
use std::time::Instant;
use bracket_lib::prelude::*;
use hecs::World;
use resources::{difficulty::SECRET, *};
use state::State;
fn main() -> BError {
let context = BTermBuilder::vga(80, 25)
.with_fps_cap(60.0)
.with_title("Kroz")
.build()?;
let mut sound_system = SoundOutput::new();
let sound_effects = SoundEffects::new(&sound_system);
sound_system.play_sound(sound_effects.startup.clone());
let mut sound_output = SoundOutput::new();
let sound_effects = SoundEffects::new(&sound_output);
sound_output.play_sound(sound_effects.startup.clone());
let starting_level = 0;
let selected_difficulty = SECRET;
let mut world = World::new();
let starting_level = 0;
world.insert(sound_system);
world.insert(LevelNumber(starting_level));
world.insert(ShowDebugInfo(false));
world.insert(StopClock(false));
world.insert(Clock {
last_ticked: Instant::now(),
has_ticked: false,
ticks: 0,
});
world.insert(sound_effects);
let selected_difficulty = difficulty::SECRET;
world.insert(selected_difficulty);
world.insert(Stats {
score: 0,
gems: selected_difficulty.starting_gems,
whips: selected_difficulty.starting_whips,
whip_power: selected_difficulty.starting_whip_power,
teleports: selected_difficulty.starting_teleports,
keys: selected_difficulty.starting_keys,
});
world.register::<Position>();
world.register::<Renderable>();
world.register::<Monster>();
world.register::<Player>();
world.register::<WantsToWhip>();
let mut map = Map::from_level(levels::get_level(starting_level));
map.spawn_entities(&mut world);
world.insert(map);
let resources = Resources {
level_number: starting_level,
show_debug_info: false,
player_input: None,
stats: Stats {
score: 0,
gems: selected_difficulty.starting_gems,
whips: selected_difficulty.starting_whips,
whip_power: selected_difficulty.starting_whip_power,
teleports: selected_difficulty.starting_teleports,
keys: selected_difficulty.starting_keys,
},
clock: Clock {
last_ticked: Instant::now(),
has_ticked: false,
ticks: 0,
},
stop_clock: false,
map,
sound_effects,
sound_output,
selected_difficulty: Some(selected_difficulty),
};
// let descent_sounds: Vec<Sound> = (20..100)
// .rev()
@ -83,5 +77,5 @@ fn main() -> BError {
// let _ = gs.sound_system.play_sound(descent_effect);
main_loop(context, State::new(world))
main_loop(context, State::new(world, resources))
}

View file

@ -8,7 +8,7 @@ use crate::{
tile_data::{self, TileType},
};
use bracket_lib::{prelude::*, random::RandomNumberGenerator};
use specs::{Builder, Entity, World, WorldExt};
use hecs::{Entity, World};
pub struct Map {
tiles: Vec<TileType>,
@ -90,85 +90,84 @@ impl Map {
let point = Point::new(index % MAP_WIDTH, index / MAP_WIDTH);
match tile {
TileType::Player => {
let player_entity = world
.create_entity()
.with(Position {
let entity = world.spawn((
Player {
last_moved: Instant::now(),
},
Position {
x: point.x,
y: point.y,
})
.with(Renderable {
},
Renderable {
glyph: to_cp437('☻'),
fg: RGB::named(vga::YELLOW_BRIGHT),
bg: RGB::named(vga::BLACK),
})
.with(Player {
last_moved: Instant::now(),
})
.build();
self.tile_content[index] = Some(player_entity);
},
));
self.tile_content[index] = Some(entity);
}
TileType::Slow => {
let mut rng = RandomNumberGenerator::new();
world
.create_entity()
.with(Position {
let entity = world.spawn((
Monster {
kind: MonsterKind::Slow,
ticks_until_move: ticks_for_kind(MonsterKind::Slow),
},
Position {
x: point.x,
y: point.y,
})
.with(Renderable {
},
Renderable {
glyph: *rng
.random_slice_entry(&glyphs_for_kind(MonsterKind::Slow))
.unwrap(),
fg: color_for_kind(MonsterKind::Slow),
bg: RGB::named(vga::BLACK),
})
.with(Monster {
kind: MonsterKind::Slow,
ticks_until_move: ticks_for_kind(MonsterKind::Slow),
})
.build();
},
));
self.tile_content[index] = Some(entity);
}
TileType::Medium => {
let mut rng = RandomNumberGenerator::new();
world
.create_entity()
.with(Position {
let entity = world.spawn((
Monster {
kind: MonsterKind::Medium,
ticks_until_move: ticks_for_kind(MonsterKind::Medium),
},
Position {
x: point.x,
y: point.y,
})
.with(Renderable {
},
Renderable {
glyph: *rng
.random_slice_entry(&glyphs_for_kind(MonsterKind::Medium))
.unwrap(),
fg: color_for_kind(MonsterKind::Medium),
bg: RGB::named(vga::BLACK),
})
.with(Monster {
kind: MonsterKind::Medium,
ticks_until_move: ticks_for_kind(MonsterKind::Medium),
})
.build();
},
));
self.tile_content[index] = Some(entity);
}
TileType::Fast => {
let mut rng = RandomNumberGenerator::new();
world
.create_entity()
.with(Position {
let entity = world.spawn((
Monster {
kind: MonsterKind::Fast,
ticks_until_move: ticks_for_kind(MonsterKind::Fast),
},
Position {
x: point.x,
y: point.y,
})
.with(Renderable {
},
Renderable {
glyph: *rng
.random_slice_entry(&glyphs_for_kind(MonsterKind::Fast))
.unwrap(),
fg: color_for_kind(MonsterKind::Fast),
bg: RGB::named(vga::BLACK),
})
.with(Monster {
kind: MonsterKind::Fast,
ticks_until_move: ticks_for_kind(MonsterKind::Fast),
})
.build();
},
));
self.tile_content[index] = Some(entity);
}
_ => {}
}

View file

@ -1,47 +1,27 @@
pub mod clock;
pub mod difficulty;
pub mod map;
pub mod sound_effects;
pub mod sound_output;
pub mod stats;
use bracket_lib::prelude::*;
pub use clock::{Clock, StopClock};
pub use difficulty::Difficulty;
pub use map::Map;
pub use sound_effects::SoundEffects;
pub use sound_output::SoundOutput;
pub use stats::Stats;
#[derive(Default)]
pub struct LevelNumber(pub u32);
#[derive(Default)]
pub struct ShowDebugInfo(pub bool);
#[derive(Default)]
pub struct PlayerInput(pub Option<VirtualKeyCode>);
pub struct Stats {
pub score: u32,
pub gems: u32,
pub whips: u32,
pub whip_power: u32,
pub teleports: u32,
pub keys: u32,
}
type PlayerSurvived = bool;
impl Stats {
pub fn take_gems(&mut self, num_gems: u32) -> PlayerSurvived {
let new_num_gems = self.gems as i64 - num_gems as i64;
if new_num_gems <= 0 {
self.gems = 0;
false
} else {
self.gems = new_num_gems as u32;
true
}
}
pub fn add_score(&mut self, score: u32) {
self.score += score;
}
pub struct Resources {
pub level_number: u32,
pub show_debug_info: bool,
pub player_input: Option<VirtualKeyCode>,
pub stats: Stats,
pub clock: Clock,
pub stop_clock: bool,
pub map: Map,
pub selected_difficulty: Option<Difficulty>,
pub sound_effects: SoundEffects,
pub sound_output: SoundOutput,
}

27
src/resources/stats.rs Normal file
View file

@ -0,0 +1,27 @@
pub struct Stats {
pub score: u32,
pub gems: u32,
pub whips: u32,
pub whip_power: u32,
pub teleports: u32,
pub keys: u32,
}
type PlayerSurvived = bool;
impl Stats {
pub fn take_gems(&mut self, num_gems: u32) -> PlayerSurvived {
let new_num_gems = self.gems as i64 - num_gems as i64;
if new_num_gems <= 0 {
self.gems = 0;
false
} else {
self.gems = new_num_gems as u32;
true
}
}
pub fn add_score(&mut self, score: u32) {
self.score += score;
}
}

View file

@ -1,23 +1,23 @@
use crate::graphics::draw;
use crate::input::handle;
use crate::systems::*;
use crate::resources::Resources;
use crate::{graphics, input, systems};
use bracket_lib::prelude::*;
use specs::prelude::*;
use hecs::World;
pub struct State {
world: World,
resources: Resources,
}
impl GameState for State {
fn tick(&mut self, bterm: &mut BTerm) {
handle(&self.world, bterm);
run_systems(&mut self.world);
draw(&self.world, bterm);
input::handle(&mut self.world, &mut self.resources, bterm);
systems::run(&mut self.world, &mut self.resources);
graphics::draw(&self.world, &self.resources, bterm);
}
}
impl State {
pub fn new(world: World) -> Self {
State { world }
pub fn new(world: World, resources: Resources) -> Self {
State { world, resources }
}
}

View file

@ -1,21 +1,13 @@
pub mod monster_ai_system;
pub mod time_system;
pub mod whip_system;
pub mod monster_ai;
pub mod time;
pub mod whip;
pub use monster_ai_system::MonsterAiSystem;
use specs::prelude::*;
pub use time_system::TimeSystem;
pub use whip_system::WhipSystem;
use hecs::World;
pub fn run_systems(world: &mut World) {
let mut whip_system = WhipSystem {};
whip_system.run_now(world);
use crate::resources::Resources;
let mut monster_ai_system = MonsterAiSystem {};
monster_ai_system.run_now(world);
let mut time_system = TimeSystem {};
time_system.run_now(world);
world.maintain();
pub fn run(world: &mut World, resources: &mut Resources) {
whip::run(world, resources);
monster_ai::run(world, resources);
time::run(resources);
}

92
src/systems/monster_ai.rs Normal file
View file

@ -0,0 +1,92 @@
use crate::{
components::{monster::*, *},
resources::Resources,
tile_data::TileType,
};
use bracket_lib::{prelude::*, random::RandomNumberGenerator};
use hecs::*;
pub fn run(world: &mut World, resources: &mut Resources) {
let player_position = world
.query::<(&Player, &Position)>()
.iter()
.map(|(_, (_, &pos))| pos)
.collect::<Vec<_>>()
.first()
.cloned();
if let Some(player_pos) = player_position {
let mut has_died = None;
for (entity, (monster, position, renderable)) in
&mut world.query::<(&mut Monster, &mut Position, &mut Renderable)>()
{
if resources.clock.has_ticked {
monster.ticks_until_move -= 1;
if monster.ticks_until_move <= 0 {
// Change glyph
let mut rng = RandomNumberGenerator::new();
if let Some(glyph) =
rng.random_slice_entry(&monster::glyphs_for_kind(monster.kind))
{
renderable.glyph = *glyph;
}
// Move monster
let (mut delta_x, mut delta_y) = (0, 0);
if player_pos.x < position.x {
delta_x = -1;
}
if player_pos.x > position.x {
delta_x = 1;
}
if player_pos.y < position.y {
delta_y = -1;
}
if player_pos.y > position.y {
delta_y = 1;
}
let destination = Point {
x: position.x + delta_x,
y: position.y + delta_y,
};
if let Some(e) = resources.map.get_tile_content_at(destination) {
if let Ok(_player) = world.get::<Player>(e) {
// TODO: Sound
resources.map.clear_tile_content_at(Point::from(*position));
resources.stats.take_gems(damage_for_kind(monster.kind));
resources.sound_output.play_sound(sound_effect_for_kind(
monster.kind,
&resources.sound_effects,
));
has_died = Some(entity);
}
} else {
let tile = resources.map.get_tile_at_mut(destination);
match tile {
TileType::Wall => {}
TileType::Block => {
// TODO: Sound
*tile = TileType::Floor;
has_died = Some(entity);
}
_ => {
resources.map.clear_tile_content_at(Point::from(*position));
position.x = destination.x;
position.y = destination.y;
resources.map.set_tile_content_at(destination, entity);
}
}
}
monster.ticks_until_move = ticks_for_kind(monster.kind);
}
}
}
if let Some(e) = has_died {
let _ = world.despawn(e);
}
}
}

View file

@ -1,120 +0,0 @@
use crate::{
components::{
monster::{self, damage_for_kind, sound_effect_for_kind, ticks_for_kind},
Monster, Player, Position, Renderable,
},
resources::{Clock, Map, SoundEffects, SoundOutput, Stats},
tile_data::TileType,
};
use bracket_lib::{prelude::*, random::RandomNumberGenerator};
use specs::prelude::*;
pub struct MonsterAiSystem {}
#[allow(clippy::type_complexity)]
impl<'a> System<'a> for MonsterAiSystem {
type SystemData = (
Entities<'a>,
ReadExpect<'a, Clock>,
WriteExpect<'a, Map>,
WriteExpect<'a, Stats>,
ReadExpect<'a, SoundEffects>,
WriteExpect<'a, SoundOutput>,
WriteStorage<'a, Monster>,
WriteStorage<'a, Position>,
WriteStorage<'a, Renderable>,
ReadStorage<'a, Player>,
);
fn run(
&mut self,
(
entities,
clock,
mut map,
mut stats,
sound_effects,
mut sound_output,
mut monsters,
mut positions,
mut renderables,
players,
): Self::SystemData,
) {
let mut player_position = None;
for (_entity, _player, position) in (&entities, &players, &positions).join() {
player_position = Some(Point::from(*position));
}
if let Some(player_pos) = player_position {
let mut data = (&entities, &mut monsters, &mut positions, &mut renderables)
.join()
.collect::<Vec<_>>();
for (entity, monster, position, renderable) in &mut data {
if clock.has_ticked {
monster.ticks_until_move -= 1;
if monster.ticks_until_move <= 0 {
// Change glyph
let mut rng = RandomNumberGenerator::new();
if let Some(glyph) =
rng.random_slice_entry(&monster::glyphs_for_kind(monster.kind))
{
renderable.glyph = *glyph;
}
// Move monster
let (mut delta_x, mut delta_y) = (0, 0);
if player_pos.x < position.x {
delta_x = -1;
}
if player_pos.x > position.x {
delta_x = 1;
}
if player_pos.y < position.y {
delta_y = -1;
}
if player_pos.y > position.y {
delta_y = 1;
}
let destination = Point {
x: position.x + delta_x,
y: position.y + delta_y,
};
if let Some(e) = map.get_tile_content_at(destination) {
if let Some(_player) = players.get(e) {
// TODO: Sound
map.clear_tile_content_at(Point::from(**position));
stats.take_gems(damage_for_kind(monster.kind));
sound_output.play_sound(sound_effect_for_kind(
monster.kind,
&sound_effects,
));
let _ = entities.delete(*entity);
}
} else {
let tile = map.get_tile_at_mut(destination);
match tile {
TileType::Wall => {}
TileType::Block => {
// TODO: Sound
*tile = TileType::Floor;
let _ = entities.delete(*entity);
}
_ => {
map.clear_tile_content_at(Point::from(**position));
position.x = destination.x;
position.y = destination.y;
map.set_tile_content_at(destination, *entity);
}
}
}
monster.ticks_until_move = ticks_for_kind(monster.kind);
}
}
}
}
}
}

36
src/systems/time.rs Normal file
View file

@ -0,0 +1,36 @@
use std::time::{Duration, Instant};
use crate::constants::CLOCK_PERIOD;
use crate::resources::{Clock, Resources};
pub fn run(resources: &mut Resources) {
if !resources.stop_clock {
try_tick(&mut resources.clock);
} else {
reset(&mut resources.clock);
}
}
fn try_tick(clock: &mut Clock) {
if Instant::now() - clock.last_ticked > Duration::from_secs_f32(CLOCK_PERIOD) {
tick(clock);
} else {
clock.has_ticked = false;
}
}
pub fn force_tick(clock: &mut Clock) {
tick(clock);
}
fn tick(clock: &mut Clock) {
clock.has_ticked = true;
clock.last_ticked = Instant::now();
clock.ticks += 1;
}
fn reset(clock: &mut Clock) {
clock.last_ticked = Instant::now();
clock.has_ticked = false;
}

View file

@ -1,45 +0,0 @@
use crate::constants::CLOCK_PERIOD;
use specs::prelude::*;
use std::time::{Duration, Instant};
use crate::resources::{Clock, StopClock};
pub struct TimeSystem {}
#[allow(clippy::type_complexity)]
impl<'a> System<'a> for TimeSystem {
type SystemData = (WriteExpect<'a, Clock>, ReadExpect<'a, StopClock>);
fn run(&mut self, (mut clock, stop_clock): Self::SystemData) {
if !stop_clock.0 {
Self::try_tick(&mut clock);
} else {
Self::reset(&mut clock);
}
}
}
impl TimeSystem {
pub fn try_tick(clock: &mut Clock) {
if Instant::now() - clock.last_ticked > Duration::from_secs_f32(CLOCK_PERIOD) {
Self::tick(clock);
} else {
clock.has_ticked = false;
}
}
pub fn force_tick(clock: &mut Clock) {
Self::tick(clock);
}
fn tick(clock: &mut Clock) {
clock.has_ticked = true;
clock.last_ticked = Instant::now();
clock.ticks += 1;
}
pub fn reset(clock: &mut Clock) {
clock.last_ticked = Instant::now();
clock.has_ticked = false;
}
}

115
src/systems/whip.rs Normal file
View file

@ -0,0 +1,115 @@
use std::time::{Duration, Instant};
use bracket_lib::prelude::*;
use hecs::{Entity, World};
use crate::{
components::{monster::damage_for_kind, Monster, Position, WantsToWhip},
resources::Resources,
};
pub fn run(world: &mut World, resources: &mut Resources) {
let mut to_kill: Vec<Entity> = Vec::new();
let mut to_remove: Vec<Entity> = Vec::new();
for (entity, (position, mut wants_to_whip)) in
&mut world.query::<(&Position, &mut WantsToWhip)>()
{
resources.stop_clock = true;
let now = Instant::now();
if now - wants_to_whip.last_frame > Duration::from_secs_f32(0.1) {
let destination = loop {
let destination = match wants_to_whip.frame {
0 => Some(Point {
x: position.x - 1,
y: position.y - 1,
}),
1 => Some(Point {
x: position.x - 1,
y: position.y,
}),
2 => Some(Point {
x: position.x - 1,
y: position.y + 1,
}),
3 => Some(Point {
x: position.x,
y: position.y + 1,
}),
4 => Some(Point {
x: position.x + 1,
y: position.y + 1,
}),
5 => Some(Point {
x: position.x + 1,
y: position.y,
}),
6 => Some(Point {
x: position.x + 1,
y: position.y - 1,
}),
7 => Some(Point {
x: position.x,
y: position.y - 1,
}),
_ => None,
};
if let Some(dest) = destination {
if resources.map.in_bounds(dest) {
break destination;
}
wants_to_whip.frame += 1;
if wants_to_whip.frame > 7 {
break None;
}
}
};
if let Some(dest) = destination {
if let Some(e) = resources.map.get_tile_content_at(dest) {
if let Ok(monster) = world.get::<Monster>(e) {
resources.stats.add_score(damage_for_kind(monster.kind));
to_kill.push(e);
resources.map.clear_tile_content_at(dest);
if let Some(sound) = &mut wants_to_whip.sound {
sound.control::<oddio::Stop<_>, _>().stop();
}
if wants_to_whip.frame == 7 {
wants_to_whip.sound = None;
resources
.sound_output
.play_sound(resources.sound_effects.whipping_hit_end.clone());
} else {
wants_to_whip.sound = Some(
resources
.sound_output
.play_sound(resources.sound_effects.whipping_hit.clone()),
);
}
}
}
}
if wants_to_whip.frame < 7 {
wants_to_whip.frame += 1;
wants_to_whip.last_frame = now;
} else {
to_remove.push(entity);
resources.stop_clock = false;
if let Some(sound) = &mut wants_to_whip.sound {
sound.control::<oddio::Stop<_>, _>().stop();
wants_to_whip.sound = None;
}
}
}
}
for e in to_kill {
let _ = world.despawn(e);
}
for e in to_remove {
let _ = world.remove_one::<WantsToWhip>(e);
}
}

View file

@ -1,135 +0,0 @@
use std::time::{Duration, Instant};
use bracket_lib::prelude::*;
use specs::prelude::*;
use crate::{
components::{monster::damage_for_kind, Monster, Position, WantsToWhip},
resources::{Map, SoundEffects, SoundOutput, Stats, StopClock},
};
pub struct WhipSystem {}
#[allow(clippy::type_complexity)]
impl<'a> System<'a> for WhipSystem {
type SystemData = (
WriteExpect<'a, Map>,
WriteExpect<'a, StopClock>,
WriteExpect<'a, SoundOutput>,
ReadExpect<'a, SoundEffects>,
WriteExpect<'a, Stats>,
ReadStorage<'a, Position>,
WriteStorage<'a, WantsToWhip>,
WriteStorage<'a, Monster>,
Entities<'a>,
);
fn run(&mut self, data: Self::SystemData) {
let (
mut map,
mut stop_clock,
mut sound_output,
sound_effects,
mut stats,
positions,
mut wants_to_whips,
monsters,
entities,
) = data;
let mut entities_to_remove: Vec<Entity> = vec![];
for (entity, position, mut wants_to_whip) in
(&entities, &positions, &mut wants_to_whips).join()
{
stop_clock.0 = true;
let now = Instant::now();
if now - wants_to_whip.last_frame > Duration::from_secs_f32(0.1) {
let destination = loop {
let destination = match wants_to_whip.frame {
0 => Some(Point {
x: position.x - 1,
y: position.y - 1,
}),
1 => Some(Point {
x: position.x - 1,
y: position.y,
}),
2 => Some(Point {
x: position.x - 1,
y: position.y + 1,
}),
3 => Some(Point {
x: position.x,
y: position.y + 1,
}),
4 => Some(Point {
x: position.x + 1,
y: position.y + 1,
}),
5 => Some(Point {
x: position.x + 1,
y: position.y,
}),
6 => Some(Point {
x: position.x + 1,
y: position.y - 1,
}),
7 => Some(Point {
x: position.x,
y: position.y - 1,
}),
_ => None,
};
if let Some(dest) = destination {
if map.in_bounds(dest) {
break destination;
}
wants_to_whip.frame += 1;
if wants_to_whip.frame > 7 {
break None;
}
}
};
if let Some(dest) = destination {
if let Some(e) = map.get_tile_content_at(dest) {
if let Some(monster) = monsters.get(e) {
stats.add_score(damage_for_kind(monster.kind));
let _ = entities.delete(e);
map.clear_tile_content_at(dest);
if let Some(sound) = &mut wants_to_whip.sound {
sound.control::<oddio::Stop<_>, _>().stop();
}
if wants_to_whip.frame == 7 {
wants_to_whip.sound = None;
sound_output.play_sound(sound_effects.whipping_hit_end.clone());
} else {
wants_to_whip.sound = Some(
sound_output.play_sound(sound_effects.whipping_hit.clone()),
);
}
}
}
}
if wants_to_whip.frame < 7 {
wants_to_whip.frame += 1;
wants_to_whip.last_frame = now;
} else {
entities_to_remove.push(entity);
stop_clock.0 = false;
if let Some(sound) = &mut wants_to_whip.sound {
sound.control::<oddio::Stop<_>, _>().stop();
wants_to_whip.sound = None;
}
}
}
}
for entity in entities_to_remove {
wants_to_whips.remove(entity);
}
}
}