diff options
Diffstat (limited to 'src/npc.rs')
-rw-r--r-- | src/npc.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/npc.rs b/src/npc.rs new file mode 100644 index 0000000..96cb489 --- /dev/null +++ b/src/npc.rs @@ -0,0 +1,92 @@ +use ggez::graphics::{spritebatch::SpriteBatch, DrawParam}; +use ggez::nalgebra::{/*distance,*/ Point2, Vector2}; +//use std::time::Instant; + +use crate::animation::Animation; +use crate::constants; +use crate::entity::{Entity, Operable}; +use crate::map::Map; +use crate::tileset::Tileset; + +#[derive(Clone)] +pub struct NPC { + entity: Entity, + behavior: Behavior, + animation: Animation, +} + +impl Operable for NPC { + fn draw(&self, spritebatch: &mut SpriteBatch) { + spritebatch.add( + DrawParam::default() + .src(self.animation.source) + .dest(self.entity.position) + .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)), + ); + } + + fn update(&mut self) { + /* + match self.behavior { + Behavior::Wandering(destination) => self.wandering(destination), + Behavior::Waiting(time) => (), + } + */ + self.animation.update(&self.entity.action); + } +} + +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), + animation: Animation::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; + } + } + } + None => { + self.behavior = Behavior::Wandering(Some(random_nearby_point( + self.spawn, + constants::WANDER_DISTANCE, + ))) + } + } + }*/ + + //fn waiting(&mut self) {} + + pub fn build_npcs(tileset: &Tileset, map: &Map) -> Vec<NPC> { + let mut npcs = Vec::new(); + + for (_name, position) in map.get_spawns() { + npcs.push(NPC::new(tileset, position, map.get_dimensions())); + } + + npcs + } +} + +#[derive(Clone)] +enum Behavior { + //Waiting(Instant), + Wandering(Option<Point2<f32>>), +} |