gameView.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import EventMgr from "../game/EventMgr";
  2. import { GameEvent } from "../game/GameEvent";
  3. import { GameMode } from "../game/GameMode";
  4. import Main from "../game/Main";
  5. import { SDKIpt } from "../game/SDKIpt";
  6. import DateTime from "../help/DateTime";
  7. import valBar from "./valBar";
  8. const {ccclass, property} = cc._decorator;
  9. @ccclass
  10. export default class gameView extends cc.Component {
  11. /**开始游戏 */
  12. @property(cc.Button)
  13. private btnPause: cc.Button = null;
  14. @property(cc.Label)
  15. private lbKillMonsterNum: cc.Label = null;
  16. @property(cc.Label)
  17. private lbBest: cc.Label = null;
  18. @property(cc.Node)
  19. private deductionText: cc.Node = null;
  20. @property(cc.Node)
  21. private endlessNode: cc.Node = null;
  22. @property(cc.Label)
  23. private lbRound: cc.Label = null;
  24. @property(cc.RichText)
  25. private endlessTime: cc.RichText = null;
  26. @property(cc.Node)
  27. private endlessWolfTitle: cc.Node = null;
  28. //无尽模式这一波狼剩余数量
  29. @property(cc.Label)
  30. private endlessWolfCount: cc.Label = null;
  31. // @property(cc.Label)
  32. // private lbHp: cc.Label = null;
  33. @property(valBar)
  34. private hpBar: valBar = null;
  35. @property(valBar)
  36. private rageBar: valBar = null;
  37. // @property(cc.Node)
  38. // private spHpTmp: cc.Node = null;
  39. // @property(cc.Node)
  40. // private hpRoot: cc.Node = null;
  41. // @property(cc.Node)
  42. // private hpPanel: cc.Node = null;
  43. // @property([cc.SpriteFrame])
  44. // private sprites:cc.SpriteFrame[] = []
  45. /**无尽模式计时器 */
  46. private tiemrId_endless;
  47. @property([cc.Node])
  48. private spHeadStats: cc.Node[] = [];
  49. @property(cc.Node)
  50. private guildTitle: cc.Node = null;
  51. // private initHp:number = 20;
  52. // private hps:cc.Node[]=[];
  53. onLoad () {
  54. this.btnPause.node.on("click",this.onBtnPause,this);
  55. this.guildTitle.active = Main.Ins.IsGuide;
  56. EventMgr.Instance.add_event_listenner(GameEvent.GuideState,this,this.onGuideChange);
  57. }
  58. onGuideChange(GuideState: GameEvent, isOpen: boolean, onGuideChange: any) {
  59. this.guildTitle.active = Main.Ins.IsGuide;
  60. }
  61. // protected start(): void {
  62. // }
  63. ON_SDK_UI_IPT(SDK_UI_IPT: GameEvent, ipt: SDKIpt, ON_SDK_UI_IPT: any) {
  64. if (!this.node.active) { return; }
  65. // if (Ticker.isPause) { return; }
  66. if (Main.Ins.MainView.active) { return; }
  67. switch (ipt) {
  68. case SDKIpt.leftFoot:
  69. Main.Ins.PauseGameView();
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. OnEndlessGameNewRound() {
  76. // console.error("OnEndlessGameNewRound-------");
  77. let me = this;
  78. if (Main.Ins.Mode ==GameMode.endless) {
  79. // console.log("第"+Main.Ins.Round+"波--"+DateTime.format_hh_mm_ss());
  80. me.lbRound.string ="第"+Main.Ins.Round+"波";
  81. let endlessTime = Main.Ins.EndlessTime;
  82. me.endlessTime.string =me.timeShow(endlessTime-2);
  83. me.endlessWolfCount.string = Main.Ins.EndlessEnemyCount.toString()+'只';
  84. Ticker.unregistCount(this.tiemrId_endless);
  85. me.tiemrId_endless = Ticker.registerCount(function (count) {
  86. if (count>=2) {
  87. me.endlessTime.string =me.timeShow(endlessTime - count);
  88. // me.endlessTime.string = (endlessTime- count).toString();
  89. }
  90. }, endlessTime);
  91. }
  92. }
  93. private timeShow(endlessTime) {
  94. // console.error(endlessTime);
  95. let richOutline = "<outline color={0} width={1}>{2}</outline>";// "<outline color=black width=2>x</outline>";
  96. let richBold = "<b>{0}</b>";//"<b>x</b>"
  97. let richColor = "<color={0}>{1}</c>";//<color=#FFE25D>20</c>
  98. let str;
  99. let count1;
  100. let count2 = endlessTime - (Main.Ins.initEndlessTime - 2);
  101. if (count2 > 0) {
  102. count1 = endlessTime - count2;
  103. // str = count1 + "+" + count2;
  104. // str = richOutline.format("black", "2", count1);
  105. // str = richBold.format(str);
  106. // str = richColor.format("#FFE25D",str);
  107. str = "<outline color=black width=2><b><color=#FFE25D>"+count1+"秒</c><color=#6DFF5D><size=36>+"+count2+"秒</size></color></b></outline>"
  108. } else {
  109. count1 = endlessTime;
  110. str = endlessTime;
  111. // str = richOutline.format("black", "2", count1);
  112. // str = richBold.format(str);
  113. // str = richColor.format("#FFE25D",str);
  114. str = "<outline color=black width=2><b><color=#FFE25D>"+count1+"秒</c></b></outline>"
  115. }
  116. // str = richOutline.format("black", "2", count1);
  117. // str = richBold.format(str)
  118. // <outline color=black width=2><b><color=#FFE25D>20</c><color=#6DFF5D>+1</color></b></outline>
  119. // if (endlessTime>Main.Ins.initEndlessTime-2) {
  120. // let allCount = endlessTime;
  121. // let count1 = Main.Ins.initEndlessTime-2;
  122. // let count2 = allCount - count1;
  123. // str = count1.toString();
  124. // if (count2>0) {
  125. // str +="+"+count2;
  126. // }
  127. // }else{
  128. // str =endlessTime;
  129. // }
  130. return str;
  131. }
  132. OnHeroSuperSkill() {
  133. this.rageBar.show(0);
  134. }
  135. OnUpdateBestKill() {
  136. this.lbBest.string = `最高记录:${Main.Ins.BestScore}分`;
  137. }
  138. private onBtnPause() {
  139. // Ticker.tooglePause();
  140. // if ( cc.director.getScheduler().getTimeScale()==1) {
  141. // //游戏开始
  142. // cc.director.getScheduler().setTimeScale(0);
  143. // } else {
  144. // //游戏暂停
  145. // cc.director.getScheduler().setTimeScale(1);
  146. // }
  147. // if (cc.director.isPaused()) {
  148. // //游戏开始
  149. // cc.director.resume();
  150. // } else {
  151. // //游戏暂停
  152. // cc.director.pause();
  153. // }
  154. EventMgr.Instance.dispatch_event(GameEvent.SDK_UI_IPT,SDKIpt.leftFoot);
  155. // Main.Ins.PauseGameView();
  156. }
  157. private tickId_pause_ipt;
  158. public Show(){
  159. let me= this;
  160. this.node.active = true;
  161. EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,me,me.ON_SDK_UI_IPT);
  162. Ticker.unregisterDelay(this.tickId_pause_ipt);
  163. this.tickId_pause_ipt = Ticker.registerDelay(function(){
  164. EventMgr.Instance.add_event_listenner(GameEvent.SDK_UI_IPT,me,me.ON_SDK_UI_IPT);
  165. // console.error("监听暂停");
  166. },100);
  167. EventMgr.Instance.add_event_listenner(GameEvent.HeroHpChange,this,this.OnHeroHpChange);
  168. EventMgr.Instance.add_event_listenner(GameEvent.HeroRageChange,this,this.OnHeroRageChange);
  169. EventMgr.Instance.add_event_listenner(GameEvent.UpdateBestKill,this,this.OnUpdateBestKill);
  170. EventMgr.Instance.add_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill);
  171. // console.error("gameview监听");
  172. EventMgr.Instance.add_event_listenner(GameEvent.EndlessGameNewRound,this,this.OnEndlessGameNewRound);
  173. EventMgr.Instance.add_event_listenner(GameEvent.update_endless_wolf_count,this,this.on_update_endless_wolf_count);
  174. }
  175. on_update_endless_wolf_count(update_endless_wolf_count: GameEvent, count: number, on_update_endless_wolf_count: any) {
  176. this.endlessWolfCount.string = count.toString()+'只';
  177. }
  178. private hasInit:boolean =false;
  179. public Hide(){
  180. this.node.active = false;
  181. Ticker.unregisterDelay(this.tiemrId_endless);
  182. EventMgr.Instance.remove_event_listenner(GameEvent.HeroHpChange,this,this.OnHeroHpChange);
  183. EventMgr.Instance.remove_event_listenner(GameEvent.HeroRageChange,this,this.OnHeroRageChange);
  184. EventMgr.Instance.remove_event_listenner(GameEvent.UpdateBestKill,this,this.OnUpdateBestKill);
  185. EventMgr.Instance.remove_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill);
  186. EventMgr.Instance.remove_event_listenner(GameEvent.EndlessGameNewRound,this,this.OnEndlessGameNewRound);
  187. EventMgr.Instance.remove_event_listenner(GameEvent.update_endless_wolf_count,this,this.on_update_endless_wolf_count);
  188. EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
  189. Ticker.unregisterDelay(this.tiemrId_endless);
  190. }
  191. public Init(hp:number){
  192. // this.initHp = hp;
  193. Ticker.unregisterDelay(this.tiemrId_endless);
  194. this.hpBar.node.active = Main.Ins.Mode == GameMode.normal;
  195. if (Main.Ins.Mode == GameMode.normal) {
  196. this.rageBar.node.y = this.hpBar.node.y-44;
  197. }else{
  198. this.rageBar.node.y = this.hpBar.node.y;
  199. }
  200. // this.rageBar.node.active = Main.Ins.Mode == GameMode.normal;
  201. this.endlessNode.active = Main.Ins.Mode == GameMode.endless;
  202. this.lbRound.string = "";
  203. if (Main.Ins.Mode == GameMode.normal) {
  204. this.hpBar.show(hp);
  205. }
  206. this.rageBar.show(0);
  207. this.OnUpdateBestKill();
  208. this.lbKillMonsterNum.string = "0";
  209. this.showHeroHead(hp);
  210. }
  211. public OnMonsterDie(killNum:number){
  212. this.lbKillMonsterNum.string = killNum.toString();
  213. }
  214. public OnHeroHpChange(msg: string, nowHp: number) {
  215. if (Main.Ins.Mode == GameMode.normal) {
  216. this.hpBar.show(nowHp);
  217. this.showHeroHead(nowHp);
  218. }
  219. }
  220. public OnHeroRageChange(msg: string, now: number) {
  221. this.rageBar.show(now);
  222. }
  223. public showDeductionText(){
  224. const lbKillWidth = this.lbKillMonsterNum.node.width
  225. const lbKillRight = this.lbKillMonsterNum.node.x
  226. this.deductionText.x = Math.ceil(Math.abs(lbKillWidth)+ Math.abs(lbKillRight)+6)*-1
  227. cc.tween(this.deductionText)
  228. .stop()
  229. .to(.3, {opacity: 200})
  230. .delay(.2)
  231. .to(.3, {opacity: 0})
  232. .start();
  233. }
  234. private showHeroHead(nowHp: number) {
  235. if (Main.Ins.Mode == GameMode.normal) {
  236. this.spHeadStats[0].active = nowHp > 8;
  237. this.spHeadStats[1].active = nowHp > 6;
  238. this.spHeadStats[2].active = nowHp > 4;
  239. this.spHeadStats[3].active = nowHp > 2;
  240. this.spHeadStats[4].active = nowHp > 1;
  241. this.spHeadStats[5].active = true;
  242. }else{
  243. this.spHeadStats[0].active = true;
  244. }
  245. }
  246. protected onDestroy(): void {
  247. EventMgr.Instance.remove_event_listenner(GameEvent.HeroHpChange,this,this.OnHeroHpChange);
  248. EventMgr.Instance.remove_event_listenner(GameEvent.HeroRageChange,this,this.OnHeroRageChange);
  249. EventMgr.Instance.remove_event_listenner(GameEvent.UpdateBestKill,this,this.OnUpdateBestKill);
  250. EventMgr.Instance.remove_event_listenner(GameEvent.HeroSuperSkill,this,this.OnHeroSuperSkill);
  251. EventMgr.Instance.remove_event_listenner(GameEvent.EndlessGameNewRound,this,this.OnEndlessGameNewRound);
  252. EventMgr.Instance.remove_event_listenner(GameEvent.update_endless_wolf_count,this,this.on_update_endless_wolf_count);
  253. EventMgr.Instance.remove_event_listenner(GameEvent.SDK_UI_IPT,this,this.ON_SDK_UI_IPT);
  254. Ticker.unregisterDelay(this.tiemrId_endless);
  255. }
  256. }