summaryrefslogtreecommitdiff
path: root/src/entity.rs
blob: 348967163cae4b3138454b4da653f7cbfec13860 (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
use ggez::graphics::spritebatch::SpriteBatch;
use ggez::nalgebra::Point2;

use crate::constants;

pub trait Operable {
    fn update(&mut self);
    fn draw(&self, spritebatch: &mut SpriteBatch);
}

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

impl Entity {
    pub fn new(spawn: Point2<f32>, map_dimensions: (f32, f32)) -> Entity {
        Entity {
            spawn,
            action: Action::IdleLeft,
            position: spawn,
            map_dimensions,
        }
    }

    pub fn update(&mut self) {
        self.move_position();
    }

    fn move_position(&mut self) {
        match self.action {
            Action::MovingUp => self.position.y -= constants::PLAYER_SPEED,
            Action::MovingUpLeft => {
                self.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            Action::MovingUpRight => {
                self.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            Action::MovingLeft => self.position.x -= constants::PLAYER_SPEED,
            Action::MovingDown => self.position.y += constants::PLAYER_SPEED,
            Action::MovingDownLeft => {
                self.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            Action::MovingDownRight => {
                self.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            Action::MovingRight => self.position.x += constants::PLAYER_SPEED,
            Action::IdleLeft | Action::IdleRight => (),
        }

        let pixel_width = constants::TILE_WIDTH * constants::TILE_SCALE;
        let pixel_height = constants::TILE_HEIGHT * constants::TILE_SCALE;

        if self.position.x < 0.0 {
            self.position.x = 0.0;
        } else if self.position.x + pixel_height > self.map_dimensions.0 {
            self.position.x = self.map_dimensions.0 - pixel_width;
        }

        if self.position.y < 0.0 {
            self.position.y = 0.0;
        } else if self.position.y + pixel_height > self.map_dimensions.1 {
            self.position.y = self.map_dimensions.1 - pixel_height;
        }
    }
}

#[derive(Clone, Hash, Eq, PartialEq, Debug)]
pub enum Action {
    IdleLeft,
    IdleRight,
    MovingUp,
    MovingDown,
    MovingLeft,
    MovingRight,
    MovingUpLeft,
    MovingUpRight,
    MovingDownLeft,
    MovingDownRight,
}