summaryrefslogtreecommitdiff
path: root/src/player.rs
blob: 976ab05cffa2cf329d93e701faa1a6d562807e8c (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
use ggez::event::KeyCode;
use ggez::graphics::{spritebatch::SpriteBatch, DrawParam};
use ggez::nalgebra::{Point2, Vector2};

use crate::constants;
use crate::tileset::Tileset;

pub struct Player {
    pub position: Point2<f32>,
    state: PlayerState,
}

impl Player {
    pub fn new() -> Player {
        Player {
            position: Point2::new(0.0, 0.0),
            state: PlayerState::Idle,
        }
    }

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

        spritebatch.add(draw_param);
    }

    pub fn update(&mut self) {
        match self.state {
            PlayerState::MovingUp => self.position.y -= constants::PLAYER_SPEED,
            PlayerState::MovingUpLeft => {
                self.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            PlayerState::MovingUpRight => {
                self.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.position.y -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            PlayerState::MovingLeft => self.position.x -= constants::PLAYER_SPEED,
            PlayerState::MovingDown => self.position.y += constants::PLAYER_SPEED,
            PlayerState::MovingDownLeft => {
                self.position.x -= constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            PlayerState::MovingDownRight => {
                self.position.x += constants::PLAYER_SPEED / 2.0_f32.sqrt();
                self.position.y += constants::PLAYER_SPEED / 2.0_f32.sqrt();
            }
            PlayerState::MovingRight => self.position.x += constants::PLAYER_SPEED,
            PlayerState::Idle => (),
        }
    }

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

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

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

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

impl Default for Player {
    fn default() -> Self {
        Player::new()
    }
}

#[derive(Clone)]
enum PlayerState {
    Idle,
    MovingUp,
    MovingDown,
    MovingLeft,
    MovingRight,
    MovingUpLeft,
    MovingUpRight,
    MovingDownLeft,
    MovingDownRight,
}