homepage.ts 18 KB

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