base_view.ts 15 KB

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