From ba6d63ff8ec6f000533ef6ea212e93c7ef876677 Mon Sep 17 00:00:00 2001 From: maxluli Date: Tue, 26 Apr 2022 14:00:10 +0200 Subject: [PATCH] Added player render, movements, and bounds detection --- doc/progress.md | 5 ++- doc/screenshots/EmptyMap_and_Player.png | Bin 0 -> 5637 bytes src/main.rs | 14 ++++++- src/map.rs | 21 +++++++++-- src/player.rs | 47 ++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 doc/screenshots/EmptyMap_and_Player.png create mode 100644 src/player.rs 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 0000000000000000000000000000000000000000..be1790157ecc9dea6aa7bf74e76eda5272bb970b GIT binary patch literal 5637 zcmeI0ZB!Fy7RLtxQ7GCYwkTlPu4iSpLhvlEf`AdkO<5|nFEqR*k?t zsX$X-#3V8BD@xg!0ssie@2;ht^8b6m-&Ur)oW?$RO0!BwOWoy{8s=A% z=Fj>h{o<+y+LB8vF1!&H7x=ri89&@CJ)6+WnS$u88c_5nR$&AWX+*F^2@ppPnZ7eV zDDBPFe>-%QDvkkw2yKl01>o?pR+|s7vB2mH?CBm20Qx`ZSq_|EdOQ-?7V;t)IJ_>J z2y|yQ4ghDGi3^ibtlDzsrOPTC>k4_Vm{v&KR~&|9^!+tv3*Tx@UX8dluj@pm!#c9Q z%>55ynUX;+RIdHBfnG*(;iITKx2z@TN9>GKD!L615*z;X_Dr2Ts^vMF_yKeshfHnn zICIoy>osDt&F6+!9Jo?1&b=)R6u9YXhqY@z_F!o5X&<2YmDU}4{S6Dvg3*&88YI%y zdbaB5vYdT?GMwb9`0{*m&Jg))49_umR+38aq^u11#C>CQB_Bc2xvLXR-*X0Zas+cp zHF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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