diff --git a/src/main.rs b/src/main.rs index d6af4ff..72c476f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use rltk::{GameState, Rltk, VirtualConsole, VirtualKeyCode, RGB}; use specs::prelude::*; use specs_derive::Component; -use std::{cmp::{max, min}, sync::TryLockResult}; +use std::cmp::{max, min}; struct State { ecs: World, @@ -13,6 +13,9 @@ impl GameState for State { // self.run_systems(); player_input(self, ctx); + let map = self.ecs.fetch::>(); + draw_map(&map, ctx); + let positions = self.ecs.read_storage::(); let renderables = self.ecs.read_storage::(); @@ -41,10 +44,14 @@ struct Renderable { 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() { - pos.x = min(79, max(0, pos.x + delta_x)); - pos.y = min(49, max(0, pos.y + delta_y)); + 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)); + } } } @@ -63,7 +70,8 @@ fn player_input(gs: &mut State, ctx: &mut Rltk) { #[derive(PartialEq, Copy, Clone)] enum TileType { - Wall, Floor + Wall, + Floor, } pub fn xy_idx(x: i32, y: i32) -> usize { @@ -71,7 +79,7 @@ pub fn xy_idx(x: i32, y: i32) -> usize { } fn new_map() -> Vec { - let mut map = vec![TileType::Floor; 80*50]; + let mut map = vec![TileType::Floor; 80 * 50]; for x in 0..80 { map[xy_idx(x, 0)] = TileType::Wall; @@ -96,6 +104,39 @@ fn new_map() -> Vec { 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; let context = RltkBuilder::simple80x50() @@ -109,7 +150,7 @@ fn main() -> rltk::BError { gs.ecs .create_entity() - .with(Position { x: 40, y: 35 }) + .with(Position { x: 40, y: 25 }) .with(Renderable { glyph: rltk::to_cp437('@'), fg: RGB::named(rltk::YELLOW),