You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdLike2048/assets/script/ui/BattleGame/NewMechanism/newMechanismNight.ts

122 lines
4.2 KiB

3 weeks ago
import { _decorator, Component, Node, Sprite, SpriteFrame, UITransform, Size } from 'cc';
import { SceneLightMgr } from '../../lightCompAndMgr/SceneLightMgr';
import { ChapterNewPlayTypeEnum } from '../../../game/ChapterPlayTypes';;
import { GEvent } from 'db://assets/mx/module/event/GEvent';
import { SceneLightComp } from '../../lightCompAndMgr/SceneLightComp';
import { DefaultSpriteFrame } from 'db://assets/mx/components/DefaultSpriteFrame';
import MTools from 'db://assets/mx/tools/MTools';
const { ccclass, property } = _decorator;
/*
新机制黑夜
1、每间隔一定时间,进入黑夜状态,黑夜区域的怪物不可视,可击中
2、美女以及角色下方2-3格默认有灯塔驱散
3、黑夜不会覆盖编辑状态下的可编辑提示,以及怪物行进路线
30秒白天10秒黑夜
灯塔障碍物可以驱散黑夜-点光源
*/
@ccclass('newMechanismNight')
export class newMechanismNight extends Component {
@property(Sprite)
topSprite: Sprite;
/**时间间隔 */
timeInterval: number = 0
/**持续时间 */
durationTime: number = 0
/**当前时间 */
curTime: number = 0
//当前状态白天
curState = 'day'
isSendMaterial = false
protected onDestroy(): void {
// console.log('onDestroy newMechanismNight')
this.unscheduleAllCallbacks()
SceneLightMgr.clear();
}
onLoad() {
//因为topSprite隐藏所以获取不到材质
let mat = this.topSprite.getSharedMaterial(0);
SceneLightMgr.initMat(mat, gg.ui.UICamera, this.topSprite.node);
let INightPropertyData = gg.data.project.getChapterNewPlayInfo(ChapterNewPlayTypeEnum.NightAndLight)
this.timeInterval = INightPropertyData.timeInterval
this.durationTime = INightPropertyData.durationTime
// console.log('INightPropertyData',this.timeInterval,this.durationTime)
this.schedule(this.updateNight, 1)
//给美女增加脚本
let children = gg.game.CurentBattle.Map.mapTargetParent.children
//添加脚本
for (let i = 0; i < children.length; i++) {
//创建一个新的节点
if(children[i].getChildByName('美女')){
if(children[i].getChildByName('美女').getChildByName('effectNode')){
continue
}
let effectNode = new Node()
effectNode.name = 'effectNode'
// effectNode 节点添加一张单色图片(40x40)
// const sprite = effectNode.getComponent(Sprite) ?? effectNode.addComponent(Sprite);
// sprite.spriteFrame = MTools.getDefaultSpriteFrame(255, 255, 255, 255);
// const uiTransform = effectNode.getComponent(UITransform) ?? effectNode.addComponent(UITransform);
// uiTransform.setContentSize(new Size(40, 40));
effectNode.parent = children[i].getChildByName('美女')
effectNode.setPosition(0,40,0)
let child = effectNode
if(!child.getComponent(SceneLightComp)){
child.addComponent(SceneLightComp)
}
}
}
}
/**更新黑夜 */
updateNight(dt: number) {
if (!gg.game.CurentBattle.canUpdateFrame()) return;
const scaled = dt * gg.game.CurentBattle.TimeScale;
if(this.curState === 'day'){
this.curTime += scaled
if(this.curTime >= this.timeInterval){
this.curTime = 0
this.topSprite.node.active = true
if(!this.isSendMaterial){
let mat = this.topSprite.getSharedMaterial(0);
SceneLightMgr.initMat(mat, gg.ui.UICamera, this.topSprite.node);
this.isSendMaterial = true
}
//切换黑夜
GEvent.Ins.emit(GEvent.UpdateLight)
this.curState = 'night'
}
}
else if(this.curState === 'night'){
this.curTime += scaled
if(this.curTime >= this.durationTime){
this.curTime = 0
this.topSprite.node.active = false
this.curState = 'day'
}
}
}
}