base_view.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import { _decorator, Animation, Color, Component, instantiate, Label, Node, Prefab, ProgressBar, resources, Size, Sprite, SpriteFrame, tween, UITransform, Vec3, Widget } from 'cc';
  2. import { gameManager } from '../gameManager';
  3. import { ClientEvent } from '../framework/clientEvent';
  4. import { config } from '../config';
  5. import { SdkUtil } from '../framework/sdkUtil';
  6. import { fail } from '../dialog/fail';
  7. import { http } from '../http/http';
  8. import { Util } from '../framework/util';
  9. import { win } from '../dialog/win';
  10. import { tools } from '../tools';
  11. import { setting } from '../homepage/setting';
  12. import { addCoinAni } from '../addCoinAni';
  13. import { clock_angle } from '../clock_angle';
  14. import { guid } from '../guid/guid';
  15. import { PoolManager } from '../framework/poolManager';
  16. const { ccclass, property } = _decorator;
  17. @ccclass('base_view')
  18. export class base_view extends Component {
  19. @property(Node) btn_back:Node = null;
  20. @property(Node) btn_pause:Node = null;
  21. @property(Node) btn_look_video:Node = null;
  22. @property(Node) btn_tip:Node = null;
  23. @property(Node) btn_coin:Node = null;
  24. @property(ProgressBar) progressbar:ProgressBar = null;
  25. @property(Node) lab_title:Node = null;
  26. @property(Node) lab_coin:Node = null;
  27. @property(Node) img_touch_err:Node = null;
  28. @property(Node) action_node:Node = null;
  29. @property(SpriteFrame) sf_btn_start:SpriteFrame = null;
  30. @property(SpriteFrame) sf_btn_pause:SpriteFrame = null;
  31. @property(Prefab) addCoinAni:Prefab = null;
  32. @property(Node) top_node:Node = null;
  33. @property(Node) lab_time_count:Node = null;
  34. @property(Node) clock_ani:Node = null;
  35. protected guid_view:Node = null;
  36. protected time_count:number = 0;
  37. protected time_all_count:number = 0;
  38. protected err_node_1:Node = null;
  39. protected isPause:boolean = false;
  40. protected reward:number = 0;
  41. protected isShowWin:boolean = false;
  42. protected categoryid:number = -1;
  43. protected old_coin_number:number = 0;
  44. protected m_data:any;
  45. protected coin_ani:Node;
  46. protected start(): void {
  47. let self = this;
  48. self.initUI()
  49. this.btn_back.on(Node.EventType.TOUCH_START,()=>{
  50. gameManager.playBtnSound()
  51. self.close();
  52. },this);
  53. this.btn_look_video.on(Node.EventType.TOUCH_START,()=>{
  54. SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Earn_coins_AdUnitId,(res)=>{
  55. if(res.isEnded){
  56. self.showLookVideoReward();
  57. }
  58. })
  59. })
  60. this.btn_tip.on(Node.EventType.TOUCH_START,()=>{
  61. gameManager.playBtnSound()
  62. this.onTipBtnClick();
  63. },this);
  64. if(this.btn_pause!=null){
  65. this.btn_pause.on(Node.EventType.TOUCH_START,()=>{
  66. self.isPause = true;
  67. self.btn_pause.getComponent(Sprite).spriteFrame = self.sf_btn_start;
  68. gameManager.playBtnSound()
  69. gameManager.addTopView(config.PREFAB_PATH.setting,(node:Node)=>{
  70. node.getComponent(setting).show(()=>{
  71. self.btn_pause.getComponent(Sprite).spriteFrame = self.sf_btn_pause;
  72. self.isPause = false;
  73. })
  74. })
  75. },this);
  76. }
  77. ClientEvent.on(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER,this.onChangeCoinNumber.bind(this),this);
  78. }
  79. protected onDestroy(): void {
  80. // if(this.categoryid===config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG){
  81. // PoolManager.instance.clearPool("font_item")
  82. // }
  83. ClientEvent.off(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER,this.onChangeCoinNumber.bind(this),this);
  84. }
  85. close(){
  86. ClientEvent.dispatchEvent(config.EVENT_MSG.ON_BACK_HOME_VIEW);
  87. this.node.removeFromParent();
  88. }
  89. onChangeCoinNumber(){
  90. let num:number = this.old_coin_number;
  91. let add_coin = gameManager.get_user_coin() - num;
  92. let obj = new Component
  93. gameManager.instance.addCoinNumber(obj,add_coin,this.lab_coin.getComponent(Label),num,()=>{
  94. this.old_coin_number= gameManager.get_user_coin();
  95. this.lab_coin.getComponent(Label).string = Util.coinNumberToK(this.old_coin_number);
  96. })
  97. }
  98. reset_time_count(){
  99. if(this.lab_time_count!=null){
  100. this.lab_time_count.getComponent(Label).string = ""
  101. }
  102. }
  103. /**
  104. 下一关 start
  105. */
  106. nextLevel(){
  107. this.reset_time_count()
  108. let is_have_next = gameManager.next_level(this.categoryid,(is_sync:boolean)=>{
  109. if(is_sync){
  110. let levels_item = gameManager.get_cur_level_by_categoryid(this.categoryid);
  111. // gameManager.showLoading();
  112. gameManager.add_coin(this.reward);
  113. ClientEvent.dispatchEvent(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER);
  114. http.get(config.static_url.levels_info(this.categoryid,levels_item.level),(err,data)=>{
  115. if(!err){
  116. let _data = JSON.parse(data)
  117. if(_data.code===config.MSG_CODE.SUCCESS){
  118. this.initView(_data.content,true);
  119. }
  120. }
  121. })
  122. }else{
  123. gameManager.hideLoading()
  124. tools.showDialogOne("请检查您的网络状态,重新连接",()=>{
  125. this.nextLevel()
  126. })
  127. }
  128. })
  129. if(!is_have_next){
  130. tools.showToast("当前已是最后一关,敬请期待最新版本");
  131. }
  132. }
  133. /**
  134. 下一关 end
  135. */
  136. /**
  137. 计时api start
  138. */
  139. stop_time_count(){
  140. this.unscheduleAllCallbacks()
  141. }
  142. start_time_count(time:number){
  143. if(this.lab_time_count==null){
  144. return
  145. }
  146. this.time_all_count = time;
  147. this.lab_time_count.active = true;
  148. let self = this;
  149. this.time_count = 0;
  150. self.progressbar.progress = 1;
  151. self.clock_ani.getComponent(clock_angle).init();
  152. this.schedule(()=>{
  153. if(self.isPause){
  154. return;
  155. }
  156. if(self.time_count<self.time_all_count){
  157. self.time_count+=1;
  158. self.progressbar.progress = (self.time_all_count-self.time_count)/self.time_all_count;
  159. self.lab_time_count.getComponent(Label).string = `${this.time_all_count-this.time_count}s`
  160. self.clock_ani.getComponent(clock_angle).addSecond();
  161. }else{
  162. self.lab_time_count.getComponent(Label).string = "0s";
  163. self.showFail()
  164. self.stop_time_count();
  165. }
  166. },1)
  167. }
  168. error_sub_time(){
  169. let self = this;
  170. if( (self.time_count + self.m_data.wrong_time) >= self.time_all_count){
  171. self.time_all_count = 0;
  172. self.lab_time_count.getComponent(Label).string = "0s";
  173. }else{
  174. self.lab_time_count.getComponent(Label).string = `${this.time_all_count-this.time_count}s`
  175. self.time_count += self.m_data.wrong_time;
  176. }
  177. self.progressbar.progress = (self.time_all_count-self.time_count)/self.time_all_count;
  178. }
  179. /**
  180. 计时api end
  181. */
  182. showFail(){
  183. if(!this.isShowWin){
  184. gameManager.addUiView(config.PREFAB_PATH.fail,(node:Node)=>{
  185. gameManager.playFailSoundByCategoryId(this.categoryid)
  186. if(this.categoryid===config.PLAY_TYPE.DOU_DI_ZHU){
  187. node.getComponent(fail).showDDZView(this.onFailReStartClick.bind(this),this.m_data.ads_restart)
  188. }else{
  189. node.getComponent(fail).showView(this.m_data.extend_time,this.onFailExtendTimeClick.bind(this),this.onFailReStartClick.bind(this),this.m_data.ads_restart)
  190. }
  191. })
  192. }
  193. }
  194. showWin(){
  195. gameManager.addTopView(config.PREFAB_PATH.win,(node:Node)=>{
  196. this.isShowWin = true;
  197. gameManager.playWinSoundByCategoryId(this.categoryid)
  198. node.getComponent(win).showView(this.reward,this.m_data,this.onWinOrdinaryClick.bind(this),this.onWinDoubleClick.bind(this));
  199. });
  200. }
  201. showLookVideoReward(){
  202. let self = this;
  203. gameManager.addTopView(config.PREFAB_PATH.win,(node:Node)=>{
  204. let reward = gameManager.get_reward_number();
  205. node.getComponent(win).showVideoAddCoinView(reward,this.m_data,(win_view:win)=>{
  206. gameManager.add_coin(reward)
  207. self.showCoinAni()
  208. tween(self.node).delay(0.5).call(()=>{
  209. ClientEvent.dispatchEvent(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER)
  210. }).start()
  211. win_view.close()
  212. },(win_view:win)=>{
  213. SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Earn_coins_AdUnitId,(res)=>{
  214. if(res.isEnded){
  215. tween(self.node).delay(0.5).call(()=>{
  216. reward = gameManager.get_deouble_reward_number();
  217. gameManager.add_coin(reward)
  218. win_view.showDoubleCoin(reward,()=>{
  219. self.showCoinAni()
  220. win_view.close()
  221. tween(self.node).delay(0.5).call(()=>{
  222. ClientEvent.dispatchEvent(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER)
  223. }).start()
  224. })
  225. }).start()
  226. }
  227. })
  228. });
  229. });
  230. }
  231. onWinOrdinaryClick(win_view:win){
  232. let self = this;
  233. gameManager.playBtnSound()
  234. tween(self.node).delay(0.5).call(()=>{
  235. self.nextLevel();
  236. }).start()
  237. win_view.close()
  238. win_view.hideAllBtn()
  239. this.showCoinAni()
  240. }
  241. onWinDoubleClick(win_view:win){
  242. gameManager.playBtnSound()
  243. let self = this;
  244. let list = this.m_data.ads_fold.split(",");
  245. let start = parseInt(list[0]);
  246. let end = parseInt(list[1]);
  247. let reward = this.reward;
  248. reward*= Util.getRandomInt(start,end);
  249. SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Super_double_AdUnitId,(res)=>{
  250. if(res.isEnded){
  251. win_view.showDoubleCoin(reward,()=>{
  252. self.showCoinAni()
  253. self.reward = reward;
  254. tween(self.node).delay(0.5).call(()=>{
  255. self.nextLevel();
  256. }).start()
  257. win_view.close()
  258. })
  259. }
  260. })
  261. }
  262. onFailExtendTimeClick(fail_view:fail){
  263. gameManager.playBtnSound()
  264. let self = this;
  265. SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Prolong_AdUnitId,(res)=>{
  266. if(res.isEnded){
  267. self.start_time_count(self.m_data.extend_time);
  268. fail_view.close()
  269. }
  270. })
  271. }
  272. showCoinAni(){
  273. if( this.coin_ani!=null){
  274. this.coin_ani.removeFromParent()
  275. this.coin_ani = null;
  276. }
  277. if(this.addCoinAni!=null){
  278. this.coin_ani = instantiate(this.addCoinAni)
  279. this.coin_ani.parent = this.action_node;
  280. this.coin_ani.position = new Vec3(200,400,0)
  281. this.coin_ani.getComponent(addCoinAni).play(()=>{
  282. // let start_pos = new Vec3()
  283. let end_pos = this.top_node.getComponent(UITransform).convertToWorldSpaceAR(this.btn_coin.position)
  284. tween(this.coin_ani).to(0.2, {position: end_pos}).hide().start()
  285. })
  286. }
  287. }
  288. showErrLab(parent:Node){
  289. let lab_err_number:Node = new Node();
  290. lab_err_number.addComponent(Label);
  291. lab_err_number.getComponent(Label).string = `-${this.m_data.wrong_time}`
  292. lab_err_number.parent = parent;
  293. lab_err_number.getComponent(Label).color = Color.RED;
  294. lab_err_number.getComponent(Label).fontSize = 25;
  295. lab_err_number.position = new Vec3(lab_err_number.position.x,30,lab_err_number.position.z)
  296. lab_err_number.getComponent(Label).isBold = true;
  297. this.clock_ani.getComponent(clock_angle).play()
  298. let end_pos = new Vec3(lab_err_number.position.x,lab_err_number.position.y+20,lab_err_number.position.z)
  299. tween(lab_err_number).to(0.5, {position: end_pos}).removeSelf().start()
  300. }
  301. checkShowGuid(){
  302. let levels_item = gameManager.get_cur_level_by_categoryid(this.categoryid);
  303. return levels_item.level === 1;
  304. }
  305. hideGuid(){
  306. if(this.guid_view!=null){
  307. this.guid_view.removeFromParent();
  308. this.guid_view = null;
  309. }
  310. }
  311. showGuid(v3:Vec3,call_back,size:Size){
  312. if(this.guid_view===null){
  313. size = new Size(50,50)
  314. resources.load(config.PREFAB_PATH.guid, Prefab, (err, prefab) => {
  315. if(err){
  316. return;
  317. }
  318. this.guid_view = instantiate(prefab);
  319. this.guid_view.parent = this.action_node;
  320. this.guid_view.getComponent(guid).showGuid(v3,()=>{
  321. call_back()
  322. },this.action_node,size)
  323. });
  324. }
  325. }
  326. protected onFailReStartClick(fail_view:fail): void{}
  327. protected initView(data,isUpdate=false): void{}
  328. protected initUI(): void{}
  329. protected onTipBtnClick(): void{}
  330. }