New weapon, two new attacks

This commit is contained in:
Vladislav Khorev committed 2026-11-26 00:35:41 +06:00
1 parent 60e84c077c
commit 722e5eb5ed
24 files changed
+650 -76

No files matched your search

+250 -21
View File
@@ -10,6 +10,14 @@ namespace {
constexpr float kPi = 3.14159265358979323846f;
// Tick counts are authoritative and therefore independent of render rate.
constexpr float ATTACK_COOLDOWN_TIME = 1.6f;
constexpr int JUMP_ATTACK_COOLDOWN_TICKS = 50;
constexpr int JUMP_ATTACK_TAKEOFF_SUBFRAME = 8 * 1000;
constexpr int JUMP_ATTACK_IMPACT_SUBFRAME = 21 * 1000;
constexpr int ANIMATION_SUBFRAMES_PER_FRAME = 1000;
constexpr int JUMP_ATTACK_FRAME_COUNT = 80;
constexpr int JUMP_ATTACK_DURATION_MS = 2000;
constexpr int POWER_ATTACK_COOLDOWN_TICKS = 5;//100;
constexpr int POWER_ATTACK_IMPACT_FRAME = 19;
constexpr float HOME_DRIFT_CHECK_INTERVAL = 5.0f;
const fp HOME_DRIFT_CHECK_INTERVAL_FP = static_cast<fp>(HOME_DRIFT_CHECK_INTERVAL);
// An NPC farther than this from its home position (squared) walks back.
@@ -181,6 +189,16 @@ void CharacterState::stopAndRotateToTarget(std::string characterId)
faceTargetId = std::move(characterId);
}
void CharacterState::leaveBattleState()
{
battle_state = 0;
attack = 0;
attackTargetId.clear();
cancelPendingJumpAttack();
powerAttackPending = false;
powerAttackTargetId.clear();
}
void CharacterState::resetPlayerAfterDeath()
{
if (!isPlayer) return;
@@ -189,18 +207,66 @@ void CharacterState::resetPlayerAfterDeath()
showWeapon = false;
battle_state = 0;
attack = 0;
jumpAttackPending = false;
jumpAttackActive = false;
jumpAttackImpactConsumed = false;
jumpAttackAnimationSubframe = 0;
jumpAttackTargetId.clear();
powerAttackPending = false;
powerAttackTargetId.clear();
}
void CharacterState::tryAttackCombo()
bool CharacterState::tryPowerAttack(const std::string& targetId)
{
if (static_cast<int>(animationFrame) < 20 ||
(currentState != AnimationState::ACTION_ATTACK &&
currentState != AnimationState::ACTION_ATTACK_2)) {
return;
if (!isPlayer || hp <= 0.f || targetId.empty() ||
powerAttackCooldownTicks > 0 || powerAttackPending ||
jumpAttackActive ||
currentState == AnimationState::ACTION_ATTACK_POWER) {
return false;
}
const auto index = static_cast<std::size_t>(currentState);
animationFrame = index < animationStartFrames.size()
? static_cast<float>(animationStartFrames[index]) : 0.f;
powerAttackPending = true;
powerAttackTargetId = targetId;
cancelPendingJumpAttack();
return true;
}
bool CharacterState::reserveJumpAttack(const std::string& targetId)
{
if (!isPlayer || !jumpAttackUnlocked || jumpAttackCooldownTicks > 0 ||
jumpAttackPending || jumpAttackActive || powerAttackPending ||
hp <= 0.f ||
battle_state != 0 ||
(currentState != AnimationState::STAND &&
currentState != AnimationState::WALK) ||
targetId.empty()) {
return false;
}
jumpAttackPending = true;
jumpAttackTargetId = targetId;
return true;
}
void CharacterState::cancelPendingJumpAttack()
{
jumpAttackPending = false;
if (!jumpAttackActive) jumpAttackTargetId.clear();
}
void CharacterState::beginJumpAttack(const Vec3fp& landingPosition)
{
jumpAttackPending = false;
jumpAttackActive = true;
jumpAttackImpactConsumed = false;
jumpAttackCooldownTicks = JUMP_ATTACK_COOLDOWN_TICKS;
jumpAttackAnimationSubframe = 0;
jumpAttackStartPosition = position;
jumpAttackLandingPosition = landingPosition;
currentState = AnimationState::ACTION_ATTACK_JUMP;
animationFrame = 0.f;
resetAnim = false;
attack = 0;
battle_state = 1;
stopInPlace();
}
void CharacterState::setHp(float newHp) {
@@ -223,17 +289,39 @@ CharacterUpdateEvents CharacterState::update(
{
CharacterUpdateEvents events;
if (!enabled) return events;
if (jumpAttackCooldownTicks > 0) --jumpAttackCooldownTicks;
if (powerAttackCooldownTicks > 0) --powerAttackCooldownTicks;
if (hp <= 0.f && jumpAttackActive) {
jumpAttackActive = false;
jumpAttackImpactConsumed = false;
jumpAttackAnimationSubframe = 0;
jumpAttackTargetId.clear();
}
const CharacterObservation* attackTarget = context.attackTarget;
const CharacterObservation* faceTarget = context.faceTarget;
if (!attackTarget || attackTarget->hp <= 0.f) {
battle_state = 0;
if (jumpAttackActive) {
battle_state = 1;
attack = 0;
} else if (!attackTarget || attackTarget->hp <= 0.f) {
// A victorious player keeps the weapon drawn and finishes the current
// attack animation. Only an explicit movement/clear command leaves
// battle state. NPCs retain their autonomous disengage behavior.
if (!isPlayer) battle_state = 0;
attack = 0;
attackTargetId.clear();
cancelPendingJumpAttack();
powerAttackPending = false;
powerAttackTargetId.clear();
} else {
const fp distanceSquared = (attackTarget->position - position).normSquared();
if (distanceSquared >= visibleRange * visibleRange ||
distanceSquared >= static_cast<fp>(kAttackRange * kAttackRange)) {
const fp combatRange = static_cast<fp>(
jumpAttackPending ? kJumpAttackRange : kAttackRange);
const bool outOfCombatRange = jumpAttackPending
? distanceSquared > combatRange * combatRange
: distanceSquared >= visibleRange * visibleRange ||
distanceSquared >= combatRange * combatRange;
if (outOfCombatRange) {
// Out of attack range: chase the target.
if ((attackTarget->position - requestedWalkTarget).normSquared() >
TARGET_REPLAN_THRESHOLD_SQUARED) {
@@ -350,7 +438,7 @@ CharacterUpdateEvents CharacterState::update(
attackTargetId.clear();
}
}
else if (battle_state == 1 && attackTarget) {
else if (battle_state == 1 && attackTarget && !jumpAttackActive) {
targetFacingAngle = fpm::atan2(
attackTarget->position.x - position.x, -(attackTarget->position.z - position.z));
@@ -358,21 +446,35 @@ CharacterUpdateEvents CharacterState::update(
currentState = AnimationState::STAND_TO_ACTION;
resetAnim = true;
}
if (canAttack && attack == 0 && attack_cooldown < 0.f &&
if (powerAttackPending &&
powerAttackTargetId == attackTargetId &&
currentState == AnimationState::ACTION_IDLE) {
powerAttackPending = false;
powerAttackCooldownTicks = POWER_ATTACK_COOLDOWN_TICKS;
currentState = AnimationState::ACTION_ATTACK_POWER;
resetAnim = true;
attack = 0;
}
if (canAttack && !powerAttackPending &&
currentState != AnimationState::ACTION_ATTACK_POWER &&
attack == 0 && attack_cooldown < 0.f &&
currentState == AnimationState::ACTION_IDLE) {
attack = 1;
attack_cooldown = ATTACK_COOLDOWN_TIME;
}
if (canAttack && attack_cooldown >= 0.f)
attack_cooldown -= static_cast<float>(deltaMs) / 1000.f;
if (attack == 1 && currentState == AnimationState::ACTION_IDLE &&
if (attack == 1 && !powerAttackPending &&
currentState == AnimationState::ACTION_IDLE &&
attackTarget->hp > 0.f) {
currentState = AnimationState::ACTION_ATTACK;
resetAnim = true;
}
} else if (currentState == AnimationState::STAND_TO_ACTION ||
} else if (!jumpAttackActive && battle_state == 0 &&
(currentState == AnimationState::STAND_TO_ACTION ||
currentState == AnimationState::ACTION_IDLE ||
currentState == AnimationState::ACTION_ATTACK) {
currentState == AnimationState::ACTION_ATTACK ||
currentState == AnimationState::ACTION_ATTACK_POWER)) {
currentState = AnimationState::ACTION_TO_STAND;
resetAnim = true;
}
@@ -402,6 +504,71 @@ CharacterUpdateEvents CharacterState::update(
}
}
if (jumpAttackActive) {
const int previousSubframe = jumpAttackAnimationSubframe;
jumpAttackAnimationSubframe += static_cast<int>(
deltaMs * JUMP_ATTACK_FRAME_COUNT *
ANIMATION_SUBFRAMES_PER_FRAME / JUMP_ATTACK_DURATION_MS);
animationFrame =
static_cast<float>(jumpAttackAnimationSubframe) /
static_cast<float>(ANIMATION_SUBFRAMES_PER_FRAME);
if (jumpAttackAnimationSubframe > JUMP_ATTACK_TAKEOFF_SUBFRAME) {
const int travelSubframes =
JUMP_ATTACK_IMPACT_SUBFRAME - JUMP_ATTACK_TAKEOFF_SUBFRAME;
const int elapsed = (std::min)(
travelSubframes,
jumpAttackAnimationSubframe - JUMP_ATTACK_TAKEOFF_SUBFRAME);
const fp progress = static_cast<fp>(elapsed) /
static_cast<fp>(travelSubframes);
position = jumpAttackStartPosition +
(jumpAttackLandingPosition - jumpAttackStartPosition) * progress;
position.y = jumpAttackLandingPosition.y;
}
if (!jumpAttackImpactConsumed &&
previousSubframe < JUMP_ATTACK_IMPACT_SUBFRAME &&
jumpAttackAnimationSubframe >= JUMP_ATTACK_IMPACT_SUBFRAME) {
position = jumpAttackLandingPosition;
jumpAttackImpactConsumed = true;
events.attackImpact = true;
events.jumpAttackImpact = true;
events.attackDamage = 20.f;
events.attackTargetId = jumpAttackTargetId;
if (attackTarget) {
events.attackDirection = attackTarget->position - position;
events.attackDirection.y = fp(0);
const fp lengthSquared = events.attackDirection.normSquared();
if (lengthSquared > static_cast<fp>(0.001f))
events.attackDirection =
events.attackDirection / fpm::sqrt(lengthSquared);
else
events.attackDirection = Vec3fp{};
}
}
const auto jumpIndex =
static_cast<std::size_t>(AnimationState::ACTION_ATTACK_JUMP);
const int jumpFrameCount = jumpIndex < animationFrameCounts.size()
? animationFrameCounts[jumpIndex] : 80;
if (jumpAttackAnimationSubframe >=
((std::max)(1, jumpFrameCount) - 1) *
ANIMATION_SUBFRAMES_PER_FRAME) {
position = jumpAttackLandingPosition;
jumpAttackActive = false;
jumpAttackImpactConsumed = false;
jumpAttackAnimationSubframe = 0;
jumpAttackTargetId.clear();
currentState = AnimationState::ACTION_IDLE;
const auto idleIndex =
static_cast<std::size_t>(AnimationState::ACTION_IDLE);
animationFrame = idleIndex < animationStartFrames.size()
? static_cast<float>(animationStartFrames[idleIndex]) : 0.f;
}
showWeapon = true;
return events;
}
if (resetAnim) {
resetAnim = false;
const auto index = static_cast<std::size_t>(currentState);
@@ -416,10 +583,10 @@ CharacterUpdateEvents CharacterState::update(
animationFrame += static_cast<float>(deltaMs) / 24.f * playbackRate;
if (previousFrame < 19 && static_cast<int>(animationFrame) >= 19 &&
(currentState == AnimationState::ACTION_ATTACK ||
currentState == AnimationState::ACTION_ATTACK_2) &&
currentState == AnimationState::ACTION_ATTACK &&
attackTarget) {
events.attackImpact = true;
events.attackDamage = 10.f;
events.attackTargetId = attackTargetId;
events.attackDirection = attackTarget->position - position;
events.attackDirection.y = static_cast<fp>(0.f);
@@ -436,11 +603,29 @@ CharacterUpdateEvents CharacterState::update(
}
}
if (previousFrame < POWER_ATTACK_IMPACT_FRAME &&
static_cast<int>(animationFrame) >= POWER_ATTACK_IMPACT_FRAME &&
currentState == AnimationState::ACTION_ATTACK_POWER &&
attackTarget) {
events.attackImpact = true;
events.powerAttackImpact = true;
events.attackDamage = 30.f;
events.attackTargetId = attackTargetId;
events.attackDirection = attackTarget->position - position;
events.attackDirection.y = fp(0);
const fp lengthSquared = events.attackDirection.normSquared();
if (lengthSquared > static_cast<fp>(0.001f))
events.attackDirection =
events.attackDirection / fpm::sqrt(lengthSquared);
else
events.attackDirection = Vec3fp{};
}
showWeapon =
(static_cast<int>(animationFrame) >= 20 && currentState == AnimationState::STAND_TO_ACTION) ||
(static_cast<int>(animationFrame) <= 32 && currentState == AnimationState::ACTION_TO_STAND) ||
currentState == AnimationState::ACTION_ATTACK_2 ||
currentState == AnimationState::ACTION_ATTACK ||
currentState == AnimationState::ACTION_ATTACK_POWER ||
currentState == AnimationState::ACTION_IDLE;
const int frameCount = animationIndex < animationFrameCounts.size()
@@ -451,7 +636,7 @@ CharacterUpdateEvents CharacterState::update(
else if (currentState == AnimationState::ACTION_TO_STAND)
currentState = AnimationState::STAND;
else if (currentState == AnimationState::ACTION_ATTACK ||
currentState == AnimationState::ACTION_ATTACK_2) {
currentState == AnimationState::ACTION_ATTACK_POWER) {
currentState = AnimationState::ACTION_IDLE;
attack = 0;
} else if (currentState == AnimationState::ACTION_TO_DEATH) {
@@ -478,6 +663,24 @@ void CharacterState::save(nlohmann::json& out) const
out["resetAnim"] = resetAnim;
out["currentState"] = static_cast<int>(currentState);
out["attack_cooldown"] = attack_cooldown;
out["jumpAttackUnlocked"] = jumpAttackUnlocked;
out["jumpAttackPending"] = jumpAttackPending;
out["jumpAttackActive"] = jumpAttackActive;
out["jumpAttackImpactConsumed"] = jumpAttackImpactConsumed;
out["jumpAttackCooldownTicks"] = jumpAttackCooldownTicks;
out["jumpAttackAnimationSubframe"] = jumpAttackAnimationSubframe;
out["jumpAttackTargetId"] = jumpAttackTargetId;
out["jumpAttackStartPosition"] = {
static_cast<double>(jumpAttackStartPosition.x),
static_cast<double>(jumpAttackStartPosition.y),
static_cast<double>(jumpAttackStartPosition.z) };
out["jumpAttackLandingPosition"] = {
static_cast<double>(jumpAttackLandingPosition.x),
static_cast<double>(jumpAttackLandingPosition.y),
static_cast<double>(jumpAttackLandingPosition.z) };
out["powerAttackPending"] = powerAttackPending;
out["powerAttackCooldownTicks"] = powerAttackCooldownTicks;
out["powerAttackTargetId"] = powerAttackTargetId;
out["showWeapon"] = showWeapon;
out["attackTargetId"] = attackTargetId;
out["faceTargetId"] = faceTargetId;
@@ -527,6 +730,32 @@ void CharacterState::load(const nlohmann::json& in)
resetAnim = in.at("resetAnim").get<bool>();
currentState = static_cast<AnimationState>(in.value("currentState", static_cast<int>(currentState)));
attack_cooldown = in.value("attack_cooldown", attack_cooldown);
jumpAttackUnlocked =
in.value("jumpAttackUnlocked", jumpAttackUnlocked);
jumpAttackPending =
in.value("jumpAttackPending", jumpAttackPending);
jumpAttackActive =
in.value("jumpAttackActive", jumpAttackActive);
jumpAttackImpactConsumed =
in.value("jumpAttackImpactConsumed", jumpAttackImpactConsumed);
jumpAttackCooldownTicks =
in.value("jumpAttackCooldownTicks", jumpAttackCooldownTicks);
jumpAttackAnimationSubframe =
in.value("jumpAttackAnimationSubframe", jumpAttackAnimationSubframe);
jumpAttackTargetId =
in.value("jumpAttackTargetId", jumpAttackTargetId);
if (in.contains("jumpAttackStartPosition"))
jumpAttackStartPosition = readVec3fp(
in["jumpAttackStartPosition"], jumpAttackStartPosition);
if (in.contains("jumpAttackLandingPosition"))
jumpAttackLandingPosition = readVec3fp(
in["jumpAttackLandingPosition"], jumpAttackLandingPosition);
powerAttackPending =
in.value("powerAttackPending", powerAttackPending);
powerAttackCooldownTicks =
in.value("powerAttackCooldownTicks", powerAttackCooldownTicks);
powerAttackTargetId =
in.value("powerAttackTargetId", powerAttackTargetId);
showWeapon = in.value("showWeapon", showWeapon);
attackTargetId = in.value("attackTargetId", attackTargetId);
faceTargetId = in.value("faceTargetId", faceTargetId);