summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/constants.rs6
-rw-r--r--src/npc.rs78
2 files changed, 49 insertions, 35 deletions
diff --git a/src/constants.rs b/src/constants.rs
index 35ddda7..0da4772 100644
--- a/src/constants.rs
+++ b/src/constants.rs
@@ -2,10 +2,10 @@ pub const TILE_WIDTH: f32 = 16.0;
pub const TILE_HEIGHT: f32 = 16.0;
pub const TILE_SCALE: f32 = 3.0;
-pub const PLAYER_SPEED: f32 = 5.0;
-pub const ENTITY_SPEED: f32 = 2.5;
+pub const PLAYER_SPEED: f32 = 2.5;
pub const WANDER_DISTANCE: f32 = 150.0;
-pub const GOAL_DISTANCE: f32 = 6.0;
+pub const GOAL_DISTANCE: f32 = 10.0;
+pub const WAIT_TIME: u64 = 3;
pub const FLOAT_PRECISION: f32 = 0.001;
diff --git a/src/npc.rs b/src/npc.rs
index 316908b..ad2a750 100644
--- a/src/npc.rs
+++ b/src/npc.rs
@@ -1,10 +1,12 @@
use ggez::graphics::spritebatch::SpriteBatch;
-use ggez::nalgebra::Point2;
+use ggez::nalgebra::{distance, Point2};
use rand::Rng;
use std::f32::consts::PI;
+use std::time::Instant;
use crate::animations::Animations;
-use crate::entity::{Entity, Operable};
+use crate::constants;
+use crate::entity::{Action, Entity, Operable};
use crate::map::Map;
use crate::tileset::Tileset;
@@ -21,12 +23,11 @@ impl Operable for NPC {
}
fn update(&mut self) {
- /*
match self.behavior {
Behavior::Wandering(destination) => self.wandering(destination),
- Behavior::Waiting(time) => (),
+ Behavior::Waiting(time) => self.waiting(time),
}
- */
+ self.entity.update();
self.animations.update(&self.entity.action);
}
}
@@ -35,39 +36,52 @@ impl NPC {
pub fn new(tileset: &Tileset, spawn: Point2<f32>, map_dimensions: (f32, f32)) -> NPC {
NPC {
entity: Entity::new(spawn, map_dimensions),
- behavior: Behavior::Wandering(None),
+ behavior: Behavior::Wandering(random_nearby_point(spawn, constants::WANDER_DISTANCE)),
animations: Animations::new(tileset),
}
}
- /*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;
- }
- }
+ fn wandering(&mut self, destination: Point2<f32>) {
+ let position = self.entity.position;
+
+ if distance(&position, &destination) < constants::GOAL_DISTANCE {
+ self.entity.action = Action::IdleRight;
+ self.behavior = Behavior::Waiting(Instant::now());
+ } else if (position.x - destination.x).abs() < constants::GOAL_DISTANCE {
+ if position.y > destination.y {
+ self.entity.action = Action::MovingUp;
+ } else {
+ self.entity.action = Action::MovingDown;
+ }
+ } else if (position.y - destination.y).abs() < constants::GOAL_DISTANCE {
+ if position.x > destination.x {
+ self.entity.action = Action::MovingLeft;
+ } else {
+ self.entity.action = Action::MovingRight;
+ }
+ } else if position.x > destination.x {
+ if position.y > destination.y {
+ self.entity.action = Action::MovingUpLeft;
+ } else {
+ self.entity.action = Action::MovingDownLeft;
}
- None => {
- self.behavior = Behavior::Wandering(Some(random_nearby_point(
- self.spawn,
- constants::WANDER_DISTANCE,
- )))
+ } else if position.x < destination.x {
+ if position.y > destination.y {
+ self.entity.action = Action::MovingUpRight;
+ } else {
+ self.entity.action = Action::MovingDownRight;
}
}
- }*/
+ }
- //fn waiting(&mut self) {}
+ fn waiting(&mut self, start: Instant) {
+ if start.elapsed().as_secs() > constants::WAIT_TIME {
+ self.behavior = Behavior::Wandering(random_nearby_point(
+ self.entity.spawn,
+ constants::WANDER_DISTANCE,
+ ));
+ }
+ }
pub fn build_npcs(tileset: &Tileset, map: &Map) -> Vec<NPC> {
let mut npcs = Vec::new();
@@ -82,8 +96,8 @@ impl NPC {
#[derive(Clone)]
enum Behavior {
- //Waiting(Instant),
- Wandering(Option<Point2<f32>>),
+ Waiting(Instant),
+ Wandering(Point2<f32>),
}
pub fn random_nearby_point(origin: Point2<f32>, within_radius: f32) -> Point2<f32> {