summaryrefslogtreecommitdiff
path: root/src/entity.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/entity.rs')
-rw-r--r--src/entity.rs101
1 files changed, 25 insertions, 76 deletions
diff --git a/src/entity.rs b/src/entity.rs
index 1c0aa35..8086b77 100644
--- a/src/entity.rs
+++ b/src/entity.rs
@@ -1,91 +1,40 @@
-use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect};
-use ggez::nalgebra::{distance, Point2, Vector2};
-use std::time::Instant;
+use ggez::graphics::spritebatch::SpriteBatch;
+use ggez::nalgebra::Point2;
-use crate::constants;
-use crate::map::Map;
-use crate::math::random_nearby_point;
-use crate::tileset::Tileset;
+pub trait Operable {
+ fn update(&mut self);
+ fn draw(&self, spritebatch: &mut SpriteBatch);
+}
#[derive(Clone)]
pub struct Entity {
- behavior: Behavior,
- position: Point2<f32>,
- source: Rect,
- spawn: Point2<f32>,
+ pub position: Point2<f32>,
+ pub spawn: Point2<f32>,
+ pub action: Action,
+ pub map_dimensions: (f32, f32),
}
impl Entity {
- pub fn new(tileset: &Tileset, spawn: Point2<f32>) -> Entity {
- let mut source = tileset.get_tile_by_entity_keyframe("player-top", 0);
- source.h += tileset.get_tile_by_entity_keyframe("player-bottom", 0).h;
-
+ pub fn new(spawn: Point2<f32>, map_dimensions: (f32, f32)) -> Entity {
Entity {
spawn,
- source,
+ action: Action::IdleLeft,
position: spawn,
- behavior: Behavior::Wandering(None),
- }
- }
-
- pub fn update(&mut self) {
- match self.behavior {
- Behavior::Wandering(destination) => self.wandering(destination),
- Behavior::Waiting(time) => (),
- }
- }
-
- pub fn wandering(&mut self, destination: Option<Point2<f32>>) {
- match destination {
- Some(destination) => {
- if distance(&self.position, &destination) < constants::GOAL_DISTANCE {
- self.behavior = Behavior::Waiting(Instant::now())
- } else {
- if self.position.x < destination.x {
- self.position.x += constants::ENTITY_SPEED;
- } else {
- self.position.x -= constants::ENTITY_SPEED;
- }
- if self.position.y < destination.y {
- self.position.y += constants::ENTITY_SPEED;
- } else {
- self.position.y -= constants::ENTITY_SPEED;
- }
- }
- }
- None => {
- self.behavior = Behavior::Wandering(Some(random_nearby_point(
- self.spawn,
- constants::WANDER_DISTANCE,
- )))
- }
- }
- }
-
- pub fn waiting(&mut self) {}
-
- pub fn draw(&self, spritebatch: &mut SpriteBatch) {
- let draw_param = DrawParam::default()
- .src(self.source)
- .dest(self.position)
- .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE));
-
- spritebatch.add(draw_param);
- }
-
- pub fn build_entities(tileset: &Tileset, map: &Map) -> Vec<Entity> {
- let mut entities = Vec::new();
-
- for (_name, position) in map.get_spawns() {
- entities.push(Entity::new(tileset, position));
+ map_dimensions,
}
-
- entities
}
}
-#[derive(Clone)]
-enum Behavior {
- Waiting(Instant),
- Wandering(Option<Point2<f32>>),
+#[derive(Clone, Hash, Eq, PartialEq)]
+pub enum Action {
+ IdleLeft,
+ IdleRight,
+ MovingUp,
+ MovingDown,
+ MovingLeft,
+ MovingRight,
+ MovingUpLeft,
+ MovingUpRight,
+ MovingDownLeft,
+ MovingDownRight,
}