|
@@ -27,11 +27,11 @@ export class LevelAction extends Component {
|
|
@property(Node)
|
|
@property(Node)
|
|
goblets: Node = null!; //原浆区
|
|
goblets: Node = null!; //原浆区
|
|
|
|
|
|
- private originCupPositions = new Map<Node, Vec3>(); // 记录原浆杯初始位置
|
|
|
|
|
|
+ private originCupPositions = new Map<string, Vec3>(); // 改用唯一ID记录
|
|
|
|
|
|
start() {
|
|
start() {
|
|
this.registerListener();
|
|
this.registerListener();
|
|
- this.generateInitialCups(); // 自动初始化
|
|
|
|
|
|
+ this.generateInitialCups();
|
|
}
|
|
}
|
|
|
|
|
|
onDestroy() {
|
|
onDestroy() {
|
|
@@ -40,14 +40,19 @@ export class LevelAction extends Component {
|
|
|
|
|
|
registerListener() {
|
|
registerListener() {
|
|
EventDispatcher.instance.on(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
|
|
EventDispatcher.instance.on(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
|
|
- EventDispatcher.instance.on(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, (cupNode: Node) => {
|
|
|
|
- this.spawnNewOriginCup(cupNode); // 传递被销毁的节点
|
|
|
|
|
|
+ EventDispatcher.instance.on(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, (uuid: string) => {
|
|
|
|
+ const cupNode = find(uuid) as Node;
|
|
|
|
+ if (cupNode) {
|
|
|
|
+ this.spawnNewOriginCup(cupNode);
|
|
|
|
+ }
|
|
}, this);
|
|
}, this);
|
|
|
|
+ EventDispatcher.instance.on(GameEvent.EVENT_COCKTAIL_CUP_DESTROYED, this.handleCupDestroyed, this);
|
|
}
|
|
}
|
|
|
|
|
|
unregisterListener() {
|
|
unregisterListener() {
|
|
EventDispatcher.instance.off(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
|
|
EventDispatcher.instance.off(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
|
|
EventDispatcher.instance.off(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, this.spawnNewOriginCup, this);
|
|
EventDispatcher.instance.off(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, this.spawnNewOriginCup, this);
|
|
|
|
+ EventDispatcher.instance.off(GameEvent.EVENT_COCKTAIL_CUP_DESTROYED, this.handleCupDestroyed, this);
|
|
}
|
|
}
|
|
|
|
|
|
private async generateInitialCups() {
|
|
private async generateInitialCups() {
|
|
@@ -118,7 +123,9 @@ export class LevelAction extends Component {
|
|
}
|
|
}
|
|
|
|
|
|
// 在生成初始原浆杯时记录位置
|
|
// 在生成初始原浆杯时记录位置
|
|
- this.originCupPositions.set(originCupNode, originCupNode.position.clone());
|
|
|
|
|
|
+ const id = originCupNode.uuid; // 使用节点唯一ID
|
|
|
|
+ this.originCupPositions.set(id, originCupNode.position.clone());
|
|
|
|
+ // console.log('在生成初始原浆杯时记录位置: ', originCupNode.position);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
@@ -131,12 +138,12 @@ export class LevelAction extends Component {
|
|
.filter(({ comp }) =>
|
|
.filter(({ comp }) =>
|
|
comp &&
|
|
comp &&
|
|
comp.cupColor === color &&
|
|
comp.cupColor === color &&
|
|
- !comp.isFull()
|
|
|
|
|
|
+ !comp.isFull
|
|
);
|
|
);
|
|
|
|
|
|
if (validCups.length === 0) return null;
|
|
if (validCups.length === 0) return null;
|
|
|
|
|
|
- const sorted = validCups.sort((a, b) => a.comp.remainingCapacity - b.comp.remainingCapacity);
|
|
|
|
|
|
+ const sorted = validCups.sort((a, b) => (a.comp.remainingCapacity ?? 0) - (b.comp.remainingCapacity ?? 0));
|
|
return sorted[0];
|
|
return sorted[0];
|
|
}
|
|
}
|
|
|
|
|
|
@@ -146,7 +153,7 @@ export class LevelAction extends Component {
|
|
for (let i = originCup.waters.children.length - 1; i >= 0; i--) {
|
|
for (let i = originCup.waters.children.length - 1; i >= 0; i--) {
|
|
const waterNode = originCup.waters.children[i];
|
|
const waterNode = originCup.waters.children[i];
|
|
if (waterNode.active) {
|
|
if (waterNode.active) {
|
|
- const water = waterNode.getComponent(Water)!;
|
|
|
|
|
|
+ const water = waterNode.getComponent(Water);
|
|
colors.push(water.color);
|
|
colors.push(water.color);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -159,7 +166,12 @@ export class LevelAction extends Component {
|
|
|
|
|
|
// 调酒区未找到,查找暂存区
|
|
// 调酒区未找到,查找暂存区
|
|
if (!targetNode) {
|
|
if (!targetNode) {
|
|
- const tempCup = this.tempCups.getComponent(TempCups)!.findAvailableTempCup();
|
|
|
|
|
|
+ const tempCupsComp = this.tempCups.getComponent(TempCups);
|
|
|
|
+ if (!tempCupsComp) {
|
|
|
|
+ console.error('TempCups component not found!');
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ const tempCup = tempCupsComp.findAvailableTempCup();
|
|
if (tempCup) {
|
|
if (tempCup) {
|
|
targetNode = tempCup.node;
|
|
targetNode = tempCup.node;
|
|
targetIsTemp = true;
|
|
targetIsTemp = true;
|
|
@@ -240,7 +252,8 @@ export class LevelAction extends Component {
|
|
|
|
|
|
private async spawnNewOriginCup(destroyedCup: Node) {
|
|
private async spawnNewOriginCup(destroyedCup: Node) {
|
|
// 获取被销毁杯子的初始位置
|
|
// 获取被销毁杯子的初始位置
|
|
- const targetPos = this.originCupPositions.get(destroyedCup) || Vec3.ZERO;
|
|
|
|
|
|
+ const id = destroyedCup.uuid;
|
|
|
|
+ const targetPos = this.originCupPositions.get(id) || Vec3.ZERO;
|
|
|
|
|
|
// 创建新原浆杯
|
|
// 创建新原浆杯
|
|
const height = TakeGobletGlobalInstance.instance.generateOriginCupHeight();
|
|
const height = TakeGobletGlobalInstance.instance.generateOriginCupHeight();
|
|
@@ -253,7 +266,7 @@ export class LevelAction extends Component {
|
|
this.goblets.addChild(newCup);
|
|
this.goblets.addChild(newCup);
|
|
|
|
|
|
// 记录新杯子的初始位置
|
|
// 记录新杯子的初始位置
|
|
- this.originCupPositions.set(newCup, targetPos);
|
|
|
|
|
|
+ this.originCupPositions.set(newCup.uuid, targetPos);
|
|
|
|
|
|
// 移动动画到原位置
|
|
// 移动动画到原位置
|
|
tween(newCup)
|
|
tween(newCup)
|
|
@@ -293,5 +306,32 @@ export class LevelAction extends Component {
|
|
waterNode.active = true;
|
|
waterNode.active = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private handleCupDestroyed(destroyedCup: Node) {
|
|
|
|
+ // 从outArea移除被销毁的杯子
|
|
|
|
+ this.outArea.removeCup(destroyedCup);
|
|
|
|
+ // 补充新杯子到等待区
|
|
|
|
+ this.generateNewCupToWaitArea();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private async generateNewCupToWaitArea() {
|
|
|
|
+ const configs = TakeGobletGlobalInstance.instance.getInitialCupsConfig();
|
|
|
|
+ const randomConfig = configs[Math.floor(Math.random() * configs.length)];
|
|
|
|
+
|
|
|
|
+ const prefab = await TakeGobletGlobalInstance.instance.loadAsyncCocktail(randomConfig.height);
|
|
|
|
+ const newCup = instantiate(prefab);
|
|
|
|
+ const cupComp = newCup.getComponent(CocktailCup)!;
|
|
|
|
+
|
|
|
|
+ cupComp.cupColor = TakeGobletGlobalInstance.instance.getRandomColor();
|
|
|
|
+ cupComp.reset();
|
|
|
|
+
|
|
|
|
+ this.waitArea.addCup(newCup);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 新增重置方法
|
|
|
|
+ public resetLevel() {
|
|
|
|
+ this.originCupPositions.clear();
|
|
|
|
+ this.generateInitialCups();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|