summaryrefslogtreecommitdiff
path: root/src/entity.rs
blob: bd29a84bf248d4bacb3223f22ed4f98ce2d3a4c6 (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
use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect};
use ggez::nalgebra::{Point2, Vector2};

use crate::constants;
use crate::map::Map;
use crate::tileset::Tileset;

#[derive(Clone)]
pub struct Entity {
    position: Point2<f32>,
    source: Rect,
    spawn: Point2<f32>,
}

impl Entity {
    pub fn new(tileset: &Tileset, spawn: Point2<f32>) -> Entity {
        let mut source = tileset.get_tile_by_entity_keyframe("player-top", 0);
        source.h += tileset.get_tile_by_entity_keyframe("player-bottom", 0).h;

        Entity {
            position: spawn,
            source,
            spawn,
        }
    }

    pub fn draw(&self, spritebatch: &mut SpriteBatch) {
        let draw_param = DrawParam::default()
            .src(self.source)
            .dest(self.position)
            .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE));

        spritebatch.add(draw_param);
    }

    pub fn build_entities(tileset: &Tileset, map: &Map) -> Vec<Entity> {
        let mut entities = Vec::new();

        for (_name, position) in map.get_spawns() {
            entities.push(Entity::new(tileset, position));
        }

        entities
    }
}