feat: 引入新的素材

This commit is contained in:
richarjiang
2025-09-22 16:04:42 +08:00
parent d6aa74cb9d
commit 1f0771b271
92 changed files with 5318 additions and 216 deletions

View File

@@ -1,4 +1,4 @@
import { _decorator, Component, Node, Vec3, input, Input, EventTouch, Camera, view, tween } from 'cc';
import { _decorator, Component, Node, Vec3, input, Input, EventTouch, Camera, view, tween, Animation } from 'cc';
import { TiledMapPathfinder } from './TiledMapPathfinder';
const { ccclass, property } = _decorator;
@@ -27,6 +27,8 @@ export class PlayerController extends Component {
private currentPath: Vec3[] = [];
private currentPathIndex: number = 0;
private originalPosition: Vec3 = new Vec3();
private currentAnimation: string = 'stand'; // 当前播放的动画
private lastTargetPosition: Vec3 = new Vec3(); // 上一个目标位置,用于方向判断
onLoad() {
// 注册触摸事件
@@ -138,12 +140,74 @@ export class PlayerController extends Component {
return clampedPosition;
}
/**
* 根据移动方向获取对应的动画名称
*/
private getAnimationNameByDirection(currentPos: Vec3, targetPos: Vec3): string {
const deltaX = targetPos.x - currentPos.x;
const deltaY = targetPos.y - currentPos.y;
// 如果移动距离很小,保持当前动画
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance < 1) {
return this.currentAnimation;
}
// 计算主要移动方向
const absX = Math.abs(deltaX);
const absY = Math.abs(deltaY);
// 添加角度判断,更精确地确定方向
const angle = Math.atan2(deltaY, deltaX) * 180 / Math.PI; // 转换为角度
if (absX > absY) {
// 水平移动为主
return deltaX > 0 ? 'walk3' : 'walk5';
} else {
// 垂直移动为主
return deltaY > 0 ? 'walk3' : 'walk5'; // 上移用walk3下移用walk5
}
}
/**
* 切换动画,避免不必要的切换
*/
private switchAnimation(animationName: string) {
if (!this.player) {
console.warn('Player节点未设置无法切换动画');
return;
}
if (this.currentAnimation === animationName) {
return; // 已经是目标动画,不需要切换
}
const animation = this.player.getComponent(Animation);
if (animation) {
// 检查动画是否存在
const state = animation.getState(animationName);
if (!state) {
console.warn(`动画 ${animationName} 不存在,使用默认动画`);
this.currentAnimation = 'stand';
animation.play('stand');
return;
}
this.currentAnimation = animationName;
animation.play(animationName);
console.log(`切换动画: ${animationName}`);
} else {
console.warn('未找到Animation组件无法播放动画');
}
}
/**
* 移动到路径中的下一个路径点
*/
private moveToNextWaypoint() {
if (!this.player || this.currentPath.length === 0 || this.currentPathIndex >= this.currentPath.length) {
this.isMoving = false;
this.player.getComponent(Animation)?.play('stand');
console.log('路径移动完成');
return;
}
@@ -151,12 +215,21 @@ export class PlayerController extends Component {
const targetPos = this.currentPath[this.currentPathIndex];
const currentPos = this.player.position;
// 根据移动方向选择动画
const animationName = this.getAnimationNameByDirection(currentPos, targetPos);
// 切换到对应的动画
this.switchAnimation(animationName);
// 计算移动距离和时间
const distance = Vec3.distance(currentPos, targetPos);
const moveTime = distance / this.moveSpeed;
console.log(`移动到路径点${this.currentPathIndex}: (${targetPos.x.toFixed(2)}, ${targetPos.y.toFixed(2)})`);
// 记录目标位置用于方向判断
this.lastTargetPosition.set(targetPos);
// 使用缓动移动到目标位置
tween(this.player)
.to(moveTime, { position: targetPos }, {
@@ -178,6 +251,9 @@ export class PlayerController extends Component {
this.isMoving = false;
this.currentPath = [];
this.currentPathIndex = 0;
// 停止移动时播放站立动画
this.switchAnimation('stand');
}
update(deltaTime: number) {