added npc destroy
This commit is contained in:
parent
9793c5bf06
commit
a8700af1d0
@ -849,6 +849,7 @@ namespace ZL
|
||||
|
||||
for (auto const& [id, remotePlayer] : remotePlayerStates)
|
||||
{
|
||||
if (deadRemotePlayers.count(id)) continue;
|
||||
|
||||
//<<<<<<< HEAD
|
||||
const ClientState& st = remotePlayer;
|
||||
|
||||
@ -94,6 +94,7 @@ namespace ZL {
|
||||
npc.targetPosition = generateRandomPosition();
|
||||
npc.lastStateUpdateMs = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
npc.destroyed = false;
|
||||
|
||||
npc.stateHistory.add_state(npc.currentState);
|
||||
npcs.push_back(npc);
|
||||
@ -105,6 +106,8 @@ namespace ZL {
|
||||
std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
|
||||
for (auto& npc : npcs) {
|
||||
if (npc.destroyed) continue;
|
||||
|
||||
uint64_t deltaMs = now_ms - npc.lastStateUpdateMs;
|
||||
if (deltaMs == 0) {
|
||||
npc.lastStateUpdateMs = now_ms;
|
||||
@ -229,6 +232,7 @@ namespace ZL {
|
||||
const float projectileHitRadius = 1.5f;
|
||||
const float boxCollisionRadius = 2.0f;
|
||||
const float shipCollisionRadius = 15.0f;
|
||||
const float npcCollisionRadius = 5.0f;
|
||||
|
||||
std::vector<std::pair<size_t, size_t>> boxProjectileCollisions;
|
||||
|
||||
@ -273,6 +277,47 @@ namespace ZL {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::pair<size_t, size_t>> npcProjectileCollisions;
|
||||
|
||||
for (size_t ni = 0; ni < npcs.size(); ++ni) {
|
||||
if (npcs[ni].destroyed) continue;
|
||||
|
||||
for (size_t pi = 0; pi < projectiles.size(); ++pi) {
|
||||
const auto& pr = projectiles[pi];
|
||||
Eigen::Vector3f diff = pr.pos - npcs[ni].currentState.position;
|
||||
float thresh = npcCollisionRadius + projectileHitRadius;
|
||||
|
||||
if (diff.squaredNorm() <= thresh * thresh) {
|
||||
npcProjectileCollisions.push_back({ ni, pi });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& [npcIdx, projIdx] : npcProjectileCollisions) {
|
||||
if (!npcs[npcIdx].destroyed) {
|
||||
npcs[npcIdx].destroyed = true;
|
||||
|
||||
DeathInfo death;
|
||||
death.targetId = npcs[npcIdx].id;
|
||||
death.serverTime = now_ms;
|
||||
death.position = npcs[npcIdx].currentState.position;
|
||||
death.killerId = projectiles[projIdx].shooterId;
|
||||
|
||||
pendingDeaths.push_back(death);
|
||||
|
||||
std::cout << "LocalClient: NPC " << npcs[npcIdx].id << " destroyed by projectile from player "
|
||||
<< projectiles[projIdx].shooterId << " at position ("
|
||||
<< npcs[npcIdx].currentState.position.x() << ", "
|
||||
<< npcs[npcIdx].currentState.position.y() << ", "
|
||||
<< npcs[npcIdx].currentState.position.z() << ")" << std::endl;
|
||||
|
||||
if (std::find(projIndicesToRemove.begin(), projIndicesToRemove.end(), (int)projIdx)
|
||||
== projIndicesToRemove.end()) {
|
||||
projIndicesToRemove.push_back(static_cast<int>(projIdx));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!projIndicesToRemove.empty()) {
|
||||
std::sort(projIndicesToRemove.rbegin(), projIndicesToRemove.rend());
|
||||
for (int idx : projIndicesToRemove) {
|
||||
@ -384,11 +429,19 @@ namespace ZL {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<DeathInfo> LocalClient::getPendingDeaths() {
|
||||
auto result = pendingDeaths;
|
||||
pendingDeaths.clear();
|
||||
return result;
|
||||
}
|
||||
|
||||
std::unordered_map<int, ClientStateInterval> LocalClient::getRemotePlayers() {
|
||||
std::unordered_map<int, ClientStateInterval> result;
|
||||
for (const auto& npc : npcs) {
|
||||
if (!npc.destroyed) {
|
||||
result[npc.id] = npc.stateHistory;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -400,16 +453,9 @@ namespace ZL {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<DeathInfo> LocalClient::getPendingDeaths() {
|
||||
auto result = pendingDeaths;
|
||||
pendingDeaths.clear();
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<BoxDestroyedInfo> LocalClient::getPendingBoxDestructions() {
|
||||
auto result = pendingBoxDestructions;
|
||||
pendingBoxDestructions.clear();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@ -30,6 +30,7 @@ namespace ZL {
|
||||
ClientStateInterval stateHistory;
|
||||
Eigen::Vector3f targetPosition;
|
||||
uint64_t lastStateUpdateMs = 0;
|
||||
bool destroyed = false;
|
||||
};
|
||||
|
||||
class LocalClient : public INetworkClient {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user