This commit is contained in:
2025-09-25 16:50:43 +08:00
parent 4947ebb29d
commit be5f00c313
6 changed files with 116 additions and 70 deletions

View File

@@ -32,30 +32,35 @@ public class SimpleFighter {
private boolean isAttacking = false; // 是否正在攻击(攻击状态标志) private boolean isAttacking = false; // 是否正在攻击(攻击状态标志)
private SimpleFighter fighter; // 添加 fighter 的声明
private Iterable<Integer> pressedKeys = new HashMap<Integer, Float>().keySet(); // 初始化 pressedKeys private Iterable<Integer> pressedKeys = new HashMap<Integer, Float>().keySet(); // 初始化 pressedKeys
public SimpleFighter(String name) { public SimpleFighter(String name) {
this.name = 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) { public void update(float deltaTime) {
updateAttackbox(); updateAttackbox("light"); // 默认使用普通攻击的攻击盒更新
// 攻击只持续一帧 // 攻击只持续一帧
if (isAttacking) { if (isAttacking) {
isAttacking = false; isAttacking = false;
changeAction(Action.IDLE); changeAction(Action.IDLE);
} }
for (int keycode : pressedKeys) {
keyPressDuration.put(keycode, keyPressDuration.getOrDefault(keycode, 0f) + deltaTime); // 更新持续时间 processPressedKeys(deltaTime);
fighter.handleInput(keycode, true); // 持续按下的键
}
// 垂直移动(跳跃或重力) // 垂直移动(跳跃或重力)
if (!isGrounded) { if (!isGrounded) {
verticalSpeed -= 980 * deltaTime; // 简单重力模拟 verticalSpeed -= 2500 * deltaTime; // 简单重力模拟
hitbox.y += verticalSpeed * deltaTime; hitbox.y += verticalSpeed * deltaTime;
if (hitbox.y <= 0) { if (hitbox.y <= 0) {
@@ -78,7 +83,7 @@ public class SimpleFighter {
if (isAttacking) { if (isAttacking) {
shapeRenderer.setColor(Color.RED); // 设置颜色为红色 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 渲染 shapeRenderer.end(); // 结束 ShapeRenderer 渲染
@@ -86,31 +91,29 @@ public class SimpleFighter {
} }
public void handleInput(int keycode, boolean isPressed, float duration) { public void handleInput(int keycode, boolean isPressed, float duration) {
// 根据按键和按下状态处理输入行为
if (isPressed) { if (isPressed) {
if (keycode == Input.Keys.LEFT || keycode == Input.Keys.A) { 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) { } 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) { if (isPressed && !isAttacking) {
attack(""); // 普通攻击 if (keycode == Input.Keys.Z || keycode == Input.Keys.J) {
} else if (keycode == Input.Keys.X || keycode == Input.Keys.K) { attack("light");
attack(""); // 重攻击(暂未区分) } else if (keycode == Input.Keys.X || keycode == Input.Keys.K) {
} else if (keycode == Input.Keys.SPACE || keycode == Input.Keys.UP || keycode == Input.Keys.W) { attack("heavy");
attack(""); // 跳跃(暂未实现跳跃逻辑) } else if (keycode == Input.Keys.SHIFT_LEFT || keycode == Input.Keys.SHIFT_RIGHT) {
} else if (keycode == Input.Keys.SHIFT_LEFT || keycode == Input.Keys.SHIFT_RIGHT) { attack("special");
attack(""); // 防御(暂未实现防御逻辑) } 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 { } else {
// 松开防御键时恢复待机状态 keyPressDuration.remove(keycode);
if ((keycode == Input.Keys.SHIFT_LEFT || keycode == Input.Keys.SHIFT_RIGHT) && if ((keycode == Input.Keys.LEFT || keycode == Input.Keys.RIGHT || keycode == Input.Keys.A
getCurrentAction() == Action.DEFEND) { || keycode == Input.Keys.D) &&
getCurrentAction() == Action.MOVE) {
changeAction(Action.IDLE); changeAction(Action.IDLE);
} }
} }
@@ -120,16 +123,6 @@ public class SimpleFighter {
handleInput(keycode, isPressed, 0f); // 调用已有方法,补充默认持续时间 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() { public Action getCurrentAction() {
return currentAction; // 获取当前动作状态 return currentAction; // 获取当前动作状态
} }
@@ -140,8 +133,9 @@ public class SimpleFighter {
public void jump() { public void jump() {
if (isGrounded) { if (isGrounded) {
verticalSpeed = 600f; verticalSpeed = 1000f;
isGrounded = false; isGrounded = false;
System.out.println("跳跃高度: " + verticalSpeed);
changeAction(Action.JUMP); 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) { public void attack(String attackType) {
isAttacking = true; // 设置攻击状态 isAttacking = true;
changeAction(Action.ATTACK); // 切换为攻击动作 changeAction(Action.ATTACK);
updateAttackbox(attackType); // 根据攻击类型更新攻击盒位置
} }
public void takeHit(int damage) { public void takeHit(int damage) {
@@ -174,12 +199,6 @@ public class SimpleFighter {
return isAttacking; // 判断是否处于攻击状态 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() { public Rectangle getHitbox() {
return hitbox; // 获取碰撞盒 return hitbox; // 获取碰撞盒

View File

@@ -3,9 +3,11 @@ package uno.mloluyu.desktop;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter; import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -22,6 +24,7 @@ public class GameScreen extends ScreenAdapter {
private SpriteBatch batch; private SpriteBatch batch;
private ShapeRenderer shapeRenderer; private ShapeRenderer shapeRenderer;
private OrthographicCamera camera; // 添加摄像机
public GameScreen(MainGame game, SimpleFighter player) { public GameScreen(MainGame game, SimpleFighter player) {
this.player = player; this.player = player;
@@ -30,6 +33,10 @@ public class GameScreen extends ScreenAdapter {
@Override @Override
public void show() { public void show() {
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(player.getHitbox().x, player.getHitbox().y, 0); // 初始化摄像机位置
camera.update();
batch = new SpriteBatch(); batch = new SpriteBatch();
shapeRenderer = new ShapeRenderer(); shapeRenderer = new ShapeRenderer();
Gdx.input.setInputProcessor(controller); Gdx.input.setInputProcessor(controller);
@@ -46,17 +53,25 @@ public class GameScreen extends ScreenAdapter {
NetworkManager.getInstance().sendPosition(player.getHitbox().x, player.getHitbox().y); NetworkManager.getInstance().sendPosition(player.getHitbox().x, player.getHitbox().y);
} }
// 更新摄像机位置
camera.position.lerp(new Vector3(player.getHitbox().x, player.getHitbox().y, 0), 0.1f);
camera.update();
batch.setProjectionMatrix(camera.combined);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
renderFighter(player, Color.BLUE); renderFighter(player, Color.BLUE);
if (player.isAttacking()) renderAttackBox(player, Color.RED); if (player.isAttacking())
renderAttackBox(player, Color.RED);
Map<String, float[]> positions = NetworkManager.getInstance().getPlayerPositions(); Map<String, float[]> positions = NetworkManager.getInstance().getPlayerPositions();
if (positions != null) { if (positions != null) {
for (Map.Entry<String, float[]> entry : positions.entrySet()) { for (Map.Entry<String, float[]> entry : positions.entrySet()) {
String id = entry.getKey(); String id = entry.getKey();
float[] pos = entry.getValue(); float[] pos = entry.getValue();
if (pos == null) continue; if (pos == null)
continue;
SimpleFighter remote = otherPlayers.computeIfAbsent(id, k -> new SimpleFighter("Remote-" + k)); SimpleFighter remote = otherPlayers.computeIfAbsent(id, k -> new SimpleFighter("Remote-" + k));
remote.setPosition(pos[0], pos[1]); remote.setPosition(pos[0], pos[1]);
remote.update(delta); remote.update(delta);
@@ -65,6 +80,12 @@ public class GameScreen extends ScreenAdapter {
} }
shapeRenderer.end(); shapeRenderer.end();
// 绘制地图边界
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.WHITE);
shapeRenderer.rect(0, 0, 1000, 1000); // 假设地图边界为1000x1000的区域
shapeRenderer.end();
} }
private void renderFighter(SimpleFighter fighter, Color color) { private void renderFighter(SimpleFighter fighter, Color color) {
@@ -80,14 +101,14 @@ public class GameScreen extends ScreenAdapter {
} }
// private void checkPlayerAttacks() { // private void checkPlayerAttacks() {
// if (!player.isAttacking()) return; // if (!player.isAttacking()) return;
// for (SimpleFighter target : otherPlayers.values()) { // for (SimpleFighter target : otherPlayers.values()) {
// if (target.isAlive() && player.getAttackbox().overlaps(target.getHitbox())) { // if (target.isAlive() && player.getAttackbox().overlaps(target.getHitbox())) {
// target.takeHit(player.getAttackPower()); // 使用访问器方法 // target.takeHit(player.getAttackPower()); // 使用访问器方法
// System.out.println("命中远程玩家:" + target.getName()); // System.out.println("命中远程玩家:" + target.getName());
// } // }
// } // }
// } // }
@Override @Override

View File

@@ -29,36 +29,42 @@ public class FighterController extends InputAdapter {
return; return;
for (int keycode : pressedKeys) { for (int keycode : pressedKeys) {
fighter.handleInput(keycode, true); // 持续按下的键 float currentDuration = keyPressDuration.getOrDefault(keycode, 0f);
currentDuration += deltaTime;
keyPressDuration.put(keycode, currentDuration);
fighter.handleInput(keycode, true, currentDuration); // 持续按下的键,传递持续时间
} }
} }
@Override @Override
public boolean keyDown(int keycode) { public boolean keyDown(int keycode) {
// System.out.println("按键按下: " + keycode);
if (fighter == null) if (fighter == null)
return false; return false;
if (!pressedKeys.contains(keycode, false)) { if (!pressedKeys.contains(keycode, false)) {
pressedKeys.add(keycode); pressedKeys.add(keycode);
keyPressDuration.put(keycode, 0f); // 初始化按键持续时间
} }
fighter.handleInput(keycode, true); // 按下事件 fighter.handleInput(keycode, true, 0f); // 按下事件,初始持续时间为 0
return true; return true;
} }
@Override @Override
public boolean keyUp(int keycode) { public boolean keyUp(int keycode) {
if (fighter == null) // System.out.println("按键松开: " + keycode);
return false;
float duration = keyPressDuration.getOrDefault(keycode, 0f); if (fighter == null)
pressedKeys.removeValue(keycode, false); return false;
keyPressDuration.remove(keycode);
// 传给角色:按键松开 + 按下时长 float duration = keyPressDuration.getOrDefault(keycode, 0f);
fighter.handleRelease(keycode, duration); pressedKeys.removeValue(keycode, false);
return true; keyPressDuration.remove(keycode);
}//松开事件
fighter.handleInput(keycode, false, duration); // 按键松开事件,传递持续时间
return true;
}// 松开事件
public SimpleFighter getFighter() { public SimpleFighter getFighter() {
return fighter; return fighter;