新增角色资源并更新战斗控制器
新增爱丽丝角色的PNG素材资源 引入用于管理战斗动作的战斗控制器类 移除旧版路径中的过时战斗控制器类 新增爱丽丝动画测试类用于验证动画效果 更新战斗角色类并新增多项功能 调整游戏核心类以集成新角色机制
This commit is contained in:
117
src/main/java/uno/mloluyu/characters/AliceAnimationTest.java
Normal file
117
src/main/java/uno/mloluyu/characters/AliceAnimationTest.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package uno.mloluyu.characters;
|
||||
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
|
||||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||
import com.badlogic.gdx.utils.viewport.Viewport;
|
||||
|
||||
import uno.mloluyu.characters.Alice;
|
||||
import uno.mloluyu.characters.Fighter;
|
||||
|
||||
public class AliceAnimationTest extends ApplicationAdapter {
|
||||
|
||||
private SpriteBatch batch;
|
||||
private OrthographicCamera camera;
|
||||
private Viewport viewport;
|
||||
|
||||
private Alice alice;
|
||||
|
||||
private float stateTimer; // 用于切换动作测试
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
// 初始化相机和批处理
|
||||
camera = new OrthographicCamera();
|
||||
viewport = new FitViewport(800, 600, camera);
|
||||
batch = new SpriteBatch();
|
||||
|
||||
// 创建 Alice 实例
|
||||
alice = new Alice();
|
||||
|
||||
// 初始位置
|
||||
alice.getHitbox().setPosition(
|
||||
viewport.getWorldWidth() / 2 - alice.getHitbox().width / 2,
|
||||
viewport.getWorldHeight() / 2 - alice.getHitbox().height / 2
|
||||
);
|
||||
|
||||
stateTimer = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
// 清屏
|
||||
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// 更新状态时间
|
||||
float delta = Gdx.graphics.getDeltaTime();
|
||||
stateTimer += delta;
|
||||
|
||||
// 控制 Alice 执行不同动作,测试动画切换
|
||||
controlAliceActions();
|
||||
|
||||
// 更新 Alice
|
||||
alice.update(delta);
|
||||
|
||||
// 绘制
|
||||
batch.setProjectionMatrix(camera.combined);
|
||||
batch.begin();
|
||||
alice.render(batch);
|
||||
batch.end();
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动切换 Alice 动作,测试各种动画
|
||||
*/
|
||||
private void controlAliceActions() {
|
||||
// 每2秒切换一个动作
|
||||
if (stateTimer < 2) {
|
||||
alice.changeAction(Fighter.Action.IDLE);
|
||||
} else if (stateTimer < 4) {
|
||||
alice.move(1, Gdx.graphics.getDeltaTime()); // 向右走
|
||||
} else if (stateTimer < 6) {
|
||||
alice.changeAction(Fighter.Action.JUMP);
|
||||
} else if (stateTimer < 8) {
|
||||
alice.attack(1); // 普通攻击1
|
||||
} else if (stateTimer < 10) {
|
||||
alice.attack(2); // 普通攻击2
|
||||
} else if (stateTimer < 12) {
|
||||
alice.attack(3); // 普通攻击3
|
||||
} else if (stateTimer < 14) {
|
||||
alice.takeHit(10); // 受击
|
||||
} else {
|
||||
// 循环
|
||||
stateTimer = 0;
|
||||
alice.getHitbox().setPosition(
|
||||
viewport.getWorldWidth() / 2 - alice.getHitbox().width / 2,
|
||||
viewport.getWorldHeight() / 2 - alice.getHitbox().height / 2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
viewport.update(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
batch.dispose();
|
||||
alice.dispose();
|
||||
}
|
||||
|
||||
// 直接运行这个 main 方法即可启动测试
|
||||
// public static void main(String[] args) {
|
||||
// Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
|
||||
// config.setTitle("Alice Animation Test");
|
||||
// config.setWindowedMode(800, 600);
|
||||
// config.setForegroundFPS(60);
|
||||
// config.useVsync(true);
|
||||
// new Lwjgl3Application(new AliceAnimationTest(), config);
|
||||
// }
|
||||
}
|
||||
@@ -49,8 +49,8 @@ public abstract class Fighter implements Disposable {
|
||||
// 精灵图表
|
||||
protected TextureAtlas atlas;
|
||||
// 缩放比例
|
||||
protected float scaleX = 3.0f;
|
||||
protected float scaleY = 3.0f;
|
||||
protected float scaleX = 1.0f;
|
||||
protected float scaleY = 1.0f;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Fighter(TextureAtlas atlas) {
|
||||
@@ -195,32 +195,38 @@ public abstract class Fighter implements Disposable {
|
||||
return;
|
||||
}
|
||||
|
||||
float x = hitbox.x;
|
||||
float y = hitbox.y;
|
||||
float width = hitbox.width;
|
||||
float height = hitbox.height;
|
||||
// 1. 计算缩放后的帧尺寸(保持原始比例)
|
||||
float frameWidth = currentFrame.getRegionWidth() * scaleX;
|
||||
float frameHeight = currentFrame.getRegionHeight() * scaleY;
|
||||
|
||||
if (isFacingRight) {
|
||||
// 正向绘制并应用缩放
|
||||
batch.draw(
|
||||
currentFrame,
|
||||
x, y, // 位置
|
||||
width / 2, height / 2, // 缩放原点(中心)
|
||||
width, height, // 宽高
|
||||
scaleX, scaleY, // 缩放比例
|
||||
0 // 旋转角度
|
||||
);
|
||||
} else {
|
||||
// 翻转绘制并应用缩放
|
||||
batch.draw(
|
||||
currentFrame,
|
||||
x, y, // 位置
|
||||
width / 2, height / 2, // 缩放原点(中心)
|
||||
width, height, // 宽高
|
||||
-scaleX, scaleY, // X轴翻转并应用缩放
|
||||
0 // 旋转角度
|
||||
);
|
||||
}
|
||||
// 2. 计算绘制位置:始终以hitbox为基准,水平居中、底部对齐(关键!)
|
||||
// 无论是否翻转,x/y坐标都基于hitbox计算,保证位置锚点一致
|
||||
float drawX = hitbox.x + (hitbox.width - frameWidth) / 2; // 水平居中(hitbox中心和帧中心对齐)
|
||||
float drawY = hitbox.y; // 底部对齐(hitbox底部和帧底部对齐)
|
||||
|
||||
// 3. 处理翻转:用TextureRegion的flip方法,避免手动偏移x坐标
|
||||
// 先记录原始flip状态(防止影响其他地方复用该帧)
|
||||
boolean wasFlippedX = currentFrame.isFlipX();
|
||||
// 根据朝向设置翻转(只翻转X轴,Y轴不变)
|
||||
currentFrame.flip(!isFacingRight && !wasFlippedX, false); // 正向→不翻,反向→翻
|
||||
currentFrame.flip(isFacingRight && wasFlippedX, false); // 修复原始已翻转的情况
|
||||
|
||||
// 4. 绘制:缩放中心为帧的中心,确保翻转/旋转时围绕自身中心
|
||||
batch.draw(
|
||||
currentFrame,
|
||||
drawX, // 绘制X(基于hitbox的居中位置,固定不变)
|
||||
drawY, // 绘制Y(基于hitbox的底部,固定不变)
|
||||
frameWidth / 2, // 缩放/旋转中心X(帧的中心)
|
||||
frameHeight / 2, // 缩放/旋转中心Y(帧的中心)
|
||||
frameWidth, // 缩放后的宽度(已乘scaleX)
|
||||
frameHeight, // 缩放后的高度(已乘scaleY)
|
||||
1f, // X轴额外缩放(这里已提前计算,设为1避免重复缩放)
|
||||
1f, // Y轴额外缩放(同上)
|
||||
0f // 旋转角度
|
||||
);
|
||||
|
||||
// 5. 恢复帧的原始flip状态(关键!避免影响后续绘制)
|
||||
currentFrame.flip(wasFlippedX != currentFrame.isFlipX(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user