DaFuWeng/assets/Script/UIRoot/DiceButtonNode.ts

272 lines
7.7 KiB
TypeScript
Raw Normal View History

/*
* @Author: WoNiu
* @Date: 2024-03-13 12:19:50
* @LastEditTime: 2024-03-29 13:01:41
* @LastEditors: WoNiu
* @Description:
*/
2024-03-25 19:50:16 +08:00
/*
* @Author: WoNiu
* @Date: 2024-03-13 12:19:50
* @LastEditTime: 2024-03-25 14:19:53
* @LastEditors: WoNiu
* @Description:
*/
import {
_decorator,
Component,
EventMouse,
Node,
Sprite,
SpriteFrame,
Texture2D,
UITransform,
v2,
} from "cc";
import { BaseSprite } from "../../GlobalScript/CommonComponent/BaseSprite";
import { NpkImage, NpkImageTool } from "../../Tool/NPKImage";
2024-03-25 19:50:16 +08:00
import {
BaseButton,
BaseButtonState,
} from "../../GlobalScript/CommonComponent/BaseButton";
import { BaseButtonAction } from "../../GlobalScript/CommonComponent/BaseButtonAction";
import { ImagePack } from "../../GlobalScript/ImagePack/ImagePack";
import { GameState } from "../../GlobalScript/GlobalGameState/GameState";
import { ImgInfo } from "../../GlobalScript/GlobalInterface/GlobalInterface";
import { GamerRoleAni, GamerRoleType } from "../Gamer/GamerRoleType";
const { ccclass } = _decorator;
2024-03-25 19:50:16 +08:00
@ccclass("DiceButtonNode")
/**
* @description:
*/
export class DiceButtonNode extends Node {
2024-03-25 19:50:16 +08:00
winButtonComponent: BaseButton;
diceButtonComponent: BaseButton;
2024-03-25 19:50:16 +08:00
winButtonBlock: Function;
diceDownBlock: Function;
diceUpBlock: Function;
2024-03-25 19:50:16 +08:00
gaugeAnimation: GaugeAnimation;
2024-03-25 19:50:16 +08:00
constructor() {
super();
2024-03-25 19:50:16 +08:00
this.initBackgroundNode();
this.initOtherWinButtonNode();
this.initdiceButtonNode();
this.initdiceDiceAniNode();
}
2024-03-25 19:50:16 +08:00
// 背景节点
private initBackgroundNode(){
const backgroundNode = new Node();
const bs = backgroundNode.addComponent(BaseSprite);
bs.updateSpriteFrame(NpkImage.ingame, 7);
this.addChild(backgroundNode);
}
2024-03-25 19:50:16 +08:00
// 其他获胜条件按钮节点
private initOtherWinButtonNode() {
/// 其他获胜条件
const winButtonNode = new Node();
this.addChild(winButtonNode);
winButtonNode.setPosition(20.5, -123);
2024-03-25 19:50:16 +08:00
const winBa = winButtonNode.addComponent(BaseButtonAction);
winBa.onMouseUp = this.winOnMouseUp.bind(this);
2024-03-25 19:50:16 +08:00
this.winButtonComponent = winButtonNode.addComponent(BaseButton);
this.winButtonComponent.init(NpkImage.ingame, 60);
}
2024-03-25 19:50:16 +08:00
// 骰子按钮节点
private initdiceButtonNode() {
/// dice 按钮
const diceButtonNode = new Node('diceButton');
this.addChild(diceButtonNode);
diceButtonNode.setPosition(89, -57);
2024-03-25 19:50:16 +08:00
const diceBc = diceButtonNode.addComponent(BaseButtonAction);
diceBc.onMouseLeftDown = this.diceOnMouseDown.bind(this);
diceBc.onMouseLeftUp = this.diceOnMouseUp.bind(this);
2024-03-25 19:50:16 +08:00
this.diceButtonComponent = diceButtonNode.addComponent(BaseButton);
this.diceButtonComponent.init(NpkImage.ingame, 8);
this.diceButtonComponent.ButtonState = BaseButtonState.Disable;
}
2024-03-25 19:50:16 +08:00
// 播放动画节点
private initdiceDiceAniNode() {
const node = new Node();
this.addChild(node);
node.setPosition(45, -15);
2024-03-25 19:50:16 +08:00
//设置节点锚点为左上角
node.addComponent(UITransform).anchorPoint = v2(0, 1);
this.gaugeAnimation = node.addComponent(GaugeAnimation);
}
/// 其他获胜条件
private winOnMouseUp(event: EventMouse) {
if (this.winButtonComponent.ButtonState == BaseButtonState.Disable) return;
if (this.winButtonBlock) this.winButtonBlock();
}
/// 投骰子
private diceOnMouseDown(event: EventMouse) {
console.log('diceOnMouseDown');
if (this.diceButtonComponent.ButtonState == BaseButtonState.Disable)
return;
if (this.diceDownBlock) this.diceDownBlock();
this.gaugeAnimation.updatePlayState(PlayAniState.playing);
}
private diceOnMouseUp(event: EventMouse) {
console.log('diceOnMouseUp');
if (this.diceButtonComponent.ButtonState == BaseButtonState.Disable)
return;
if (this.diceUpBlock) this.diceUpBlock();
this.gaugeAnimation.updatePlayState(PlayAniState.pause);
}
}
//* ─── 动画播放 ────────────────────────────────────────────────────────────────────
2024-03-25 19:50:16 +08:00
export enum PlayAniState {
//未播放
unplayed,
//播放中
playing,
//倒放
reversed,
//暂停
pause,
//播放完成
done,
}
/**
* @description:
*/
export class GaugeAnimation extends Component {
// 所有精灵帧缓存
private frameMap: Map<number, SpriteFrame> = new Map<number, SpriteFrame>();
// 精灵帧的数量
private frameCount: number = 100;
// 精灵帧初始化状态
private InitState: boolean = false;
// 总的播放时间
private aniTime: number = 1;
// 当前帧显示时间
private nowFrameTime: number = 0;
// 每一帧需要显示的时间
private frameShowTime: number = 0;
// 当前帧
private nowFrameIndex: number = -1;
//* 动画状态
private playState: PlayAniState = PlayAniState.unplayed;
// 节点的精灵
private sprite: Sprite;
onLoad() {
if (!this.node.getComponent(Sprite)) {
this.sprite = this.node.addComponent(Sprite);
} else {
this.sprite = this.node.getComponent(Sprite);
}
2024-03-25 19:50:16 +08:00
//设置类型
this.sprite.sizeMode = Sprite.SizeMode.RAW;
//设置
this.sprite.trim = false;
NpkImageTool.GetNpkImageAll(NpkImage.gauge, (count,frameMap)=>{
this.frameMap = frameMap;
this.frameCount = count;
this.InitState = true;
2024-03-25 19:50:16 +08:00
});
}
start() {}
/**
* @description:
* @param {PlayAniState} palyState: 动画状态
* @param {number} aniTime
*/
updatePlayState(palyState: PlayAniState, aniTime?: number) {
this.playState = palyState;
// 数据的初始化
if (this.playState == PlayAniState.unplayed) {
this.sprite.spriteFrame = null;
this.nowFrameIndex = -1;
} else if (
this.playState == (PlayAniState.playing || PlayAniState.reversed)
) {
this.aniTime = aniTime ?? this.aniTime;
// 单帧显示时间
this.frameShowTime = this.aniTime * 1000 / this.frameCount;
// this.frameShowTime = this.frameShowTime / this.frameCount;
}
2024-03-25 19:50:16 +08:00
// 精灵帧的初始化
if (this.playState == PlayAniState.playing) {
this.sprite.spriteFrame = this.frameMap.get(0);
} else if (this.playState == PlayAniState.reversed) {
this.sprite.spriteFrame = this.frameMap.get(this.frameCount - 1);
}
2024-03-25 19:50:16 +08:00
}
update(dt: number) {
//如果游戏世界处于暂停的模式下 不再继续播放
if (GameState.getInstance().IsPauseState()) return;
//如果初始化未完成,不播放
if (!this.InitState) return;
//如果不在播放中
if (this.playState != PlayAniState.playing && this.playState != PlayAniState.reversed)
return;
//每帧增加过去的时间 取千分之一秒为单位
this.nowFrameTime += Math.trunc(dt * 1000);
// 计算当前加减帧数
const index = Math.trunc(this.nowFrameTime / this.frameShowTime);
// 当前帧时间不足 一帧显示就跳过
if (index < 1) return;
2024-03-25 19:50:16 +08:00
// 将多余的时间 重新赋值回去
this.nowFrameTime = this.nowFrameTime % this.frameShowTime;
if (this.playState == PlayAniState.playing) {
this.nowFrameIndex += index
this.sprite.spriteFrame = this.frameMap.get(this.nowFrameIndex);
// 播放到最后一帧 修改播放状态 为倒放
if (this.nowFrameIndex >= this.frameCount - 1) {
this.playState = PlayAniState.reversed;
}
} else if (this.playState == PlayAniState.reversed) {
this.nowFrameIndex -= index;
this.sprite.spriteFrame = this.frameMap.get(this.nowFrameIndex);
// 播放到第一帧 修改播放状态 为倒放
if (this.nowFrameIndex <= 0) {
this.playState = PlayAniState.playing;
}
}
}
}