更新
This commit is contained in:
@@ -32,30 +32,35 @@ public class SimpleFighter {
|
||||
|
||||
private boolean isAttacking = false; // 是否正在攻击(攻击状态标志)
|
||||
|
||||
private SimpleFighter fighter; // 添加 fighter 的声明
|
||||
|
||||
private Iterable<Integer> pressedKeys = new HashMap<Integer, Float>().keySet(); // 初始化 pressedKeys
|
||||
|
||||
public SimpleFighter(String name) {
|
||||
this.name = name; // 构造函数,初始化角色名称
|
||||
this.fighter = this; // 初始化 fighter 为当前实例
|
||||
}
|
||||
|
||||
private void processPressedKeys(float deltaTime) {
|
||||
for (int keycode : pressedKeys) {
|
||||
float currentDuration = keyPressDuration.getOrDefault(keycode, 0f);
|
||||
currentDuration += deltaTime;
|
||||
keyPressDuration.put(keycode, currentDuration);
|
||||
handleInput(keycode, true, currentDuration);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(float deltaTime) {
|
||||
updateAttackbox();
|
||||
updateAttackbox("light"); // 默认使用普通攻击的攻击盒更新
|
||||
|
||||
// 攻击只持续一帧
|
||||
if (isAttacking) {
|
||||
isAttacking = false;
|
||||
changeAction(Action.IDLE);
|
||||
}
|
||||
for (int keycode : pressedKeys) {
|
||||
keyPressDuration.put(keycode, keyPressDuration.getOrDefault(keycode, 0f) + deltaTime); // 更新持续时间
|
||||
fighter.handleInput(keycode, true); // 持续按下的键
|
||||
}
|
||||
|
||||
processPressedKeys(deltaTime);
|
||||
|
||||
// 垂直移动(跳跃或重力)
|
||||
if (!isGrounded) {
|
||||
verticalSpeed -= 980 * deltaTime; // 简单重力模拟
|
||||
verticalSpeed -= 2500 * deltaTime; // 简单重力模拟
|
||||
hitbox.y += verticalSpeed * deltaTime;
|
||||
|
||||
if (hitbox.y <= 0) {
|
||||
@@ -78,7 +83,7 @@ public class SimpleFighter {
|
||||
|
||||
if (isAttacking) {
|
||||
shapeRenderer.setColor(Color.RED); // 设置颜色为红色
|
||||
shapeRenderer.rect(attackbox.x, attackbox.y, attackbox.width, attackbox.height); // 绘制攻击盒
|
||||
shapeRenderer.rect(attackbox.x, attackbox.y, attackbox.width, attackbox.height); // 绘制攻击框
|
||||
}
|
||||
|
||||
shapeRenderer.end(); // 结束 ShapeRenderer 渲染
|
||||
@@ -86,31 +91,29 @@ public class SimpleFighter {
|
||||
}
|
||||
|
||||
public void handleInput(int keycode, boolean isPressed, float duration) {
|
||||
// 根据按键和按下状态处理输入行为
|
||||
|
||||
if (isPressed) {
|
||||
if (keycode == Input.Keys.LEFT || keycode == Input.Keys.A) {
|
||||
move(-1, Gdx.graphics.getDeltaTime()); // 向左移动
|
||||
move(-1, Gdx.graphics.getDeltaTime());
|
||||
} else if (keycode == Input.Keys.RIGHT || keycode == Input.Keys.D) {
|
||||
move(1, Gdx.graphics.getDeltaTime()); // 向右移动
|
||||
move(1, Gdx.graphics.getDeltaTime());
|
||||
}
|
||||
if (keycode == Input.Keys.Z || keycode == Input.Keys.J) {
|
||||
attack(""); // 普通攻击
|
||||
} else if (keycode == Input.Keys.X || keycode == Input.Keys.K) {
|
||||
attack(""); // 重攻击(暂未区分)
|
||||
} else if (keycode == Input.Keys.SPACE || keycode == Input.Keys.UP || keycode == Input.Keys.W) {
|
||||
attack(""); // 跳跃(暂未实现跳跃逻辑)
|
||||
} else if (keycode == Input.Keys.SHIFT_LEFT || keycode == Input.Keys.SHIFT_RIGHT) {
|
||||
attack(""); // 防御(暂未实现防御逻辑)
|
||||
if (isPressed && !isAttacking) {
|
||||
if (keycode == Input.Keys.Z || keycode == Input.Keys.J) {
|
||||
attack("light");
|
||||
} else if (keycode == Input.Keys.X || keycode == Input.Keys.K) {
|
||||
attack("heavy");
|
||||
} else if (keycode == Input.Keys.SHIFT_LEFT || keycode == Input.Keys.SHIFT_RIGHT) {
|
||||
attack("special");
|
||||
} else if (keycode == Input.Keys.SPACE || keycode == Input.Keys.UP || keycode == Input.Keys.W) {
|
||||
jump();
|
||||
}
|
||||
}
|
||||
if (keycode == Input.Keys.SPACE || keycode == Input.Keys.UP || keycode == Input.Keys.W) {
|
||||
System.out.println("点击了跳跃");
|
||||
jump();
|
||||
}
|
||||
|
||||
} else {
|
||||
// 松开防御键时恢复待机状态
|
||||
if ((keycode == Input.Keys.SHIFT_LEFT || keycode == Input.Keys.SHIFT_RIGHT) &&
|
||||
getCurrentAction() == Action.DEFEND) {
|
||||
keyPressDuration.remove(keycode);
|
||||
if ((keycode == Input.Keys.LEFT || keycode == Input.Keys.RIGHT || keycode == Input.Keys.A
|
||||
|| keycode == Input.Keys.D) &&
|
||||
getCurrentAction() == Action.MOVE) {
|
||||
changeAction(Action.IDLE);
|
||||
}
|
||||
}
|
||||
@@ -120,16 +123,6 @@ public class SimpleFighter {
|
||||
handleInput(keycode, isPressed, 0f); // 调用已有方法,补充默认持续时间
|
||||
}
|
||||
|
||||
public void handleRelease(int keycode, float duration) {
|
||||
// 处理按键释放逻辑
|
||||
System.out.println("按键释放: " + keycode + ", 持续时间: " + duration);
|
||||
keyPressDuration.remove(keycode);
|
||||
if (keycode == Input.Keys.LEFT || keycode == Input.Keys.RIGHT || keycode == Input.Keys.A
|
||||
|| keycode == Input.Keys.D) {
|
||||
changeAction(Action.IDLE);
|
||||
}
|
||||
}
|
||||
|
||||
public Action getCurrentAction() {
|
||||
return currentAction; // 获取当前动作状态
|
||||
}
|
||||
@@ -140,8 +133,9 @@ public class SimpleFighter {
|
||||
|
||||
public void jump() {
|
||||
if (isGrounded) {
|
||||
verticalSpeed = 600f;
|
||||
verticalSpeed = 1000f;
|
||||
isGrounded = false;
|
||||
System.out.println("跳跃高度: " + verticalSpeed);
|
||||
changeAction(Action.JUMP);
|
||||
}
|
||||
}
|
||||
@@ -156,9 +150,40 @@ public class SimpleFighter {
|
||||
}
|
||||
}
|
||||
|
||||
private void updateAttackbox(String attackType) {
|
||||
float offsetX;
|
||||
float offsetY = 20; // 默认偏移量
|
||||
float width = 80;
|
||||
float height = 80;
|
||||
|
||||
switch (attackType) {
|
||||
case "heavy":
|
||||
offsetX = isFacingRight ? hitbox.width : -100;
|
||||
offsetY = 40;
|
||||
width = 100;
|
||||
height = 100;
|
||||
break;
|
||||
case "light":
|
||||
offsetX = isFacingRight ? hitbox.width - 10 : -attackbox.width + 10;
|
||||
break;
|
||||
case "special":
|
||||
offsetX = isFacingRight ? hitbox.width + 20 : -attackbox.width - 20;
|
||||
offsetY = 50;
|
||||
width = 120;
|
||||
height = 60;
|
||||
break;
|
||||
default:
|
||||
offsetX = isFacingRight ? hitbox.width - 10 : -attackbox.width + 10;
|
||||
}
|
||||
|
||||
attackbox.setPosition(hitbox.x + offsetX, hitbox.y + offsetY);
|
||||
attackbox.setSize(width, height);
|
||||
}
|
||||
|
||||
public void attack(String attackType) {
|
||||
isAttacking = true; // 设置攻击状态
|
||||
changeAction(Action.ATTACK); // 切换为攻击动作
|
||||
isAttacking = true;
|
||||
changeAction(Action.ATTACK);
|
||||
updateAttackbox(attackType); // 根据攻击类型更新攻击盒位置
|
||||
}
|
||||
|
||||
public void takeHit(int damage) {
|
||||
@@ -174,12 +199,6 @@ public class SimpleFighter {
|
||||
return isAttacking; // 判断是否处于攻击状态
|
||||
}
|
||||
|
||||
private void updateAttackbox() {
|
||||
// 根据朝向更新攻击盒位置,使其位于角色前方
|
||||
float offsetX = isFacingRight ? hitbox.width - 10 : -attackbox.width + 10;
|
||||
attackbox.setPosition(hitbox.x + offsetX, hitbox.y + 20);
|
||||
}
|
||||
|
||||
// 常用访问器
|
||||
public Rectangle getHitbox() {
|
||||
return hitbox; // 获取碰撞盒
|
||||
|
||||
Reference in New Issue
Block a user