summaryrefslogtreecommitdiff
path: root/src/player.rs
blob: 2941934b6abac467238c7dbe2ca6ae736e4508ef (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use ggez::event::KeyCode;
use ggez::graphics::{spritebatch::SpriteBatch, DrawParam};
use ggez::nalgebra::{Point2, Vector2};

use crate::animation::Animations;
use crate::constants;
use crate::entity::{Action, Entity, Operable};
use crate::tileset::Tileset;

pub struct Player {
    entity: Entity,
    animations: Animations,
}

impl Operable for Player {
    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) {
        self.move_position();
        self.animations.update(&self.entity.action);
    }
}

impl Player {
    pub fn new(tileset: &Tileset, dimensions: (f32, f32)) -> Player {
        Player {
            entity: Entity::new(Point2::new(0.0, 0.0), dimensions),
            animations: Animations::new(tileset),
        }
    }

    pub fn get_position(&self) -> Point2<f32> {
        self.entity.position
    }

    fn move_position(&mut self) {
        match self.entity.action {
            Action::MovingUp => self.entity.position.y -= constants::PLAYER_SPEED,
            Action::MovingUpLeft => {
                self.entity.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.entity.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            Action::MovingUpRight => {
                self.entity.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.entity.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            Action::MovingLeft => self.entity.position.x -= constants::PLAYER_SPEED,
            Action::MovingDown => self.entity.position.y += constants::PLAYER_SPEED,
            Action::MovingDownLeft => {
                self.entity.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.entity.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            Action::MovingDownRight => {
                self.entity.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.entity.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            Action::MovingRight => self.entity.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.entity.position.x < 0.0 {
            self.entity.position.x = 0.0;
        } else if self.entity.position.x + pixel_height > self.entity.map_dimensions.0 {
            self.entity.position.x = self.entity.map_dimensions.0 - pixel_width;
        }

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

    pub fn give_key_down(&mut self, keycode: KeyCode) {
        let original_state = self.entity.action.clone();

        self.entity.action = match keycode {
            KeyCode::W => match original_state {
                Action::MovingLeft => Action::MovingUpLeft,
                Action::MovingRight => Action::MovingUpRight,
                _ => Action::MovingUp,
            },
            KeyCode::A => match original_state {
                Action::MovingUp => Action::MovingUpLeft,
                Action::MovingDown => Action::MovingDownLeft,
                _ => Action::MovingLeft,
            },
            KeyCode::S => match original_state {
                Action::MovingLeft => Action::MovingDownLeft,
                Action::MovingRight => Action::MovingDownRight,
                _ => Action::MovingDown,
            },
            KeyCode::D => match original_state {
                Action::MovingUp => Action::MovingUpRight,
                Action::MovingDown => Action::MovingDownRight,
                _ => Action::MovingRight,
            },
            _ => original_state,
        }
    }

    pub fn give_key_up(&mut self, keycode: KeyCode) {
        let original_state = self.entity.action.clone();

        self.entity.action = match keycode {
            KeyCode::W => match original_state {
                Action::MovingUpLeft => Action::MovingLeft,
                Action::MovingUpRight => Action::MovingRight,
                _ => Action::IdleLeft,
            },
            KeyCode::A => match original_state {
                Action::MovingUpLeft => Action::MovingUp,
                Action::MovingDownLeft => Action::MovingDown,
                _ => Action::IdleLeft,
            },
            KeyCode::S => match original_state {
                Action::MovingDownLeft => Action::MovingLeft,
                Action::MovingDownRight => Action::MovingRight,
                _ => Action::IdleRight,
            },
            KeyCode::D => match original_state {
                Action::MovingUpRight => Action::MovingUp,
                Action::MovingDownRight => Action::MovingDown,
                _ => Action::IdleRight,
            },
            _ => original_state,
        }
    }
}