summaryrefslogtreecommitdiff
path: root/src/npc.rs
blob: 48e08b928c6e54566e327478dac7c7a8c4baec72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use ggez::graphics::{spritebatch::SpriteBatch, DrawParam};
use ggez::nalgebra::{/*distance,*/ Point2, Vector2};
//use std::time::Instant;

use crate::animation::Animations;
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,
    animations: Animations,
}

impl Operable for NPC {
    fn draw(&self, spritebatch: &mut SpriteBatch) {
        spritebatch.add(
            DrawParam::default()
                .src(self.animations.current.current.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.animations.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),
            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;
                    }
                }
            }
            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>>),
}