更改人物父类
This commit is contained in:
@@ -15,7 +15,7 @@ public abstract class Fighter implements Disposable {
|
|||||||
// 动作类型枚举 - 所有角色共用的基础动作
|
// 动作类型枚举 - 所有角色共用的基础动作
|
||||||
public enum Action {
|
public enum Action {
|
||||||
IDLE, WALK, JUMP, FALL,
|
IDLE, WALK, JUMP, FALL,
|
||||||
ATTACK1, ATTACK2, ATTACK3,
|
ATTACK1, ATTACK2, ATTACK3,ATTACK4,
|
||||||
HIT, DEFEND,
|
HIT, DEFEND,
|
||||||
SPECIAL1, SPECIAL2,
|
SPECIAL1, SPECIAL2,
|
||||||
DEATH
|
DEATH
|
||||||
|
|||||||
@@ -5,18 +5,20 @@ import com.badlogic.gdx.Gdx;
|
|||||||
import com.badlogic.gdx.graphics.GL20;
|
import com.badlogic.gdx.graphics.GL20;
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
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 uno.mloluyu.desktop.character.Alice;
|
import uno.mloluyu.desktop.character.Alice;
|
||||||
|
|
||||||
public class GameCore implements ApplicationListener {
|
public class GameCore implements ApplicationListener {
|
||||||
private SpriteBatch batch;
|
private SpriteBatch batch;
|
||||||
private Texture img;
|
private TextureAtlas atlas;
|
||||||
private Alice alice1 = new Alice();
|
private Alice alice1;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create() {
|
public void create() {
|
||||||
alice1.create();
|
atlas = new TextureAtlas(Gdx.files.internal("characters/alice.atlas"));
|
||||||
|
alice1= new Alice();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,8 +27,10 @@ public class GameCore implements ApplicationListener {
|
|||||||
Gdx.gl.glClearColor(150, 150, 150, 1);
|
Gdx.gl.glClearColor(150, 150, 150, 1);
|
||||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
|
||||||
alice1.update(Gdx.graphics.getDeltaTime());
|
alice1.update(Gdx.graphics.getDeltaTime());
|
||||||
alice1.render();
|
batch.begin();
|
||||||
|
alice1.render(batch);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,179 +1,57 @@
|
|||||||
package uno.mloluyu.desktop.character;
|
package uno.mloluyu.desktop.character;
|
||||||
|
import uno.mloluyu.characters.Fighter;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||||
|
|
||||||
import uno.mloluyu.util.SimpleFormatter;
|
/**
|
||||||
|
* 角色类,继承自Fighter父类
|
||||||
|
*/
|
||||||
|
public class Alice extends Fighter {
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
public Alice(TextureAtlas atlas) {
|
||||||
import com.badlogic.gdx.Input;
|
super(atlas);
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
speed = 350f; // 速度更快
|
||||||
import com.badlogic.gdx.graphics.g2d.Animation;
|
maxHealth = 90; // 生命值较低
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
health = maxHealth;
|
||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
attackPower = 12; // 攻击力中等
|
||||||
import com.badlogic.gdx.math.Vector2;
|
|
||||||
import com.badlogic.gdx.utils.Array;
|
|
||||||
|
|
||||||
public class Alice {
|
|
||||||
private SpriteBatch batch;
|
|
||||||
private Animation<TextureRegion> walkAnimation; // 行走动画
|
|
||||||
private Animation<TextureRegion> idleAnimation; // idle动画
|
|
||||||
private TextureRegion currentFrame; // 当前显示的帧
|
|
||||||
private float stateTime; // 动画播放时间
|
|
||||||
|
|
||||||
private String path = "character/alice/";
|
|
||||||
private final float MOVEMENT_SPEED = 200f;
|
|
||||||
private Vector2 position;
|
|
||||||
private float width; // 角色宽度
|
|
||||||
private float height; // 角色高度
|
|
||||||
private boolean isMoving = false; // 是否在移动
|
|
||||||
private boolean isFacingRight = true; // 是否面朝右
|
|
||||||
|
|
||||||
public void create() {
|
|
||||||
batch = new SpriteBatch();
|
|
||||||
position = new Vector2();
|
|
||||||
|
|
||||||
// 加载动画帧
|
|
||||||
loadAnimations();
|
|
||||||
|
|
||||||
// 初始化位置(屏幕中心)
|
|
||||||
setToCenter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void loadAnimations() {
|
||||||
|
loadAnimationFromAtlas(Action.IDLE, "stand", 15, true);
|
||||||
|
loadAnimationFromAtlas(Action.WALK, "walkFront", 9, true);
|
||||||
|
loadAnimationFromAtlas(Action.JUMP, "jump", 8, false);
|
||||||
|
loadAnimationFromAtlas(Action.FALL, "hitSpin", 5, false);
|
||||||
|
loadAnimationFromAtlas(Action.ATTACK1, "attackAa", 6, false);
|
||||||
|
loadAnimationFromAtlas(Action.ATTACK2, "attackAb", 6, false);
|
||||||
|
loadAnimationFromAtlas(Action.ATTACK3, "attackAc", 6, false);
|
||||||
|
loadAnimationFromAtlas(Action.ATTACK4, "attackAd", 6, false);
|
||||||
|
|
||||||
/**
|
loadAnimationFromAtlas(Action.HIT, "hitSpin", 5, false);
|
||||||
* 加载动画帧序列
|
|
||||||
*/
|
// 为忍者特定动作设置帧间隔
|
||||||
private void loadAnimations() {
|
setFrameDuration(Action.WALK, 0.08f); // 行走更快
|
||||||
try {
|
setFrameDuration(Action.ATTACK1, 0.07f); // 攻击更快
|
||||||
// 1. 加载静止动画
|
setFrameDuration(Action.SPECIAL2, 0.06f); // 特殊技能2非常快
|
||||||
Array<TextureRegion> idleFrames = new Array<>();
|
|
||||||
for (int i = 0; i < 16; i++) {
|
|
||||||
Texture texture = new Texture(Gdx.files.internal(path + "stand" + SimpleFormatter.addLeadingZeros(i,3) + ".png")); //文件路径
|
|
||||||
System.out.println(texture);
|
|
||||||
idleFrames.add(new TextureRegion(texture));
|
|
||||||
}
|
|
||||||
|
|
||||||
idleAnimation = new Animation<>(0.1f, idleFrames, Animation.PlayMode.LOOP); // 每帧时间
|
|
||||||
|
|
||||||
// 2. 加载行走动画
|
|
||||||
Array<TextureRegion> walkFrames = new Array<>();
|
|
||||||
for (int i = 0; i < 5; i++) {
|
|
||||||
Texture texture = new Texture(Gdx.files.internal(path + "walkFront" + SimpleFormatter.addLeadingZeros(i,3) + ".png"));
|
|
||||||
System.out.println("调试代码"+texture);
|
|
||||||
walkFrames.add(new TextureRegion(texture));
|
|
||||||
}
|
|
||||||
walkAnimation = new Animation<>(0.1f, walkFrames, Animation.PlayMode.LOOP);
|
|
||||||
|
|
||||||
// 设置角色尺寸
|
|
||||||
width = idleFrames.get(0).getRegionWidth();
|
|
||||||
height = idleFrames.get(0).getRegionHeight();
|
|
||||||
|
|
||||||
Gdx.app.log("Alice", "动画加载成功");
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
Gdx.app.error("Alice", "动画加载失败: " + e.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// 忍者特定的移动状态处理
|
||||||
* 设置角色到屏幕中心
|
@Override
|
||||||
*/
|
protected void handleMoveState() {
|
||||||
private void setToCenter() {
|
// 忍者在跳跃时也能移动
|
||||||
float x = (Gdx.graphics.getWidth() - width) / 2f;
|
if (currentAction != Action.ATTACK1 && currentAction != Action.ATTACK2 &&
|
||||||
float y = (Gdx.graphics.getHeight() - height) / 2f;
|
currentAction != Action.ATTACK3 && currentAction != Action.SPECIAL1 &&
|
||||||
position.set(x, y);
|
currentAction != Action.SPECIAL2 && currentAction != Action.DEFEND) {
|
||||||
}
|
|
||||||
|
if (currentAction != Action.JUMP && currentAction != Action.FALL) {
|
||||||
/**
|
changeAction(Action.WALK);
|
||||||
* 更新逻辑和动画
|
|
||||||
*/
|
|
||||||
public void update(float deltaTime) {
|
|
||||||
if (idleAnimation == null) return;
|
|
||||||
|
|
||||||
stateTime += deltaTime;
|
|
||||||
|
|
||||||
handleMovement(deltaTime);
|
|
||||||
|
|
||||||
if (isMoving) {
|
|
||||||
currentFrame = walkAnimation.getKeyFrame(stateTime);
|
|
||||||
} else {
|
|
||||||
currentFrame = idleAnimation.getKeyFrame(stateTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((isFacingRight && currentFrame.isFlipX()) ||
|
|
||||||
(!isFacingRight && !currentFrame.isFlipX())) {
|
|
||||||
currentFrame.flip(true, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理移动逻辑
|
|
||||||
*/
|
|
||||||
private void handleMovement(float deltaTime) {
|
|
||||||
float moveX = 0;
|
|
||||||
float moveY = 0;
|
|
||||||
isMoving = false;
|
|
||||||
|
|
||||||
// 方向键控制
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
|
|
||||||
moveY += MOVEMENT_SPEED * deltaTime;
|
|
||||||
isMoving = true;
|
|
||||||
}
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
|
|
||||||
moveY -= MOVEMENT_SPEED * deltaTime;
|
|
||||||
isMoving = true;
|
|
||||||
}
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
|
|
||||||
moveX += MOVEMENT_SPEED * deltaTime;
|
|
||||||
isMoving = true;
|
|
||||||
isFacingRight = true; // 朝右
|
|
||||||
}
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
|
|
||||||
moveX -= MOVEMENT_SPEED * deltaTime;
|
|
||||||
isMoving = true;
|
|
||||||
isFacingRight = false; // 朝左
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新位置并限制在屏幕内
|
|
||||||
position.add(moveX, moveY);
|
|
||||||
clampPosition();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 限制在屏幕范围内
|
|
||||||
*/
|
|
||||||
private void clampPosition() {
|
|
||||||
float maxX = Gdx.graphics.getWidth() - width;
|
|
||||||
float maxY = Gdx.graphics.getHeight() - height;
|
|
||||||
position.x = Math.max(0, Math.min(position.x, maxX));
|
|
||||||
position.y = Math.max(0, Math.min(position.y, maxY));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 渲染角色和动画
|
|
||||||
*/
|
|
||||||
public void render() {
|
|
||||||
if (batch == null || currentFrame == null) return;
|
|
||||||
batch.begin();
|
|
||||||
batch.draw(currentFrame, position.x, position.y);
|
|
||||||
batch.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 释放资源
|
|
||||||
*/
|
|
||||||
public void dispose() {
|
|
||||||
if (batch != null) {
|
|
||||||
batch.dispose();
|
|
||||||
batch = null;
|
|
||||||
}
|
|
||||||
if (idleAnimation != null) {
|
|
||||||
for (TextureRegion region : idleAnimation.getKeyFrames()) {
|
|
||||||
region.getTexture().dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (walkAnimation != null) {
|
|
||||||
for (TextureRegion region : walkAnimation.getKeyFrames()) {
|
|
||||||
region.getTexture().dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 忍者可以在空中攻击
|
||||||
|
@Override
|
||||||
|
protected boolean canAttack() {
|
||||||
|
return super.canAttack() || currentAction == Action.JUMP || currentAction == Action.FALL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user