39 lines
835 B
Rust
39 lines
835 B
Rust
use super::{traits::*, WalkError};
|
|
use crate::board::*;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Ferris {
|
|
pub position: (i32, i32),
|
|
}
|
|
|
|
impl Entity for Ferris {}
|
|
|
|
impl HasPosition for Ferris {
|
|
fn get_position(&self) -> (i32, i32) {
|
|
self.position
|
|
}
|
|
}
|
|
|
|
impl Walker for Ferris {
|
|
fn walk(
|
|
&mut self,
|
|
board: &Board,
|
|
direction: Direction,
|
|
) -> Result<Position, (Position, WalkError)> {
|
|
let dpos = direction.to_position::<i32>();
|
|
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))
|
|
}
|
|
}
|
|
}
|