添加角色类Alice,继承自Fighter,设置属性和动画,

This commit is contained in:
2025-09-21 22:20:07 +08:00
parent 3b9d794163
commit 87cfe5aed6
7 changed files with 95 additions and 90 deletions

View File

@@ -1,5 +1,5 @@
package uno.mloluyu.desktop.character; package uno.mloluyu.characters;
import uno.mloluyu.characters.Fighter;
import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureAtlas;
/** /**
@@ -17,27 +17,27 @@ public class Alice extends Fighter {
@Override @Override
protected void loadAnimations() { protected void loadAnimations() {
loadAnimationFromAtlas(Action.IDLE, "stand", 15, true); loadAnimationFromAtlas(Action.IDLE, "stand/stand", 15, true);
loadAnimationFromAtlas(Action.WALK, "walkFront", 9, true); loadAnimationFromAtlas(Action.WALK, "walkFront/walkFront", 9, true);
loadAnimationFromAtlas(Action.JUMP, "jump", 8, false); loadAnimationFromAtlas(Action.JUMP, "jump/jump", 8, false);
loadAnimationFromAtlas(Action.FALL, "hitSpin", 5, false); loadAnimationFromAtlas(Action.FALL, "hitSpin/hitSpin", 5, false);
loadAnimationFromAtlas(Action.ATTACK1, "attackAa", 6, false); loadAnimationFromAtlas(Action.ATTACK1, "attackAa/attackAa", 6, false);
loadAnimationFromAtlas(Action.ATTACK2, "attackAb", 6, false); loadAnimationFromAtlas(Action.ATTACK2, "attackAb/attackAb", 6, false);
loadAnimationFromAtlas(Action.ATTACK3, "attackAc", 6, false); loadAnimationFromAtlas(Action.ATTACK3, "attackAc/attackAc", 6, false);
loadAnimationFromAtlas(Action.ATTACK4, "attackAd", 6, false); loadAnimationFromAtlas(Action.ATTACK4, "attackAd/attackAd", 6, false);
loadAnimationFromAtlas(Action.HIT, "hitSpin", 5, false); loadAnimationFromAtlas(Action.HIT, "hitSpin/hitSpin", 5, false);
// 忍者特定动作设置帧间隔 // 为特定动作设置帧间隔
setFrameDuration(Action.WALK, 0.08f); // 行走更快 setFrameDuration(Action.WALK, 0.08f); // 行走更快
setFrameDuration(Action.ATTACK1, 0.07f); // 攻击更快 setFrameDuration(Action.ATTACK1, 0.07f); // 攻击更快
setFrameDuration(Action.SPECIAL2, 0.06f); // 特殊技能2非常快 setFrameDuration(Action.SPECIAL2, 0.06f); // 特殊技能2非常快
} }
// 忍者特定的移动状态处理 // 特定的移动状态处理
@Override @Override
protected void handleMoveState() { protected void handleMoveState() {
// 忍者在跳跃时也能移动 // 在跳跃时也能移动
if (currentAction != Action.ATTACK1 && currentAction != Action.ATTACK2 && if (currentAction != Action.ATTACK1 && currentAction != Action.ATTACK2 &&
currentAction != Action.ATTACK3 && currentAction != Action.SPECIAL1 && currentAction != Action.ATTACK3 && currentAction != Action.SPECIAL1 &&
currentAction != Action.SPECIAL2 && currentAction != Action.DEFEND) { currentAction != Action.SPECIAL2 && currentAction != Action.DEFEND) {
@@ -48,10 +48,9 @@ public class Alice extends Fighter {
} }
} }
// 忍者可以在空中攻击 // 可以在空中攻击
@Override @Override
protected boolean canAttack() { protected boolean canAttack() {
return super.canAttack() || currentAction == Action.JUMP || currentAction == Action.FALL; return super.canAttack() || currentAction == Action.JUMP || currentAction == Action.FALL;
} }
} }

View File

@@ -1,5 +1,6 @@
package uno.mloluyu.characters; package uno.mloluyu.characters;
import uno.mloluyu.util.SimpleFormatter;
import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureAtlas;
@@ -88,6 +89,7 @@ public abstract class Fighter implements Disposable {
/** /**
* 从精灵图表加载动画 * 从精灵图表加载动画
*
* @param action 动作类型 * @param action 动作类型
* @param regionPrefix 该动作在图表中的区域前缀 * @param regionPrefix 该动作在图表中的区域前缀
* @param frameCount 帧数 * @param frameCount 帧数
@@ -99,7 +101,11 @@ public abstract class Fighter implements Disposable {
// 从精灵图表中获取所有帧 // 从精灵图表中获取所有帧
for (int i = 0; i < frameCount; i++) { for (int i = 0; i < frameCount; i++) {
String regionName = regionPrefix + i; // 生成带三位前导零的序号000, 001, 002...
String formattedIndex = SimpleFormatter.addLeadingZeros(i, 3);
// 拼接完整的区域名称(如"stand/stand" + "000" → "stand/stand000"
String regionName = regionPrefix + formattedIndex;
TextureRegion region = atlas.findRegion(regionName); TextureRegion region = atlas.findRegion(regionName);
if (region == null) { if (region == null) {
@@ -112,8 +118,7 @@ public abstract class Fighter implements Disposable {
// 创建动画 // 创建动画
Animation<TextureRegion> animation = new Animation<>( Animation<TextureRegion> animation = new Animation<>(
frameDurations[action.ordinal()], frameDurations[action.ordinal()],
frames frames);
);
animation.setPlayMode(loop ? Animation.PlayMode.LOOP : Animation.PlayMode.NORMAL); animation.setPlayMode(loop ? Animation.PlayMode.LOOP : Animation.PlayMode.NORMAL);
animations[action.ordinal()] = animation; animations[action.ordinal()] = animation;
} }
@@ -350,4 +355,3 @@ public abstract class Fighter implements Disposable {
public void dispose() { public void dispose() {
} }
} }

View File

@@ -7,7 +7,7 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import uno.mloluyu.desktop.character.Alice; import uno.mloluyu.characters.Alice;
public class GameCore implements ApplicationListener { public class GameCore implements ApplicationListener {
private SpriteBatch batch; private SpriteBatch batch;
@@ -17,8 +17,9 @@ public class GameCore implements ApplicationListener {
@Override @Override
public void create() { public void create() {
atlas = new TextureAtlas(Gdx.files.internal("characters/alice.atlas")); batch = new SpriteBatch();
alice1= new Alice(); atlas = new TextureAtlas(Gdx.files.internal("src\\main\\resources\\character\\alice\\alice.atlas"));
alice1= new Alice(atlas);
} }
@@ -31,6 +32,7 @@ public class GameCore implements ApplicationListener {
alice1.update(Gdx.graphics.getDeltaTime()); alice1.update(Gdx.graphics.getDeltaTime());
batch.begin(); batch.begin();
alice1.render(batch); alice1.render(batch);
batch.end();
} }
@Override @Override