Added player render, movements, and bounds detection
This commit is contained in:
@@ -2,3 +2,6 @@
|
||||
|
||||
First running programm with an empty map
|
||||
<img src="./screenshots/EmptyMap.png" width="80%">
|
||||
|
||||
Added Player movement and render
|
||||
<img src="./screenshots/EmptyMap_and_Player.png" width="80%">
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 5.5 KiB |
+12
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+18
-3
@@ -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<usize> {
|
||||
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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user