summaryrefslogtreecommitdiffstats
path: root/src/GameState.cpp
blob: 3ccff374f17af012ea7722e08aba93d85324ef04 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "GameState.hpp"

GameState::GameState(NetworkClient *Net, bool &quit) : nc(Net), isRunning(quit) {
	Front = glm::vec3(0.0f, 0.0f, -1.0f);
	this->SetPosition(glm::vec3(0.0f, 0.0f, 3.0f));
	this->WorldUp = glm::vec3(0.0f, 1.0f, 0.0f);
	this->updateCameraVectors();
}

void GameState::Update(float deltaTime) {
	if (g_IsGameStarted) {
		std::chrono::steady_clock clock;
		static auto timeOfPreviousSendedPacket(clock.now());
		auto delta = clock.now() - timeOfPreviousSendedPacket;
		using namespace std::chrono_literals;
		if (delta >= 50ms) {
			nc->SendPacket(std::make_shared<PacketPlayerPositionAndLookSB>(g_PlayerX, g_PlayerY, g_PlayerZ, g_PlayerYaw,
			                                                               g_PlayerPitch, g_OnGround));
			timeOfPreviousSendedPacket = clock.now();
		}

		const float gravity = -9.8f;
		g_PlayerVelocityY += gravity * deltaTime;

		bool isCollides = world.isPlayerCollides(g_PlayerX, g_PlayerY + g_PlayerVelocityY * deltaTime,
		                                         g_PlayerZ);
		if (!isCollides) {
			g_PlayerY += g_PlayerVelocityY * deltaTime;
			g_OnGround = false;
		} else {
			g_PlayerVelocityY = 0;
			if (g_OnGround == false) {
				auto updatePacket = std::make_shared<PacketPlayerPosition>(g_PlayerX, g_PlayerY, g_PlayerZ, true);
				nc->SendPacket(updatePacket);
			}
			g_OnGround = true;
		}

		isCollides = world.isPlayerCollides(g_PlayerX + g_PlayerVelocityX * deltaTime, g_PlayerY,
		                                    g_PlayerZ + g_PlayerVelocityZ * deltaTime);
		if (!isCollides) {
			g_PlayerX += g_PlayerVelocityX * deltaTime;
			g_PlayerZ += g_PlayerVelocityZ * deltaTime;
		}

		const float AirResistance = 10.0f;
		glm::vec3 vel(g_PlayerVelocityX, 0, g_PlayerVelocityZ);
		glm::vec3 resistForce = -vel * AirResistance * deltaTime;
		vel += resistForce;
		g_PlayerVelocityX = vel.x;
		g_PlayerVelocityZ = vel.z;
	}


	//Packet handling
	auto ptr = nc->ReceivePacket();
	while (ptr != nullptr) {
		switch ((PacketNamePlayCB) ptr->GetPacketId()) {
			case SpawnObject:
				break;
			case SpawnExperienceOrb:
				break;
			case SpawnGlobalEntity:
				break;
			case SpawnMob:
				break;
			case SpawnPainting:
				break;
			case SpawnPlayer:
				break;
			case AnimationCB:
				break;
			case Statistics:
				break;
			case BlockBreakAnimation:
				break;
			case UpdateBlockEntity:
				break;
			case BlockAction:
				break;
			case BlockChange:
				break;
			case BossBar:
				break;
			case ServerDifficulty:
				break;
			case TabCompleteCB:
				break;
			case ChatMessageCB:
				break;
			case MultiBlockChange:
				break;
			case ConfirmTransactionCB:
				break;
			case CloseWindowCB:
				break;
			case OpenWindow:
				break;
			case WindowItems:
				break;
			case WindowProperty:
				break;
			case SetSlot:
				break;
			case SetCooldown:
				break;
			case PluginMessageCB:
				break;
			case NamedSoundEffect:
				break;
			case DisconnectPlay: {
				auto packet = std::static_pointer_cast<PacketDisconnectPlay>(ptr);
				LOG(INFO) << "Disconnect reason: " << packet->Reason;
				isRunning = false;
				break;
			}
			case EntityStatus:
				break;
			case Explosion:
				break;
			case UnloadChunk:
				break;
			case ChangeGameState:
				break;
			case KeepAliveCB:
				LOG(WARNING) << "Receive KeepAlive packet in GameState handler";
				break;
			case ChunkData: {
				auto packet = std::static_pointer_cast<PacketChunkData>(ptr);
				world.ParseChunkData(packet);
				break;
			}
			case Effect:
				break;
			case Particle:
				break;
			case JoinGame: {
				auto packet = std::static_pointer_cast<PacketJoinGame>(ptr);
				g_PlayerEid = packet->EntityId;
				g_Gamemode = (packet->Gamemode & 0b11111011);
				g_Dimension = packet->Dimension;
				g_Difficulty = packet->Difficulty;
				g_MaxPlayers = packet->MaxPlayers;
				g_LevelType = packet->LevelType;
				g_ReducedDebugInfo = packet->ReducedDebugInfo;
				LOG(INFO) << "Gamemode is " << g_Gamemode << ", Difficulty is " << (int) g_Difficulty
				          << ", Level Type is " << g_LevelType;
				break;
			}
			case Map:
				break;
			case EntityRelativeMove:
				break;
			case EntityLookAndRelativeMove:
				break;
			case EntityLook:
				break;
			case Entity:
				break;
			case VehicleMove:
				break;
			case OpenSignEditor:
				break;
			case PlayerAbilitiesCB:
				break;
			case CombatEvent:
				break;
			case PlayerListItem:
				break;
			case PlayerPositionAndLookCB: {
				auto packet = std::static_pointer_cast<PacketPlayerPositionAndLookCB>(ptr);
				if ((packet->Flags & 0x10) != 0) {
					g_PlayerPitch += packet->Pitch;
				} else {
					g_PlayerPitch = packet->Pitch;
				};

				if ((packet->Flags & 0x08) != 0) {
					g_PlayerYaw += packet->Yaw;
				} else {
					g_PlayerYaw = packet->Yaw;
				}

				if ((packet->Flags & 0x01) != 0) {
					g_PlayerX += packet->X;
				} else {
					g_PlayerX = packet->X;
				}

				if ((packet->Flags & 0x02) != 0) {
					g_PlayerY += packet->Y;
				} else {
					g_PlayerY = packet->Y;
				}

				if ((packet->Flags & 0x04) != 0) {
					g_PlayerZ += packet->Z;
				} else {
					g_PlayerZ = packet->Z;
				}

				//if (!g_IsGameStarted)
				LOG(INFO) << "PlayerPos is " << g_PlayerX << ", " << g_PlayerY << ", " << g_PlayerZ << "\t\tAngle: "
				          << g_PlayerYaw << "," << g_PlayerPitch;

				g_IsGameStarted = true;

				auto packetResponse = std::make_shared<PacketTeleportConfirm>(packet->TeleportId);
				auto packetPerformRespawn = std::make_shared<PacketClientStatus>(0);

				nc->SendPacket(packetResponse);
				nc->SendPacket(packetPerformRespawn);
				break;
			}
			case UseBed:
				break;
			case UnlockRecipes:
				break;
			case DestroyEntities:
				break;
			case RemoveEntityEffect:
				break;
			case ResourcePackSend:
				break;
			case Respawn:
				break;
			case EntityHeadLook:
				break;
			case SelectAdvancementTab:
				break;
			case WorldBorder:
				break;
			case Camera:
				break;
			case HeldItemChangeCB:
				break;
			case DisplayScoreboard:
				break;
			case EntityMetadata:
				break;
			case AttachEntity:
				break;
			case EntityVelocity:
				break;
			case EntityEquipment:
				break;
			case SetExperience:
				break;
			case UpdateHealth: {
				auto packet = std::static_pointer_cast<PacketUpdateHealth>(ptr);
				g_PlayerHealth = packet->Health;
				if (g_PlayerHealth <= 0) {
					LOG(INFO) << "Player is dead. Respawning...";
					auto packetPerformRespawn = std::make_shared<PacketClientStatus>(0);
					nc->SendPacket(packetPerformRespawn);
				}
				break;
			}
			case ScoreboardObjective:
				break;
			case SetPassengers:
				break;
			case Teams:
				break;
			case UpdateScore:
				break;
			case SpawnPosition: {
				auto packet = std::static_pointer_cast<PacketSpawnPosition>(ptr);
				g_SpawnPosition = packet->Location;
				LOG(INFO) << "Spawn position is " << g_SpawnPosition.GetX() << "," << g_SpawnPosition.GetY() << ","
				          << g_SpawnPosition.GetZ();
				break;
			}
			case TimeUpdate:
				break;
			case Title:
				break;
			case SoundEffect:
				break;
			case PlayerListHeaderAndFooter:
				break;
			case CollectItem:
				break;
			case EntityTeleport:
				break;
			case Advancements:
				break;
			case EntityProperties:
				break;
			case EntityEffect:
				break;
		}
		ptr = nc->ReceivePacket();
	}
}

