From 7a0d44bcc67988c2087b6629a28737e0dbd5eaf3 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Fri, 28 Oct 2022 21:22:24 +1100 Subject: [PATCH] Split code into multiple files --- src/components.rs | 20 ++++++++ src/main.rs | 127 ++++++++-------------------------------------- src/map.rs | 71 ++++++++++++++++++++++++++ src/player.rs | 31 +++++++++++ src/rect.rs | 0 5 files changed, 143 insertions(+), 106 deletions(-) create mode 100644 src/components.rs create mode 100644 src/map.rs create mode 100644 src/player.rs create mode 100644 src/rect.rs diff --git a/src/components.rs b/src/components.rs new file mode 100644 index 0000000..21a25af --- /dev/null +++ b/src/components.rs @@ -0,0 +1,20 @@ +use specs::prelude::*; +use specs_derive::Component; +use rltk::{RGB}; + + +#[derive(Component)] +pub struct Position { + pub x: i32, + pub y: i32, +} + +#[derive(Component)] +pub struct Renderable { + pub glyph: rltk::FontCharType, + pub fg: RGB, + pub bg: RGB, +} + +#[derive(Component, Debug)] +pub struct Player {} diff --git a/src/main.rs b/src/main.rs index 72c476f..fc55214 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,31 @@ -use rltk::{GameState, Rltk, VirtualConsole, VirtualKeyCode, RGB}; +use rltk::{GameState, Rltk, RGB}; use specs::prelude::*; -use specs_derive::Component; -use std::cmp::{max, min}; -struct State { - ecs: World, +mod components; +pub use components::*; + +mod map; +pub use map::*; + +mod player; +pub use player::*; + + +pub struct State { + pub ecs: World, } + +impl State { + fn run_systems(&mut self) { + self.ecs.maintain(); + } +} + impl GameState for State { fn tick(&mut self, ctx: &mut Rltk) { ctx.cls(); - // self.run_systems(); + self.run_systems(); player_input(self, ctx); let map = self.ecs.fetch::>(); @@ -25,117 +40,17 @@ impl GameState for State { } } -#[derive(Component)] -struct Player {} -#[derive(Component)] -struct Position { - x: i32, - y: i32, -} -#[derive(Component)] -struct Renderable { - glyph: rltk::FontCharType, - fg: RGB, - bg: RGB, -} -fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { - let mut positions = ecs.write_storage::(); - let mut players = ecs.write_storage::(); - let map = ecs.fetch::>(); - for (_player, pos) in (&mut players, &mut positions).join() { - let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y); - if map[destination_idx] != TileType::Wall { - pos.x = min(79, max(0, pos.x + delta_x)); - pos.y = min(49, max(0, pos.y + delta_y)); - } - } -} -fn player_input(gs: &mut State, ctx: &mut Rltk) { - match ctx.key { - None => {} - Some(key) => match key { - VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs), - VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs), - VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs), - VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs), - _ => {} - }, - } -} -#[derive(PartialEq, Copy, Clone)] -enum TileType { - Wall, - Floor, -} -pub fn xy_idx(x: i32, y: i32) -> usize { - (y as usize * 80) + x as usize -} -fn new_map() -> Vec { - let mut map = vec![TileType::Floor; 80 * 50]; - for x in 0..80 { - map[xy_idx(x, 0)] = TileType::Wall; - map[xy_idx(x, 49)] = TileType::Wall; - } - for y in 0..50 { - map[xy_idx(0, y)] = TileType::Wall; - map[xy_idx(79, y)] = TileType::Wall; - } - let mut rng = rltk::RandomNumberGenerator::new(); - for _i in 0..400 { - let x = rng.roll_dice(1, 79); - let y = rng.roll_dice(1, 49); - let idx = xy_idx(x, y); - if idx != xy_idx(40, 25) { - map[idx] = TileType::Wall; - } - } - - map -} - -fn draw_map(map: &[TileType], ctx: &mut Rltk) { - let mut y = 0; - let mut x = 0; - for tile in map.iter() { - match tile { - TileType::Floor => { - ctx.set( - x, - y, - RGB::from_f32(0.5, 0.5, 0.5), - RGB::from_f32(0., 0., 0.), - rltk::to_cp437('.'), - ); - } - TileType::Wall => { - ctx.set( - x, - y, - RGB::from_f32(0.0, 1.0, 0.0), - RGB::from_f32(0., 0., 0.), - rltk::to_cp437('#'), - ); - } - } - - x += 1; - if x > 79 { - x = 0; - y += 1; - } - } -} fn main() -> rltk::BError { use rltk::RltkBuilder; diff --git a/src/map.rs b/src/map.rs new file mode 100644 index 0000000..62a78a2 --- /dev/null +++ b/src/map.rs @@ -0,0 +1,71 @@ +use rltk::{GameState, Rltk, RGB}; +use std::cmp::{max, min}; + +#[derive(PartialEq, Copy, Clone)] +pub enum TileType { + Wall, + Floor, +} + +pub fn xy_idx(x: i32, y: i32) -> usize { + (y as usize * 80) + x as usize +} + +pub fn new_map() -> Vec { + let mut map = vec![TileType::Floor; 80 * 50]; + + for x in 0..80 { + map[xy_idx(x, 0)] = TileType::Wall; + map[xy_idx(x, 49)] = TileType::Wall; + } + for y in 0..50 { + map[xy_idx(0, y)] = TileType::Wall; + map[xy_idx(79, y)] = TileType::Wall; + } + + let mut rng = rltk::RandomNumberGenerator::new(); + + for _i in 0..400 { + let x = rng.roll_dice(1, 79); + let y = rng.roll_dice(1, 49); + let idx = xy_idx(x, y); + if idx != xy_idx(40, 25) { + map[idx] = TileType::Wall; + } + } + + map +} + +pub fn draw_map(map: &[TileType], ctx: &mut Rltk) { + let mut y = 0; + let mut x = 0; + for tile in map.iter() { + match tile { + TileType::Floor => { + ctx.set( + x, + y, + RGB::from_f32(0.5, 0.5, 0.5), + RGB::from_f32(0., 0., 0.), + rltk::to_cp437('.'), + ); + } + TileType::Wall => { + ctx.set( + x, + y, + RGB::from_f32(0.0, 1.0, 0.0), + RGB::from_f32(0., 0., 0.), + rltk::to_cp437('#'), + ); + } + } + + x += 1; + if x > 79 { + x = 0; + y += 1; + } + } +} \ No newline at end of file diff --git a/src/player.rs b/src/player.rs new file mode 100644 index 0000000..a3057be --- /dev/null +++ b/src/player.rs @@ -0,0 +1,31 @@ +use rltk::{VirtualKeyCode, Rltk}; +use specs::prelude::*; +use super::{Position, Player, TileType, xy_idx, State}; +use std::cmp::{min, max}; + +pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { + let mut positions = ecs.write_storage::(); + let mut players = ecs.write_storage::(); + let map = ecs.fetch::>(); + + for (_player, pos) in (&mut players, &mut positions).join() { + let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y); + if map[destination_idx] != TileType::Wall { + pos.x = min(79, max(0, pos.x + delta_x)); + pos.y = min(49, max(0, pos.y + delta_y)); + } + } +} + +pub fn player_input(gs: &mut State, ctx: &mut Rltk) { + match ctx.key { + None => {} + Some(key) => match key { + VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs), + VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs), + VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs), + VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs), + _ => {} + }, + } +} \ No newline at end of file diff --git a/src/rect.rs b/src/rect.rs new file mode 100644 index 0000000..e69de29