diff --git a/tgx-games-client/assets/level1/Script/WoodenManPlayer.ts b/tgx-games-client/assets/level1/Script/WoodenManPlayer.ts index cea39d8..789008e 100644 --- a/tgx-games-client/assets/level1/Script/WoodenManPlayer.ts +++ b/tgx-games-client/assets/level1/Script/WoodenManPlayer.ts @@ -1,6 +1,15 @@ import { _decorator, Component, Node, SkeletalAnimation, Input, EventTouch, input, Label } from 'cc'; const { ccclass, property } = _decorator; +/** 玩家状态 */ +enum EPlayerState { + IDLE, + WALK, + RUN, + DIE, + VICTORY +} + @ccclass('WoodenManPlayer') export class WoodenManPlayer extends Component { @@ -15,6 +24,7 @@ export class WoodenManPlayer extends Component { private readonly WALK_SPEED = 3; // 走路速度 private readonly RUN_SPEED = 6; // 跑步速度 private isStoppingMove: boolean = false; // 添加在类的开头 + private _playerState: EPlayerState = EPlayerState.IDLE; @property(Node) @@ -29,13 +39,12 @@ export class WoodenManPlayer extends Component { this.id = id; this.isLocalPlayer = isLocal; this.animator = this.getComponentInChildren(SkeletalAnimation); - if (!this.animator) { console.error(`Player ${id} 缺少 SkeletalAnimation 组件`); } else { - const idleState = this.animator.getState('player_idle'); - if (idleState) { - this.animator.crossFade('player_idle', 0.1); + const idleState = this.animator.getState('player_idle1'); + if (idleState && this._playerState === EPlayerState.IDLE) { + this.animator.crossFade('player_idle1', 0.1); } } } @@ -57,7 +66,8 @@ export class WoodenManPlayer extends Component { if (!alive) { // 死亡时停止所有动画和移动 this.isMoving = false; - if (this.animator) { + this._playerState = EPlayerState.DIE; + if (this._getAnimator()) { this.animator.stop(); // 停止当前动画 } } @@ -72,10 +82,11 @@ export class WoodenManPlayer extends Component { if (this.hasPlayedDieAnimation) return; // 先停止当前动画 - if (this.animator) { + if (this._getAnimator()) { this.animator.stop(); const state = this.animator.getState('player_die'); if (state) { + state.wrapMode = 1; // 1 表示只播放一次 this.animator.crossFade('player_die', 0.5); if (callback) { state.once(SkeletalAnimation.EventType.FINISHED, callback); @@ -95,10 +106,10 @@ export class WoodenManPlayer extends Component { // 播放行走动画 playWalkAnimation() { - if (this.animator) { - // 使用 play 而不是 crossFade,并设置 wrapMode 为循环 + if (this._getAnimator()) { const state = this.animator.getState('player_walk'); if (state) { + this._playerState = EPlayerState.WALK; state.wrapMode = 2; // 2 表示循环模式 this.animator.play('player_walk'); } @@ -107,10 +118,10 @@ export class WoodenManPlayer extends Component { // 播放跑步动画 playRunAnimation() { - if (this.animator) { - // 使用 play 而不是 crossFade,并设置 wrapMode 为循环 + if (this._getAnimator()) { const state = this.animator.getState('player_run'); if (state) { + this._playerState = EPlayerState.RUN; state.wrapMode = 2; // 2 表示循环模式 this.animator.play('player_run'); } @@ -124,8 +135,9 @@ export class WoodenManPlayer extends Component { this.isStoppingMove = true; // 设置标志 this.isMoving = false; this.moveTimer = 0; - if (this.animator) { - this.animator.crossFade('player_idle', 0.3); + this._playerState = EPlayerState.IDLE; + if (this._getAnimator()) { + this.animator.crossFade('player_idle1', 0.3); } // 同步按钮状态 if (this.isMovingByButton && this.buttonLabel) { @@ -155,13 +167,24 @@ export class WoodenManPlayer extends Component { } } + private _getAnimator() { + if (!this.animator) { + this.animator = this.getComponentInChildren(SkeletalAnimation); + if (!this.animator) { + console.error(`Player ${this.id} 缺少 SkeletalAnimation 组件`); + } + } + return this.animator; + + } + public startMoving() { if (!this.isAlive() || this.isMoving) return; this.isMoving = true; this.moveTimer = 0; // 重置计时器 - if (this.animator) { + if (this._getAnimator()) { this.playWalkAnimation(); } } @@ -171,7 +194,8 @@ export class WoodenManPlayer extends Component { this.moveTimer += dt; // 根据移动时间切换动画 - if (this.moveTimer >= 1 && this.animator) { + if (this.moveTimer >= 1 && this._getAnimator() && this._playerState !== EPlayerState.RUN) { + this._playerState = EPlayerState.RUN; this.animator.getState('player_run').wrapMode = 2; this.animator.crossFade('player_run', 0.3); } diff --git a/tgx-games-client/assets/module_arean/common/role/Character001.prefab b/tgx-games-client/assets/module_arean/common/role/Character001.prefab index 5d7b5b1..8504af7 100644 --- a/tgx-games-client/assets/module_arean/common/role/Character001.prefab +++ b/tgx-games-client/assets/module_arean/common/role/Character001.prefab @@ -564,7 +564,7 @@ "propertyPath": [ "playOnLoad" ], - "value": false + "value": true }, { "__type__": "CCPropertyOverrideInfo", diff --git a/tgx-games-client/assets/module_arean/common/role/Character002.prefab b/tgx-games-client/assets/module_arean/common/role/Character002.prefab index 3ec4e56..fb3efb1 100644 --- a/tgx-games-client/assets/module_arean/common/role/Character002.prefab +++ b/tgx-games-client/assets/module_arean/common/role/Character002.prefab @@ -464,7 +464,7 @@ "propertyPath": [ "playOnLoad" ], - "value": false + "value": true }, { "__type__": "cc.TargetInfo", diff --git a/tgx-games-client/assets/module_arean/common/role/Character003.prefab b/tgx-games-client/assets/module_arean/common/role/Character003.prefab index 67c3267..533f23a 100644 --- a/tgx-games-client/assets/module_arean/common/role/Character003.prefab +++ b/tgx-games-client/assets/module_arean/common/role/Character003.prefab @@ -464,7 +464,7 @@ "propertyPath": [ "playOnLoad" ], - "value": false + "value": true }, { "__type__": "cc.TargetInfo", diff --git a/tgx-games-client/assets/module_arean/common/role/Character004.prefab b/tgx-games-client/assets/module_arean/common/role/Character004.prefab index 8bc2292..d4d5130 100644 --- a/tgx-games-client/assets/module_arean/common/role/Character004.prefab +++ b/tgx-games-client/assets/module_arean/common/role/Character004.prefab @@ -464,7 +464,7 @@ "propertyPath": [ "playOnLoad" ], - "value": false + "value": true }, { "__type__": "cc.TargetInfo", diff --git a/tgx-games-client/assets/module_arean/common/role/Character005.prefab b/tgx-games-client/assets/module_arean/common/role/Character005.prefab index ea0389b..5901522 100644 --- a/tgx-games-client/assets/module_arean/common/role/Character005.prefab +++ b/tgx-games-client/assets/module_arean/common/role/Character005.prefab @@ -464,7 +464,7 @@ "propertyPath": [ "playOnLoad" ], - "value": false + "value": true }, { "__type__": "cc.TargetInfo",