void GameState::HandleMovement(GameState::Direction direction, float deltaTime) {
	const float PlayerSpeed = 40.0;
	float velocity = PlayerSpeed * deltaTime;
	glm::vec3 vel(g_PlayerVelocityX, g_PlayerVelocityY, g_PlayerVelocityZ);
	glm::vec3 front(cos(glm::radians(this->Yaw())) * cos(glm::radians(this->Pitch())), 0,
	                sin(glm::radians(this->Yaw())) * cos(glm::radians(this->Pitch())));
	front = glm::normalize(front);
	glm::vec3 right = glm::normalize(glm::cross(front, this->WorldUp));
	switch (direction) {
		case FORWARD:
			vel += front * velocity;
			break;
		case BACKWARD:
			vel -= front * velocity;
			break;
		case RIGHT:
			vel += right * velocity;
			break;
		case LEFT:
			vel -= right * velocity;
			break;
		case JUMP:
			if (g_OnGround) {
				vel.y += 5;
				g_OnGround = false;
			}
			break;
	}
	g_PlayerVelocityX = vel.x;
	g_PlayerVelocityY = vel.y;
	g_PlayerVelocityZ = vel.z;
}

void GameState::HandleRotation(double yaw, double pitch) {
	this->SetYaw(Yaw() + yaw);
	this->SetPitch(Pitch() + pitch);
	if (this->Pitch() > 89.0f)
		this->SetPitch(89.0f);
	if (this->Pitch() < -89.0f)
		this->SetPitch(-89.0f);
	this->updateCameraVectors();

	auto updatePacket = std::make_shared<PacketPlayerLook>(g_PlayerYaw, g_PlayerPitch, g_OnGround);
	nc->SendPacket(updatePacket);
}

