Package: com.tencent.tmgp.pubgmhd

#include <vector>
#include <cmath>

struct Vector3 {
    float x, y, z;

    float DistanceTo(const Vector3& other) const {
        float dx = x - other.x;
        float dy = y - other.y;
        float dz = z - other.z;
        return std::sqrt(dx * dx + dy * dy + dz * dz);
    }

    Vector3 operator-(const Vector3& other) const {
        return {x - other.x, y - other.y, z - other.z};
    }

    Vector3 Normalize() const {
        float len = std::sqrt(x * x + y * y + z * z);
        if (len == 0) return {0, 0, 0};
        return {x / len, y / len, z / len};
    }
};

class Character {
public:
    Vector3 position;
    bool isEnemy;
    bool isAlive;

    Vector3 GetHeadPosition() const {
        Vector3 head = position;
        head.y += 1.6f;
        return head;
    }

    Character(Vector3 pos, bool enemy)
        : position(pos), isEnemy(enemy), isAlive(true) {}
};

class Bullet {
public:
    Vector3 position;
    Vector3 direction;
    float speed = 100000.0f; // 极速飞行，瞬间命中
    Character* target = nullptr;

    Bullet(Vector3 startPos, Character* enemy) {
        position = startPos;
        target = enemy;
        if (target) {
            Vector3 headPos = target->GetHeadPosition();
            direction = (headPos - position).Normalize();
        }
    }

    void Update(float deltaTime) {
        if (target && target->isAlive) {
            Vector3 headPos = target->GetHeadPosition();
            Vector3 desiredDir = (headPos - position).Normalize();
            direction = desiredDir;

            position.x += direction.x * speed * deltaTime;
            position.y += direction.y * speed * deltaTime;
            position.z += direction.z * speed * deltaTime;

            if ((position - headPos).DistanceTo({0, 0, 0}) < 0.1f) {
                target->isAlive = false;
            }
        }
    }
};

class Camera {
public:
    Vector3 rotation; // 摄像机角度（pitch/yaw）
    float fov = 90.0f;

    void AimAt(const Vector3& targetPosition, const Vector3& cameraPosition) {
        Vector3 dir = targetPosition - cameraPosition;
        float distance = std::sqrt(dir.x * dir.x + dir.z * dir.z);

        // 计算 yaw 和 pitch
        rotation.y = std::atan2(dir.z, dir.x); // Yaw
        rotation.x = -std::atan2(dir.y, distance); // Pitch

        // 单位换算为角度制（如果需要）
        rotation.x = rotation.x * 180.0f / M_PI;
        rotation.y = rotation.y * 180.0f / M_PI;
    }
};

class Player {
public:
    Vector3 position;
    bool isAiming; // 是否开镜
    bool isFiring;
    Camera camera;

    void Fire(std::vector<Character*>& enemies, std::vector<Bullet>& bullets) {
        if (!isFiring) return;

        Character* nearest = FindNearestEnemy(enemies);
        if (nearest) {
            bullets.emplace_back(position, nearest);
            AimToHead(nearest);
        }
    }

    void AimToHead(Character* enemy) {
        Vector3 headPos = enemy->GetHeadPosition();
        camera.AimAt(headPos, position);
    }

    Character* FindNearestEnemy(std::vector<Character*>& enemies) {
        Character* result = nullptr;
        float minDist = 30000.0f;

        for (auto ch : enemies) {
            if (ch->isEnemy && ch->isAlive) {
                float dist = position.DistanceTo(ch->position);
                if (dist < minDist) {
                    minDist = dist;
                    result = ch;
                }
            }
        }

        return result;
    }
};

class GameWorld {
public:
    Player player;
    std::vector<Character*> enemies;
    std::vector<Bullet> bullets;

    void SimulateFire() {
        player.Fire(enemies, bullets);
    }

    void Update(float deltaTime) {
        for (auto& bullet : bullets) {
            bullet.Update(deltaTime);
        }

        bullets.erase(
            std::remove_if(bullets.begin(), bullets.end(),
                           [](const Bullet& b) { return b.target == nullptr || !b.target->isAlive; }),
            bullets.end());
    }
};