Merge with main
This commit is contained in:
commit
8a374a4231
9 files changed
+794
-241
No files matched your search
+169
-30
@@ -349,6 +349,7 @@ namespace ZL
|
||||
uint64_t now = SDL_GetTicks64();
|
||||
if (now - lastProjectileFireTime >= static_cast<uint64_t>(projectileCooldownMs)) {
|
||||
lastProjectileFireTime = now;
|
||||
const float projectileSpeed = 60.0f;
|
||||
|
||||
this->fireProjectiles();
|
||||
|
||||
@@ -358,15 +359,21 @@ namespace ZL
|
||||
Eigen::Vector3f centerPos = Environment::shipState.position +
|
||||
Environment::shipState.rotation * Vector3f{ 0, 0.9f, 5.0f };
|
||||
|
||||
Eigen::Quaternionf q(Environment::shipState.rotation);
|
||||
float speedToSend = projectileSpeed + Environment::shipState.velocity;
|
||||
int shotCount = 2;
|
||||
|
||||
std::string fireMsg = "FIRE:" +
|
||||
std::to_string(now) + ":" +
|
||||
std::to_string(centerPos.x()) + ":" +
|
||||
std::to_string(centerPos.y()) + ":" +
|
||||
std::to_string(centerPos.z()) + ":" +
|
||||
std::to_string(worldForward.x()) + ":" +
|
||||
std::to_string(worldForward.y()) + ":" +
|
||||
std::to_string(worldForward.z()) + ":" +
|
||||
"2";
|
||||
std::to_string(q.w()) + ":" +
|
||||
std::to_string(q.x()) + ":" +
|
||||
std::to_string(q.y()) + ":" +
|
||||
std::to_string(q.z()) + ":" +
|
||||
std::to_string(speedToSend) + ":" +
|
||||
std::to_string(shotCount);
|
||||
|
||||
networkClient->Send(fireMsg);
|
||||
}
|
||||
@@ -734,13 +741,13 @@ namespace ZL
|
||||
// Биндим текстуру корабля один раз для всех удаленных игроков (оптимизация батчинга)
|
||||
glBindTexture(GL_TEXTURE_2D, spaceshipTexture->getTexID());
|
||||
|
||||
auto now = std::chrono::system_clock::now();
|
||||
/*auto now = std::chrono::system_clock::now();
|
||||
|
||||
//Apply server delay:
|
||||
now -= std::chrono::milliseconds(CLIENT_DELAY);
|
||||
|
||||
latestRemotePlayers = networkClient->getRemotePlayers();
|
||||
|
||||
*/
|
||||
// Если сервер прислал коробки, применяем их однократно вместо локальной генерации
|
||||
if (!serverBoxesApplied && networkClient) {
|
||||
auto sboxes = networkClient->getServerBoxes();
|
||||
@@ -763,15 +770,9 @@ namespace ZL
|
||||
}
|
||||
|
||||
// Итерируемся по актуальным данным из extrapolateRemotePlayers
|
||||
for (auto const& [id, remotePlayer] : latestRemotePlayers) {
|
||||
|
||||
if (!remotePlayer.canFetchClientStateAtTime(now))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ClientState playerState = remotePlayer.fetchClientStateAtTime(now);
|
||||
for (auto const& [id, remotePlayer] : remotePlayerStates) {
|
||||
|
||||
const ClientState& playerState = remotePlayer;
|
||||
|
||||
renderer.PushMatrix();
|
||||
renderer.LoadIdentity();
|
||||
@@ -811,18 +812,15 @@ namespace ZL
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
// Берем удаленных игроков
|
||||
latestRemotePlayers = networkClient->getRemotePlayers();
|
||||
//latestRemotePlayers = networkClient->getRemotePlayers();
|
||||
|
||||
auto now = std::chrono::system_clock::now();
|
||||
now -= std::chrono::milliseconds(CLIENT_DELAY);
|
||||
|
||||
for (auto const& [id, remotePlayer] : latestRemotePlayers)
|
||||
for (auto const& [id, remotePlayer] : remotePlayerStates)
|
||||
{
|
||||
if (!remotePlayer.canFetchClientStateAtTime(now))
|
||||
continue;
|
||||
|
||||
ClientState st = remotePlayer.fetchClientStateAtTime(now);
|
||||
|
||||
|
||||
const ClientState& st = remotePlayer;
|
||||
// Позиция корабля в мире
|
||||
Vector3f shipWorld = st.position;
|
||||
|
||||
@@ -872,6 +870,9 @@ namespace ZL
|
||||
lastTickCount = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch()
|
||||
).count();
|
||||
|
||||
lastTickCount = (lastTickCount / 50)*50;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -880,6 +881,8 @@ namespace ZL
|
||||
std::chrono::system_clock::now().time_since_epoch()
|
||||
).count();
|
||||
|
||||
newTickCount = (newTickCount / 50) * 50;
|
||||
|
||||
if (newTickCount - lastTickCount > CONST_TIMER_INTERVAL) {
|
||||
|
||||
size_t delta = newTickCount - lastTickCount;
|
||||
@@ -893,15 +896,7 @@ namespace ZL
|
||||
sparkEmitter.update(static_cast<float>(delta));
|
||||
planetObject.update(static_cast<float>(delta));
|
||||
|
||||
static float pingTimer = 0.0f;
|
||||
pingTimer += delta;
|
||||
if (pingTimer >= 1000.0f) {
|
||||
std::string pingMsg = "UPD:" + std::to_string(now_ms) + ":" + Environment::shipState.formPingMessageContent();
|
||||
|
||||
networkClient->Send(pingMsg);
|
||||
std::cout << "Sending: " << pingMsg << std::endl;
|
||||
pingTimer = 0.0f;
|
||||
}
|
||||
|
||||
//Handle input:
|
||||
|
||||
@@ -957,9 +952,44 @@ namespace ZL
|
||||
std::cout << "Sending: " << msg << std::endl;
|
||||
}
|
||||
|
||||
Environment::shipState.simulate_physics(delta);
|
||||
long long leftoverDelta = delta;
|
||||
while (leftoverDelta > 0)
|
||||
{
|
||||
long long miniDelta = 50;
|
||||
Environment::shipState.simulate_physics(miniDelta);
|
||||
leftoverDelta -= miniDelta;
|
||||
}
|
||||
Environment::inverseShipMatrix = Environment::shipState.rotation.inverse();
|
||||
|
||||
static float pingTimer = 0.0f;
|
||||
pingTimer += delta;
|
||||
if (pingTimer >= 1000.0f) {
|
||||
std::string pingMsg = "UPD:" + std::to_string(now_ms) + ":" + Environment::shipState.formPingMessageContent();
|
||||
|
||||
networkClient->Send(pingMsg);
|
||||
std::cout << "Sending: " << pingMsg << std::endl;
|
||||
pingTimer = 0.0f;
|
||||
}
|
||||
|
||||
|
||||
auto latestRemotePlayers = networkClient->getRemotePlayers();
|
||||
|
||||
std::chrono::system_clock::time_point nowRounded{ std::chrono::milliseconds(newTickCount) };
|
||||
|
||||
|
||||
for (auto const& [id, remotePlayer] : latestRemotePlayers) {
|
||||
|
||||
if (!remotePlayer.canFetchClientStateAtTime(nowRounded))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ClientState playerState = remotePlayer.fetchClientStateAtTime(nowRounded);
|
||||
|
||||
remotePlayerStates[id] = playerState;
|
||||
|
||||
}
|
||||
|
||||
for (auto& p : projectiles) {
|
||||
if (p && p->isActive()) {
|
||||
p->update(static_cast<float>(delta), renderer);
|
||||
@@ -1027,11 +1057,16 @@ namespace ZL
|
||||
if (!uiGameOverShown) {
|
||||
if (uiManager.pushMenuFromFile("resources/config/game_over.json", this->renderer, CONST_ZIP_FILE)) {
|
||||
uiManager.setButtonCallback("restartButton", [this](const std::string& name) {
|
||||
|
||||
std::string respawnMsg = "RESPAWN:" + std::to_string(networkClient->GetClientId());
|
||||
networkClient->Send(respawnMsg);
|
||||
|
||||
this->shipAlive = true;
|
||||
this->gameOver = false;
|
||||
this->uiGameOverShown = false;
|
||||
this->showExplosion = false;
|
||||
this->explosionEmitter.setEmissionPoints(std::vector<Vector3f>());
|
||||
this->deadRemotePlayers.clear();
|
||||
|
||||
Environment::shipState.position = Vector3f{ 0, 0, 45000.f };
|
||||
Environment::shipState.velocity = 0.0f;
|
||||
@@ -1358,7 +1393,37 @@ namespace ZL
|
||||
const float projectileSpeed = 60.0f;
|
||||
const float lifeMs = 5000.0f;
|
||||
const float size = 0.5f;
|
||||
for (const auto& pi : pending) {
|
||||
const std::vector<Vector3f> localOffsets = {
|
||||
Vector3f{ -1.5f, 0.9f, 5.0f },
|
||||
Vector3f{ 1.5f, 0.9f, 5.0f }
|
||||
//Vector3f{},
|
||||
//Vector3f{}
|
||||
};
|
||||
|
||||
Vector3f localForward = { 0, 0, -1 };
|
||||
Vector3f worldForward = pi.rotation * localForward;
|
||||
|
||||
float len = worldForward.norm();
|
||||
if (len <= 1e-6f) {
|
||||
continue;
|
||||
}
|
||||
worldForward /= len;
|
||||
|
||||
Vector3f baseVel = worldForward * pi.velocity;
|
||||
|
||||
for (const auto& off : localOffsets) {
|
||||
Vector3f shotPos = pi.position + (pi.rotation * off);
|
||||
|
||||
for (auto& p : projectiles) {
|
||||
if (!p->isActive()) {
|
||||
p->init(shotPos, baseVel, lifeMs, size, projectileTexture, renderer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
auto remotePlayersSnapshot = networkClient->getRemotePlayers();
|
||||
for (const auto& pi : pending) {
|
||||
Eigen::Vector3f dir = pi.direction;
|
||||
@@ -1399,6 +1464,80 @@ namespace ZL
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
// Обработка событий смерти, присланных сервером
|
||||
auto deaths = networkClient->getPendingDeaths();
|
||||
if (!deaths.empty()) {
|
||||
int localId = networkClient->GetClientId();
|
||||
std::cout << "Client: Received " << deaths.size() << " death events" << std::endl;
|
||||
|
||||
for (const auto& d : deaths) {
|
||||
std::cout << "Client: Processing death - target=" << d.targetId
|
||||
<< ", killer=" << d.killerId << ", pos=("
|
||||
<< d.position.x() << ", " << d.position.y() << ", " << d.position.z() << ")" << std::endl;
|
||||
|
||||
showExplosion = true;
|
||||
explosionEmitter.setUseWorldSpace(true);
|
||||
explosionEmitter.setEmissionPoints(std::vector<Vector3f>{ d.position });
|
||||
explosionEmitter.emit();
|
||||
lastExplosionTime = SDL_GetTicks64();
|
||||
std::cout << "Client: Explosion emitted at (" << d.position.x() << ", "
|
||||
<< d.position.y() << ", " << d.position.z() << ")" << std::endl;
|
||||
|
||||
if (d.targetId == localId) {
|
||||
std::cout << "Client: Local ship destroyed!" << std::endl;
|
||||
shipAlive = false;
|
||||
gameOver = true;
|
||||
Environment::shipState.velocity = 0.0f;
|
||||
|
||||
if (!uiGameOverShown) {
|
||||
if (uiManager.pushMenuFromFile("resources/config/game_over.json",
|
||||
this->renderer, CONST_ZIP_FILE)) {
|
||||
uiManager.setButtonCallback("restartButton", [this](const std::string& name) {
|
||||
std::cout << "Client: Restart button clicked, sending RESPAWN" << std::endl;
|
||||
|
||||
std::string respawnMsg = "RESPAWN:" + std::to_string(networkClient->GetClientId());
|
||||
networkClient->Send(respawnMsg);
|
||||
|
||||
this->shipAlive = true;
|
||||
this->gameOver = false;
|
||||
this->uiGameOverShown = false;
|
||||
this->showExplosion = false;
|
||||
this->explosionEmitter.setEmissionPoints(std::vector<Vector3f>());
|
||||
this->deadRemotePlayers.clear();
|
||||
|
||||
Environment::shipState.position = Vector3f{ 0, 0, 45000.f };
|
||||
Environment::shipState.velocity = 0.0f;
|
||||
Environment::shipState.rotation = Eigen::Matrix3f::Identity();
|
||||
Environment::inverseShipMatrix = Eigen::Matrix3f::Identity();
|
||||
Environment::zoom = DEFAULT_ZOOM;
|
||||
Environment::tapDownHold = false;
|
||||
|
||||
uiManager.popMenu();
|
||||
std::cerr << "Game restarted\n";
|
||||
});
|
||||
|
||||
uiManager.setButtonCallback("gameOverExitButton", [this](const std::string& name) {
|
||||
Environment::exitGameLoop = true;
|
||||
});
|
||||
|
||||
uiGameOverShown = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
deadRemotePlayers.insert(d.targetId);
|
||||
std::cout << "Marked remote player " << d.targetId << " as dead" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto respawns = networkClient->getPendingRespawns();
|
||||
if (!respawns.empty()) {
|
||||
for (const auto& respawnId : respawns) {
|
||||
deadRemotePlayers.erase(respawnId);
|
||||
std::cout << "Client: Remote player " << respawnId << " respawned, removed from dead list" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user