Files
cocos_zhuzhan/assets/scripts/controller/PropController.ts

80 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { _decorator, Component, Vec3 } from 'cc';
import { GameManager } from '../manager/GameManager';
import { forEachLevelEntityNode } from '../level/TileLayout';
import { PlayerController } from './PlayerController';
const { ccclass } = _decorator;
/**
* 可拾取物(对齐 Unity PropController OnTriggerEnter2D
* 玩家进入道具格且靠近时拾取(由 PlayerController 判定时机)
*/
@ccclass('PropController')
export class PropController extends Component {
private collected = false;
private spawnCell: Vec3 | null = null;
setSpawnCell(cell: Vec3) {
this.spawnCell = cell.clone();
}
getSpawnCell(): Vec3 | null {
return this.spawnCell ? this.spawnCell.clone() : null;
}
matchesCell(cell: Vec3): boolean {
const propCell = this.getLogicCell();
return propCell.x === cell.x && propCell.y === cell.y;
}
private getLogicCell(): Vec3 {
if (this.spawnCell) return this.spawnCell;
const gm = GameManager.instance!;
return gm.worldToCell(this.node.position);
}
/** 玩家落格后尝试拾取(由 PlayerController 调用) */
static tryCollectAtCell(cell: Vec3, player: PlayerController) {
const gm = GameManager.instance;
const level = gm?.curLevel;
if (!level) return;
forEachLevelEntityNode(level, (ch) => {
const prop = ch.getComponent(PropController);
if (!prop || prop.collected || !prop.matchesCell(cell)) return;
prop.collect(player);
});
}
update() {
if (this.collected || !GameManager.instance) return;
const player = GameManager.instance.findNodeByName('Player');
const pc = player?.getComponent(PlayerController);
if (!pc?.canCollectProp(this)) return;
this.collect(pc);
}
collect(player: PlayerController) {
if (this.collected) return;
this.onCollected(player);
}
/** 同关重开:金币只隐藏不销毁,立刻放回格子,避免异步补刷拖过编辑器 200ms */
restoreForRestart() {
this.collected = false;
if (this.node?.isValid) this.node.active = true;
}
private onCollected(player: PlayerController) {
if (this.collected) return;
this.collected = true;
this.node.active = false;
const gm = GameManager.instance!;
player.addCoins();
const propCell = this.getLogicCell();
gm.removePropAtCell(propCell);
if (gm.allPropsCollected()) {
player.externalCallResult(true);
}
}
}