This commit is contained in:
2025-09-25 22:03:19 +08:00
parent 8e07b3204a
commit 7153ac9b97
6 changed files with 242 additions and 0 deletions

View File

@@ -29,12 +29,36 @@ public class SimpleFighter {
private boolean attackJustStarted = false; // 攻击是否刚开始
private float attackTimer = 0f; // 攻击计时器
private static final float ATTACK_DURATION = 0.15f; // 攻击持续时间
// 新增:连续攻击序号(本地,用于避免重复伤害)
private int attackSequence = 0;
private String lastAttackType = "light"; // 记录最后一次攻击类型,供伤害判定
private int lastDamageAppliedSeq = -1; // 已经对目标造成伤害的序号,避免重复
// 击退 & 无敌
private float knockbackX = 0f;
private float knockbackTimer = 0f;
private float invulnerableTimer = 0f; // 无敌帧时间(被击中后短暂无敌)
private static final float INVULNERABLE_DURATION = 0.3f;
private static final float KNOCKBACK_DURATION = 0.12f;
public SimpleFighter(String name) {
this.name = name;
}
public void update(float deltaTime) {
// 处理无敌帧计时
if (invulnerableTimer > 0) {
invulnerableTimer -= deltaTime;
if (invulnerableTimer < 0)
invulnerableTimer = 0;
}
// 处理击退
if (knockbackTimer > 0) {
hitbox.x += knockbackX * deltaTime;
knockbackTimer -= deltaTime;
if (knockbackTimer <= 0) {
knockbackX = 0;
}
}
if (isAttacking) {
if (attackJustStarted) {
attackJustStarted = false;
@@ -135,6 +159,19 @@ public class SimpleFighter {
}
}
// 供远程同步:根据位置变化量更新朝向(不触发移动动作,只用于攻击盒朝向正确)
public void updateFacingByDelta(float dx) {
if (dx > 0) {
isFacingRight = true;
} else if (dx < 0) {
isFacingRight = false;
}
}
public boolean isFacingRight() {
return isFacingRight;
}
private void updateAttackbox(String attackType) {
float offsetX, offsetY = 20, width = 80, height = 80;
switch (attackType) {
@@ -166,11 +203,21 @@ public class SimpleFighter {
attackJustStarted = true;
changeAction(Action.ATTACK);
updateAttackbox(attackType);
lastAttackType = attackType;
attackSequence++; // 本地每次攻击自增
lastDamageAppliedSeq = -1; // 新一次攻击重置
}
public void takeHit(int damage) {
if (invulnerableTimer > 0 || health <= 0)
return; // 无敌或已死亡
health = Math.max(0, health - damage);
changeAction(health > 0 ? Action.HIT : Action.DEAD);
// 设置无敌 & 击退
invulnerableTimer = INVULNERABLE_DURATION;
// 击退方向与幅度
knockbackX = isFacingRight ? -600f : 600f;
knockbackTimer = KNOCKBACK_DURATION;
}
public boolean isAlive() {
@@ -181,6 +228,10 @@ public class SimpleFighter {
return isAttacking;
}
public boolean isInvulnerable() {
return invulnerableTimer > 0;
}
public Rectangle getHitbox() {
return hitbox;
}
@@ -193,6 +244,43 @@ public class SimpleFighter {
return health;
}
public int getAttackSequence() {
return attackSequence;
}
public String getLastAttackType() {
return lastAttackType;
}
public int getLastDamageAppliedSeq() {
return lastDamageAppliedSeq;
}
public void setLastDamageAppliedSeq(int seq) {
this.lastDamageAppliedSeq = seq;
}
public boolean canDealDamage() {
return isAttacking && attackSequence != lastDamageAppliedSeq; // 未对当前序号造成过伤害
}
public void markDamageApplied() {
lastDamageAppliedSeq = attackSequence;
}
// 根据攻击类型返回伤害数值
public int getDamageForAttack(String type) {
switch (type) {
case "heavy":
return 20;
case "special":
return 30;
case "light":
default:
return 10;
}
}
public String getName() {
return name;
}