aboutsummaryrefslogtreecommitdiff
path: root/src/entities.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/entities.rs')
-rw-r--r--src/entities.rs117
1 files changed, 67 insertions, 50 deletions
diff --git a/src/entities.rs b/src/entities.rs
index a01d86a..80e1235 100644
--- a/src/entities.rs
+++ b/src/entities.rs
@@ -1,12 +1,27 @@
use std::cmp;
-use pancurses::{Window};
-use crate::world::{Point};
use crate::tiling::TileType;
+use crate::world::{apply_movement, Movement, Point};
pub trait Entity {
+ /// Get information about the entity
fn info(&self) -> String;
+ /// Initial placement of the entity
fn place(&mut self, location: Point);
+ /// Get the tiletype for the entity
+ fn get_tiletype(&self) -> &TileType;
+ /// Get the entity's current location
+ fn get_location(&self) -> &Point;
+ /// Get the entity's previous location (before it moved)
+ fn get_previous_location(&self) -> &Point;
+ /// Move the entity to another point
+ fn move_to(&mut self, location: Point);
+ /// Move the entity with a movement differential
+ fn move_by(&mut self, movement: Movement) -> Result<(), String>;
+ /// Know if the entity needs to be re-rendered
+ fn is_dirty(&self) -> bool;
+ /// Declare the entity clean
+ fn clean(&mut self);
}
#[derive(Clone)]
@@ -15,43 +30,26 @@ pub struct Character {
pub class: String,
pub health: i32,
pub level: i32,
- pub location: Point,
+ location: Point,
+ previous_location: Point,
+ dirty: bool,
max_health: i32,
attack: i32,
dodge: i32,
luck: i32,
xp: i32,
- tile_type: TileType
-}
-
-pub trait Render {
- fn render(&self, window: &Window);
+ tile_type: TileType,
}
pub trait Enemy {
- fn new(
- class: String,
- health: i32,
- attack: i32,
- dodge: i32,
- luck: i32,
- location: Point
- ) -> Self;
+ fn new(class: String, health: i32, attack: i32, dodge: i32, luck: i32, location: Point)
+ -> Self;
fn set_tile_type(&mut self, tile_type: TileType);
}
pub trait Player {
- fn new(
- name: String,
- class: String,
- health: i32,
- attack: i32,
- dodge: i32,
- luck: i32,
- level: i32,
- location: Point
- ) -> Self;
+ fn new(name: String, class: String, health: i32, attack: i32, dodge: i32, luck: i32) -> Self;
fn damage(&mut self, damage_amount: i32);
fn heal(&mut self, heal_amount: i32);
fn attack(&self) -> i32;
@@ -59,26 +57,11 @@ pub trait Player {
fn stats(&self) -> String;
}
-impl Render for Character {
- fn render(&self, window: &Window) {
- // window.mv(window.get_max_y() - 2, 0);
- // window.clrtoeol();
-
- // window.refresh();
-
- // window.addstr(self.character.info() + "\n");
-
- // window.mv(self.character.location.1 as i32,self.character.location.0 as i32);
- // window.refresh();
- // draw_block(&window, self.character.tile_type);
- // window.refresh();
-
- }
-}
-
impl Entity for Character {
fn place(&mut self, location: Point) {
self.location = location;
+ self.previous_location = location;
+ self.dirty = true;
}
fn info(&self) -> String {
@@ -87,6 +70,38 @@ impl Entity for Character {
self.class, self.health, self.attack, self.dodge, self.luck
)
}
+
+ fn get_tiletype(&self) -> &TileType {
+ &self.tile_type
+ }
+
+ fn get_location(&self) -> &Point {
+ &self.location
+ }
+
+ fn get_previous_location(&self) -> &Point {
+ &self.previous_location
+ }
+
+ fn move_to(&mut self, location: Point) {
+ self.previous_location = self.location;
+ self.location = location;
+ self.dirty = true;
+ }
+
+ fn move_by(&mut self, movement: Movement) -> Result<(), String> {
+ self.previous_location = self.location;
+ self.location = apply_movement(self.location, movement)?;
+ Ok(())
+ }
+
+ fn is_dirty(&self) -> bool {
+ self.dirty
+ }
+
+ fn clean(&mut self) {
+ self.dirty = false;
+ }
}
impl Enemy for Character {
@@ -96,7 +111,7 @@ impl Enemy for Character {
attack: i32,
dodge: i32,
luck: i32,
- location: Point
+ location: Point,
) -> Character {
Character {
name: class.clone(),
@@ -109,7 +124,9 @@ impl Enemy for Character {
level: 0,
xp: 0,
location: location,
- tile_type: TileType::Character
+ previous_location: location,
+ tile_type: TileType::Character,
+ dirty: false,
}
}
@@ -126,8 +143,6 @@ impl Player for Character {
attack: i32,
dodge: i32,
luck: i32,
- level: i32,
- location: Point
) -> Character {
Character {
name: name,
@@ -139,8 +154,10 @@ impl Player for Character {
luck: luck,
xp: 0,
level: 0,
- location: location,
- tile_type: TileType::Player
+ location: (0, 0),
+ previous_location: (0, 0),
+ tile_type: TileType::Player,
+ dirty: false,
}
}
@@ -177,7 +194,7 @@ mod tests {
use super::*;
fn test_attack() {
- let bob: Character = Enemy::new("Rogue".to_string(), 1, 4, 1, 4, (0, 0));
+ let bob: Character = Enemy::new(String::from("Rogue"), 1, 4, 1, 4, (0, 0));
assert_eq!(bob.attack(), 6);
}