新增FighterController并更新角色类:
引入FighterController类来管理战斗角色动作 更新Alice、Fighter及Fighter$Action类的新实现 修改GameCore和Launcher类以集成最新变更
@@ -0,0 +1,115 @@
|
||||
package uno.mloluyu.FighterController;
|
||||
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.InputAdapter;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import uno.mloluyu.characters.Fighter;
|
||||
|
||||
public class FighterController extends InputAdapter {
|
||||
private final Fighter fighter;
|
||||
private final Array<Integer> pressedKeys = new Array<>();
|
||||
|
||||
// 输入缓冲时间(防止按键重复触发)
|
||||
private static final float INPUT_BUFFER = 0.2f;
|
||||
private float attackBufferTimer = 0;
|
||||
private float jumpBufferTimer = 0;
|
||||
|
||||
public FighterController(Fighter fighter) {
|
||||
this.fighter = fighter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新角色状态和输入缓冲
|
||||
*/
|
||||
public void update(float deltaTime) {
|
||||
// 更新输入缓冲计时器
|
||||
if (attackBufferTimer > 0) attackBufferTimer -= deltaTime;
|
||||
if (jumpBufferTimer > 0) jumpBufferTimer -= deltaTime;
|
||||
|
||||
// 处理移动输入
|
||||
handleMovementInput(deltaTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理移动输入
|
||||
*/
|
||||
private void handleMovementInput(float deltaTime) {
|
||||
float moveX = 0;
|
||||
|
||||
// 检查按键状态
|
||||
if (isKeyPressed(Input.Keys.RIGHT) || isKeyPressed(Input.Keys.D)) {
|
||||
moveX += 1;
|
||||
}
|
||||
if (isKeyPressed(Input.Keys.LEFT) || isKeyPressed(Input.Keys.A)) {
|
||||
moveX -= 1;
|
||||
}
|
||||
|
||||
// 调用角色移动方法
|
||||
fighter.move(moveX, deltaTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理按键按下事件
|
||||
*/
|
||||
@Override
|
||||
public boolean keyDown(int keycode) {
|
||||
if (!pressedKeys.contains(keycode, false)) {
|
||||
pressedKeys.add(keycode);
|
||||
}
|
||||
|
||||
// 攻击按键
|
||||
if ((keycode == Input.Keys.Z || keycode == Input.Keys.J) && attackBufferTimer <= 0) {
|
||||
fighter.attack(1); // 普通攻击
|
||||
attackBufferTimer = INPUT_BUFFER;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 特殊攻击
|
||||
if ((keycode == Input.Keys.X || keycode == Input.Keys.K) && attackBufferTimer <= 0) {
|
||||
fighter.attack(4); // 特殊攻击1
|
||||
attackBufferTimer = INPUT_BUFFER;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 跳跃
|
||||
if ((keycode == Input.Keys.SPACE || keycode == Input.Keys.W || keycode == Input.Keys.UP) && jumpBufferTimer <= 0) {
|
||||
// 这里假设你已经实现了跳跃方法
|
||||
// fighter.jump();
|
||||
jumpBufferTimer = INPUT_BUFFER;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理按键释放事件
|
||||
*/
|
||||
@Override
|
||||
public boolean keyUp(int keycode) {
|
||||
pressedKeys.removeValue(keycode, false);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查按键是否被按下
|
||||
*/
|
||||
private boolean isKeyPressed(int keycode) {
|
||||
return pressedKeys.contains(keycode, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 绘制角色
|
||||
*/
|
||||
public void render(SpriteBatch batch) {
|
||||
fighter.render(batch);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取控制的角色
|
||||
*/
|
||||
public Fighter getFighter() {
|
||||
return fighter;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,21 @@
|
||||
package uno.mloluyu.characters;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
|
||||
/**
|
||||
* 角色类,继承自Fighter父类
|
||||
*/
|
||||
public class Alice extends Fighter {
|
||||
private TextureAtlas atlas;
|
||||
|
||||
public Alice(TextureAtlas atlas) {
|
||||
super(atlas);
|
||||
public Alice() {
|
||||
super(new TextureAtlas(Gdx.files.internal("src\\main\\resources\\character\\alice\\精灵1.2.atlas")));
|
||||
speed = 350f; // 速度更快
|
||||
maxHealth = 90; // 生命值较低
|
||||
health = maxHealth;
|
||||
attackPower = 12; // 攻击力中等
|
||||
attackPower = 12; // 攻击力中等\
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -29,6 +32,7 @@ public class Alice extends Fighter {
|
||||
loadAnimationFromAtlas(Action.HIT, "hitSpin/hitSpin", 5, false);
|
||||
|
||||
// 为特定动作设置帧间隔
|
||||
setFrameDuration(Action.IDLE, 0.04f);
|
||||
setFrameDuration(Action.WALK, 0.08f); // 行走更快
|
||||
setFrameDuration(Action.ATTACK1, 0.07f); // 攻击更快
|
||||
setFrameDuration(Action.SPECIAL2, 0.06f); // 特殊技能2非常快
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package uno.mloluyu.characters;
|
||||
|
||||
import uno.mloluyu.util.SimpleFormatter;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.Animation;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
@@ -13,7 +15,6 @@ import com.badlogic.gdx.utils.Disposable;
|
||||
* 格斗角色父类,封装所有角色共有的动画和状态管理逻辑
|
||||
*/
|
||||
public abstract class Fighter implements Disposable {
|
||||
// 动作类型枚举 - 所有角色共用的基础动作
|
||||
public enum Action {
|
||||
IDLE, WALK, JUMP, FALL,
|
||||
ATTACK1, ATTACK2, ATTACK3, ATTACK4,
|
||||
@@ -22,7 +23,7 @@ public abstract class Fighter implements Disposable {
|
||||
DEATH
|
||||
}
|
||||
|
||||
// 动画帧间隔(秒)
|
||||
// 画帧间隔(秒)
|
||||
protected static final float DEFAULT_FRAME_DURATION = 0.1f;
|
||||
protected float[] frameDurations;
|
||||
|
||||
@@ -47,38 +48,35 @@ public abstract class Fighter implements Disposable {
|
||||
|
||||
// 精灵图表
|
||||
protected TextureAtlas atlas;
|
||||
// 缩放比例
|
||||
protected float scaleX = 3.0f;
|
||||
protected float scaleY = 3.0f;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Fighter(TextureAtlas atlas) {
|
||||
this.atlas = atlas;
|
||||
int actionCount = Action.values().length;
|
||||
|
||||
// 初始化动画数组和帧间隔数组
|
||||
animations = new Animation[actionCount];
|
||||
frameDurations = new float[actionCount];
|
||||
|
||||
// 设置默认帧间隔
|
||||
for (int i = 0; i < actionCount; i++) {
|
||||
frameDurations[i] = DEFAULT_FRAME_DURATION;
|
||||
}
|
||||
|
||||
// 初始化碰撞框
|
||||
hitbox = new Rectangle(0, 0, 64, 128);
|
||||
attackbox = new Rectangle(0, 0, 80, 80);
|
||||
|
||||
// 初始化默认属性(子类可以重写)
|
||||
speed = 300f;
|
||||
maxHealth = 100;
|
||||
health = maxHealth;
|
||||
attackPower = 10;
|
||||
|
||||
// 初始状态
|
||||
isFacingRight = true;
|
||||
currentAction = Action.IDLE;
|
||||
stateTime = 0;
|
||||
isAnimationFinished = false;
|
||||
|
||||
// 加载动画(由子类实现具体的动画帧)
|
||||
loadAnimations();
|
||||
}
|
||||
|
||||
@@ -97,25 +95,33 @@ public abstract class Fighter implements Disposable {
|
||||
*/
|
||||
protected void loadAnimationFromAtlas(Action action, String regionPrefix,
|
||||
int frameCount, boolean loop) {
|
||||
if (atlas == null) {
|
||||
throw new IllegalStateException("TextureAtlas 未初始化!");
|
||||
}
|
||||
if (frameCount <= 0) {
|
||||
throw new IllegalArgumentException("帧数必须大于0: " + frameCount);
|
||||
}
|
||||
|
||||
if (frameDurations == null || frameDurations.length <= action.ordinal()) {
|
||||
throw new IllegalStateException("frameDurations 未初始化或大小不足");
|
||||
}
|
||||
|
||||
Array<TextureRegion> frames = new Array<>();
|
||||
|
||||
// 从精灵图表中获取所有帧
|
||||
for (int i = 0; i < frameCount; i++) {
|
||||
// 生成带三位前导零的序号(000, 001, 002...)
|
||||
String formattedIndex = SimpleFormatter.addLeadingZeros(i, 3);
|
||||
// 拼接完整的区域名称(如"stand/stand" + "000" → "stand/stand000")
|
||||
String regionName = regionPrefix + formattedIndex;
|
||||
|
||||
TextureRegion region = atlas.findRegion(regionName);
|
||||
|
||||
if (region == null) {
|
||||
throw new IllegalArgumentException("精灵图表中未找到区域: " + regionName);
|
||||
throw new IllegalArgumentException("精灵图表中未找到区域: " + regionName +
|
||||
" (前缀: " + regionPrefix + ", 索引: " + i + ")");
|
||||
}
|
||||
|
||||
frames.add(region);
|
||||
}
|
||||
|
||||
// 创建动画
|
||||
Animation<TextureRegion> animation = new Animation<>(
|
||||
frameDurations[action.ordinal()],
|
||||
frames);
|
||||
@@ -129,7 +135,6 @@ public abstract class Fighter implements Disposable {
|
||||
protected void setFrameDuration(Action action, float duration) {
|
||||
frameDurations[action.ordinal()] = duration;
|
||||
|
||||
// 如果动画已加载,更新它
|
||||
if (animations[action.ordinal()] != null) {
|
||||
animations[action.ordinal()].setFrameDuration(duration);
|
||||
}
|
||||
@@ -142,10 +147,8 @@ public abstract class Fighter implements Disposable {
|
||||
stateTime += deltaTime;
|
||||
isAnimationFinished = animations[currentAction.ordinal()].isAnimationFinished(stateTime);
|
||||
|
||||
// 处理动画完成后的状态转换
|
||||
handleAnimationTransitions();
|
||||
|
||||
// 更新碰撞框位置
|
||||
updateHitboxes();
|
||||
}
|
||||
|
||||
@@ -155,7 +158,6 @@ public abstract class Fighter implements Disposable {
|
||||
protected void handleAnimationTransitions() {
|
||||
Animation<TextureRegion> currentAnim = animations[currentAction.ordinal()];
|
||||
|
||||
// 检查非循环动画是否已完成
|
||||
if (currentAnim.getPlayMode() != Animation.PlayMode.LOOP && isAnimationFinished) {
|
||||
switch (currentAction) {
|
||||
case ATTACK1:
|
||||
@@ -163,18 +165,14 @@ public abstract class Fighter implements Disposable {
|
||||
case ATTACK3:
|
||||
case SPECIAL1:
|
||||
case SPECIAL2:
|
||||
// 攻击动作完成后回到 idle
|
||||
changeAction(Action.IDLE);
|
||||
break;
|
||||
case HIT:
|
||||
// 受击后回到 idle
|
||||
changeAction(Action.IDLE);
|
||||
break;
|
||||
case JUMP:
|
||||
// 跳跃后进入下落状态
|
||||
changeAction(Action.FALL);
|
||||
break;
|
||||
// 其他默认转换逻辑
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,18 +181,45 @@ public abstract class Fighter implements Disposable {
|
||||
* 绘制角色
|
||||
*/
|
||||
public void render(SpriteBatch batch) {
|
||||
TextureRegion currentFrame = animations[currentAction.ordinal()].getKeyFrame(stateTime, true);
|
||||
Animation<TextureRegion> currentAnimation = animations[currentAction.ordinal()];
|
||||
if (currentAnimation == null) {
|
||||
Gdx.app.error("Fighter", "动画未初始化: " + currentAction);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean loop = currentAnimation.getPlayMode() == Animation.PlayMode.LOOP;
|
||||
TextureRegion currentFrame = currentAnimation.getKeyFrame(stateTime, loop);
|
||||
|
||||
if (currentFrame == null) {
|
||||
Gdx.app.error("Fighter", "动画帧为空: " + currentAction);
|
||||
return;
|
||||
}
|
||||
|
||||
// 绘制角色,考虑方向翻转
|
||||
float x = hitbox.x;
|
||||
float y = hitbox.y;
|
||||
float width = hitbox.width;
|
||||
float height = hitbox.height;
|
||||
|
||||
if (isFacingRight) {
|
||||
batch.draw(currentFrame, x, y, width, height);
|
||||
// 正向绘制并应用缩放
|
||||
batch.draw(
|
||||
currentFrame,
|
||||
x, y, // 位置
|
||||
width / 2, height / 2, // 缩放原点(中心)
|
||||
width, height, // 宽高
|
||||
scaleX, scaleY, // 缩放比例
|
||||
0 // 旋转角度
|
||||
);
|
||||
} else {
|
||||
batch.draw(currentFrame, x + width, y, -width, height);
|
||||
// 翻转绘制并应用缩放
|
||||
batch.draw(
|
||||
currentFrame,
|
||||
x, y, // 位置
|
||||
width / 2, height / 2, // 缩放原点(中心)
|
||||
width, height, // 宽高
|
||||
-scaleX, scaleY, // X轴翻转并应用缩放
|
||||
0 // 旋转角度
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,12 +227,10 @@ public abstract class Fighter implements Disposable {
|
||||
* 改变角色动作
|
||||
*/
|
||||
public boolean changeAction(Action newAction) {
|
||||
// 某些动作不能被打断
|
||||
if (isActionUninterruptible(currentAction)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果是新动作,重置状态时间
|
||||
if (currentAction != newAction) {
|
||||
currentAction = newAction;
|
||||
stateTime = 0;
|
||||
@@ -228,7 +251,6 @@ public abstract class Fighter implements Disposable {
|
||||
* 更新碰撞框位置
|
||||
*/
|
||||
protected void updateHitboxes() {
|
||||
// 根据角色朝向更新攻击框位置
|
||||
if (isFacingRight) {
|
||||
attackbox.setPosition(hitbox.x + hitbox.width - 10, hitbox.y + 20);
|
||||
} else {
|
||||
@@ -241,16 +263,12 @@ public abstract class Fighter implements Disposable {
|
||||
*/
|
||||
public void move(float x, float deltaTime) {
|
||||
if (x != 0) {
|
||||
// 改变朝向
|
||||
isFacingRight = x > 0;
|
||||
|
||||
// 移动位置
|
||||
hitbox.x += x * speed * deltaTime;
|
||||
|
||||
// 处理移动状态转换
|
||||
handleMoveState();
|
||||
} else if (currentAction == Action.WALK) {
|
||||
// 如果停止移动且当前是行走状态,切换到 idle
|
||||
changeAction(Action.IDLE);
|
||||
}
|
||||
}
|
||||
@@ -259,7 +277,6 @@ public abstract class Fighter implements Disposable {
|
||||
* 处理移动状态转换,子类可重写
|
||||
*/
|
||||
protected void handleMoveState() {
|
||||
// 如果不是攻击或特殊动作,切换到行走动画
|
||||
if (currentAction != Action.ATTACK1 && currentAction != Action.ATTACK2 &&
|
||||
currentAction != Action.ATTACK3 && currentAction != Action.SPECIAL1 &&
|
||||
currentAction != Action.SPECIAL2 && currentAction != Action.JUMP &&
|
||||
@@ -272,7 +289,6 @@ public abstract class Fighter implements Disposable {
|
||||
* 执行攻击
|
||||
*/
|
||||
public boolean attack(int attackType) {
|
||||
// 只能在允许攻击的状态下攻击
|
||||
if (!canAttack()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -3,24 +3,20 @@ package uno.mloluyu.desktop;
|
||||
import com.badlogic.gdx.ApplicationListener;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
|
||||
import uno.mloluyu.characters.Alice;
|
||||
|
||||
public class GameCore implements ApplicationListener {
|
||||
private SpriteBatch batch;
|
||||
private TextureAtlas atlas;
|
||||
private Alice alice1;
|
||||
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
batch = new SpriteBatch();
|
||||
atlas = new TextureAtlas(Gdx.files.internal("src\\main\\resources\\character\\alice\\alice.atlas"));
|
||||
alice1= new Alice(atlas);
|
||||
|
||||
alice1= new Alice();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,7 +9,7 @@ public class Launcher {
|
||||
public static void main(String[] args) {
|
||||
Lwjgl3ApplicationConfiguration configuration = new Lwjgl3ApplicationConfiguration();
|
||||
configuration.setTitle("Test Game");
|
||||
configuration.setWindowedMode(1200, 800);
|
||||
configuration.setWindowedMode(2600,1600);
|
||||
configuration.setForegroundFPS(60);
|
||||
configuration.useVsync(true);
|
||||
new Lwjgl3Application(new GameCore(), configuration);
|
||||
|
||||
|
Before Width: | Height: | Size: 4.0 MiB |
|
Before Width: | Height: | Size: 1.3 MiB |
BIN
src/main/resources/character/alice/精灵1.2-0.png
Normal file
|
After Width: | Height: | Size: 6.3 MiB |
BIN
src/main/resources/character/alice/精灵1.2-1.png
Normal file
|
After Width: | Height: | Size: 5.2 MiB |
BIN
src/main/resources/character/alice/精灵1.2-2.png
Normal file
|
After Width: | Height: | Size: 5.3 MiB |
BIN
src/main/resources/character/alice/精灵1.2-3.png
Normal file
|
After Width: | Height: | Size: 7.5 MiB |
BIN
src/main/resources/character/alice/精灵1.2-4.png
Normal file
|
After Width: | Height: | Size: 3.7 MiB |
BIN
src/main/resources/character/alice/精灵1.2-5.png
Normal file
|
After Width: | Height: | Size: 4.5 MiB |
BIN
src/main/resources/character/alice/精灵1.2-6.png
Normal file
|
After Width: | Height: | Size: 3.1 MiB |
BIN
src/main/resources/character/alice/精灵1.2-7.png
Normal file
|
After Width: | Height: | Size: 5.3 MiB |
BIN
src/main/resources/character/alice/精灵1.2-8.png
Normal file
|
After Width: | Height: | Size: 6.9 MiB |
BIN
src/main/resources/character/alice/精灵1.2-9.png
Normal file
|
After Width: | Height: | Size: 748 KiB |