perf: 优化

This commit is contained in:
richarjiang
2025-09-28 10:04:21 +08:00
parent e09c9a84cb
commit 1a6580e195
347 changed files with 6650 additions and 1203 deletions

View File

@@ -41,6 +41,7 @@ export class PlayerController extends Component {
private isUpgraded: boolean = false; // 玩家是否已升级
private isGameOver: boolean = false; // 游戏是否结束(玩家死亡)
private isWin: boolean = false; // 游戏是否胜利(到达终点)
private currentDirection: number = 5; // 当前玩家朝向3表示左/上5表示右/下默认为5
private hasWinTimes = 0
@@ -254,10 +255,12 @@ export class PlayerController extends Component {
if (absX > absY) {
// 水平移动为主
return deltaX > 0 ? 'walk3' : 'walk5';
this.currentDirection = deltaX < 0 ? 3 : 5;
return deltaX < 0 ? 'walk3' : 'walk5';
} else {
// 垂直移动为主
return deltaY > 0 ? 'walk3' : 'walk5'; // 上移用walk3下移用walk5
this.currentDirection = deltaY < 0 ? 3 : 5;
return deltaY < 0 ? 'walk3' : 'walk5'; // 上移用walk3下移用walk5
}
}
@@ -270,10 +273,16 @@ export class PlayerController extends Component {
return;
}
// 如果切换到站立动画,根据当前方向选择对应的站立动画
let targetAnimationName = animationName;
if (animationName === 'stand') {
targetAnimationName = this.currentDirection === 3 ? 'stand3' : 'stand5';
}
// 如果玩家已升级,在动画名称后添加 "_2" 后缀
let finalAnimationName = animationName;
if (this.isUpgraded && !animationName.endsWith('_2')) {
finalAnimationName = animationName + '_2';
let finalAnimationName = targetAnimationName;
if (this.isUpgraded && !targetAnimationName.endsWith('_2')) {
finalAnimationName = targetAnimationName + '_2';
}
if (this.currentAnimation === finalAnimationName) {
@@ -287,8 +296,8 @@ export class PlayerController extends Component {
const state = animation.getState(finalAnimationName);
if (!state) {
console.warn(`动画 ${finalAnimationName} 不存在,使用默认动画`);
this.currentAnimation = 'stand';
animation.play('stand');
this.currentAnimation = 'stand5';
animation.play('stand5');
return;
}
@@ -364,6 +373,8 @@ export class PlayerController extends Component {
update(deltaTime: number) {
// 更新逻辑现在主要由缓动系统处理
// 这里可以添加其他需要每帧更新的逻辑
}
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
@@ -417,7 +428,7 @@ export class PlayerController extends Component {
if (monsterAnimation) {
monsterAnimation.play(`${otherCollider.node.name}_attack`);
}
this.switchAnimation('attack');
this.switchAnimation(this.currentDirection === 3 ? 'attack3' : 'attack5');
// 2秒后判定攻击结果
this.scheduleOnce(async () => {
@@ -449,14 +460,17 @@ export class PlayerController extends Component {
this.scheduleOnce(() => {
otherCollider.node.destroy();
console.log('怪物已消失');
if (this.hasWinTimes === 10) {
this.isWin = true
this.showBonusPopup()
}
}, 1);
this.switchAnimation('stand'); // 玩家站立
if (this.hasWinTimes === 10) {
this.isWin = true
this.showBonusPopup()
}
} else {
// 怪物获胜
console.log('怪物获胜玩家生命值变为0');
@@ -587,9 +601,7 @@ export class PlayerController extends Component {
}, {
easing: 'quadOut',
onComplete: () => {
// 道具到达玩家位置后消失
prop.destroy();
console.log(`道具 ${prop.name} 已到达玩家位置并消失`);
prop.active = false;
resolve();
}
})

View File

@@ -0,0 +1,52 @@
import { _decorator, Component, Node, Graphics, Color, UITransform } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('Shadow2D')
export class Shadow2D extends Component {
@property(Node)
target: Node | null = null; // 角色节点
@property
shadowWidth: number = 60;
@property
shadowHeight: number = 20;
@property
shadowAlpha: number = 80; // 0-255
private shadowNode: Node | null = null;
start() {
if (!this.target) {
console.warn("Shadow2D: 未绑定角色节点 target");
return;
}
// 创建影子节点
this.shadowNode = new Node("Shadow");
let g = this.shadowNode.addComponent(Graphics);
this.target.addChild(this.shadowNode);
// 调整大小
let ui = this.shadowNode.addComponent(UITransform);
ui.setContentSize(this.shadowWidth, this.shadowHeight);
// 画椭圆
g.fillColor = new Color(0, 0, 0, this.shadowAlpha);
g.ellipse(0, 0, this.shadowWidth / 2, this.shadowHeight / 2);
g.fill();
// 放到角色脚下
this.shadowNode.setPosition(0, -this.target.getComponent(UITransform).height / 2);
}
update(dt: number) {
if (!this.shadowNode || !this.target) return;
// 始终保持在角色底部(比如角色动画高度变化时也能跟随)
let ui = this.target.getComponent(UITransform);
this.shadowNode.setPosition(0, -ui.height / 2);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "d627b8d3-b81d-4e6a-b9ac-f3d9629bdaba",
"files": [],
"subMetas": {},
"userData": {}
}