added npc destroy
This commit is contained in:
parent
9793c5bf06
commit
a8700af1d0
@ -849,6 +849,7 @@ namespace ZL
|
|||||||
|
|
||||||
for (auto const& [id, remotePlayer] : remotePlayerStates)
|
for (auto const& [id, remotePlayer] : remotePlayerStates)
|
||||||
{
|
{
|
||||||
|
if (deadRemotePlayers.count(id)) continue;
|
||||||
|
|
||||||
//<<<<<<< HEAD
|
//<<<<<<< HEAD
|
||||||
const ClientState& st = remotePlayer;
|
const ClientState& st = remotePlayer;
|
||||||
|
|||||||
@ -94,6 +94,7 @@ namespace ZL {
|
|||||||
npc.targetPosition = generateRandomPosition();
|
npc.targetPosition = generateRandomPosition();
|
||||||
npc.lastStateUpdateMs = std::chrono::duration_cast<std::chrono::milliseconds>(
|
npc.lastStateUpdateMs = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
std::chrono::system_clock::now().time_since_epoch()).count();
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
||||||
|
npc.destroyed = false;
|
||||||
|
|
||||||
npc.stateHistory.add_state(npc.currentState);
|
npc.stateHistory.add_state(npc.currentState);
|
||||||
npcs.push_back(npc);
|
npcs.push_back(npc);
|
||||||
@ -105,6 +106,8 @@ namespace ZL {
|
|||||||
std::chrono::system_clock::now().time_since_epoch()).count();
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
||||||
|
|
||||||
for (auto& npc : npcs) {
|
for (auto& npc : npcs) {
|
||||||
|
if (npc.destroyed) continue;
|
||||||
|
|
||||||
uint64_t deltaMs = now_ms - npc.lastStateUpdateMs;
|
uint64_t deltaMs = now_ms - npc.lastStateUpdateMs;
|
||||||
if (deltaMs == 0) {
|
if (deltaMs == 0) {
|
||||||
npc.lastStateUpdateMs = now_ms;
|
npc.lastStateUpdateMs = now_ms;
|
||||||
@ -229,6 +232,7 @@ namespace ZL {
|
|||||||
const float projectileHitRadius = 1.5f;
|
const float projectileHitRadius = 1.5f;
|
||||||
const float boxCollisionRadius = 2.0f;
|
const float boxCollisionRadius = 2.0f;
|
||||||
const float shipCollisionRadius = 15.0f;
|
const float shipCollisionRadius = 15.0f;
|
||||||
|
const float npcCollisionRadius = 5.0f;
|
||||||
|
|
||||||
std::vector<std::pair<size_t, size_t>> boxProjectileCollisions;
|
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()) {
|
if (!projIndicesToRemove.empty()) {
|
||||||
std::sort(projIndicesToRemove.rbegin(), projIndicesToRemove.rend());
|
std::sort(projIndicesToRemove.rbegin(), projIndicesToRemove.rend());
|
||||||
for (int idx : projIndicesToRemove) {
|
for (int idx : projIndicesToRemove) {
|
||||||
@ -384,11 +429,19 @@ namespace ZL {
|
|||||||
return result;
|
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> LocalClient::getRemotePlayers() {
|
||||||
std::unordered_map<int, ClientStateInterval> result;
|
std::unordered_map<int, ClientStateInterval> result;
|
||||||
for (const auto& npc : npcs) {
|
for (const auto& npc : npcs) {
|
||||||
|
if (!npc.destroyed) {
|
||||||
result[npc.id] = npc.stateHistory;
|
result[npc.id] = npc.stateHistory;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -400,16 +453,9 @@ namespace ZL {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<DeathInfo> LocalClient::getPendingDeaths() {
|
|
||||||
auto result = pendingDeaths;
|
|
||||||
pendingDeaths.clear();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<BoxDestroyedInfo> LocalClient::getPendingBoxDestructions() {
|
std::vector<BoxDestroyedInfo> LocalClient::getPendingBoxDestructions() {
|
||||||
auto result = pendingBoxDestructions;
|
auto result = pendingBoxDestructions;
|
||||||
pendingBoxDestructions.clear();
|
pendingBoxDestructions.clear();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -30,6 +30,7 @@ namespace ZL {
|
|||||||
ClientStateInterval stateHistory;
|
ClientStateInterval stateHistory;
|
||||||
Eigen::Vector3f targetPosition;
|
Eigen::Vector3f targetPosition;
|
||||||
uint64_t lastStateUpdateMs = 0;
|
uint64_t lastStateUpdateMs = 0;
|
||||||
|
bool destroyed = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
class LocalClient : public INetworkClient {
|
class LocalClient : public INetworkClient {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user