import { _decorator, CCBoolean, CCFloat, Component, Node, NodeEventType } from 'cc'; import { SceneLightMgr } from './SceneLightMgr'; import { GEvent } from 'db://assets/mx/module/event/GEvent'; import { TableNames } from '../../game/ConfigTableData'; import { ObstacleType } from '../../battle/map/legacy/MapObstacle'; const { ccclass, property } = _decorator; /** * 2D简易光照-点光源 */ @ccclass('SceneLightComp') export class SceneLightComp extends Component { @property({type:CCFloat, tooltip: '内圈半径'}) radius: number = 100; @property({type:CCFloat, tooltip: '外扩宽度'}) outLen: number = 30; @property({tooltip: '是否生成阴影'}) isShadow: boolean = false; public onUpdatePos() { if (!this.node.active) return; SceneLightMgr.addLightPos(this.node.uuid, this.node.worldPosition, this.radius, this.outLen, this.isShadow); } private _timer = null; public onEnable(): void { //获取灯塔数据配置 let id = ObstacleType.灯塔 let config = null let par1 = config.par1.split('|') let data = par1[0].split(',').map(Number) this.radius = data[0] this.outLen = data[1] this._timer = setTimeout(() => { this._timer = null; this.onUpdatePos(); }, 100); // this.onUpdatePos(); SceneLightMgr.addComp(this); // 节点位置变化监听(可根据自己项目实际调整) // 出于性能和操作简便考虑,目前仅监听自己和父节点 // this.node.on(NodeEventType.TRANSFORM_CHANGED, this.onUpdatePos, this); // this.node.parent.on(NodeEventType.TRANSFORM_CHANGED, this.onUpdatePos, this); GEvent.Ins.on(GEvent.UpdateLight, this.updateLight, this); } public onDisable(): void { SceneLightMgr.cleanComp(this); SceneLightMgr.removeLight(this.node.uuid); // this.node.off(NodeEventType.TRANSFORM_CHANGED, this.onUpdatePos, this); // this.node.parent.off(NodeEventType.TRANSFORM_CHANGED, this.onUpdatePos, this); GEvent.Ins.off(GEvent.UpdateLight, this.updateLight, this); } autoUpdateCom(){ this._timer = setTimeout(() => { this._timer = null; this.onUpdatePos(); }, 100); // this.onUpdatePos(); SceneLightMgr.addComp(this); GEvent.Ins.on(GEvent.UpdateLight, this.updateLight, this); } updateLight() { this.node.active = false this.scheduleOnce(() => { this.node.active = true }, 0.03) } public onDestroy(): void { if (this._timer) { clearTimeout(this._timer); this._timer = null; } SceneLightMgr.removeLight(this.node.uuid); } }