import { _decorator, Vec3 } from 'cc'; import { Direction, GridType } from '../core/Define'; import { EventManager, EventType } from '../core/EventManager'; import { JsBridge } from '../bridge/JsBridge'; import { GameManager } from '../manager/GameManager'; import { Movement } from '../gameplay/Movement'; import { PlayerController, ExternalDataList } from './PlayerController'; import { VisualAssets } from '../visual/VisualAssets'; const { ccclass } = _decorator; @ccclass('VehicleController') export class VehicleController extends Movement { private player: PlayerController | null = null; onLoad() { this.moverRole = 'vehicle'; this.moveState = 0; } start() { super.start(); this.setIcon(); } setPlayer(p: PlayerController | null) { this.player = p; } setPosition(pos: Vec3) { this.node.setPosition(pos); } setName(name: string) { /* 可挂 Label 显示名称 */ } override setDirection(dir: Direction) { super.setDirection(dir); this.setIcon(); if (Movement.callEach) return; Movement.callEach = true; this.player?.setDirection(dir); Movement.callEach = false; } setIcon() { const style = GameManager.instance?.uiStyle ?? 'default'; VisualAssets.applyVehicleSprite(this.node, this.direction, style); } protected onMoveNextSet(isJump: boolean) { if (Movement.callEach) return; Movement.callEach = true; this.player?.syncFromVehicle(this.targetGridType, isJump); Movement.callEach = false; } protected onMoving() { if (Movement.callEach) return; Movement.callEach = true; if (this.player) { this.player.onMoving(); this.player.setPosition(this.node.worldPosition); } Movement.callEach = false; } protected onMoveToTarget() { GameManager.instance!.removeObj(this.lastPosition); GameManager.instance!.addObj(this.node.worldPosition, GridType.Ride, this.node); if (Movement.callEach) return; Movement.callEach = true; if (this.player) { this.player.setPosition(this.node.worldPosition); this.player.syncMoveToTargetFromVehicle(); } Movement.callEach = false; this.externalCall(); } protected onMoveFail(isJump: boolean) { if (!GameManager.instance!.multMode) { EventManager.dispatch(EventType.InputEnd, GameManager.instance!.gameState); } } callVehicleInfo() { this.externalCall(); } externalCall() { const gm = GameManager.instance!; const list: ExternalDataList = { direction: this.direction, externalDatas: [] }; const self = gm.worldToCell(this.node.worldPosition); list.externalDatas.push({ position: { x: self.x, y: self.y, z: 0 }, gridType: this.curGrid, direction: 'self', }); for (let d = Direction.North; d <= Direction.West; d++) { const wp = gm.nextGridPosition(this.node.worldPosition, d); const cell = gm.worldToCell(wp); list.externalDatas.push({ position: { x: cell.x, y: cell.y, z: 0 }, gridType: gm.calculateNextGridType(this.node.worldPosition, d), direction: gm.getRelativePosition(this.direction, d), }); } const json = JSON.stringify(list); if (gm.multMode) JsBridge.call(`process${this.node.name}`, json); else JsBridge.call('processVehicleData', json); } }