BaseProp.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import gameScene from "../gameScene";
  2. const { ccclass, property } = cc._decorator;
  3. @ccclass
  4. export default class BaseProp extends cc.Component {
  5. /**
  6. * 初始化数据
  7. * @param element json道具数据
  8. */
  9. public SetData(element: any): void {
  10. this.TransfromSet(element, this.node);
  11. this.node.x = Number(element.x);
  12. this.node.y = Number(element.y);
  13. if (element.wid) this.node.width = Number(element.wid);
  14. if (element.path) this.SetTexture(element.path);
  15. if (element.hei) this.node.height = Number(element.hei);
  16. if (element.ang) this.node.angle = Number(element.ang);
  17. if (element.scaleX) this.node.scaleX = Number(element.scaleX);
  18. // console.log('element:', element)
  19. this.SetPhyBox();
  20. this.SpecicalSet(element);
  21. }
  22. /**
  23. * 道具内部特殊设置
  24. * @param element
  25. */
  26. public SpecicalSet(element: any) {
  27. }
  28. /**
  29. * 设置物理盒子宽高同步
  30. */
  31. private SetPhyBox() {
  32. if (this.node.getComponent(cc.PhysicsBoxCollider)) {
  33. var phyBox = this.node.getComponent(cc.PhysicsBoxCollider);
  34. phyBox.size.width = this.node.width;
  35. phyBox.size.height = this.node.height;
  36. phyBox.apply();
  37. }
  38. }
  39. /**
  40. * 触发函数
  41. * @param flag 是否dead
  42. */
  43. public TriggerFunc(flag: boolean = true): void {
  44. if (flag) gameScene.instance.GameFail();
  45. }
  46. public SetOtherDoor(other: any): void {
  47. console.log('SetOtherDoor:super');
  48. }
  49. /**
  50. * 传送门设置
  51. */
  52. private TransfromSet(element: any, nodes: any) {
  53. if (element.num == 'O') {
  54. if (!gameScene.instance.tempTransform)
  55. gameScene.instance.tempTransform = nodes;
  56. else {
  57. gameScene.instance.tempTransform.getComponent(BaseProp).SetOtherDoor(nodes);
  58. this.SetOtherDoor(gameScene.instance.tempTransform);
  59. gameScene.instance.tempTransform = null;
  60. }
  61. }
  62. }
  63. private SetTexture(tex: any): void {
  64. var self = this;
  65. // console.log('装饰:', tex)
  66. this.GetTexture('texture/' + tex).then((res: any) => {
  67. self.node.getComponent(cc.Sprite).spriteFrame = res;
  68. self.node.width = res._originalSize.width;
  69. self.node.height = res._originalSize.height;
  70. });
  71. }
  72. private GetTexture(name: string) {
  73. var self = this;
  74. return new Promise((resolve) => {
  75. cc.loader.loadRes(name, cc.SpriteFrame, function (err, spriteFrame) {
  76. if (err) {
  77. console.log("err", err);
  78. } else {
  79. resolve(spriteFrame);
  80. }
  81. })
  82. });
  83. }
  84. }