base_view.ts 16 KB

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