gameUI.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import DataObj from "../../configData/DataObj";
  2. import GameConfig from "../../configData/GameConfig";
  3. import path from "../../configData/path";
  4. import path1 from "../../configData/path1";
  5. import path2 from "../../configData/path2";
  6. import gameManger from "./gameManger";
  7. import monsterManger from ".././monster/monsterManger";
  8. import TapNode from "./TapNode";
  9. import towerManger from ".././tower/towerManger";
  10. import skillManger from "../tower/skillManger";
  11. import SkillSolate from "../tower/SkillSolate";
  12. import path3 from "../../configData/path3";
  13. import mainManager from "../../mainFace/mainManager";
  14. import { AudioManager } from "../../configData/AudioManager";
  15. const { ccclass, property } = cc._decorator;
  16. @ccclass
  17. export default class gameUI extends cc.Component {
  18. @property(cc.Node)
  19. private pausedbg: cc.Node = null;
  20. @property(cc.Label)
  21. private HpLabel: cc.Label = null;
  22. @property(cc.Label)
  23. private GoupLabel: cc.Label = null;
  24. @property(cc.Label)
  25. private GoldLabel: cc.Label = null;
  26. /**陨石技能 */
  27. @property(cc.Sprite)
  28. private yunshi: cc.Sprite = null;
  29. /**援兵技能 */
  30. @property(cc.Sprite)
  31. private yuanBin: cc.Sprite = null;
  32. /**加速按钮 */
  33. @property(cc.Button)
  34. private soonStep: cc.Button = null;
  35. @property(cc.SpriteFrame)
  36. private soonStep1: cc.SpriteFrame = null;
  37. @property(cc.SpriteFrame)
  38. private soonStep2: cc.SpriteFrame = null;
  39. private GoPos: cc.Vec3[] = [];
  40. private yunshiCoint = 0;
  41. private yuanbinCoint = 0;
  42. private isCanPlayYunshi = false;
  43. private isCanPlayYuanbin = false;
  44. private yunshiDelay = 1;
  45. private yuanbinDelay = 1;
  46. private hpnum = 0;
  47. private groupNow = 0;
  48. private groupTotal = 0;
  49. private goldnum = 0;
  50. public myDataObj: DataObj = null;
  51. public static instance: gameUI = null;
  52. protected onLoad(): void {
  53. gameUI.instance = this;
  54. this.node.opacity = 255;
  55. this.pausedbg.active = false;
  56. this.pausedbg.opacity = 255;
  57. this.yunshi.node.active = false;
  58. this.yuanBin.node.active = false;
  59. this.pausedbg.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  60. this.myDataObj = new DataObj();
  61. // this.node.getChildByName("pause").getComponent(cc.Button).enabled = false;
  62. // this.node.getChildByName("pause").getComponent(cc.Button).disabledColor = cc.color(0,0,0);
  63. }
  64. /**初始化关卡数据 */
  65. public initData() {
  66. this.myDataObj.writeLevel_Config(GameConfig.instance().palyLevel);
  67. this.hpnum = this.myDataObj.LevelData.level_hp;
  68. let maptype = this.myDataObj.LevelData.map_type;
  69. AudioManager.palyAudio(maptype + 2);
  70. this.HpLabel.string = this.hpnum.toString();
  71. this.goldnum = 0;
  72. this.AddGold(this.myDataObj.LevelData.start_gold);
  73. // this.soonStep.normalSprite = this.soonStep2;
  74. // this.soonStep.pressedSprite = this.soonStep2;
  75. this.soonStep.node.getChildByName('Background').getComponent(cc.Sprite).spriteFrame = this.soonStep1;
  76. }
  77. /** 游戏加速*/
  78. private setSpeedSoon() {
  79. let timescale = cc.director.getScheduler().getTimeScale();
  80. if (timescale < 4) {
  81. timescale += 1;
  82. GameConfig.instance().speed = timescale;
  83. cc.director.getScheduler().setTimeScale(timescale);
  84. if (timescale == 4) {
  85. this.soonStep.node.getChildByName('Background').getComponent(cc.Sprite).spriteFrame = this.soonStep2;
  86. }
  87. }
  88. else {
  89. timescale = 1;
  90. GameConfig.instance().speed = timescale;
  91. cc.director.getScheduler().setTimeScale(timescale);
  92. this.soonStep.node.getChildByName('Background').getComponent(cc.Sprite).spriteFrame = this.soonStep1;
  93. }
  94. }
  95. /**获取所有路径点 */
  96. public getGoPos(): cc.Vec3[] {
  97. return this.GoPos;
  98. }
  99. /**本关卡最高塔级数 */
  100. public getLevelLimit(): number[] {
  101. return this.myDataObj.LevelData.tower_limit;
  102. }
  103. /**设置波次 */
  104. public setTotalGroup(nownum: number, totalnum: number) {
  105. this.groupNow = nownum;
  106. this.groupTotal = totalnum;
  107. this.GoupLabel.string = this.groupNow.toString() + "/" + this.groupTotal.toString();
  108. }
  109. /**血量减少 */
  110. public HpCut(num: number) {
  111. this.hpnum -= num;
  112. if (this.hpnum < 0) {
  113. this.hpnum = 0;
  114. }
  115. this.HpLabel.string = this.hpnum.toString();
  116. if (this.hpnum == 0) {
  117. gameManger.instance.gameOver(this.hpnum);
  118. }
  119. }
  120. /**获取血量 */
  121. public getHp(): number {
  122. return this.hpnum;
  123. }
  124. /**获取金币数 */
  125. public getGold(): number {
  126. return this.goldnum;
  127. }
  128. /**增加金币数 */
  129. public AddGold(num: number) {
  130. this.goldnum += num;
  131. this.goldnum = Math.floor(this.goldnum);
  132. this.GoldLabel.string = this.goldnum.toString();
  133. }
  134. /**获取钻石数 */
  135. public getDiamind(): number {
  136. return GameConfig.instance().storageData.Diamond;
  137. }
  138. /**增加钻石数 */
  139. public addDiamind(num: number, pacent: number) {
  140. let raaom = Math.random() * 100;
  141. if (raaom <= pacent) {
  142. GameConfig.instance().storageData.Diamond += num;
  143. GameConfig.instance().setStorage();
  144. }
  145. }
  146. /**暂停、开始游戏 */
  147. private pauseGame() {
  148. AudioManager.palyAudio(6);
  149. GameConfig.instance().isPause = !GameConfig.instance().isPause;
  150. this.pausedbg.active = true;
  151. this.pauseFun();
  152. }
  153. /**暂停 */
  154. public pauseFun() {
  155. towerManger.instance.setAniGo(GameConfig.instance().isPause);
  156. cc.director.pause();
  157. }
  158. /**释放暂停 */
  159. public ClosepauseFun() {
  160. cc.director.resume();
  161. }
  162. /**返回主界面 */
  163. private backtomain() {
  164. AudioManager.palyAudio(6);
  165. mainManager.instance.node.active = true;
  166. mainManager.instance.backInit();
  167. this.pausedbg.active = false;
  168. }
  169. /**重玩 */
  170. private repaly() {
  171. AudioManager.palyAudio(6);
  172. gameManger.instance.initLevel();
  173. this.pausedbg.active = false;
  174. // cc.director.loadScene("gameScence");
  175. }
  176. /**继续 */
  177. private goongame() {
  178. AudioManager.palyAudio(6);
  179. GameConfig.instance().isPause = !GameConfig.instance().isPause;
  180. cc.director.resume();
  181. towerManger.instance.setAniGo(GameConfig.instance().isPause);
  182. this.pausedbg.active = false;
  183. }
  184. /**打开技能按钮 */
  185. public openSkillBtn() {
  186. this.myDataObj.writeSkill_Config(60);
  187. this.yunshiDelay = this.myDataObj.SkillData.parameter_4;
  188. if (towerManger.instance.talantData[22].length > 0) {
  189. this.yunshiDelay -= towerManger.instance.talantData[22][2];
  190. }
  191. if (towerManger.instance.talantData[23].length > 0) {
  192. this.yunshiDelay -= towerManger.instance.talantData[23][2];
  193. }
  194. this.myDataObj.writeSkill_Config(61);
  195. this.yuanbinDelay = this.myDataObj.SkillData.parameter_4;
  196. this.yunshi.node.active = true;
  197. this.yuanBin.node.active = true;
  198. this.yunshiCoint = 0;
  199. this.yuanbinCoint = 0;
  200. this.yunshiSchle();
  201. this.yuanbinSchle();
  202. }
  203. /**陨石技能进度条 */
  204. private yunshiSchle() {
  205. if (this.yunshiCoint >= 10000) {
  206. return;
  207. }
  208. this.yunshiCoint++;
  209. let myprogress = this.yunshi.getComponent(cc.ProgressBar);
  210. let total: number = this.yunshiDelay;
  211. // let total: number = 2;
  212. myprogress.progress = 1 - this.yunshiCoint / (total * 10);
  213. if (this.yunshiCoint >= total * 10) {
  214. this.yunshi.node.on(cc.Node.EventType.TOUCH_START, this.yunshiTouchOn, this);
  215. return;
  216. }
  217. this.scheduleOnce(function () {
  218. this.yunshiSchle();
  219. }, 0.1);
  220. }
  221. /**援兵技能进度条 */
  222. private yuanbinSchle() {
  223. if (this.yuanbinCoint >= 10000) {
  224. return;
  225. }
  226. this.yuanbinCoint++;
  227. let myprogress = this.yuanBin.getComponent(cc.ProgressBar);
  228. let total: number = this.yuanbinDelay;
  229. // let total = 2;
  230. myprogress.progress = 1 - this.yuanbinCoint / (total * 10);
  231. if (this.yuanbinCoint >= total * 10) {
  232. this.yuanBin.node.on(cc.Node.EventType.TOUCH_START, this.yuanbingTouchOn, this);
  233. return;
  234. }
  235. this.scheduleOnce(function () {
  236. this.yuanbinSchle();
  237. }, 0.1);
  238. }
  239. /**记录路径点 */
  240. public loadGoPos() {
  241. this.GoPos = path.instance.getPathData().concat();
  242. if (path1.instance.getpathData().length > 0) {
  243. this.GoPos = this.GoPos.concat(path1.instance.getpathData());
  244. }
  245. if (path2.instance.getpathData().length > 0) {
  246. this.GoPos = this.GoPos.concat(path2.instance.getpathData());
  247. }
  248. if (path3.instance.getpathData().length > 0) {
  249. this.GoPos = this.GoPos.concat(path3.instance.getpathData());
  250. }
  251. }
  252. /**点击援兵和陨石位置判断 */
  253. private getIsInGopos(location: cc.Vec2): boolean {
  254. let leth = 73;
  255. let laca = cc.v3(location.x, location.y);
  256. for (let i = 0; i < this.GoPos.length; i++) {
  257. const element = this.GoPos[i];
  258. if (element.sub(laca).mag() < leth) {
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. /**发动陨石技能 */
  265. private yunshiTouchOn() {
  266. AudioManager.palyAudio(6);
  267. this.yunshiCoint = 0;
  268. this.yunshi.node.off(cc.Node.EventType.TOUCH_START, this.yunshiTouchOn, this);
  269. this.isCanPlayYunshi = true;
  270. TapNode.instance.node.active = true;
  271. }
  272. /**发动援兵技能 */
  273. private yuanbingTouchOn() {
  274. AudioManager.palyAudio(6);
  275. this.yuanbinCoint = 0;
  276. this.yuanBin.node.off(cc.Node.EventType.TOUCH_START, this.yuanbingTouchOn, this);
  277. this.isCanPlayYuanbin = true;
  278. TapNode.instance.node.active = true;
  279. }
  280. /**陨石、援兵坐标 */
  281. public SkillTap(location: cc.Vec2) {
  282. if (this.isCanPlayYunshi == true || this.isCanPlayYuanbin == true) {
  283. if (this.getIsInGopos(location)) {
  284. TapNode.instance.node.active = false;
  285. if (this.isCanPlayYunshi == true) {
  286. this.isCanPlayYunshi = false;
  287. this.yunshiSchle();
  288. skillManger.instance.yunshiBuild(location);
  289. if (towerManger.instance.talantData[24].length > 0) {
  290. for (let i = 0; i < towerManger.instance.talantData[24][1]; i++) {
  291. let ppos = this.GoPos[Math.floor(this.GoPos.length * Math.random())];
  292. skillManger.instance.yunshiBuild(cc.v2(ppos));
  293. }
  294. }
  295. }
  296. if (this.isCanPlayYuanbin == true) {
  297. this.isCanPlayYuanbin = false;
  298. this.yuanbinSchle();
  299. cc.resources.load("prefabs/skillSolate", cc.Prefab, function (err, prefab: cc.Prefab) {
  300. var newNode = cc.instantiate(prefab);
  301. newNode.setPosition(location);
  302. towerManger.instance.node.addChild(newNode, 1006);
  303. let calssName = newNode.getComponent(SkillSolate);
  304. calssName.towerType = 4;
  305. calssName.initID();
  306. towerManger.instance.towerArr.push(calssName);
  307. });
  308. }
  309. }
  310. else {
  311. monsterManger.instance.updatest(location);
  312. }
  313. }
  314. }
  315. /**
  316. * touchstart 回调
  317. * @param e 参数
  318. */
  319. private onTouchStart(e: cc.Event.EventTouch): void {
  320. }
  321. }