remove casts

This commit is contained in:
Joachim Lusiardi 2023-11-21 08:46:24 +01:00
parent 1cfe16c4ef
commit f9deea4bde
6 changed files with 73 additions and 64 deletions

View File

@ -7,14 +7,14 @@ pub struct Game {
} }
impl Game { impl Game {
pub fn move_player(&mut self, dx: i8, dy: i8) { pub fn move_player(&mut self, dx: i16, dy: i16) {
// check if move is allowed first // check if move is allowed first
let player = &self.player; let player = &self.player;
let player_pos = player.get_position(); let player_pos = player.get_position();
let mut level = &self.levels[player_pos.get_level() as usize]; let level = &self.levels[player_pos.get_level()];
let new_x = ((player_pos.get_x() as i16) + dx as i16) as u16; let new_x: i16 = player_pos.get_x() as i16 + dx;
let new_y = ((player_pos.get_y() as i16) + dy as i16) as u16; let new_y: i16 = player_pos.get_y() as i16 + dy;
let can_go: bool = match level.get_element(new_x as i16, new_y as i16) { let can_go: bool = match level.get_element(new_x, new_y) {
None => { false } None => { false }
Some(t) => { Some(t) => {
match t { match t {
@ -24,20 +24,21 @@ impl Game {
} }
}; };
if can_go { if can_go {
if level.get_element(new_x as i16, new_y as i16).unwrap() == LevelElement::StairDown { if level.get_element(new_x, new_y).unwrap() == LevelElement::StairDown {
let start_x = self.levels[(player_pos.get_level() + 1) as usize].start_x; let next_level = player_pos.get_level() + 1;
let start_y = self.levels[(player_pos.get_level() + 1) as usize].start_y; let start_x: usize = self.levels[next_level].start_x;
self.player.set_position(player_pos.get_level() + 1, start_x as u16, start_y as u16); let start_y: usize = self.levels[next_level].start_y;
self.levels[(player_pos.get_level() + 1) as usize].discover(start_x as i16, start_y as i16); self.player.set_position(next_level, start_x, start_y);
} else if level.get_element(new_x as i16, new_y as i16).unwrap() == LevelElement::StairUp { self.levels[next_level].discover(self.player.get_position());
} else if level.get_element(new_x, new_y).unwrap() == LevelElement::StairUp {
self.player.set_position( self.player.set_position(
player_pos.get_level() - 1, player_pos.get_level() - 1,
self.levels[(player_pos.get_level() - 1) as usize].end_x as u16, self.levels[player_pos.get_level() - 1].end_x,
self.levels[(player_pos.get_level() - 1) as usize].end_y as u16, self.levels[player_pos.get_level() - 1].end_y,
); );
} else { } else {
self.player.change_position(dx, dy); self.player.change_position(dx, dy);
self.levels[player_pos.get_level() as usize].discover(new_x as i16, new_y as i16); self.levels[player_pos.get_level()].discover(self.player.get_position());
} }
} }
} }

View File

@ -1,4 +1,5 @@
use std::cmp::{max, min}; use std::cmp::{max, min};
use crate::position::Position;
pub const LEVEL_WIDTH: usize = 50; pub const LEVEL_WIDTH: usize = 50;
pub const LEVEL_HEIGHT: usize = 25; pub const LEVEL_HEIGHT: usize = 25;
@ -48,48 +49,46 @@ impl Level {
if x < 0 || y < 0 { if x < 0 || y < 0 {
return None; return None;
} }
if x >= LEVEL_WIDTH as i16 || y >= LEVEL_HEIGHT as i16 { let x = x as usize;
let y = y as usize;
if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT {
return None; return None;
} }
if self.discovered[x as usize][y as usize] { if self.discovered[x][y] {
return Some(self.structure[x as usize][y as usize]); return Some(self.structure[x][y]);
} }
return Some(LevelElement::Unknown); return Some(LevelElement::Unknown);
} }
/// discover the area with in the level around the given position /// discover the area with in the level around the given position
pub fn discover(&mut self, x: i16, y: i16) { pub fn discover(&mut self, pos: Position) {
for x_r in x - 1..=x + 1 { let x = pos.get_x();
for y_r in y - 1..=y + 1 { let y = pos.get_y();
let tx = min(max(x_r, 0), (LEVEL_WIDTH - 1) as i16) as usize; for x_r in max(x - 1, 0)..=min(x + 1, LEVEL_WIDTH - 1) {
let ty = min(max(y_r, 0), (LEVEL_HEIGHT - 1) as i16) as usize; for y_r in max(y - 1, 0)..=min(y + 1, LEVEL_HEIGHT - 1) {
self.discovered[tx][ty] = true; self.discovered[x_r][y_r] = true;
} }
} }
for x_r in x..=x + 2 { for x_r in x..=min(x + 2, LEVEL_WIDTH - 1) {
let tx = min(max(x_r, 0), (LEVEL_WIDTH - 1) as i16) as usize; self.discovered[x_r][y] = true;
self.discovered[tx][y as usize] = true; if self.structure[x_r][y] == LevelElement::Wall {
if self.structure[tx][y as usize] == LevelElement::Wall {
break; break;
} }
} }
for y_r in y..=y + 2 { for y_r in y..=min(y + 2, LEVEL_WIDTH - 1) {
let ty = min(max(y_r, 0), (LEVEL_HEIGHT - 1) as i16) as usize; self.discovered[x][y_r] = true;
self.discovered[x as usize][ty] = true; if self.structure[x][y_r] == LevelElement::Wall {
if self.structure[x as usize][ty] == LevelElement::Wall {
break; break;
} }
} }
for x_r in (x-2..x).rev() { for x_r in (max(x - 2, 0)..x).rev() {
let tx = min(max(x_r, 0), (LEVEL_WIDTH - 1) as i16) as usize; self.discovered[x_r][y] = true;
self.discovered[tx][y as usize] = true; if self.structure[x_r][y] == LevelElement::Wall {
if self.structure[tx][y as usize] == LevelElement::Wall {
break; break;
} }
} }
for y_r in (y-2..y).rev() { for y_r in (max(y - 2, 0)..y).rev() {
let ty = min(max(y_r, 0), (LEVEL_HEIGHT - 1) as i16) as usize; self.discovered[x][y_r] = true;
self.discovered[x as usize][ty] = true; if self.structure[x][y_r] == LevelElement::Wall {
if self.structure[x as usize][ty] == LevelElement::Wall {
break; break;
} }
} }

View File

@ -2,7 +2,7 @@ use ratatui::prelude::*;
use ratatui::widgets::Widget; use ratatui::widgets::Widget;
use crate::game::Game; use crate::game::Game;
use crate::level::LevelElement; use crate::level::{Level, LevelElement};
const FG_BROWN: Color = Color::Rgb(186, 74, 0); const FG_BROWN: Color = Color::Rgb(186, 74, 0);
@ -70,10 +70,10 @@ impl<'a> Widget for LevelWidget<'a> {
fn render(self, area: Rect, buf: &mut Buffer) { fn render(self, area: Rect, buf: &mut Buffer) {
let player = &self.game.player; let player = &self.game.player;
let player_pos = player.get_position(); let player_pos = player.get_position();
let level = &self.game.levels[player_pos.get_level() as usize]; let level: &Level = &self.game.levels[player_pos.get_level()];
let al = area.left(); let al: u16 = area.left();
let at = area.top(); let at: u16 = area.top();
// draw the level elements // draw the level elements
for x in al..area.right() { for x in al..area.right() {
@ -111,7 +111,9 @@ impl<'a> Widget for LevelWidget<'a> {
} }
// draw player // draw player
self.set_cell(buf, al + player_pos.get_x(), at + player_pos.get_y(), "8", Color::Red, Color::Gray); let player_x = al + player_pos.get_x() as u16;
let player_y = at + player_pos.get_y() as u16;
self.set_cell(buf, player_x, player_y, "8", Color::Red, Color::Gray);
} }
} }

View File

@ -36,7 +36,7 @@ fn main() -> Result<()> {
levels, levels,
}; };
g.move_player((levels[0].start_x) as i8, (levels[0].start_y) as i8); g.move_player(levels[0].start_x as i16, levels[0].start_y as i16);
stdout().execute(EnterAlternateScreen)?; stdout().execute(EnterAlternateScreen)?;
enable_raw_mode()?; enable_raw_mode()?;
@ -89,6 +89,9 @@ fn main() -> Result<()> {
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') { if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
break; break;
} }
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('d') {
;
}
if key.kind == KeyEventKind::Press && key.code == KeyCode::Left { if key.kind == KeyEventKind::Press && key.code == KeyCode::Left {
g.move_player(-1, 0); g.move_player(-1, 0);
} }

View File

@ -5,12 +5,12 @@ use crate::position::Position;
pub struct Player { pub struct Player {
name: String, name: String,
position: Position, position: Position,
life: i16, life: usize,
max_life: i16, max_life: usize,
} }
impl Player { impl Player {
pub fn new(name: &str, max_life: i16) -> Player { pub fn new(name: &str, max_life: usize) -> Player {
Player { Player {
name: name.to_string(), name: name.to_string(),
position: Position::new(0, 0, 0), position: Position::new(0, 0, 0),
@ -21,19 +21,23 @@ impl Player {
pub fn get_name(&self) -> String { pub fn get_name(&self) -> String {
return self.name.clone(); return self.name.clone();
} }
pub fn get_life(&self) -> i16 { self.life } pub fn get_life(&self) -> usize { self.life }
pub fn get_max_life(&self) -> i16 { self.max_life } pub fn get_max_life(&self) -> usize { self.max_life }
pub fn change_life(&mut self, by: i16) { pub fn change_life(&mut self, by: i16) {
self.life = max(0, min(self.max_life, self.life + by)); self.life = max(0,
min(self.max_life,
(self.life as i16 + by) as usize,
),
);
} }
pub fn get_position(&self) -> Position { pub fn get_position(&self) -> Position {
self.position self.position
} }
pub fn change_position(&mut self, dx: i8, dy: i8) { pub fn change_position(&mut self, dx: i16, dy: i16) {
self.position.change(dx, dy); self.position.change(dx, dy);
} }
pub fn set_position(&mut self, level: u8, x: u16, y:u16) { pub fn set_position(&mut self, level: usize, x: usize, y: usize) {
self.position.set(level, x, y); self.position.set(level, x, y);
} }
} }

View File

@ -1,35 +1,35 @@
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub struct Position { pub struct Position {
level: u8, level: usize,
x: u16, x: usize,
y: u16, y: usize,
} }
impl Position { impl Position {
pub fn new(level: u8, x: u16, y: u16) -> Self { pub fn new(level: usize, x: usize, y: usize) -> Self {
Self { Self {
level, level,
x, x,
y, y,
} }
} }
pub fn get_level(&self) -> u8 { pub fn get_level(&self) -> usize{
self.level self.level
} }
pub fn get_x(&self) -> u16 { pub fn get_x(&self) -> usize {
self.x self.x
} }
pub fn get_y(&self) -> u16 { pub fn get_y(&self) -> usize {
self.y self.y
} }
pub fn change(&mut self, dx: i8, dy: i8) { pub fn change(&mut self, dx: i16, dy: i16) {
self.x = ((self.x as i16) + dx as i16) as u16; self.x = ((self.x as i16) + dx) as usize;
self.y = ((self.y as i16) + dy as i16) as u16; self.y = ((self.y as i16) + dy) as usize;
} }
pub fn set(&mut self, level: u8, x: u16, y:u16) { pub fn set(&mut self, level: usize, x: usize, y:usize) {
self.level = level; self.level = level;
self.x = x; self.x = x;
self.y = y; self.y = y;