Added hp and attack actions, and labels

This commit is contained in:
Vladislav Khorev committed 2026-04-25 12:07:52 +03:00
1 parent 393ebcd831
commit 44cc0fba67
8 files changed
+166 -9

No files matched your search

+95
View File
@@ -1,7 +1,9 @@
#include "Character.h"
#include "render/TextRenderer.h"
#include <cmath>
#include <iostream>
#include <random>
#include <array>
#include "GameConstants.h"
#include "Environment.h"
@@ -104,6 +106,8 @@ AnimationState Character::resolveActiveState() const {
}
void Character::update(int64_t deltaMs) {
if (initialHp <= 0.f) initialHp = hp;
//weaponInitialRotation = Eigen::AngleAxisf(x/180.0, Eigen::Vector3f::UnitZ()).toRotationMatrix();
//weaponInitialRotation = Eigen::AngleAxisf(-M_PI*0.5, Eigen::Vector3f::UnitZ()).toRotationMatrix();
@@ -820,4 +824,95 @@ void Character::prepareHitSparksForDraw(const Eigen::Matrix4f& cameraViewMatrix)
hitSparkEmitter.prepareForDraw(cameraViewMatrix);
}
void Character::drawName(TextRenderer& textRenderer,
const Eigen::Matrix4f& cameraViewMatrix,
const Eigen::Matrix4f& projectionMatrix)
{
if (isPlayer) return;
if (hp <= 0.f) return;
if (npcName.empty()) return;
Eigen::Vector4f worldPos(position.x(), position.y() + 1.83f, position.z(), 1.f);
Eigen::Vector4f clip = projectionMatrix * cameraViewMatrix * worldPos;
if (clip.w() <= 0.f) return; // behind the camera
float ndcX = clip.x() / clip.w();
float ndcY = clip.y() / clip.w();
if (ndcX < -1.f || ndcX > 1.f || ndcY < -1.f || ndcY > 1.f) return; // off-screen
float screenX = (ndcX * 0.5f + 0.5f) * Environment::projectionWidth;
float screenY = (ndcY * 0.5f + 0.5f) * Environment::projectionHeight;
std::array<float, 4> color = canAttack
? std::array<float, 4>{ 1.f, 0.f, 0.f, 1.f }
: std::array<float, 4>{ 0.f, 1.f, 0.f, 1.f };
textRenderer.drawText(npcName, screenX, screenY, 1.0f, true, color);
}
void Character::drawHealthBar(Renderer& renderer,
const Eigen::Matrix4f& cameraViewMatrix,
const Eigen::Matrix4f& projectionMatrix)
{
if (!isPlayer && !canAttack) return;
if (hp <= 0.f) return;
if (initialHp <= 0.f) return;
Eigen::Vector4f worldPos(position.x(), position.y() + 1.75f, position.z(), 1.f);
Eigen::Vector4f clip = projectionMatrix * cameraViewMatrix * worldPos;
if (clip.w() <= 0.f) return;
float ndcX = clip.x() / clip.w();
float ndcY = clip.y() / clip.w();
if (ndcX < -1.2f || ndcX > 1.2f || ndcY < -1.2f || ndcY > 1.2f) return;
const float sx = (ndcX * 0.5f + 0.5f) * Environment::projectionWidth;
const float sy = (ndcY * 0.5f + 0.5f) * Environment::projectionHeight;
const float barW = 60.f;
const float barH = 6.f;
const float left = sx - barW * 0.5f;
const float right = sx + barW * 0.5f;
const float bottom = sy - barH * 0.5f;
const float top = sy + barH * 0.5f;
float frac = hp / initialHp;
if (frac < 0.f) frac = 0.f;
if (frac > 1.f) frac = 1.f;
const float split = left + barW * frac;
VertexDataStruct data;
const Eigen::Vector3f green(0.f, 1.f, 0.f);
const Eigen::Vector3f red (1.f, 0.f, 0.f);
auto pushQuad = [&](float x0, float x1, const Eigen::Vector3f& col) {
data.PositionData.push_back({ x0, bottom, 0.f });
data.PositionData.push_back({ x1, bottom, 0.f });
data.PositionData.push_back({ x1, top, 0.f });
data.PositionData.push_back({ x0, bottom, 0.f });
data.PositionData.push_back({ x1, top, 0.f });
data.PositionData.push_back({ x0, top, 0.f });
for (int i = 0; i < 6; ++i) data.ColorData.push_back(col);
};
if (frac > 0.f) pushQuad(left, split, green);
if (frac < 1.f) pushQuad(split, right, red);
healthBarMesh.AssignFrom(data);
healthBarMesh.RefreshVBO();
renderer.shaderManager.PushShader("defaultColor");
renderer.PushProjectionMatrix(0.f, Environment::projectionWidth,
0.f, Environment::projectionHeight,
-1.f, 1.f);
renderer.PushMatrix();
renderer.LoadIdentity();
renderer.DrawVertexRenderStruct(healthBarMesh);
renderer.PopMatrix();
renderer.PopProjectionMatrix();
renderer.shaderManager.PopShader();
}
} // namespace ZL