-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.h
93 lines (79 loc) · 2.4 KB
/
player.h
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
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
class Player {
private:
float x; // Player's X position
float y; // Player's Y position
float rotation; // Player's rotation angle
float movementSpeed; // Player's movement speed
public:
Player(float startX, float startY, float startRotation, float startSpeed)
: x(startX), y(startY), rotation(startRotation), movementSpeed(startSpeed) {}
// Getters and Setters
float getX() const { return x; }
float getY() const { return y; }
float getRotation() const { return rotation; }
float getMovementSpeed() const { return movementSpeed; }
void setPosition(float newX, float newY) {
x = newX;
y = newY;
}
void setRotation(float newRotation) {
rotation = newRotation;
}
// Key Binding Functions
void processKeyInput(int key, int action, float deltaTime) {
if (action == GLFW_PRESS) {
switch (key) {
case GLFW_KEY_W:
moveForward(deltaTime);
break;
case GLFW_KEY_S:
moveBackward(deltaTime);
break;
case GLFW_KEY_A:
rotate(-M_PI_2 * deltaTime);
break;
case GLFW_KEY_D:
rotate(M_PI_2 * deltaTime);
break;
default:
break;
}
}
}
private:
// Movement Functions
void moveForward(float deltaTime) {
moveInDirection(rotation, deltaTime);
}
void moveBackward(float deltaTime) {
moveInDirection(rotation + M_PI, deltaTime);
}
void moveLeft(float deltaTime) {
moveInDirection(rotation - M_PI_2, deltaTime);
}
void moveRight(float deltaTime) {
moveInDirection(rotation + M_PI_2, deltaTime);
}
// Rotate the player
void rotate(float angle) {
rotation += angle;
normalizeRotation();
}
void moveInDirection(float direction, float deltaTime) {
float dx = movementSpeed * cos(direction) * deltaTime;
float dy = movementSpeed * sin(direction) * deltaTime;
x += dx;
y += dy;
}
void normalizeRotation() {
while (rotation < 0)
rotation += 2 * M_PI;
while (rotation >= 2 * M_PI)
rotation -= 2 * M_PI;
}
};