121 lines
3.3 KiB
TypeScript
121 lines
3.3 KiB
TypeScript
/*
|
|
* @Author: WoNiu
|
|
* @Date: 2024-03-27 15:58:07
|
|
* @LastEditTime: 2024-03-29 14:40:52
|
|
* @LastEditors: WoNiu
|
|
* @Description:
|
|
*/
|
|
import {
|
|
_decorator,
|
|
Component,
|
|
Node,
|
|
Sprite,
|
|
SpriteFrame,
|
|
} from "cc";
|
|
import { NpkImageTool } from "../../Tool/NPKImage";
|
|
import { GamerRoleAni, GamerRoleType } from "./GamerRoleType";
|
|
import { GameState } from "../../GlobalScript/GlobalGameState/GameState";
|
|
const { ccclass, property } = _decorator;
|
|
@ccclass("GamerRoleNode")
|
|
|
|
//* ─── 玩家在地图上的角色 Node ────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* @description: 玩家在地图上的角色 节点
|
|
*/
|
|
export class GamerRoleNode extends Node {
|
|
constructor(type: GamerRoleType) {
|
|
super();
|
|
|
|
//角色类型 设置后自动播放动画
|
|
this.addComponent(RoleAnimation).type = type;
|
|
}
|
|
|
|
}
|
|
|
|
//* ─── 角色动画播放 Component ───────────────────────────────────────────────────────────────────
|
|
|
|
export enum PlayAniState {
|
|
//播放中
|
|
playing,
|
|
//暂停
|
|
pause,
|
|
}
|
|
|
|
/**
|
|
* @description: 角色的动画
|
|
*/
|
|
export class RoleAnimation extends Component {
|
|
/** 节点精灵 */
|
|
private sprite: Sprite;
|
|
/** 精灵帧总数 */
|
|
private frameCount: number;
|
|
/** 是否初始化完成 */
|
|
private InitState: boolean;
|
|
/** 精灵帧map缓存 */
|
|
private frameMap: Map<number, SpriteFrame> = new Map<number, SpriteFrame>();
|
|
|
|
/** 每一帧需要显示的时间 千分之一秒 */
|
|
private static frameShowTime: number = 60;
|
|
|
|
/** 当前帧显示了的时间 */
|
|
private nowFrameTime: number = 0;
|
|
|
|
/** 当前帧 */
|
|
private nowFrameIndex: number = 0;
|
|
|
|
/** 动画状态 */
|
|
private playState: PlayAniState = PlayAniState.playing;
|
|
|
|
/** 角色类型 设置后自动播放动画 */
|
|
set type(type: GamerRoleType) {
|
|
|
|
const node = new Node();
|
|
this.node.addChild(node);
|
|
|
|
this.sprite = node.addComponent(Sprite);
|
|
|
|
//设置类型
|
|
this.sprite.sizeMode = Sprite.SizeMode.RAW;
|
|
//设置
|
|
this.sprite.trim = false;
|
|
|
|
// 获取所有帧图片
|
|
NpkImageTool.GetNpkImageAll(
|
|
GamerRoleAni.getPath(type),
|
|
(count, frameMap) => {
|
|
this.frameCount = count;
|
|
this.frameMap = frameMap;
|
|
this.InitState = true;
|
|
// 显示第一帧
|
|
this.sprite.spriteFrame = this.frameMap.get(this.nowFrameIndex);
|
|
}
|
|
);
|
|
}
|
|
|
|
start() {}
|
|
|
|
update(dt: number) {
|
|
//如果游戏世界处于暂停的模式下 不再继续播放
|
|
if (GameState.getInstance().IsPauseState()) return;
|
|
//如果初始化未完成,不播放
|
|
if (!this.InitState) return;
|
|
//如果不在播放中
|
|
if (this.playState != PlayAniState.playing) return;
|
|
|
|
//每帧增加过去的时间 取千分之一秒为单位
|
|
this.nowFrameTime += Math.trunc(dt * 1000);
|
|
|
|
// 大于60毫秒换下一帧
|
|
if (this.nowFrameTime < 60) return;
|
|
|
|
this.nowFrameTime -= 60;
|
|
this.nowFrameTime++;
|
|
|
|
// 播放完成后 重新播放
|
|
if (this.nowFrameIndex >= this.frameCount) this.nowFrameIndex = 0;
|
|
|
|
this.sprite.spriteFrame = this.frameMap.get(this.nowFrameIndex);
|
|
}
|
|
}
|