fix shooting

This commit is contained in:
Vlad 2026-01-31 16:44:00 +06:00
parent e8fb14b809
commit 50232c0816
2 changed files with 24 additions and 5 deletions

View File

@ -120,7 +120,9 @@ class Session : public std::enable_shared_from_this<Session> {
std::cout << "Player " << id_ << " fired " << shotCount << " shots → broadcasting\n"; std::cout << "Player " << id_ << " fired " << shotCount << " shots → broadcasting\n";
for (auto& session : g_sessions) { for (auto& session : g_sessions) {
session->send_message(broadcast); if (session->get_id() != id_) {
session->send_message(broadcast);
}
} }
} }
else { else {

View File

@ -233,6 +233,8 @@ namespace ZL
if (now - lastProjectileFireTime >= static_cast<uint64_t>(projectileCooldownMs)) { if (now - lastProjectileFireTime >= static_cast<uint64_t>(projectileCooldownMs)) {
lastProjectileFireTime = now; lastProjectileFireTime = now;
this->fireProjectiles();
Eigen::Vector3f localForward = { 0, 0, -1 }; Eigen::Vector3f localForward = { 0, 0, -1 };
Eigen::Vector3f worldForward = (Environment::shipState.rotation * localForward).normalized(); Eigen::Vector3f worldForward = (Environment::shipState.rotation * localForward).normalized();
@ -1130,15 +1132,29 @@ namespace ZL
const float lifeMs = 5000.0f; const float lifeMs = 5000.0f;
const float size = 0.5f; const float size = 0.5f;
auto remotePlayersSnapshot = networkClient->getRemotePlayers();
for (const auto& pi : pending) { for (const auto& pi : pending) {
Eigen::Vector3f dir = pi.direction; Eigen::Vector3f dir = pi.direction;
float len = dir.norm(); float len = dir.norm();
if (len <= 1e-6f) continue; if (len <= 1e-6f) continue;
dir /= len; dir /= len;
Eigen::Vector3f baseVel = dir * projectileSpeed;
int shotCount = 1; Eigen::Matrix3f shooterRot = Eigen::Matrix3f::Identity();
shotCount = 2; float shooterVel = 0.0f;
auto it = remotePlayersSnapshot.find(pi.shooterId);
if (it != remotePlayersSnapshot.end()) {
std::chrono::system_clock::time_point pktTime{ std::chrono::milliseconds(pi.clientTime) };
if (it->second.canFetchClientStateAtTime(pktTime)) {
ClientState shooterState = it->second.fetchClientStateAtTime(pktTime);
shooterRot = shooterState.rotation;
shooterVel = shooterState.velocity;
}
}
float speedWithOwner = projectileSpeed + shooterVel;
Eigen::Vector3f baseVel = dir * speedWithOwner;
int shotCount = 2;
std::vector<Eigen::Vector3f> localOffsets = { std::vector<Eigen::Vector3f> localOffsets = {
{-1.5f, 0.9f, 5.0f}, {-1.5f, 0.9f, 5.0f},
@ -1146,7 +1162,8 @@ namespace ZL
}; };
for (int i = 0; i < shotCount; ++i) { for (int i = 0; i < shotCount; ++i) {
Eigen::Vector3f shotPos = pi.position + localOffsets[i]; Eigen::Vector3f rotatedOffset = shooterRot * localOffsets[i];
Eigen::Vector3f shotPos = pi.position + rotatedOffset;
for (auto& p : projectiles) { for (auto& p : projectiles) {
if (!p->isActive()) { if (!p->isActive()) {