import gameScene from "../gameScene"; const { ccclass, property } = cc._decorator; @ccclass export default class BaseProp extends cc.Component { /** * 初始化数据 * @param element json道具数据 */ public SetData(element: any): void { this.TransfromSet(element, this.node); this.node.x = Number(element.x); this.node.y = Number(element.y); if (element.wid) this.node.width = Number(element.wid); if (element.path) this.SetTexture(element.path); if (element.hei) this.node.height = Number(element.hei); if (element.ang) this.node.angle = Number(element.ang); if (element.scaleX) this.node.scaleX = Number(element.scaleX); // console.log('element:', element) this.SetPhyBox(); this.SpecicalSet(element); } /** * 道具内部特殊设置 * @param element */ public SpecicalSet(element: any) { } /** * 设置物理盒子宽高同步 */ private SetPhyBox() { if (this.node.getComponent(cc.PhysicsBoxCollider)) { var phyBox = this.node.getComponent(cc.PhysicsBoxCollider); phyBox.size.width = this.node.width; phyBox.size.height = this.node.height; phyBox.apply(); } } /** * 触发函数 * @param flag 是否dead */ public TriggerFunc(flag: boolean = true): void { if (flag) gameScene.instance.GameFail(); } public SetOtherDoor(other: any): void { console.log('SetOtherDoor:super'); } /** * 传送门设置 */ private TransfromSet(element: any, nodes: any) { if (element.num == 'O') { if (!gameScene.instance.tempTransform) gameScene.instance.tempTransform = nodes; else { gameScene.instance.tempTransform.getComponent(BaseProp).SetOtherDoor(nodes); this.SetOtherDoor(gameScene.instance.tempTransform); gameScene.instance.tempTransform = null; } } } private SetTexture(tex: any): void { var self = this; // console.log('装饰:', tex) this.GetTexture('texture/' + tex).then((res: any) => { self.node.getComponent(cc.Sprite).spriteFrame = res; self.node.width = res._originalSize.width; self.node.height = res._originalSize.height; }); } private GetTexture(name: string) { var self = this; return new Promise((resolve) => { cc.loader.loadRes(name, cc.SpriteFrame, function (err, spriteFrame) { if (err) { console.log("err", err); } else { resolve(spriteFrame); } }) }); } }