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 {
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
let player = &self.player;
let player_pos = player.get_position();
let mut level = &self.levels[player_pos.get_level() as usize];
let new_x = ((player_pos.get_x() as i16) + dx as i16) as u16;
let new_y = ((player_pos.get_y() as i16) + dy as i16) as u16;
let can_go: bool = match level.get_element(new_x as i16, new_y as i16) {
let level = &self.levels[player_pos.get_level()];
let new_x: i16 = player_pos.get_x() as i16 + dx;
let new_y: i16 = player_pos.get_y() as i16 + dy;
let can_go: bool = match level.get_element(new_x, new_y) {
None => { false }
Some(t) => {
match t {
@ -24,20 +24,21 @@ impl Game {
}
};
if can_go {
if level.get_element(new_x as i16, new_y as i16).unwrap() == LevelElement::StairDown {
let start_x = self.levels[(player_pos.get_level() + 1) as usize].start_x;
let start_y = self.levels[(player_pos.get_level() + 1) as usize].start_y;
self.player.set_position(player_pos.get_level() + 1, start_x as u16, start_y as u16);
self.levels[(player_pos.get_level() + 1) as usize].discover(start_x as i16, start_y as i16);
} else if level.get_element(new_x as i16, new_y as i16).unwrap() == LevelElement::StairUp {
if level.get_element(new_x, new_y).unwrap() == LevelElement::StairDown {
let next_level = player_pos.get_level() + 1;
let start_x: usize = self.levels[next_level].start_x;
let start_y: usize = self.levels[next_level].start_y;
self.player.set_position(next_level, start_x, start_y);
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(
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) as usize].end_y as u16,
self.levels[player_pos.get_level() - 1].end_x,
self.levels[player_pos.get_level() - 1].end_y,
);
} else {
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 crate::position::Position;
pub const LEVEL_WIDTH: usize = 50;
pub const LEVEL_HEIGHT: usize = 25;
@ -48,48 +49,46 @@ impl Level {
if x < 0 || y < 0 {
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;
}
if self.discovered[x as usize][y as usize] {
return Some(self.structure[x as usize][y as usize]);
if self.discovered[x][y] {
return Some(self.structure[x][y]);
}
return Some(LevelElement::Unknown);
}
/// discover the area with in the level around the given position
pub fn discover(&mut self, x: i16, y: i16) {
for x_r in x - 1..=x + 1 {
for y_r in y - 1..=y + 1 {
let tx = min(max(x_r, 0), (LEVEL_WIDTH - 1) as i16) as usize;
let ty = min(max(y_r, 0), (LEVEL_HEIGHT - 1) as i16) as usize;
self.discovered[tx][ty] = true;
pub fn discover(&mut self, pos: Position) {
let x = pos.get_x();
let y = pos.get_y();
for x_r in max(x - 1, 0)..=min(x + 1, LEVEL_WIDTH - 1) {
for y_r in max(y - 1, 0)..=min(y + 1, LEVEL_HEIGHT - 1) {
self.discovered[x_r][y_r] = true;
}
}
for x_r in x..=x + 2 {
let tx = min(max(x_r, 0), (LEVEL_WIDTH - 1) as i16) as usize;
self.discovered[tx][y as usize] = true;
if self.structure[tx][y as usize] == LevelElement::Wall {
for x_r in x..=min(x + 2, LEVEL_WIDTH - 1) {
self.discovered[x_r][y] = true;
if self.structure[x_r][y] == LevelElement::Wall {
break;
}
}
for y_r in y..=y + 2 {
let ty = min(max(y_r, 0), (LEVEL_HEIGHT - 1) as i16) as usize;
self.discovered[x as usize][ty] = true;
if self.structure[x as usize][ty] == LevelElement::Wall {
for y_r in y..=min(y + 2, LEVEL_WIDTH - 1) {
self.discovered[x][y_r] = true;
if self.structure[x][y_r] == LevelElement::Wall {
break;
}
}
for x_r in (x-2..x).rev() {
let tx = min(max(x_r, 0), (LEVEL_WIDTH - 1) as i16) as usize;
self.discovered[tx][y as usize] = true;
if self.structure[tx][y as usize] == LevelElement::Wall {
for x_r in (max(x - 2, 0)..x).rev() {
self.discovered[x_r][y] = true;
if self.structure[x_r][y] == LevelElement::Wall {
break;
}
}
for y_r in (y-2..y).rev() {
let ty = min(max(y_r, 0), (LEVEL_HEIGHT - 1) as i16) as usize;
self.discovered[x as usize][ty] = true;
if self.structure[x as usize][ty] == LevelElement::Wall {
for y_r in (max(y - 2, 0)..y).rev() {
self.discovered[x][y_r] = true;
if self.structure[x][y_r] == LevelElement::Wall {
break;
}
}

View File

@ -2,7 +2,7 @@ use ratatui::prelude::*;
use ratatui::widgets::Widget;
use crate::game::Game;
use crate::level::LevelElement;
use crate::level::{Level, LevelElement};
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) {
let player = &self.game.player;
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 at = area.top();
let al: u16 = area.left();
let at: u16 = area.top();
// draw the level elements
for x in al..area.right() {
@ -111,7 +111,9 @@ impl<'a> Widget for LevelWidget<'a> {
}
// 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,
};
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)?;
enable_raw_mode()?;
@ -89,6 +89,9 @@ fn main() -> Result<()> {
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
break;
}
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('d') {
;
}
if key.kind == KeyEventKind::Press && key.code == KeyCode::Left {
g.move_player(-1, 0);
}

View File

@ -5,12 +5,12 @@ use crate::position::Position;
pub struct Player {
name: String,
position: Position,
life: i16,
max_life: i16,
life: usize,
max_life: usize,
}
impl Player {
pub fn new(name: &str, max_life: i16) -> Player {
pub fn new(name: &str, max_life: usize) -> Player {
Player {
name: name.to_string(),
position: Position::new(0, 0, 0),
@ -21,19 +21,23 @@ impl Player {
pub fn get_name(&self) -> String {
return self.name.clone();
}
pub fn get_life(&self) -> i16 { self.life }
pub fn get_max_life(&self) -> i16 { self.max_life }
pub fn get_life(&self) -> usize { self.life }
pub fn get_max_life(&self) -> usize { self.max_life }
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 {
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);
}
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);
}
}

View File

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