added npc

This commit is contained in:
Vlad 2026-02-10 12:42:19 +06:00
parent 629c9ba7b1
commit 5df2216da6
2 changed files with 160 additions and 58 deletions

View File

@ -10,6 +10,7 @@ namespace ZL {
void LocalClient::Connect(const std::string& host, uint16_t port) {
generateBoxes();
initializeNPCs();
lastUpdateMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
}
@ -64,8 +65,89 @@ namespace ZL {
std::cout << "LocalClient: Generated " << serverBoxes.size() << " boxes\n";
}
Eigen::Vector3f LocalClient::generateRandomPosition() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> distrib(-50.0, 50.0);
return Eigen::Vector3f(
(float)distrib(gen),
(float)distrib(gen),
(float)distrib(gen) + 45000.0f
);
}
void LocalClient::initializeNPCs() {
npcs.clear();
for (int i = 0; i < 3; ++i) {
LocalNPC npc;
npc.id = 100 + i;
npc.speed = 20.0f + (i * 5.0f);
npc.currentState.id = npc.id;
npc.currentState.position = generateRandomPosition();
npc.currentState.rotation = Eigen::Matrix3f::Identity();
npc.currentState.velocity = npc.speed;
npc.targetPosition = generateRandomPosition();
npc.lastStateUpdateMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
npc.stateHistory.add_state(npc.currentState);
npcs.push_back(npc);
std::cout << "LocalClient: Created NPC with id=" << npc.id
<< " at pos (" << npc.currentState.position.x() << ", "
<< npc.currentState.position.y() << ", "
<< npc.currentState.position.z() << ")" << std::endl;
}
}
void LocalClient::updateNPCs() {
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
for (auto& npc : npcs) {
uint64_t deltaMs = now_ms - npc.lastStateUpdateMs;
float dt = deltaMs / 1000.0f;
npc.lastStateUpdateMs = now_ms;
Eigen::Vector3f direction = npc.targetPosition - npc.currentState.position;
float distance = direction.norm();
if (distance < 5.0f) {
npc.targetPosition = generateRandomPosition();
direction = npc.targetPosition - npc.currentState.position;
distance = direction.norm();
}
if (distance > 0.001f) {
direction.normalize();
npc.currentState.position += direction * npc.speed * dt;
npc.currentState.velocity = npc.speed;
Eigen::Vector3f localForward(0.0f, 0.0f, -1.0f);
Eigen::Vector3f worldForward = direction;
Eigen::Vector3f cross = localForward.cross(worldForward);
float dot = localForward.dot(worldForward);
if (cross.norm() > 0.001f) {
float angle = std::atan2(cross.norm(), dot);
cross.normalize();
Eigen::AngleAxisf aa(angle * 0.05f, cross);
npc.currentState.rotation = npc.currentState.rotation * aa.toRotationMatrix();
}
}
npc.currentState.lastUpdateServerTime = std::chrono::system_clock::time_point(
std::chrono::milliseconds(now_ms));
npc.stateHistory.add_state(npc.currentState);
}
}
void LocalClient::Poll() {
updatePhysics();
updateNPCs();
checkCollisions();
}
@ -265,6 +347,14 @@ namespace ZL {
return result;
}
std::unordered_map<int, ClientStateInterval> LocalClient::getRemotePlayers() {
std::unordered_map<int, ClientStateInterval> result;
for (const auto& npc : npcs) {
result[npc.id] = npc.stateHistory;
}
return result;
}
std::vector<std::pair<Eigen::Vector3f, Eigen::Matrix3f>> LocalClient::getServerBoxes() {
std::vector<std::pair<Eigen::Vector3f, Eigen::Matrix3f>> result;
for (const auto& box : serverBoxes) {

View File

@ -24,6 +24,15 @@ namespace ZL {
float lifeMs = 5000.0f;
};
struct LocalNPC {
int id = -1;
ClientState currentState;
ClientStateInterval stateHistory;
Eigen::Vector3f targetPosition;
float speed = 30.0f;
uint64_t lastStateUpdateMs = 0;
};
class LocalClient : public INetworkClient {
private:
std::queue<std::string> messageQueue;
@ -38,9 +47,14 @@ namespace ZL {
ClientState localPlayerState;
bool hasLocalPlayerState = false;
std::vector<LocalNPC> npcs;
void updatePhysics();
void checkCollisions();
void generateBoxes();
void initializeNPCs();
void updateNPCs();
Eigen::Vector3f generateRandomPosition();
public:
void Connect(const std::string& host, uint16_t port) override;
@ -53,9 +67,7 @@ namespace ZL {
int GetClientId() const override { return 1; }
std::vector<ProjectileInfo> getPendingProjectiles() override;
std::unordered_map<int, ClientStateInterval> getRemotePlayers() override {
return std::unordered_map<int, ClientStateInterval>();
}
std::unordered_map<int, ClientStateInterval> getRemotePlayers() override;
std::vector<std::pair<Eigen::Vector3f, Eigen::Matrix3f>> getServerBoxes() override;