Sort ECS stuff into modules

This commit is contained in:
Alex Page 2022-01-26 00:54:14 -05:00
parent 5ba82fd26c
commit 8bdc8e31d6
9 changed files with 75 additions and 46 deletions

View file

@ -0,0 +1,5 @@
use specs::prelude::*;
use specs_derive::Component;
#[derive(Component)]
pub struct LeftMover {}

9
src/components/mod.rs Normal file
View file

@ -0,0 +1,9 @@
pub use left_mover::LeftMover;
pub use player::Player;
pub use position::Position;
pub use renderable::Renderable;
pub mod left_mover;
pub mod player;
pub mod position;
pub mod renderable;

14
src/components/player.rs Normal file
View file

@ -0,0 +1,14 @@
use std::time::Instant;
use specs::prelude::*;
use specs_derive::Component;
#[derive(Component, Debug)]
pub struct Player {
pub last_moved: Instant,
pub score: u32,
pub gems: u32,
pub whips: u32,
pub teleports: u32,
pub keys: u32,
}

View file

@ -0,0 +1,8 @@
use specs::prelude::*;
use specs_derive::Component;
#[derive(Component)]
pub struct Position {
pub x: i32,
pub y: i32,
}

View file

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

View file

@ -1,51 +1,27 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
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 resources::LevelNumber;
use sound::SoundSystem;
use sound_effects::SoundEffects;
use specs::prelude::*;
use specs_derive::Component;
use std::time::{Duration, Instant};
use systems::LeftWalker;
use vga_color as vga;
pub mod components;
pub mod constants;
mod map;
pub mod resources;
mod sidebar;
mod sound;
mod sound_effects;
pub mod systems;
pub mod vga_color;
#[derive(Default)]
struct LevelNumber(u32);
#[derive(Component)]
struct Position {
x: i32,
y: i32,
}
#[derive(Component)]
struct Renderable {
glyph: FontCharType,
fg: RGB,
bg: RGB,
}
#[derive(Component)]
struct LeftMover {}
#[derive(Component, Debug)]
struct Player {
last_moved: Instant,
score: u32,
gems: u32,
whips: u32,
teleports: u32,
keys: u32,
}
struct State {
ecs: World,
sound_system: SoundSystem,
@ -164,21 +140,6 @@ impl GameState for State {
}
}
struct LeftWalker {}
impl<'a> System<'a> for LeftWalker {
type SystemData = (ReadStorage<'a, LeftMover>, WriteStorage<'a, Position>);
fn run(&mut self, (lefty, mut pos): Self::SystemData) {
for (_lefty, pos) in (&lefty, &mut pos).join() {
pos.x -= 1;
if pos.x < 0 {
pos.x = 65;
}
}
}
}
impl State {
fn run_systems(&mut self) {
let mut lw = LeftWalker {};
@ -193,8 +154,8 @@ fn main() -> BError {
.with_title("Kroz")
.with_tile_dimensions(8, 16)
.build()?;
let mut ss = SoundSystem::new();
let mut ss = SoundSystem::new();
let sound_effects = SoundEffects::new(&ss);
ss.play_sound(sound_effects.startup.clone());

2
src/resources/mod.rs Normal file
View file

@ -0,0 +1,2 @@
#[derive(Default)]
pub struct LevelNumber(pub u32);

View file

@ -0,0 +1,17 @@
use crate::components::{left_mover::LeftMover, position::Position};
use specs::prelude::*;
pub struct LeftWalker {}
impl<'a> System<'a> for LeftWalker {
type SystemData = (ReadStorage<'a, LeftMover>, WriteStorage<'a, Position>);
fn run(&mut self, (lefty, mut pos): Self::SystemData) {
for (_lefty, pos) in (&lefty, &mut pos).join() {
pos.x -= 1;
if pos.x < 0 {
pos.x = 65;
}
}
}
}

3
src/systems/mod.rs Normal file
View file

@ -0,0 +1,3 @@
pub use left_walker::LeftWalker;
pub mod left_walker;