diff --git a/doc/progress.md b/doc/progress.md index c656353..5195365 100644 --- a/doc/progress.md +++ b/doc/progress.md @@ -1,4 +1,7 @@ # Devloppement Progress with screenshots First running programm with an empty map - \ No newline at end of file + + +Added Player movement and render + diff --git a/doc/screenshots/EmptyMap_and_Player.png b/doc/screenshots/EmptyMap_and_Player.png new file mode 100644 index 0000000..be17901 Binary files /dev/null and b/doc/screenshots/EmptyMap_and_Player.png differ diff --git a/src/main.rs b/src/main.rs index e53ebe7..1a1a804 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,12 +4,14 @@ /// date : 26/04/2022 /// brief : Main game file mod map; +mod player; mod prelude { pub use bracket_lib::prelude::*; pub const SCREEN_WIDTH: i32 = 80; pub const SCREEN_HEIGHT: i32 = 50; pub use crate::map::*; + pub use crate::player::*; } use prelude::*; @@ -17,18 +19,26 @@ const FPS_CAP : f32 = 30.0; struct State { map: Map, + player: Player, } impl State { fn new() -> Self { - Self {map: Map::new()} + Self { + map: Map::new(), + player: Player::new( + //spawns the player in the middle of the map + Point::new(SCREEN_WIDTH / 2,SCREEN_HEIGHT / 2) + ) + } } } impl GameState for State{ fn tick(&mut self,ctx: &mut BTerm){ ctx.cls(); - //ctx.print(1,1,"Hello, Bracket Terminal!"); + self.player.update(ctx,&self.map); self.map.render(ctx); + self.player.render(ctx); } } diff --git a/src/map.rs b/src/map.rs index ba054df..6afc02c 100644 --- a/src/map.rs +++ b/src/map.rs @@ -20,16 +20,31 @@ pub fn map_idx(x: i32, y: i32) -> usize { ((y * SCREEN_WIDTH) + x)as usize } impl Map { + /// Constructor pub fn new() -> Self{ Self { tiles: vec![TileType::Floor; NUM_TILES], } } - /* + + /// Bounds Logic pub fn in_bounds(&self,point : Point) -> bool{ - point.x >= + point.x >=0 && point.x < SCREEN_WIDTH + && point.y >= 0 && point.y < SCREEN_HEIGHT } - */ + pub fn can_enter_tile(&self,point: Point) -> bool { + self.in_bounds(point) + && self.tiles[map_idx(point.x, point.y)] == TileType::Floor + } + pub fn try_idx(&self, point : Point) -> Option { + if !self.in_bounds(point){ + None + }else{ + Some(map_idx(point.x,point.y)) + } + } + + /// Render Logic pub fn render(&self, ctx: &mut BTerm){ for y in 0..SCREEN_HEIGHT{ for x in 0..SCREEN_WIDTH { diff --git a/src/player.rs b/src/player.rs new file mode 100644 index 0000000..7b70ae6 --- /dev/null +++ b/src/player.rs @@ -0,0 +1,47 @@ +/// file : player.rs +/// author : Maxime Rohmer (from Herbet Wolverson book) +/// version : 0.1.0 +/// date : 26/04/2022 +/// brief : File that contains all the Player related stuff + +use crate::prelude::*; + +pub struct Player { + pub position: Point +} +impl Player{ + /// Construtor + pub fn new(position:Point) -> Self{ + Self{ + position + } + } + + /// Render Logic + pub fn render(&self,ctx: &mut BTerm) { + ctx.set( + self.position.x, + self.position.y, + WHITE, + BLACK, + to_cp437('@'), + ); + } + + /// Player Movement Logic + pub fn update(&mut self,ctx: &mut BTerm, map:&Map){ + if let Some(key) = ctx.key{ + let delta = match key{ + VirtualKeyCode::Left => Point::new(-1,0), + VirtualKeyCode::Right => Point::new(1,0), + VirtualKeyCode::Up => Point::new(0,-1), + VirtualKeyCode::Down => Point::new(0,1), + _ => Point::zero() + }; + let new_position = self.position + delta; + if map.can_enter_tile(new_position) { + self.position = new_position; + } + } + } +} \ No newline at end of file