cultivar/common/src/entities/ferris.rs

39 lines
835 B
Rust
Raw Normal View History

2023-04-01 14:02:42 +00:00
use super::{traits::*, WalkError};
2023-04-01 05:52:41 +00:00
use crate::board::*;
2023-04-01 14:02:42 +00:00
#[derive(Clone, Debug)]
2023-04-01 05:52:41 +00:00
pub struct Ferris {
2023-04-01 14:02:42 +00:00
pub position: (i32, i32),
2023-04-01 05:52:41 +00:00
}
impl Entity for Ferris {}
2023-04-01 14:02:42 +00:00
impl HasPosition for Ferris {
2023-04-01 05:52:41 +00:00
fn get_position(&self) -> (i32, i32) {
self.position
}
}
impl Walker for Ferris {
2023-04-01 14:02:42 +00:00
fn walk(
&mut self,
board: &Board,
direction: Direction,
) -> Result<Position, (Position, WalkError)> {
2023-04-01 05:52:41 +00:00
let dpos = direction.to_position::<i32>();
2023-04-01 14:02:42 +00:00
let new_x = self.position.0.saturating_add(dpos.0);
let new_y = self.position.1.saturating_add(dpos.1);
if new_x >= board.origin.0
&& new_x < board.origin.0 + board.size.0
&& new_y >= board.origin.1
&& new_y < board.origin.1 + board.size.1
{
self.position.0 = new_x;
self.position.1 = new_y;
Ok(self.position)
} else {
Err((self.position, WalkError::OutOfBoard))
}
2023-04-01 05:52:41 +00:00
}
}