VOX
A little voxel engine
Loading...
Searching...
No Matches
ClientWorld.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include "define.hpp"
5
6#include "ecs.hpp"
7#include "List.hpp"
8#include "Player.hpp"
9#include "WorldGenerator.hpp"
10#include "logger.hpp"
11#include "CreateMeshData.hpp"
12#include "RenderAPI.hpp"
13#include "Camera.hpp"
14#include "World.hpp"
15#include "SoundEngine.hpp"
16#include "EventManager.hpp"
17
18#include "Packets.hpp"
19#include "Client.hpp"
20
21#include <unordered_map>
22#include <shared_mutex>
23#include <list>
24
25class ClientWorld : public World
26{
27
28public:
29
31 {
32 bool hit;
33 glm::vec3 block_position;
35 };
36
38 RenderAPI & render_api,
39 Sound::Engine & sound_engine,
40 Event::Manager & event_manager,
41 Client & client,
42 uint64_t my_player_id = 0
43 );
45
46 ClientWorld(ClientWorld & other) = delete;
47 ClientWorld(ClientWorld && other) = delete;
48 ClientWorld & operator=(ClientWorld & other) = delete;
49 ClientWorld & operator=(ClientWorld && other) = delete;
50
51
52 void updateBlock(glm::dvec3 position);
53 void updateSystems(const double delta_time_second);
54
55 int getRenderDistance() const;
56 void setRenderDistance(const int & render_distance);
57 int getServerLoadDistance() const;
58 void setServerLoadDistance(const int & server_load_distance);
59
60 std::pair<glm::dvec3, glm::dvec3> calculatePlayerMovement(
61 const uint64_t player_id,
62 const int8_t forward,
63 const int8_t backward,
64 const int8_t left,
65 const int8_t right,
66 const int8_t up,
67 const int8_t down,
68 const double delta_time_second
69 );
71 const uint64_t player_id,
72 const double x_offset,
73 const double y_offset
74 );
76 const uint64_t player_id
77 );
78 std::pair<bool, glm::vec3> playerAttack(
79 const uint64_t player_id,
80 bool attack
81 );
83 const uint64_t player_id,
84 bool use
85 );
87 const uint64_t player_id
88 );
89 void manageScroll(
90 const double x_offset,
91 const double y_offset
92 );
93 void updatePlayer(
94 const uint64_t player_id,
95 std::function<void(Player &)> update
96 );
97
98 void createMob();
99 void updateMobs(
100 const double delta_time_second
101 );
102 void createBaseMob(const glm::dvec3 & position, uint64_t player_id);
103
104 void modifyBlock(
105 const glm::vec3 & position,
106 const BlockInfo::Type & block_id
107 );
108
109 //Server side
110 uint64_t createPlayer(const glm::vec3 & position);
111
112 //Client side
113 void addPlayer(const uint64_t player_id, const glm::dvec3 & position);
114 void removePlayer(const uint64_t player_id);
115
116 void updatePlayerPosition(const uint64_t & player_id, const glm::dvec3 & position);
117 void applyPlayerMovement(const uint64_t & player_id, const glm::dvec3 & displacement);
118
119 Camera getCamera(const uint64_t player_id);
120 glm::dvec3 getPlayerPosition(const uint64_t player_id);
121
122
123 void addChunk(std::shared_ptr<Chunk> chunk);
124 void removeChunk(const glm::ivec3 & chunkPosition);
125 std::vector<glm::ivec3> getNeededChunks(const glm::vec3 & playerPosition);
126
127
128 std::shared_ptr<Chunk> localGetChunk(const glm::ivec3 & position) const;
129
130 void otherUpdate();
131
132 /*****************\
133 * NETWORK
134 \*****************/
135 void handlePacket(std::shared_ptr<IPacket> packet);
136
137
139private:
140
141 RenderAPI & m_render_api;
142 Sound::Engine & m_sound_engine;
143 Event::Manager & m_event_manager;
144 Client & m_client;
145
146 int m_render_distance = 16;
147 int m_server_load_distance = 0;
148
149 /*************************************
150 * CHUNKS AND MAP
151 *************************************/
152 std::unordered_set<glm::ivec2> m_loaded_chunks;
153 TracyLockableN (std::mutex, m_loaded_chunks_mutex, "Loaded Chunks");
154
155 // std::unordered_set<glm::ivec2> m_visible_chunks;
156 // std::mutex m_visible_chunks_mutex;
157
158 std::unordered_set<glm::ivec2> m_chunks_to_mesh;
159 TracyLockableN (std::mutex, m_chunks_to_mesh_mutex, "Chunks to mesh");
160
161 std::unordered_set<glm::ivec2> m_unload_set;
162 TracyLockableN (std::mutex, m_unload_set_mutex, "Unload Set");
163
164 std::queue<std::pair<glm::vec3, BlockInfo::Type>> m_blocks_to_set;
165 TracyLockableN (std::mutex, m_blocks_to_set_mutex, "Blocks to set");
166
167 std::queue<glm::ivec3> m_block_light_update;
168 TracyLockableN (std::mutex, m_block_light_update_mutex, "Block light update");
169
170
171
172 /*********************************************************
173 *
174 * METHODS
175 *
176 *********************************************************/
177
178
179 /*************************************
180 * CHUNKS AND MAP
181 *************************************/
182
183 void unloadChunk(const glm::ivec3 & chunkPosition);
184
192 void meshChunks(const glm::vec3 & playerPosition);
193
199 void meshChunk(const glm::ivec2 & chunkPosition);
200
206 void updateChunks(const glm::vec3 & playerPosition);
207
208 void doBlockSets();
209
210 void updateLights();
211
212 void setChunkNotMeshed(const glm::ivec2 & chunkPosition);
213
214
215 /*************************************
216 * ENTITIES
217 *************************************/
218 bool hitboxCollisionWithBlock(
219 const HitBox & hitbox,
220 const glm::dvec3 & position,
221 const uint64_t block_properties
222 );
223
224 /*************************************
225 * RAYCAST
226 *************************************/
227 RayCastOnBlockResult rayCastOnBlock(
228 const glm::vec3 & origin,
229 const glm::vec3 & direction,
230 const double max_distance
231 );
232
233 /*************************************
234 * NETWORK
235 *************************************/
236 void handleConnectionPacket(std::shared_ptr<ConnectionPacket> packet);
237 void handlePlayerMovePacket(std::shared_ptr<PlayerMovePacket> packet);
238 void handleDisconnectPacket(std::shared_ptr<DisconnectPacket> packet);
239 void handleBlockActionPacket(std::shared_ptr<BlockActionPacket> packet);
240 void handlePingPacket(std::shared_ptr<PingPacket> packet);
241 void handlePlayerListPacket(std::shared_ptr<PlayerListPacket> packet);
242 void handleChunkPacket(std::shared_ptr<ChunkPacket> packet);
243 void handleChunkUnloadPacket(std::shared_ptr<ChunkUnloadPacket> packet);
244 void handleLoadDistancePacket(std::shared_ptr<LoadDistancePacket> packet);
245
246
247 /*************************************
248 * MOBS
249 *************************************/
250 ecs::Manager<> m_ecs_manager;
251
252 void MovementSystem(const double delta_time_second);
253 void AISystem();
254 // void CollisionSystem();
255 void renderSystem();
256
257 /*************
258 * COMPONENTS
259 **************/
260 struct Position
261 {
262 glm::dvec3 p;
263 };
264
265 struct Velocity
266 {
267 glm::dvec3 v;
268 };
269
270 struct Acceleration
271 {
272 glm::dvec3 a;
273 };
274
275 struct AITarget
276 {
277 uint64_t target_id;
278 };
279 struct Mesh
280 {
281 uint64_t id;
282 };
283};
Type
Definition: Block.hpp:33
A simple camera class.
Definition: Camera.hpp:41
Definition: ClientWorld.hpp:26
ClientWorld & operator=(ClientWorld &other)=delete
std::shared_ptr< Chunk > localGetChunk(const glm::ivec3 &position) const
Definition: ClientWorld.cpp:1155
void otherUpdate()
Definition: ClientWorld.cpp:419
PlayerUseResult playerUse(const uint64_t player_id, bool use)
Definition: ClientWorld.cpp:697
void applyPlayerMovement(const uint64_t &player_id, const glm::dvec3 &displacement)
Definition: ClientWorld.cpp:368
void addChunk(std::shared_ptr< Chunk > chunk)
Definition: ClientWorld.cpp:41
void updatePlayerTargetBlock(const uint64_t player_id)
Definition: ClientWorld.cpp:613
void handlePacket(std::shared_ptr< IPacket > packet)
Definition: ClientWorldNetwork.cpp:3
void setRenderDistance(const int &render_distance)
Definition: ClientWorld.cpp:1166
std::pair< glm::dvec3, glm::dvec3 > calculatePlayerMovement(const uint64_t player_id, const int8_t forward, const int8_t backward, const int8_t left, const int8_t right, const int8_t up, const int8_t down, const double delta_time_second)
Definition: ClientWorld.cpp:424
void setServerLoadDistance(const int &server_load_distance)
Definition: ClientWorld.cpp:1176
void updateBlock(glm::dvec3 position)
Definition: ClientWorld.cpp:27
ClientWorld(ClientWorld &other)=delete
void updatePlayerPosition(const uint64_t &player_id, const glm::dvec3 &position)
Definition: ClientWorld.cpp:384
void modifyBlock(const glm::vec3 &position, const BlockInfo::Type &block_id)
Definition: ClientWorld.cpp:1138
void updatePlayer(const uint64_t player_id, std::function< void(Player &)> update)
Definition: ClientWorld.cpp:792
void manageScroll(const double x_offset, const double y_offset)
Definition: ClientWorld.cpp:780
void updateMobs(const double delta_time_second)
Definition: ClientWorld.cpp:824
void updatePlayerCamera(const uint64_t player_id, const double x_offset, const double y_offset)
Definition: ClientWorld.cpp:739
int getServerLoadDistance() const
Definition: ClientWorld.cpp:1181
void updateSystems(const double delta_time_second)
Definition: ClientWorld.cpp:1258
std::pair< bool, glm::vec3 > playerAttack(const uint64_t player_id, bool attack)
Definition: ClientWorld.cpp:658
void addPlayer(const uint64_t player_id, const glm::dvec3 &position)
Definition: ClientWorld.cpp:1111
void changePlayerViewMode(const uint64_t player_id)
Definition: ClientWorld.cpp:760
~ClientWorld()
Definition: ClientWorld.cpp:23
glm::dvec3 getPlayerPosition(const uint64_t player_id)
Definition: ClientWorld.cpp:963
Camera getCamera(const uint64_t player_id)
Definition: ClientWorld.cpp:956
void removePlayer(const uint64_t player_id)
Definition: ClientWorld.cpp:1129
void removeChunk(const glm::ivec3 &chunkPosition)
Definition: ClientWorld.cpp:71
std::vector< glm::ivec3 > getNeededChunks(const glm::vec3 &playerPosition)
void createBaseMob(const glm::dvec3 &position, uint64_t player_id)
Definition: ClientWorld.cpp:1191
int getRenderDistance() const
Definition: ClientWorld.cpp:1171
void createMob()
Definition: ClientWorld.cpp:802
ClientWorld(ClientWorld &&other)=delete
ClientWorld & operator=(ClientWorld &&other)=delete
uint64_t m_my_player_id
Definition: ClientWorld.hpp:138
uint64_t createPlayer(const glm::vec3 &position)
The Client side interface with the network module.
Definition: Client.hpp:37
Definition: EventManager.hpp:63
Definition: HitBox.hpp:11
Type
Definition: Item.hpp:14
Definition: Player.hpp:32
Definition: RenderAPI.hpp:288
Definition: SoundEngine.hpp:42
Definition: World.hpp:24
This class is the main class of the ECS system you can use it to create entitites,...
Definition: Manager.hpp:34
Definition: ClientWorld.hpp:31
glm::vec3 block_position
Definition: ClientWorld.hpp:33
ItemInfo::Type used_item
Definition: ClientWorld.hpp:34
bool hit
Definition: ClientWorld.hpp:32
Definition: RenderAPI.hpp:168
Definition: Player.hpp:22
Definition: test_main.cpp:13