gameManger.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import GameConfig from "../../configData/GameConfig";
  2. import { TowerType } from "../../configData/Enum";
  3. import gameData from "../../configData/gameData";
  4. import path from "../../configData/path";
  5. import path1 from "../../configData/path1";
  6. import path2 from "../../configData/path2";
  7. import gameUI from "./gameUI";
  8. import monsterManger from ".././monster/monsterManger";
  9. import towerManger from ".././tower/towerManger";
  10. import viewComtent from "./viewComtent";
  11. import path3 from "../../configData/path3";
  12. import { AudioManager } from "../../configData/AudioManager";
  13. const {ccclass, property} = cc._decorator;
  14. @ccclass
  15. export default class gameManger extends cc.Component {
  16. public static instance:gameManger = null;
  17. private _touchId = null;
  18. private beijing:cc.Node = null;
  19. protected onLoad (): void {
  20. gameManger.instance = this;
  21. this.beijing = this.node.getChildByName("beijing");
  22. let self = this;
  23. gameData.instance().readGameData(function(){
  24. // self.initLevel();
  25. AudioManager.palyAudio(1);
  26. });
  27. this.bindTouchEvents();
  28. }
  29. onDestroy(){
  30. // gameManger.instance = null;
  31. }
  32. /**加载底图 */
  33. private loadGamebg()
  34. {
  35. let self = this;
  36. cc.resources.load("gamebg/game_"+gameUI.instance.myDataObj.LevelData.scene_path, cc.SpriteFrame, function (err, myspriteFrame: cc.SpriteFrame) {
  37. self.beijing.getComponent(cc.Sprite).spriteFrame = myspriteFrame;
  38. });
  39. }
  40. public initLevel()
  41. {
  42. // console.log("--------------------ceshi");
  43. GameConfig.instance().isPause = false;
  44. cc.director.getScheduler().setTimeScale(1);
  45. GameConfig.instance().speed = 1;
  46. cc.director.resume();
  47. GameConfig.instance().levelDiff = 0;
  48. gameUI.instance.initData();
  49. this.loadGamebg();
  50. path.instance.changeLevelData();
  51. path1.instance.changeLevelData();
  52. path2.instance.changeLevelData();
  53. path3.instance.changeLevelData();
  54. gameUI.instance.loadGoPos();
  55. monsterManger.instance.initMonster();
  56. towerManger.instance.initTowerTaside();
  57. towerManger.instance.updateTowerActionSchle();
  58. }
  59. /**游戏结算 */
  60. public gameOver(HpNum:number){
  61. towerManger.instance.setAniGo(true);
  62. viewComtent.instance.gameOver(HpNum);
  63. }
  64. /**
  65. * touchstart 回调
  66. * @param e 参数
  67. */
  68. private onTouchStart(e: cc.Event.EventTouch): void {
  69. if (this._touchId !== null) {
  70. return;
  71. }
  72. // console.log("坐标:"+e.getLocation());
  73. this._touchId = e.getID();
  74. // let turnPos = e.getLocation().sub(cc.v2(960,540));
  75. towerManger.instance.closedHuan();
  76. // towerManger.instance.chaqiGetPos(turnPos.add(cc.v2(0,viewComtent.instance.getOff())));
  77. }
  78. /**
  79. * touchend 回调
  80. */
  81. private onTouchMoved(e: cc.Event.EventTouch): void {
  82. if (e.getID() !== this._touchId) {
  83. return;
  84. }
  85. }
  86. /**
  87. * touchend 回调
  88. */
  89. private onTouchEnd(e: cc.Event.EventTouch): void {
  90. this.touchEnd(e);
  91. }
  92. /**
  93. * touchcancel 回调
  94. * @param e 参数
  95. */
  96. private onTouchCancel(e: cc.Event.EventTouch): void {
  97. // console.log("取消触摸");
  98. this.touchEnd(e);
  99. }
  100. private touchEnd(e: cc.Event.EventTouch): void{
  101. if (e.getID() !== this._touchId) {
  102. return;
  103. }
  104. this._touchId = null;
  105. }
  106. /**
  107. * 绑定点击事件
  108. */
  109. private bindTouchEvents(): void {
  110. this.beijing.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  111. this.beijing.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMoved, this);
  112. this.beijing.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
  113. this.beijing.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  114. // console.log("加载触摸");
  115. }
  116. /**
  117. * 解绑点击事件
  118. */
  119. private unbindTouchEvents(): void {
  120. this.beijing.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  121. this.beijing.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMoved, this);
  122. this.beijing.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
  123. this.beijing.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  124. // console.log("关闭触摸");
  125. }
  126. }