VOX
A little voxel engine
Loading...
Searching...
No Matches
Player.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "define.hpp"
4#include "Transform.hpp"
5#include "Camera.hpp"
6#include "HitBox.hpp"
7#include "Block.hpp"
8#include "Item.hpp"
9
10#include <glm/glm.hpp>
11#include <glm/gtc/matrix_transform.hpp>
12
13#include "Tracy.hpp"
14
15#include <cstdint>
16#include <chrono>
17#include <mutex>
18#include <optional>
19#include <array>
20
22{
23 bool hit;
24 glm::vec3 block_position;
25 glm::vec3 normal;
26 glm::vec3 hit_position;
29};
30
31class Player
32{
33
34public:
35
36 Player();
37 ~Player();
38
39 Player(Player & other) = delete;
40 Player(Player && other) = delete;
41 Player & operator=(Player & other) = delete;
42 Player & operator=(Player && other) = delete;
43
44 void moveDirection(double x_offset, double y_offset);
45
46 Camera camera() const;
47
48 glm::dvec3 getTransformedMovement(glm::dvec3 move) const;
49
50 glm::dvec3 cameraPosition() const; // the position of the eyes relative to entity position
51 glm::dvec3 direction() const;
52
53 bool shouldCollide() const;
54 bool shouldFall() const;
55 bool isFlying() const;
56
57 bool canJump() const;
58 void startJump();
59
60 void startFall();
61 double fallDuration();
62
63 bool canAttack() const;
64 void startAttack();
65
66 bool canUse() const;
67 void startUse();
68
69 TracyLockableN (std::mutex, mutex, "Player internal");
70
71 enum class GameMode
72 {
77
78 enum class ViewMode
79 {
83
84 double default_speed = 4.0;
85 double sprint_speed_factor = 1.5;
86 double sneak_speed_factor = 0.3;
87 double fly_speed_factor = 3.0;
88 double jump_speed_factor = 1.1;
89 double jump_force = 10.0;
90 double gravity = -30.0;
91
92
93 bool on_ground = false;
94 bool flying = false;
95 bool sneaking = false;
96 bool sprinting = false;
97 bool jumping = false;
98 bool swimming = false;
99
101
102 Transform transform = Transform({0.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {1.0, 1.0, 1.0});
103 HitBox hitbox = HitBox({-0.3, 0, -0.3}, {0.6, 1.8, 0.6});
104 HitBox feet = HitBox({-0.3, -0.01, -0.3}, {0.6, 0.1, 0.6});
105
106 glm::dvec3 velocity = glm::dvec3(0.0);
107
109
110 glm::dvec3 eye_position = glm::dvec3(0.0, 1.6, 0.0);
111 double yaw = 0.0;
112 double pitch = 0.0;
113
114 RayCastOnBlockResult targeted_block{false, glm::vec3(0.0), glm::vec3(0.0), glm::vec3(0.0), BlockInfo::Type::Air, false};
115
116 uint64_t connection_id = 0;
117 uint64_t player_id = 0;
118 uint64_t player_ticket_id = 0;
119
120
121 std::array<ItemInfo::Type, 9> toolbar_items = {
131 };
133
134private:
135
136 std::chrono::nanoseconds fall_start_time = std::chrono::nanoseconds(0);
137 glm::dvec3 fall_start_position = glm::dvec3(0.0);
138
139 std::chrono::nanoseconds last_jump_time = std::chrono::steady_clock::now().time_since_epoch();
140 std::chrono::milliseconds jump_delai = std::chrono::milliseconds(500);
141 std::chrono::milliseconds jump_delai_after_fall = std::chrono::milliseconds(100);
142
143 std::chrono::nanoseconds last_attack_time = std::chrono::steady_clock::now().time_since_epoch();
144 std::chrono::milliseconds attack_delai = std::chrono::milliseconds(200);
145
146 std::chrono::nanoseconds last_use_time = std::chrono::steady_clock::now().time_since_epoch();
147 std::chrono::milliseconds use_delai = std::chrono::milliseconds(200);
148
149 void updateTransform();
150};
Type
Definition: Block.hpp:33
A simple camera class.
Definition: Camera.hpp:41
Definition: HitBox.hpp:11
Definition: Player.hpp:32
void moveDirection(double x_offset, double y_offset)
Definition: Player.cpp:19
bool canAttack() const
Definition: Player.cpp:113
glm::dvec3 cameraPosition() const
Definition: Player.cpp:51
void startAttack()
Definition: Player.cpp:118
bool swimming
Definition: Player.hpp:98
Player & operator=(Player &other)=delete
bool jumping
Definition: Player.hpp:97
bool canJump() const
Definition: Player.cpp:98
double gravity
Definition: Player.hpp:90
glm::dvec3 direction() const
Definition: Player.cpp:32
double yaw
Definition: Player.hpp:111
void startUse()
Definition: Player.cpp:128
enum Player::GameMode gameMode
glm::dvec3 eye_position
Definition: Player.hpp:110
int toolbar_cursor
Definition: Player.hpp:132
bool on_ground
Definition: Player.hpp:93
double default_speed
Definition: Player.hpp:84
double fly_speed_factor
Definition: Player.hpp:87
std::array< ItemInfo::Type, 9 > toolbar_items
Definition: Player.hpp:121
bool shouldFall() const
Definition: Player.cpp:77
uint64_t player_id
Definition: Player.hpp:117
bool sprinting
Definition: Player.hpp:96
Player & operator=(Player &&other)=delete
bool canUse() const
Definition: Player.cpp:123
BlockInfo::Type ground_block
Definition: Player.hpp:100
Player(Player &&other)=delete
GameMode
Definition: Player.hpp:72
~Player()
Definition: Player.cpp:15
void startFall()
Definition: Player.cpp:87
RayCastOnBlockResult targeted_block
Definition: Player.hpp:114
uint64_t connection_id
Definition: Player.hpp:116
bool sneaking
Definition: Player.hpp:95
bool flying
Definition: Player.hpp:94
double jump_force
Definition: Player.hpp:89
void startJump()
Definition: Player.cpp:106
double jump_speed_factor
Definition: Player.hpp:88
glm::dvec3 velocity
Definition: Player.hpp:106
HitBox hitbox
Definition: Player.hpp:103
ViewMode
Definition: Player.hpp:79
enum Player::ViewMode view_mode
double fallDuration()
Definition: Player.cpp:93
uint64_t player_ticket_id
Definition: Player.hpp:118
double sneak_speed_factor
Definition: Player.hpp:86
double sprint_speed_factor
Definition: Player.hpp:85
HitBox feet
Definition: Player.hpp:104
Camera camera() const
Definition: Player.cpp:27
int jump_remaining
Definition: Player.hpp:108
bool isFlying() const
Definition: Player.cpp:82
bool shouldCollide() const
Definition: Player.cpp:72
Player(Player &other)=delete
glm::dvec3 getTransformedMovement(glm::dvec3 move) const
Definition: Player.cpp:41
TracyLockableN(std::mutex, mutex, "Player internal")
double pitch
Definition: Player.hpp:112
Player()
Definition: Player.cpp:11
Definition: Transform.hpp:11
Definition: Player.hpp:22
glm::vec3 hit_position
Definition: Player.hpp:26
BlockInfo::Type block
Definition: Player.hpp:27
glm::vec3 block_position
Definition: Player.hpp:24
bool inside_block
Definition: Player.hpp:28
bool hit
Definition: Player.hpp:23
glm::vec3 normal
Definition: Player.hpp:25
Definition: test_main.cpp:24