import EventType from "./EventType"; const {ccclass, property} = cc._decorator; @ccclass export default class Map extends cc.Component { onLoad(){ window.map = this; } start(){ this.createMapByMapInfo(); } createMapByMapInfo(){ window.resource.mapInfo.json.forEach(this.createNodeByPrefabInfo,this); } createNodeByPrefabInfo(prefabInfo:PrefabInfo):void{ let node:cc.Node = cc.instantiate(window.resource['pf_'+prefabInfo.name]); node.setPosition(prefabInfo.position); node.setContentSize(prefabInfo.size); node.angle = prefabInfo.angle; if(prefabInfo.name=='Ground'||prefabInfo.name=='Box'){ if(prefabInfo.name=='Ground'){ node.removeComponent(cc.Sprite) } if(prefabInfo.name=='Box'){ node.addComponent(Box).init(prefabInfo); } node.getComponent(cc.PhysicsBoxCollider).size = prefabInfo.size; node.getComponent(cc.BoxCollider).size = cc.size(prefabInfo.size.width, 2); node.getComponent(cc.BoxCollider).offset = cc.v2(0, prefabInfo.size.height/2); node.group = EventType.GROUP_GROUND; } this.node.addChild(node); } } class Box extends cc.Component{ prefabInfo:PrefabInfo; hasTouched:boolean = false; init(prefabInfo:PrefabInfo){ this.prefabInfo = prefabInfo; } onCollisionEnter(){ if(this.hasTouched)return; this.hasTouched = true; this.node.runAction(cc.sequence( cc.fadeOut(1), cc.callFunc(()=>{ let prefabInfo = this.prefabInfo; setTimeout(() => { window.map.createNodeByPrefabInfo(prefabInfo); }, 3000); this.node.destroy(); },this) )); } }