glm::mat4 GameState::GetViewMatrix() {
	auto pos = this->Position();
	pos.y+=1.62;
	return glm::lookAt(pos, pos + this->Front, this->Up);
}

void GameState::updateCameraVectors() {
	glm::vec3 front;
	front.x = cos(glm::radians(this->Yaw())) * cos(glm::radians(this->Pitch()));
	front.y = sin(glm::radians(this->Pitch()));
	front.z = sin(glm::radians(this->Yaw())) * cos(glm::radians(this->Pitch()));
	this->Front = glm::normalize(front);
	this->Right = glm::normalize(glm::cross(this->Front, this->WorldUp));
	this->Up = glm::normalize(glm::cross(this->Right, this->Front));
}

float GameState::Yaw() {
	return g_PlayerYaw + 90;
}

float GameState::Pitch() {
	return -g_PlayerPitch;
}

void GameState::SetYaw(float yaw) {
	g_PlayerYaw = yaw - 90;
}

void GameState::SetPitch(float pitch) {
	g_PlayerPitch = -pitch;
}

glm::vec3 GameState::Position() {
	return glm::vec3(g_PlayerX, g_PlayerY, g_PlayerZ);
}

void GameState::SetPosition(glm::vec3 Position) {
	g_PlayerX = Position.x;
	g_PlayerY = Position.y;
	g_PlayerZ = Position.z;
}