diff options
author | tom barrett <spalf0@gmail.com> | 2019-07-03 05:54:53 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-07-03 05:54:53 -0500 |
commit | c04c59c7dd5ee6e3982a8ba45e42072b34f2be8a (patch) | |
tree | 20536de65ea296cfa46ddb613b1a192cf13ee422 /src/npc.rs | |
parent | 976ad40137cf468eda32b1d947c524bfaeeaa7da (diff) |
refactored how entities work
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>>), +} |