|
@@ -1,4 +1,4 @@
|
|
|
-import { _decorator, Color, Component, Enum, Node, Sprite, tween, Vec3, UITransform, view } from 'cc';
|
|
|
+import { _decorator, Color, Component, Enum, Node, Sprite, tween, Vec3, UITransform, view, sp, Game } from 'cc';
|
|
|
import { CupHeight, WaterColorHex, WaterColors } from '../TakeGobletGlobalInstance';
|
|
|
import { Water } from './Water';
|
|
|
import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
|
|
@@ -7,13 +7,24 @@ import { OutArea } from './OutArea';
|
|
|
import { GameUtil } from '../GameUtil';
|
|
|
const { ccclass, property, executeInEditMode } = _decorator;
|
|
|
|
|
|
+//调酒杯状态 默认 接水
|
|
|
+export enum CocktailCupState {
|
|
|
+ Default,
|
|
|
+ GetWater,
|
|
|
+ Move,
|
|
|
+ Full
|
|
|
+}
|
|
|
+
|
|
|
/** 调酒杯组件脚本*/
|
|
|
@ccclass('CocktailCup')
|
|
|
@executeInEditMode
|
|
|
export class CocktailCup extends Component {
|
|
|
|
|
|
- @property(Sprite)
|
|
|
- sprite: Sprite = null!
|
|
|
+ @property(Node)
|
|
|
+ waters: Node = null! //水流节点
|
|
|
+
|
|
|
+ @property(sp.Skeleton)
|
|
|
+ colorB: sp.Skeleton = null! //水流颜色骨骼
|
|
|
|
|
|
@property({ type: Enum(CupHeight), displayName: '杯高度' })
|
|
|
cupHeight: CupHeight = CupHeight.Two
|
|
@@ -30,56 +41,32 @@ export class CocktailCup extends Component {
|
|
|
this.changeColor()
|
|
|
}
|
|
|
|
|
|
- @property(Node)
|
|
|
- waters: Node = null!; //水节点
|
|
|
-
|
|
|
currentLayers: number = 0;
|
|
|
|
|
|
onLoad() {
|
|
|
- // 初始化时隐藏所有水层
|
|
|
- this.waters.children.forEach(water => water.active = false);
|
|
|
+ this.playAnimationByState(CocktailCupState.Default);
|
|
|
}
|
|
|
|
|
|
changeColor() {
|
|
|
- if (!this.sprite) return
|
|
|
- this.sprite.color = new Color(WaterColorHex[this._cupColor])
|
|
|
+ const color = new Color(WaterColorHex[this.cupColor]);
|
|
|
+ this.waters.children.forEach((water, index) => {
|
|
|
+ water.getComponent(sp.Skeleton)!.color = color;
|
|
|
+ })
|
|
|
+ this.colorB.color = color;
|
|
|
}
|
|
|
|
|
|
- // 添加水层
|
|
|
- async addLayer(color: WaterColors) {
|
|
|
- const nextIndex = this.waters.children.filter(n => n.active).length;
|
|
|
- const waterNode = this.waters.children[nextIndex];
|
|
|
- if (waterNode) {
|
|
|
- const water = waterNode.getComponent(Water)!;
|
|
|
- water.color = color;
|
|
|
- waterNode.active = true;
|
|
|
-
|
|
|
- // 添加后检查是否满杯
|
|
|
- await this.checkAndDestroy(); // 等待销毁流程完成
|
|
|
- }
|
|
|
+ // 添加水层 倒水时调用
|
|
|
+ async addLayer() {
|
|
|
+ this.currentLayers++;
|
|
|
+ this.playAnimationByState(CocktailCupState.GetWater);
|
|
|
+ await GameUtil.delay(0.5);
|
|
|
+ await this.checkAndDestroy();
|
|
|
}
|
|
|
|
|
|
//补满水
|
|
|
public async fillUp() {
|
|
|
- const currentActive = this.waters.children.filter(n => n.active).length;
|
|
|
- const layersToAdd = this.cupHeight - currentActive;
|
|
|
-
|
|
|
- for (let i = 0; i < layersToAdd; i++) {
|
|
|
- const waterIndex = currentActive + i;
|
|
|
- const waterNode = this.waters.children[waterIndex];
|
|
|
- const water = waterNode.getComponent(Water)!;
|
|
|
-
|
|
|
- water.color = this.cupColor;
|
|
|
- waterNode.active = true;
|
|
|
- waterNode.scale = Vec3.ZERO;
|
|
|
-
|
|
|
- await new Promise<void>(resolve => {
|
|
|
- tween(waterNode)
|
|
|
- .to(0.5 / layersToAdd, { scale: Vec3.ONE }, { easing: 'sineOut' })
|
|
|
- .call(() => resolve())
|
|
|
- .start();
|
|
|
- });
|
|
|
- }
|
|
|
+ this.playAnimationByState(CocktailCupState.Full);
|
|
|
+ await GameUtil.delay(0.5);
|
|
|
|
|
|
// 补满后执行移动销毁动画
|
|
|
const uiTransform = this.node.getComponent(UITransform)!;
|
|
@@ -89,40 +76,54 @@ export class CocktailCup extends Component {
|
|
|
await new Promise<void>(resolve => {
|
|
|
tween(this.node)
|
|
|
.to(0.5, { position: new Vec3(targetX, 0, 0) }, { easing: 'sineIn' })
|
|
|
- .call(() => {
|
|
|
- this.node.destroy();
|
|
|
+ .call(async () => {
|
|
|
+ const outArea = this.node.parent?.parent?.getComponent(OutArea);
|
|
|
+ if (outArea) {
|
|
|
+ this.node.removeFromParent(); // 先从父节点移除
|
|
|
+ this.node.destroy();
|
|
|
+ await outArea.arrangeCups(); // 等待排列完成
|
|
|
+ }
|
|
|
resolve();
|
|
|
})
|
|
|
.start();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ //播放动画
|
|
|
+ playAnimationByState(state: CocktailCupState) {
|
|
|
+ const waters = this.waters.children;
|
|
|
+ const siblingNodes = this.node.children.filter(child => child.name !== 'waters');
|
|
|
+ const allNodes = [...waters, ...siblingNodes];
|
|
|
+
|
|
|
+ switch (state) {
|
|
|
+ case CocktailCupState.GetWater:
|
|
|
+ allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, `pour_0${this.currentLayers}`, false));
|
|
|
+ break;
|
|
|
+ case CocktailCupState.Move:
|
|
|
+ allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'move', false));
|
|
|
+ break;
|
|
|
+ case CocktailCupState.Full:
|
|
|
+ allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'full', false));
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'idle', false));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 清空水层(消除时调用)
|
|
|
reset() {
|
|
|
- this.waters.children.forEach(water => water.active = false);
|
|
|
this.currentLayers = 0;
|
|
|
}
|
|
|
|
|
|
get isFull(): boolean {
|
|
|
- return this.waters.children.every(node => node.active);
|
|
|
+ return this.currentLayers >= this.cupHeight;
|
|
|
}
|
|
|
|
|
|
private async checkAndDestroy() {
|
|
|
if (this.isFull) {
|
|
|
- await new Promise<void>(resolve => {
|
|
|
- tween(this.node)
|
|
|
- .to(0.3, { scale: Vec3.ZERO })
|
|
|
- .call(async () => {
|
|
|
- const outArea = this.node.parent?.parent?.getComponent(OutArea);
|
|
|
- if (outArea) {
|
|
|
- this.node.removeFromParent(); // 先从父节点移除
|
|
|
- this.node.destroy();
|
|
|
- await outArea.arrangeCups(); // 等待排列完成
|
|
|
- }
|
|
|
- resolve();
|
|
|
- })
|
|
|
- .start();
|
|
|
- });
|
|
|
+ await this.fillUp();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -131,7 +132,7 @@ export class CocktailCup extends Component {
|
|
|
}
|
|
|
|
|
|
public get remainingCapacity(): number {
|
|
|
- return this.cupHeight - this.waters.children.filter(n => n.active).length;
|
|
|
+ return this.cupHeight - this.currentLayers;
|
|
|
}
|
|
|
}
|
|
|
|