GameStates.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. BDebug: {
  5. default: true,
  6. serializable: true,
  7. },
  8. ActionPowerlabel: {
  9. default: null,
  10. type: cc.Node,
  11. serializable: true,
  12. },
  13. GameConfig: {
  14. default: null,
  15. type: cc.Node,
  16. serializable: true,
  17. },
  18. },
  19. onLoad () {
  20. this.ActionPower = 99;
  21. this.Coin = 5000;
  22. this.Diamond = 40;
  23. this.Fans=0;
  24. this.FavorableImpression=0;
  25. this.TalentRemainTime = 0;
  26. this.BTalentFinished = 0;
  27. this.MakeUpRemainTime = 0;
  28. this.BMakeUpFinished = 0;
  29. this.EloquenceRemainTime = 0;
  30. this.BEloquenceFinished = 0;
  31. this.FashionRemainTime = 0;
  32. this.BFashionFinished = 0;
  33. this.DanceRemainTime = 0;
  34. this.BDanceFinished = 0;
  35. //Event
  36. this.VarietyShowRemainTime = 0;
  37. this.BVarietyShowFinished = 0;
  38. this.InterviewRemainTime = 0;
  39. this.BInterviewFinished = 0;
  40. this.MovieRemainTime = 0;
  41. this.BMovieFinished = 0;
  42. this.HangOutRemainTime = 0;
  43. this.BHangOutFinished = 0;
  44. this.GirlAnimations = ['Idle'];
  45. },
  46. start () {
  47. this.GameConfigScript = this.GameConfig.getComponent('GameConfig');
  48. },
  49. //actionPower
  50. GetActionPower () {
  51. if(!this.BDebug) this.GetValueFromServer('ActionPower');
  52. return this.ActionPower;
  53. },
  54. SetActionPower (ActionPower) {
  55. this.SetValueFromServer('ActionPower',ActionPower);
  56. },
  57. PlusActionPower (Num,bUpdate) {
  58. let CurrentNum = this.ActionPower;
  59. let NewNum =CurrentNum + Num;
  60. if(bUpdate) this.SetValueFromServer('ActionPower',NewNum);
  61. this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
  62. this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
  63. },
  64. //Coin
  65. GetCoin () {
  66. if(!this.BDebug) this.GetValueFromServer('Coin');
  67. return this.Coin;
  68. },
  69. SetCoin (Coin) {
  70. this.SetValueFromServer('Coin',Coin);
  71. },
  72. PlusCoin (Num,bUpdate) {
  73. let CurrentNum = this.Coin;
  74. let NewNum =CurrentNum + Num;
  75. if(bUpdate) this.SetValueFromServer('Coin',NewNum);
  76. // this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
  77. // this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
  78. },
  79. //diamond
  80. GetDiamond () {
  81. if(!this.BDebug) this.GetValueFromServer('Diamond');
  82. return this.Diamond;
  83. },
  84. SetDiamond (Diamond) {
  85. this.SetValueFromServer('Diamond',Diamond);
  86. },
  87. PlusDiamond (Num,bUpdate) {
  88. let CurrentNum = this.Diamond;
  89. let NewNum =CurrentNum + Num;
  90. if(bUpdate) this.SetValueFromServer('Diamond',NewNum);
  91. // this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
  92. // this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
  93. },
  94. //fans
  95. GetFans () {
  96. if(!this.BDebug) this.GetValueFromServer('Fans');
  97. return this.Fans;
  98. },
  99. SetFans (Fans) {
  100. this.SetValueFromServer('Fans',Fans);
  101. },
  102. PlusFans (Num,bUpdate) {
  103. let CurrentNum = this.Fans;
  104. let NewNum =CurrentNum + Num;
  105. if(bUpdate) this.SetValueFromServer('Fans',NewNum);
  106. // this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
  107. // this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
  108. },
  109. //favorableImpression
  110. GetFavorableImpression () {
  111. if(!this.BDebug) this.GetValueFromServer('FavorableImpression');
  112. return this.FavorableImpression;
  113. },
  114. SetFavorableImpression (FavorableImpression) {
  115. this.SetValueFromServer('FavorableImpression',FavorableImpression);
  116. },
  117. PlusFavorableImpression (Num,bUpdate) {
  118. let CurrentNum = this.FavorableImpression;
  119. let NewNum =CurrentNum + Num;
  120. if(bUpdate) this.SetValueFromServer('FavorableImpression',NewNum);
  121. // this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
  122. // this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
  123. },
  124. GetValueFromServer(Key)
  125. {
  126. let URL = this.GameConfigScript.Host+'GameStates/Get'+Key+'/?Openid='+ cc.sys.localStorage.getItem('Openid');
  127. let XHR = new XMLHttpRequest();
  128. let Self = this;
  129. XHR.onreadystatechange = function () {
  130. if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400)) {
  131. let response = XHR.responseText;
  132. Self.SaveValueFromServer(Key,response);
  133. // console.log(response);
  134. }
  135. };
  136. XHR.open("GET", URL, false);
  137. XHR.setRequestHeader("Content-Type" , "application/json");
  138. XHR.send();
  139. },
  140. SetValueFromServer(Key,Value)
  141. {
  142. let Data = 'Openid='+ cc.sys.localStorage.getItem('Openid')+'&'+'Value='+Value;
  143. let URL = this.GameConfigScript.Host+'GameStates/Set'+Key+'/';
  144. let XHR = new XMLHttpRequest();
  145. var Self =this;
  146. XHR.onreadystatechange = function ()
  147. {
  148. if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400))
  149. {
  150. let response = XHR.responseText;
  151. // console.log(response);
  152. Self.SaveValueFromServer(Key,response);
  153. }
  154. };
  155. XHR.open("POST", URL, false);
  156. XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  157. XHR.send(Data);
  158. },
  159. SaveValueFromServer(Key,Value)
  160. {
  161. if(Key=='ActionPower')
  162. {
  163. this.ActionPower = Number(Value);
  164. }
  165. else if(Key=='Coin')
  166. {
  167. this.Coin = Number(Value);
  168. }
  169. else if(Key=='Diamond')
  170. {
  171. this.Diamond = Number(Value);
  172. }
  173. else if(Key=='Fans')
  174. {
  175. this.Fans = Number(Value);
  176. }
  177. else if(Key=='FavorableImpression')
  178. {
  179. this.FavorableImpression = Number(Value);
  180. }
  181. }
  182. });