homepage.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import { _decorator, Component, Director, director, Enum, instantiate, Label, Node, Prefab, resources, SpriteFrame, tween, UITransform, Vec3 } from 'cc';
  2. import { config } from '../config';
  3. import { gameManager } from '../gameManager';
  4. import { play_list_item } from './play_list_item';
  5. import { server_play_list_data_item } from '../data/server_play_list_data';
  6. import { tools } from '../tools';
  7. import { ClientEvent } from '../framework/clientEvent';
  8. import { http } from '../http/http';
  9. import { view_game_play_shuang_tu_zhao_bu_tong } from '../play_list/zhaocha_up_down/view_game_play_shuang_tu_zhao_bu_tong';
  10. import { view_game_play_han_zi_zhao_bu_tong } from '../play_list/zhaocha_font/view_game_play_han_zi_zhao_bu_tong';
  11. import { view_game_play_mei_nv_zhao_xi_jie } from '../play_list/zhaocha_component/view_game_play_mei_nv_zhao_xi_jie';
  12. import { AudioManager } from '../framework/audioManager';
  13. import { SdkUtil } from '../framework/sdkUtil';
  14. import { win } from '../dialog/win';
  15. import { addCoinAni } from '../addCoinAni';
  16. import { Util } from '../framework/util';
  17. import { PoolManager } from '../framework/poolManager';
  18. import { game_play_dou_di_zhu } from '../play_list/ddz/game_play_dou_di_zhu';
  19. import { view_game_play_shuang_tu_zhao_xiang_tong } from '../play_list/zhaocha_xiang_tong/view_game_play_shuang_tu_zhao_xiang_tong';
  20. import { view_game_play_ji_yi_li } from '../play_list/ji_yi_li/view_game_play_ji_yi_li';
  21. import { view_game_play_zhao_bu_tong } from '../play_list/zhao_bu_tong/view_game_play_zhao_bu_tong';
  22. const { ccclass, property } = _decorator;
  23. export enum ETest {
  24. None = 0,
  25. ONe = 1,
  26. Two = 2
  27. }
  28. @ccclass('homepage')
  29. export class homepage extends Component {
  30. @property(Node) content:Node = null;
  31. @property(SpriteFrame) sfList:SpriteFrame[]=[];
  32. @property(Node) btn_setting:Node = null;
  33. @property(Node) btn_coin:Node = null;
  34. @property(Node) btn_look_video:Node = null;
  35. @property(Node) lab_coin:Node;
  36. @property(Prefab) addCoinAni:Prefab = null;
  37. @property(Node) top_node:Node = null;
  38. @property(Node) action_node:Node = null;
  39. @property(Prefab) item_prefab;
  40. protected coin_ani:Node;
  41. protected old_coin_number:number = 0;
  42. private sp_map:Map<number,SpriteFrame> = new Map();
  43. protected start(): void {
  44. this.sp_map.set(config.PLAY_TYPE.YI_QI_ZHAO_CHA,this.sfList[0])
  45. this.sp_map.set(config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG,this.sfList[1])
  46. this.sp_map.set(config.PLAY_TYPE.MEI_NV_ZHAO_XI_JIE,this.sfList[2])
  47. this.sp_map.set(config.PLAY_TYPE.ZHAO_XIANG_TONG,this.sfList[3])
  48. this.sp_map.set(config.PLAY_TYPE.ZHAO_BU_TONG,this.sfList[4])
  49. this.sp_map.set(config.PLAY_TYPE.DOU_DI_ZHU,this.sfList[5])
  50. this.sp_map.set(config.PLAY_TYPE.JI_YI_LI,this.sfList[6])
  51. this.btn_setting.on(Node.EventType.TOUCH_START,()=>{
  52. gameManager.playBtnSound()
  53. gameManager.addTopView(config.PREFAB_PATH.setting,()=>{});
  54. },this)
  55. this.btn_look_video.on(Node.EventType.TOUCH_START,()=>{
  56. SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Earn_coins_AdUnitId,(res)=>{
  57. if(res.isEnded){
  58. this.showLookVideoReward();
  59. }
  60. })
  61. })
  62. if(!PoolManager.instance.isHavePool(config.POOL_KEY)){
  63. PoolManager.instance.preloadPoolByName(config.POOL_KEY,11*15,this.item_prefab)
  64. }
  65. ClientEvent.on(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER,this.onChangeCoinNumber.bind(this),this);
  66. ClientEvent.on(config.EVENT_MSG.ON_INIT_GAME_LIST,this.initPlayList.bind(this),this);
  67. ClientEvent.on(config.EVENT_MSG.ON_BACK_HOME_VIEW,this.onBackHomeView.bind(this),this);
  68. ClientEvent.on(config.EVENT_MSG.ON_UPDATE_HOME_VIEW,this.onUpdateHomeView.bind(this),this);
  69. ClientEvent.on(config.EVENT_MSG.ON_SHOW_REWARD_VIEW,this.onShowRewardView.bind(this),this);
  70. }
  71. onShowRewardView(){
  72. this.showLookVideoReward()
  73. }
  74. onUpdateHomeView(){
  75. let self = this;
  76. for (let index = 0; index < self.content.children.length; index++) {
  77. self.content.children[index].getComponent(play_list_item).updateView();
  78. }
  79. this.old_coin_number = gameManager.get_user_coin();
  80. this.lab_coin.getComponent(Label).string = Util.coinNumberToK(this.old_coin_number);
  81. gameManager.playMainMusic(true);
  82. }
  83. onBackHomeView(){
  84. let self = this;
  85. for (let index = 0; index < self.content.children.length; index++) {
  86. self.content.children[index].getComponent(play_list_item).updateView();
  87. self.content.children[index].getComponent(play_list_item).play(index*0.2,index)
  88. }
  89. this.old_coin_number = gameManager.get_user_coin();
  90. this.lab_coin.getComponent(Label).string = Util.coinNumberToK(this.old_coin_number);
  91. gameManager.playMainMusic(true);
  92. }
  93. onChangeCoinNumber(){
  94. let self = this;
  95. let num:number = this.old_coin_number;
  96. let obj = new Component
  97. gameManager.instance.addCoinNumber( obj,gameManager.get_user_coin() - num ,self.lab_coin.getComponent(Label),num,()=>{
  98. this.old_coin_number = gameManager.get_user_coin();
  99. this.lab_coin.getComponent(Label).string = Util.coinNumberToK(this.old_coin_number);
  100. })
  101. for (let index = 0; index < self.content.children.length; index++) {
  102. self.content.children[index].getComponent(play_list_item).updateView();
  103. }
  104. }
  105. showCoinAni(){
  106. if( this.coin_ani!=null){
  107. this.coin_ani.removeFromParent()
  108. this.coin_ani = null;
  109. }
  110. if(this.addCoinAni!=null){
  111. this.coin_ani = instantiate(this.addCoinAni)
  112. this.coin_ani.parent = this.action_node;
  113. this.coin_ani.position = new Vec3(200,400,0)
  114. this.coin_ani.getComponent(addCoinAni).play(()=>{
  115. let end_pos = this.top_node.getComponent(UITransform).convertToWorldSpaceAR(this.btn_coin.position)
  116. tween(this.coin_ani).to(0.2, {position: end_pos}).hide().start()
  117. })
  118. }
  119. }
  120. showLookVideoReward(){
  121. let self = this;
  122. gameManager.addTopView(config.PREFAB_PATH.win,(node:Node)=>{
  123. let reward = gameManager.get_reward_number();
  124. node.getComponent(win).showVideoAddCoinView(reward,null,(win_view:win)=>{
  125. gameManager.add_coin(reward)
  126. setTimeout(()=>{
  127. ClientEvent.dispatchEvent(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER)
  128. },500)
  129. self.showCoinAni()
  130. win_view.close()
  131. },(win_view:win)=>{
  132. SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Earn_coins_AdUnitId,(res)=>{
  133. if(res.isEnded){
  134. tween(self.btn_setting).delay(0.5).call(()=>{
  135. reward = gameManager.get_deouble_reward_number();
  136. gameManager.add_coin(reward)
  137. win_view.showDoubleCoin(reward,()=>{
  138. self.showCoinAni()
  139. win_view.close()
  140. ClientEvent.dispatchEvent(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER)
  141. })
  142. }).start()
  143. }
  144. })
  145. });
  146. });
  147. }
  148. protected onDestroy(): void {
  149. ClientEvent.off(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER,this.onChangeCoinNumber.bind(this),this);
  150. ClientEvent.off(config.EVENT_MSG.ON_INIT_GAME_LIST,this.initPlayList.bind(this),this);
  151. ClientEvent.off(config.EVENT_MSG.ON_BACK_HOME_VIEW,this.onBackHomeView.bind(this),this);
  152. }
  153. initPlayList(){
  154. let self = this;
  155. this.content.removeAllChildren();
  156. let call_back = (prefab)=>{
  157. let list = gameManager.g_server_play_list_data.list;
  158. for (let index = 0; index < list.length; index++) {
  159. // if(list[index].id<=3){
  160. let newNode:Node = instantiate(prefab);
  161. newNode.parent = self.content;
  162. newNode.getComponent(play_list_item).initView(list[index],this.sp_map.get(list[index].id),this.onItemClick.bind(this));
  163. // }
  164. }
  165. }
  166. resources.load(config.PREFAB_PATH.play_list_item, Prefab, (err, prefab) => {
  167. if(err){
  168. console.log("err::",err);
  169. return;
  170. }
  171. call_back(prefab);
  172. });
  173. }
  174. hideAllItem(){
  175. let self = this;
  176. for (let index = 0; index < self.content.children.length; index++) {
  177. self.content.children[index].getComponent(play_list_item).hide();
  178. }
  179. }
  180. onItemClick(data:server_play_list_data_item){
  181. gameManager.playBtnSound()
  182. this.hideAllItem()
  183. switch(data.id){
  184. case config.PLAY_TYPE.YI_QI_ZHAO_CHA:
  185. {
  186. let categoryid = config.PLAY_TYPE.YI_QI_ZHAO_CHA;
  187. let levels_item = gameManager.get_cur_level_by_categoryid(categoryid);
  188. // gameManager.showLoading();
  189. http.get(config.static_url.levels_info(categoryid,levels_item.level),(err,data)=>{
  190. if(!err){
  191. let _data = JSON.parse(data)
  192. if(_data.code===config.MSG_CODE.SUCCESS){
  193. gameManager.addUiView(config.PREFAB_PATH.game_play_shuang_tu_zhao_bu_tong,(node:Node)=>{
  194. node.getComponent(view_game_play_shuang_tu_zhao_bu_tong).initView(_data.content);
  195. })
  196. }
  197. }
  198. })
  199. break;
  200. }
  201. case config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG:
  202. {
  203. let categoryid = config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG;
  204. let levels_item = gameManager.get_cur_level_by_categoryid(categoryid);
  205. // gameManager.showLoading();
  206. http.get(config.static_url.levels_info(categoryid,levels_item.level),(err,data)=>{
  207. if(!err){
  208. let _data = JSON.parse(data)
  209. if(_data.code===config.MSG_CODE.SUCCESS){
  210. if(gameManager.uiViewList.get(config.PREFAB_PATH.game_play_han_zi_zhao_bu_tong)){
  211. gameManager.uiViewList.get(config.PREFAB_PATH.game_play_han_zi_zhao_bu_tong).getComponent(view_game_play_han_zi_zhao_bu_tong).initView(_data.content);
  212. }else{
  213. gameManager.addUiView(config.PREFAB_PATH.game_play_han_zi_zhao_bu_tong,(node)=>{
  214. node.getComponent(view_game_play_han_zi_zhao_bu_tong).initView(_data.content);
  215. })
  216. }
  217. }
  218. }
  219. })
  220. break;
  221. }
  222. case config.PLAY_TYPE.MEI_NV_ZHAO_XI_JIE:
  223. {
  224. let categoryid = config.PLAY_TYPE.MEI_NV_ZHAO_XI_JIE;
  225. let levels_item = gameManager.get_cur_level_by_categoryid(categoryid);
  226. // gameManager.showLoading();
  227. http.get(config.static_url.levels_info(categoryid,levels_item.level),(err,data)=>{
  228. if(!err){
  229. let _data = JSON.parse(data)
  230. if(_data.code===config.MSG_CODE.SUCCESS){
  231. gameManager.addUiView(config.PREFAB_PATH.game_play_mei_nv_zhao_xi_jie,(node)=>{
  232. node.getComponent(view_game_play_mei_nv_zhao_xi_jie).initView(_data.content);
  233. })
  234. }
  235. }
  236. })
  237. break;
  238. }
  239. case config.PLAY_TYPE.ZHAO_XIANG_TONG:
  240. {
  241. let categoryid = config.PLAY_TYPE.ZHAO_XIANG_TONG;
  242. let levels_item = gameManager.get_cur_level_by_categoryid(categoryid);
  243. // gameManager.showLoading();
  244. http.get(config.static_url.levels_info(categoryid,levels_item.level),(err,data)=>{
  245. if(!err){
  246. let _data = JSON.parse(data)
  247. if(_data.code===config.MSG_CODE.SUCCESS){
  248. gameManager.addUiView(config.PREFAB_PATH.game_play_shuang_tu_zhao_xiang_tong,(node:Node)=>{
  249. node.getComponent(view_game_play_shuang_tu_zhao_xiang_tong).initView(_data.content);
  250. })
  251. }
  252. }
  253. })
  254. break;
  255. }
  256. case config.PLAY_TYPE.ZHAO_BU_TONG:
  257. {
  258. let categoryid = config.PLAY_TYPE.ZHAO_BU_TONG;
  259. let levels_item = gameManager.get_cur_level_by_categoryid(categoryid);
  260. // gameManager.showLoading();
  261. http.get(config.static_url.levels_info(categoryid,levels_item.level),(err,data)=>{
  262. if(!err){
  263. let _data = JSON.parse(data)
  264. if(_data.code===config.MSG_CODE.SUCCESS){
  265. gameManager.addUiView(config.PREFAB_PATH.game_play_zhao_bu_tong,(node:Node)=>{
  266. node.getComponent(view_game_play_zhao_bu_tong).initView(_data.content);
  267. })
  268. }
  269. }
  270. })
  271. break;
  272. }
  273. case config.PLAY_TYPE.DOU_DI_ZHU:
  274. {
  275. let categoryid = config.PLAY_TYPE.DOU_DI_ZHU;
  276. let levels_item = gameManager.get_cur_level_by_categoryid(categoryid);
  277. // gameManager.showLoading();
  278. console.log("斗地主")
  279. http.get(config.static_url.levels_info(categoryid,levels_item.level),(err,data)=>{
  280. if(!err){
  281. let _data = JSON.parse(data)
  282. if(_data.code===config.MSG_CODE.SUCCESS){
  283. gameManager.addUiView(config.PREFAB_PATH.game_play_dou_di_zhu,(node:Node)=>{
  284. node.getComponent(game_play_dou_di_zhu).initView(_data.content);
  285. })
  286. }
  287. }
  288. })
  289. break;
  290. }
  291. case config.PLAY_TYPE.JI_YI_LI:
  292. {
  293. let categoryid = config.PLAY_TYPE.JI_YI_LI;
  294. let levels_item = gameManager.get_cur_level_by_categoryid(categoryid);
  295. const element = gameManager.get_play_list_item_by_categoryid(config.PLAY_TYPE.JI_YI_LI);
  296. if(element.barrier_number<levels_item.level){
  297. levels_item.level = 1;
  298. gameManager.set_unlock_levels_data(levels_item)
  299. gameManager.sync_data(()=>{},config.sync_data_type.LEVELS)
  300. }
  301. if(levels_item.level<=0){
  302. levels_item.level = 1;
  303. gameManager.set_unlock_levels_data(levels_item)
  304. }
  305. http.get(config.static_url.levels_info(categoryid,levels_item.level),(err,data)=>{
  306. if(!err){
  307. let _data = JSON.parse(data)
  308. if(_data.code===config.MSG_CODE.SUCCESS){
  309. gameManager.addUiView(config.PREFAB_PATH.game_play_ji_yi_li,(node:Node)=>{
  310. node.getComponent(view_game_play_ji_yi_li).initView(_data.content);
  311. })
  312. }
  313. }
  314. })
  315. break;
  316. }
  317. }
  318. }
  319. }