import { _decorator, Component, Vec3 } from 'cc'; import { GameManager } from '../manager/GameManager'; import { PlayerController } from './PlayerController'; const { ccclass } = _decorator; @ccclass('PropController') export class PropController extends Component { private collected = false; update() { if (this.collected || !GameManager.instance) return; const gm = GameManager.instance; const players = gm.curLevel?.children.filter((c) => c.name.includes('Player')) ?? []; const propCell = gm.worldToCell(this.node.worldPosition); for (const p of players) { const pc = p.getComponent(PlayerController); if (!pc) continue; const pcCell = gm.worldToCell(p.worldPosition); if (pcCell.x !== propCell.x || pcCell.y !== propCell.y) continue; this.onCollected(pc); break; } } private onCollected(player: PlayerController) { if (this.collected) return; this.collected = true; const gm = GameManager.instance!; player.addCoins(); gm.removeProp(this.node.worldPosition); const remaining = (gm.curLevel?.children.filter((c) => c.name.includes('Prop') && c !== this.node).length ?? 0); this.node.destroy(); if (remaining === 0) { if (gm.multMode) this.resolveMultWin(gm); else player.externalCallResult(true); } } private resolveMultWin(gm: GameManager) { const sum = (names: string[]) => names.reduce((t, n) => t + (gm.findNodeByName(n)?.getComponent(PlayerController)?.coins ?? 0), 0); const totalA = sum(['PlayerA1', 'PlayerA2', 'PlayerA3']); const totalB = sum(['PlayerB1', 'PlayerB2', 'PlayerB3']); const winA = totalA > totalB || (totalA === totalB && gm.stepA1Num + gm.stepA2Num + gm.stepA3Num < gm.stepB1Num + gm.stepB2Num + gm.stepB3Num); const set = (names: string[], win: boolean) => { for (const n of names) gm.findNodeByName(n)?.getComponent(PlayerController)?.externalCallResult(win); }; if (winA) { set(['PlayerA1', 'PlayerA2', 'PlayerA3'], true); set(['PlayerB1', 'PlayerB2', 'PlayerB3'], false); } else { set(['PlayerA1', 'PlayerA2', 'PlayerA3'], false); set(['PlayerB1', 'PlayerB2', 'PlayerB3'], true); } } }