GameCourse.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. GameStates: {
  5. default: null,
  6. type: cc.Node,
  7. serializable: true,
  8. },
  9. GameConfig: {
  10. default: null,
  11. type: cc.Node,
  12. serializable: true,
  13. },
  14. GameTalent: {
  15. default: null,
  16. type: cc.Node,
  17. serializable: true,
  18. },
  19. GameMakeUp: {
  20. default: null,
  21. type: cc.Node,
  22. serializable: true,
  23. },
  24. GameEloquence: {
  25. default: null,
  26. type: cc.Node,
  27. serializable: true,
  28. },
  29. GameFashion: {
  30. default: null,
  31. type: cc.Node,
  32. serializable: true,
  33. },
  34. GameDance: {
  35. default: null,
  36. type: cc.Node,
  37. serializable: true,
  38. },
  39. },
  40. OnActive()
  41. {
  42. //Game Logic
  43. this.GameConfigScript = this.GameConfig.getComponent('GameConfig');
  44. this.GameStatesScript = this.GameStates.getComponent('GameStates');
  45. //Talent
  46. this.TalentStartCourseBtn = this.GameTalent.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
  47. this.TalentFinishBtn = this.GameTalent.getChildByName('UnlockBtn').getChildByName('FinishBtn');
  48. this.TalentTotalTimeLabel = this.GameTalent.getChildByName('Panel').getChildByName('TotalTimeLabel');
  49. this.TalentTotalTimeScript = this.GameTalent.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
  50. this.TalentTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.TalentTotalTime);
  51. this.TalentProgressBar = this.GameTalent.getChildByName('Panel').getChildByName('ProgressBar');
  52. this.TalentFinishCourseFrame = this.GameTalent.getChildByName('Panel').getChildByName('GameFinishCourseFrame');
  53. //MakeUp
  54. this.MakeUpStartCourseBtn = this.GameMakeUp.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
  55. this.MakeUpFinishBtn = this.GameMakeUp.getChildByName('UnlockBtn').getChildByName('FinishBtn');
  56. this.MakeUpTotalTimeLabel = this.GameMakeUp.getChildByName('Panel').getChildByName('TotalTimeLabel');
  57. this.MakeUpTotalTimeScript = this.GameMakeUp.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
  58. this.MakeUpTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.MakeUpTotalTime);
  59. this.MakeUpProgressBar = this.GameMakeUp.getChildByName('Panel').getChildByName('ProgressBar');
  60. this.MakeUpFinishCourseFrame = this.GameMakeUp.getChildByName('Panel').getChildByName('GameFinishCourseFrame');
  61. //GameEloquence
  62. this.EloquenceStartCourseBtn = this.GameEloquence.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
  63. this.EloquenceFinishBtn = this.GameEloquence.getChildByName('UnlockBtn').getChildByName('FinishBtn');
  64. this.EloquenceTotalTimeLabel = this.GameEloquence.getChildByName('Panel').getChildByName('TotalTimeLabel');
  65. this.EloquenceTotalTimeScript = this.GameEloquence.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
  66. this.EloquenceTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.EloquenceTotalTime);
  67. this.EloquenceProgressBar = this.GameEloquence.getChildByName('Panel').getChildByName('ProgressBar');
  68. this.EloquenceFinishCourseFrame = this.GameEloquence.getChildByName('Panel').getChildByName('GameFinishCourseFrame');
  69. // Fashion
  70. this.FashionStartCourseBtn = this.GameFashion.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
  71. this.FashionFinishBtn = this.GameFashion.getChildByName('UnlockBtn').getChildByName('FinishBtn');
  72. this.FashionTotalTimeLabel = this.GameFashion.getChildByName('Panel').getChildByName('TotalTimeLabel');
  73. this.FashionTotalTimeScript = this.GameFashion.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
  74. this.FashionTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.FashionTotalTime);
  75. this.FashionProgressBar = this.GameFashion.getChildByName('Panel').getChildByName('ProgressBar');
  76. this.FashionFinishCourseFrame = this.GameFashion.getChildByName('Panel').getChildByName('GameFinishCourseFrame');
  77. // Dance
  78. this.DanceStartCourseBtn = this.GameDance.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
  79. this.DanceFinishBtn = this.GameDance.getChildByName('UnlockBtn').getChildByName('FinishBtn');
  80. this.DanceTotalTimeLabel = this.GameDance.getChildByName('Panel').getChildByName('TotalTimeLabel');
  81. this.DanceTotalTimeScript = this.GameDance.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
  82. this.DanceTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.DanceTotalTime);
  83. this.DanceProgressBar = this.GameDance.getChildByName('Panel').getChildByName('ProgressBar');
  84. this.DanceFinishCourseFrame = this.GameDance.getChildByName('Panel').getChildByName('GameFinishCourseFrame');
  85. this.GetRemainTime(function () {
  86. // Talent
  87. if( this.GameStatesScript.TalentRemainTime>0)
  88. {
  89. this.TalentFinishBtn.active=true;
  90. this.TalentFinishBtn.getComponent(cc.Button).interactable = false;
  91. this.TalentStartCourseBtn.active = false;
  92. this.ShowProgressBar('Talent');
  93. }
  94. else
  95. {
  96. if(this.GameStatesScript.BTalentFinished)
  97. {
  98. this.TalentFinishBtn.active=false;
  99. this.TalentStartCourseBtn.active = true;
  100. }
  101. else {
  102. this.TalentFinishBtn.active=true;
  103. this.TalentFinishBtn.getComponent(cc.Button).interactable = true;
  104. this.TalentStartCourseBtn.active = false;
  105. this.TalentFinishCourseFrame.active=true;
  106. }
  107. }
  108. // MakeUp
  109. if( this.GameStatesScript.MakeUpRemainTime>0)
  110. {
  111. this.MakeUpFinishBtn.active=true;
  112. this.MakeUpFinishBtn.getComponent(cc.Button).interactable = false;
  113. this.MakeUpStartCourseBtn.active = false;
  114. this.ShowProgressBar('MakeUp');
  115. }
  116. else
  117. {
  118. if(this.GameStatesScript.BMakeUpFinished)
  119. {
  120. this.MakeUpFinishBtn.active=false;
  121. this.MakeUpStartCourseBtn.active = true;
  122. }
  123. else {
  124. this.MakeUpFinishBtn.active=true;
  125. this.MakeUpFinishBtn.getComponent(cc.Button).interactable = true;
  126. this.MakeUpStartCourseBtn.active = false;
  127. this.MakeUpFinishCourseFrame.active=true;
  128. }
  129. }
  130. // Eloquence
  131. if( this.GameStatesScript.EloquenceRemainTime>0)
  132. {
  133. this.EloquenceFinishBtn.active=true;
  134. this.EloquenceFinishBtn.getComponent(cc.Button).interactable = false;
  135. this.EloquenceStartCourseBtn.active = false;
  136. this.ShowProgressBar('Eloquence');
  137. }
  138. else
  139. {
  140. if(this.GameStatesScript.BEloquenceFinished)
  141. {
  142. this.EloquenceFinishBtn.active=false;
  143. this.EloquenceStartCourseBtn.active = true;
  144. }
  145. else {
  146. this.EloquenceFinishBtn.active=true;
  147. this.EloquenceFinishBtn.getComponent(cc.Button).interactable = true;
  148. this.EloquenceStartCourseBtn.active = false;
  149. this.EloquenceFinishCourseFrame.active=true;
  150. }
  151. }
  152. // Fashion
  153. if( this.GameStatesScript.FashionRemainTime>0)
  154. {
  155. this.FashionFinishBtn.active=true;
  156. this.FashionFinishBtn.getComponent(cc.Button).interactable = false;
  157. this.FashionStartCourseBtn.active = false;
  158. this.ShowProgressBar('Fashion');
  159. }
  160. else
  161. {
  162. if(this.GameStatesScript.BFashionFinished)
  163. {
  164. this.FashionFinishBtn.active=false;
  165. this.FashionStartCourseBtn.active = true;
  166. }
  167. else {
  168. this.FashionFinishBtn.active=true;
  169. this.FashionFinishBtn.getComponent(cc.Button).interactable = true;
  170. this.FashionStartCourseBtn.active = false;
  171. this.FashionFinishCourseFrame.active=true;
  172. }
  173. }
  174. // Dance
  175. if( this.GameStatesScript.DanceRemainTime>0)
  176. {
  177. this.EloquenceFinishBtn.active=true;
  178. this.EloquenceFinishBtn.getComponent(cc.Button).interactable = false;
  179. this.EloquenceStartCourseBtn.active = false;
  180. this.ShowProgressBar('Eloquence');
  181. }
  182. else
  183. {
  184. if(this.GameStatesScript.BEloquenceFinished)
  185. {
  186. this.EloquenceFinishBtn.active=false;
  187. this.EloquenceStartCourseBtn.active = true;
  188. }
  189. else {
  190. this.EloquenceFinishBtn.active=true;
  191. this.EloquenceFinishBtn.getComponent(cc.Button).interactable = true;
  192. this.EloquenceStartCourseBtn.active = false;
  193. this.EloquenceFinishCourseFrame.active=true;
  194. }
  195. }
  196. // Fashion
  197. // Dance
  198. }.bind(this));
  199. },
  200. GetRemainTime(CallBack)
  201. {
  202. let URL = this.GameConfigScript.Host+'GameCourse/GetCourseRemainTime?Openid='+ cc.sys.localStorage.getItem('Openid');
  203. let XHR = new XMLHttpRequest();
  204. XHR.onreadystatechange = function () {
  205. if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400)) {
  206. let response = XHR.responseText;
  207. // console.log(response);
  208. let ResultJson = JSON.parse(response);
  209. this.GameStatesScript.TalentRemainTime = ResultJson.TalentRemainTime;
  210. this.GameStatesScript.BTalentFinished = ResultJson.BTalentFinished;
  211. this.GameStatesScript.MakeUpRemainTime = ResultJson.MakeUpRemainTime;
  212. this.GameStatesScript.BMakeUpFinished = ResultJson.BMakeUpFinished;
  213. this.GameStatesScript.EloquenceRemainTime = ResultJson.EloquenceRemainTime;
  214. this.GameStatesScript.BEloquenceFinished = ResultJson.BEloquenceFinished;
  215. // Fashion
  216. this.GameStatesScript.FashionRemainTime = ResultJson.FashionRemainTime;
  217. this.GameStatesScript.BFashionFinished = ResultJson.BFashionFinished;
  218. // Dance
  219. this.GameStatesScript.DanceRemainTime = ResultJson.DanceRemainTime;
  220. this.GameStatesScript.BDanceFinished = ResultJson.BDanceFinished;
  221. CallBack();
  222. }
  223. }.bind(this);
  224. XHR.open("GET", URL, true);
  225. XHR.setRequestHeader("Content-Type" , "application/json");
  226. XHR.send();
  227. },
  228. OnStarCourse(event, customEventData)
  229. {
  230. let CourseName = customEventData;
  231. let Data = 'Openid='+ cc.sys.localStorage.getItem('Openid');
  232. let URL = this.GameConfigScript.Host+'GameCourse/Start'+CourseName+'Course/';
  233. let XHR = new XMLHttpRequest();
  234. XHR.onreadystatechange = function ()
  235. {
  236. if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400))
  237. {
  238. let response = XHR.responseText;
  239. console.log(response);
  240. if(CourseName == 'Talent')
  241. {
  242. this.TalentStartCourseBtn.active = false;
  243. this.TalentFinishBtn.active = true;
  244. }
  245. else if(CourseName == 'MakeUp')
  246. {
  247. this.MakeUpStartCourseBtn.active = false;
  248. this.MakeUpFinishBtn.active = true;
  249. }
  250. else if(CourseName == 'Eloquence')
  251. {
  252. this.EloquenceStartCourseBtn.active = false;
  253. this.EloquenceFinishBtn.active = true;
  254. }
  255. else if(CourseName == 'Fashion')
  256. {
  257. this.FashionStartCourseBtn.active = false;
  258. this.FashionFinishBtn.active = true;
  259. }
  260. else if(CourseName == 'Dance')
  261. {
  262. this.DanceStartCourseBtn.active = false;
  263. this.DanceFinishBtn.active = true;
  264. }
  265. this.UpdateGameStates(response);
  266. this.GetRemainTime(function () {
  267. this.ShowProgressBar(CourseName);
  268. }.bind(this));
  269. }
  270. }.bind(this);
  271. XHR.open("POST", URL, false);
  272. XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  273. XHR.send(Data);
  274. },
  275. ShowProgressBar(CourseName)
  276. {
  277. let CourseScript = null;
  278. let TotalTime = 0;
  279. let RemainTime = 0;
  280. if(CourseName=='Talent')
  281. {
  282. this.TalentProgressBar.active = true;
  283. CourseScript = this.TalentTotalTimeScript;
  284. TotalTime= this.GameConfigScript.TalentTotalTime;
  285. RemainTime = this.GameStatesScript.TalentRemainTime;
  286. }
  287. else if(CourseName=='MakeUp')
  288. {
  289. this.MakeUpProgressBar.active = true;
  290. CourseScript = this.MakeUpTotalTimeScript;
  291. TotalTime= this.GameConfigScript.MakeUpTotalTime;
  292. RemainTime = this.GameStatesScript.MakeUpRemainTime;
  293. }
  294. else if(CourseName=='Eloquence')
  295. {
  296. this.EloquenceProgressBar.active = true;
  297. CourseScript = this.EloquenceTotalTimeScript;
  298. TotalTime= this.GameConfigScript.EloquenceTotalTime;
  299. RemainTime = this.GameStatesScript.EloquenceRemainTime;
  300. }
  301. else if(CourseName=='Fashion')
  302. {
  303. this.FashionProgressBar.active = true;
  304. CourseScript = this.FashionTotalTimeScript;
  305. TotalTime= this.GameConfigScript.FashionTotalTime;
  306. RemainTime = this.GameStatesScript.FashionRemainTime;
  307. }
  308. else if(CourseName=='Dance')
  309. {
  310. this.DanceProgressBar.active = true;
  311. CourseScript = this.DanceTotalTimeScript;
  312. TotalTime= this.GameConfigScript.DanceTotalTime;
  313. RemainTime = this.GameStatesScript.DanceRemainTime;
  314. }
  315. // 重复次数
  316. let Repeat = cc.REPEAT_FOREVER;
  317. // 以秒为单位的时间间隔
  318. let Interval = 1;
  319. // 开始延时
  320. let Delay = 0.01;
  321. let Index = Math.floor(RemainTime);
  322. let StartCourseSchedule = function()
  323. {
  324. Index--;
  325. CourseScript.SetProgressBar(Index,TotalTime);
  326. if(0==Index)
  327. {
  328. this.FinishCourse(CourseName);
  329. this.unschedule(StartCourseSchedule);
  330. }
  331. }.bind(this);
  332. this.schedule(StartCourseSchedule, Interval, Repeat-1, Delay);
  333. },
  334. FinishCourse(CourseName)
  335. {
  336. // console.log(CourseName)
  337. if(CourseName=='Talent')
  338. {
  339. this.TalentFinishBtn.getComponent(cc.Button).interactable = true;
  340. this.TalentProgressBar.active = false;
  341. this.TalentTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.TalentTotalTime);
  342. this.TalentFinishCourseFrame.active=true;
  343. }
  344. else if(CourseName=='MakeUp')
  345. {
  346. this.MakeUpFinishBtn.getComponent(cc.Button).interactable = true;
  347. this.MakeUpProgressBar.active = false;
  348. this.MakeUpTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.MakeUpTotalTime);
  349. this.MakeUpFinishCourseFrame.active=true;
  350. }
  351. else if(CourseName=='Eloquence')
  352. {
  353. this.EloquenceFinishBtn.getComponent(cc.Button).interactable = true;
  354. this.EloquenceProgressBar.active = false;
  355. this.EloquenceTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.EloquenceTotalTime);
  356. this.EloquenceFinishCourseFrame.active=true;
  357. }
  358. else if(CourseName=='Fashion')
  359. {
  360. this.FashionFinishBtn.getComponent(cc.Button).interactable = true;
  361. this.FashionProgressBar.active = false;
  362. this.FashionTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.FashionTotalTime);
  363. this.FashionFinishCourseFrame.active=true;
  364. }
  365. else if(CourseName=='Dance')
  366. {
  367. this.DanceFinishBtn.getComponent(cc.Button).interactable = true;
  368. this.DanceProgressBar.active = false;
  369. this.DanceTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.DanceTotalTime);
  370. this.DanceFinishCourseFrame.active=true;
  371. }
  372. },
  373. OnCourseFinished(event, customEventData)
  374. {
  375. let CourseName = customEventData;
  376. if(CourseName=='Talent')
  377. {
  378. this.TalentFinishBtn.getComponent(cc.Button).interactable = false;
  379. this.TalentFinishBtn.active = false;
  380. this.TalentStartCourseBtn.active = true;
  381. this.TalentFinishCourseFrame.active=false;
  382. }
  383. else if(CourseName=='MakeUp')
  384. {
  385. this.MakeUpFinishBtn.getComponent(cc.Button).interactable = false;
  386. this.MakeUpFinishBtn.active = false;
  387. this.MakeUpStartCourseBtn.active = true;
  388. this.MakeUpFinishCourseFrame.active=false;
  389. }
  390. else if(CourseName=='Eloquence')
  391. {
  392. this.EloquenceFinishBtn.getComponent(cc.Button).interactable = false;
  393. this.EloquenceFinishBtn.active = false;
  394. this.EloquenceStartCourseBtn.active = true;
  395. this.EloquenceFinishCourseFrame.active=false;
  396. }
  397. else if(CourseName=='Fashion')
  398. {
  399. this.FashionFinishBtn.getComponent(cc.Button).interactable = false;
  400. this.FashionFinishBtn.active = false;
  401. this.FashionStartCourseBtn.active = true;
  402. this.FashionFinishCourseFrame.active=false;
  403. }
  404. else if(CourseName=='Dance')
  405. {
  406. this.DanceFinishBtn.getComponent(cc.Button).interactable = false;
  407. this.DanceFinishBtn.active = false;
  408. this.DanceStartCourseBtn.active = true;
  409. this.DanceFinishCourseFrame.active=false;
  410. }
  411. let Data = 'Openid='+ cc.sys.localStorage.getItem('Openid');
  412. let URL = this.GameConfigScript.Host+'GameCourse/Finish'+CourseName+'/';
  413. // console.log(URL)
  414. let XHR = new XMLHttpRequest();
  415. XHR.onreadystatechange = function ()
  416. {
  417. if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400))
  418. {
  419. let response = XHR.responseText;
  420. this.UpdateGameStates(response);
  421. }
  422. }.bind(this);
  423. XHR.open("POST", URL, false);
  424. XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  425. XHR.send(Data);
  426. },
  427. UpdateGameStates(response)
  428. {
  429. let GameStates = JSON.parse(response);
  430. // console.log(ResultJson);
  431. // Game
  432. // let GameStates = ResultJson.GameConfig;
  433. // console.log(GameConfig);
  434. this.GameStatesScript.PlusActionPower(GameStates.ActionPower,false);
  435. this.GameStatesScript.PlusCoin(GameStates.Coin);
  436. this.GameStatesScript.PlusDiamond(GameStates.Diamond);
  437. this.GameStatesScript.PlusFans(GameStates.Fans);
  438. this.GameStatesScript.PlusFavorableImpression(GameStates.FavorableImpression);
  439. },
  440. CastSecondsToHours(Time)
  441. {
  442. if(Time<60) return Time+'秒';
  443. if(Time<3600 && Time >=60)
  444. {
  445. return Math.floor(Time/60)+'分'+Time%60+'秒';
  446. }
  447. if(Time>=3600)
  448. {
  449. let Hour = Math.floor(Time/3600);
  450. let s = Time%3600;
  451. let Min = Math.floor(s/60);
  452. return Hour+'小时'+Min+'分';
  453. }
  454. return Math.floor(Time/3600);
  455. }
  456. });