From c04c59c7dd5ee6e3982a8ba45e42072b34f2be8a Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 3 Jul 2019 05:54:53 -0500 Subject: refactored how entities work --- src/npc.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/npc.rs (limited to 'src/npc.rs') 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, 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>) { + 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 { + 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>), +} -- cgit v1.2.3