UserInteraction.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. const interactInfo = require('../data/interactInfo');
  2. var { UserInteractionType } = require('../utils/GameEnum');
  3. var UserInformationApi = require('../net/UserInformationApi');
  4. const DWTool = require('../utils/DWTool');
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. //互动次数已满提示
  9. interactionFullNode: cc.Node,
  10. //互动CD中提示
  11. interactionCDNode: cc.Node,
  12. timeLabel: cc.Label,
  13. //亲密度显示
  14. intimacyNode: cc.Node,
  15. //反抗成功
  16. revoltSuccessNode: cc.Node,
  17. //讨好成功
  18. playUpSuccessNode: cc.Node,
  19. //互动选项节点
  20. interactionSelectNode: cc.Node,
  21. interactionTitleRichText: cc.RichText,
  22. //互动选项item
  23. interactionItemPrefab: cc.Prefab,
  24. },
  25. // LIFE-CYCLE CALLBACKS:
  26. init(uid, interactionInfo, userInformation) {
  27. this.node.parent = cc.find("Canvas");
  28. this.node.setContentSize(cc.view.getVisibleSize());
  29. this.node.active = false;
  30. this.uid = uid;
  31. this.userInformation = userInformation;
  32. this.interactionInfo = interactionInfo;
  33. },
  34. onLoad () {
  35. },
  36. start () {
  37. },
  38. onDisable() {
  39. this.node.zIndex = 0;
  40. this.hideAllNode();
  41. //销毁选项
  42. for (let child of this.interactionSelectNode.children) {
  43. if (child.getComponent('UserInteractionItem') != undefined) {
  44. child.destroy();
  45. }
  46. }
  47. this.activeNode = null;
  48. },
  49. update (dt) {
  50. if (this.interactionCDNode.active === true) {
  51. var curTimestamp = parseInt(new Date().getTime());
  52. if ((this.cdEnd - curTimestamp) > 0) {
  53. this.timeLabel.string = DWTool.calculateTime((this.cdEnd - curTimestamp) / 1000);
  54. } else {
  55. this.showInteraction();
  56. }
  57. }
  58. },
  59. closeNodeAction() {
  60. // this.node.active = false;
  61. this.hideNodeAnimation();
  62. },
  63. hideAllNode() {
  64. this.interactionFullNode.active = false;
  65. this.interactionCDNode.active = false;
  66. this.intimacyNode.active = false;
  67. this.revoltSuccessNode.active = false;
  68. this.playUpSuccessNode.active = false;
  69. this.interactionSelectNode.active = false;
  70. },
  71. showNodeAnimation(showNode) {
  72. this.activeNode = showNode;
  73. showNode.setScale(0,0);
  74. let scaleAnimation = cc.scaleTo(0.15,1,1);
  75. showNode.runAction(scaleAnimation);
  76. },
  77. hideNodeAnimation() {
  78. if (this.activeNode) {
  79. let scaleAnimation = cc.scaleTo(0.15,0,0);
  80. let finish = cc.callFunc(() => {
  81. this.node.active = false;
  82. }, this);
  83. let sequenceAcation = cc.sequence(scaleAnimation, finish);
  84. this.activeNode.runAction(sequenceAcation);
  85. } else {
  86. this.node.active = false;
  87. }
  88. },
  89. //亲密度显示
  90. showIntimacyInfo(isCommon) {
  91. this.node.zIndex += 1;
  92. this.node.active = true;
  93. this.hideAllNode();
  94. this.intimacyNode.active = true;
  95. this.showNodeAnimation(this.intimacyNode);
  96. this.intimacyMng = this.intimacyNode.getComponent('UserIntimacyInfo');
  97. this.intimacyMng.configIntimacyInfo(this.interactionInfo,true, isCommon);
  98. },
  99. //互动操作相关
  100. showInteraction(interactionType) {
  101. this.node.zIndex += 1;
  102. this.node.active = true;
  103. if (interactionType != undefined) {
  104. this.interactionType = interactionType;
  105. }
  106. if (this.interactionInfo.cnt >= 2) {
  107. this.interactionIsFull();
  108. return;
  109. }
  110. if (this.interactionIsCD() == true ) {
  111. return;
  112. }
  113. this.hideAllNode();
  114. this.interactionSelectNode.active = true;
  115. this.showNodeAnimation(this.interactionSelectNode);
  116. var originY = (this.interactionSelectNode.height/2) - 85;
  117. switch (this.interactionType) {
  118. case UserInteractionType.Common:
  119. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>互 动</b></outline>";
  120. interactInfo.interactInfo5.forEach(item => {
  121. var itemNode = cc.instantiate(this.interactionItemPrefab);
  122. itemNode.parent = this.interactionSelectNode;
  123. itemNode.y = originY;
  124. originY -= (18 + 80);
  125. var itemMng = itemNode.getComponent('UserInteractionItem');
  126. itemMng.init(this);
  127. itemMng.configData(item);
  128. });
  129. break;
  130. case UserInteractionType.PlayUp:
  131. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>讨 好</b></outline>";
  132. interactInfo.interactInfo1.forEach(item => {
  133. var itemNode = cc.instantiate(this.interactionItemPrefab);
  134. itemNode.parent = this.interactionSelectNode;
  135. itemNode.y = originY;
  136. originY -= (18 + 80);
  137. var itemMng = itemNode.getComponent('UserInteractionItem');
  138. itemMng.init(this);
  139. itemMng.configData(item);
  140. });
  141. break;
  142. case UserInteractionType.Revolt:
  143. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>反 抗</b></outline>";
  144. interactInfo.interactInfo2.forEach(item => {
  145. var itemNode = cc.instantiate(this.interactionItemPrefab);
  146. itemNode.parent = this.interactionSelectNode;
  147. itemNode.y = originY;
  148. originY -= (18 + 80);
  149. var itemMng = itemNode.getComponent('UserInteractionItem');
  150. itemMng.init(this);
  151. itemMng.configData(item);
  152. });
  153. break;
  154. case UserInteractionType.Pacify:
  155. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>安 抚</b></outline>";
  156. interactInfo.interactInfo3.forEach(item => {
  157. var itemNode = cc.instantiate(this.interactionItemPrefab);
  158. itemNode.parent = this.interactionSelectNode;
  159. itemNode.y = originY;
  160. originY -= (18 + 80);
  161. var itemMng = itemNode.getComponent('UserInteractionItem');
  162. itemMng.init(this);
  163. itemMng.configData(item);
  164. });
  165. break;
  166. case UserInteractionType.Order:
  167. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>使 唤</b></outline>";
  168. interactInfo.interactInfo4.forEach(item => {
  169. var itemNode = cc.instantiate(this.interactionItemPrefab);
  170. itemNode.parent = this.interactionSelectNode;
  171. itemNode.y = originY;
  172. originY -= (18 + 80);
  173. var itemMng = itemNode.getComponent('UserInteractionItem');
  174. itemMng.init(this);
  175. itemMng.configData(item);
  176. });
  177. break;
  178. }
  179. },
  180. selectInteraction() {
  181. UserInformationApi.postInteract(this.uid, this.interactionType, (responseData) => {
  182. console.log('互动操作成功' + JSON.stringify(responseData));
  183. this.interactionResponse(responseData);
  184. this.userInformation.configIntimacy(responseData);
  185. }, (error) => {
  186. console.log('互动操作失败' + error);
  187. });
  188. },
  189. interactionResponse(responseData) {
  190. this.hideAllNode();
  191. switch (this.interactionType) {
  192. case UserInteractionType.Common:
  193. this.playUpSuccessNode.active = true;
  194. this.showNodeAnimation(this.playUpSuccessNode);
  195. break;
  196. case UserInteractionType.PlayUp:
  197. this.playUpSuccessNode.active = true;
  198. this.showNodeAnimation(this.playUpSuccessNode);
  199. break;
  200. case UserInteractionType.Revolt:
  201. this.revoltSuccessNode.active = true;
  202. this.showNodeAnimation(this.revoltSuccessNode);
  203. break;
  204. case UserInteractionType.Pacify:
  205. this.playUpSuccessNode.active = true;
  206. this.showNodeAnimation(this.playUpSuccessNode);
  207. break;
  208. case UserInteractionType.Order:
  209. this.revoltSuccessNode.active = true;
  210. this.showNodeAnimation(this.revoltSuccessNode);
  211. break;
  212. }
  213. this.interactionInfo.cd = responseData.cd;
  214. this.interactionInfo.cdEnd = responseData.cdEnd;
  215. this.interactionInfo.cnt = responseData.cnt;
  216. this.interactionInfo.heart = responseData.heart;
  217. this.interactionInfo.intimacy = responseData.intimacy;
  218. this.playUpSuccessMng = this.playUpSuccessNode.getComponent('UserIntimacyInfo');
  219. let isCommon = this.interactionType == UserInteractionType.Common ? true : false;
  220. this.playUpSuccessMng.configIntimacyInfo(this.interactionInfo,false, isCommon);
  221. },
  222. //当天互动次数已满
  223. interactionIsFull() {
  224. this.hideAllNode();
  225. this.interactionFullNode.active = true;
  226. this.showNodeAnimation(this.interactionFullNode);
  227. },
  228. //正在CD中
  229. interactionIsCD() {
  230. this.hideAllNode();
  231. var curTimestamp = parseInt(new Date().getTime()); //当前时间戳
  232. this.cdEnd = this.interactionInfo.cdEnd;
  233. if ((this.cdEnd - curTimestamp) > 0) {
  234. this.interactionCDNode.active = true;
  235. this.showNodeAnimation(this.interactionCDNode);
  236. this.timeLabel.string = DWTool.calculateTime((this.cdEnd - curTimestamp) / 1000);
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. },
  242. //讨好、安抚成功分享
  243. playUpShare() {
  244. if (!CC_WECHATGAME) {
  245. return;
  246. }
  247. var shareTitle = '一起来互动增长亲密度吧!';
  248. window.wx.shareAppMessage({
  249. title: shareTitle,
  250. imageUrl: 'http://t2.hddhhn.com/uploads/tu/201806/9999/eae9e74ec8.jpg',
  251. });
  252. },
  253. //反抗、使唤成功分享
  254. revoltShare() {
  255. if (!CC_WECHATGAME) {
  256. return;
  257. }
  258. var shareTitle = '真解气,一起来反抗老板的剥削!';
  259. if (this.interactionType == UserInteractionType.Order) {
  260. shareTitle = '真爽,一起来使唤艺人!';
  261. }
  262. window.wx.shareAppMessage({
  263. title: shareTitle,
  264. imageUrl: 'http://t2.hddhhn.com/uploads/tu/201806/9999/eae9e74ec8.jpg',
  265. });
  266. },
  267. //邀请互动
  268. inviteInteractionShare() {
  269. if (!CC_WECHATGAME) {
  270. return;
  271. }
  272. var shareTitle = '一起来互动增长亲密度吧!';
  273. window.wx.shareAppMessage({
  274. title: shareTitle,
  275. imageUrl: 'http://t2.hddhhn.com/uploads/tu/201806/9999/eae9e74ec8.jpg',
  276. });
  277. }
  278. });