UserInteraction.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. const interactInfo = require('../data/interactInfo');
  2. var { UserInteractionType, UserIntimacyType } = require('../utils/GameEnum');
  3. var UserInformationApi = require('../net/UserInformationApi');
  4. const DWTool = require('../utils/DWTool');
  5. const GameModule = require('../utils/GameModule');
  6. const interactImage = [
  7. {
  8. image: 'interact_1.png',
  9. title: '为你点了一杯咖啡,点击查收'
  10. },
  11. {
  12. image: 'interact_2.png',
  13. title: '这个礼物。。。你喜欢么?'
  14. },
  15. {
  16. image: 'interact_3.png',
  17. title: '五星级料理,你来我就请!'
  18. },
  19. ]
  20. cc.Class({
  21. extends: cc.Component,
  22. properties: {
  23. //互动次数已满提示
  24. interactionFullNode: cc.Node,
  25. //互动CD中提示
  26. interactionCDNode: cc.Node,
  27. timeLabel: cc.Label,
  28. //亲密度显示
  29. intimacyNode: cc.Node,
  30. //反抗成功
  31. revoltSuccessNode: cc.Node,
  32. //讨好成功
  33. playUpSuccessNode: cc.Node,
  34. //互动选项节点
  35. interactionSelectNode: cc.Node,
  36. interactionTitleRichText: cc.RichText,
  37. interactionCountLabel: cc.Label,
  38. //互动选项item
  39. interactionItemPrefab: cc.Prefab,
  40. },
  41. // LIFE-CYCLE CALLBACKS:
  42. init(uid, interactionInfo, userInformation) {
  43. this.node.parent = cc.find("Canvas");
  44. this.node.setContentSize(cc.view.getVisibleSize());
  45. this.node.active = false;
  46. this.uid = uid;
  47. this.userInformation = userInformation;
  48. this.interactionInfo = interactionInfo;
  49. },
  50. onLoad() {
  51. GameModule.homeGuide.on('Fire_state26', this.selectInteraction, this);
  52. GameModule.homeGuide.on('Fire_state27', this.playUpShare, this);
  53. },
  54. start() {
  55. },
  56. onDisable() {
  57. this.node.zIndex = 0;
  58. this.hideAllNode();
  59. //销毁选项
  60. for (let child of this.interactionSelectNode.children) {
  61. if (child.getComponent('UserInteractionItem') != undefined) {
  62. child.destroy();
  63. }
  64. }
  65. this.activeNode = null;
  66. },
  67. update(dt) {
  68. if (this.interactionCDNode.active === true) {
  69. var curTimestamp = parseInt(new Date().getTime());
  70. if ((this.cdEnd - curTimestamp) > 0) {
  71. this.timeLabel.string = DWTool.calculateTime((this.cdEnd - curTimestamp) / 1000);
  72. } else {
  73. this.showInteraction();
  74. }
  75. }
  76. },
  77. closeNodeAction() {
  78. // this.node.active = false;
  79. this.hideNodeAnimation();
  80. },
  81. hideAllNode() {
  82. this.interactionFullNode.active = false;
  83. this.interactionCDNode.active = false;
  84. this.intimacyNode.active = false;
  85. this.revoltSuccessNode.active = false;
  86. this.playUpSuccessNode.active = false;
  87. this.interactionSelectNode.active = false;
  88. },
  89. showNodeAnimation(showNode) {
  90. this.activeNode = showNode;
  91. showNode.setScale(0, 0);
  92. let scaleAnimation = cc.scaleTo(0.15, 1, 1);
  93. showNode.runAction(scaleAnimation);
  94. },
  95. hideNodeAnimation() {
  96. if (this.activeNode) {
  97. let scaleAnimation = cc.scaleTo(0.15, 0, 0);
  98. let finish = cc.callFunc(() => {
  99. this.node.active = false;
  100. }, this);
  101. let sequenceAcation = cc.sequence(scaleAnimation, finish);
  102. this.activeNode.runAction(sequenceAcation);
  103. } else {
  104. this.node.active = false;
  105. }
  106. },
  107. //亲密度显示
  108. showIntimacyInfo(isCommon) {
  109. this.node.zIndex += 1;
  110. this.node.active = true;
  111. this.hideAllNode();
  112. this.intimacyNode.active = true;
  113. this.showNodeAnimation(this.intimacyNode);
  114. this.intimacyMng = this.intimacyNode.getComponent('UserIntimacyInfo');
  115. this.intimacyMng.configIntimacyInfo(this.interactionInfo, true, isCommon);
  116. },
  117. //互动操作相关
  118. showInteraction(interactionType) {
  119. this.node.zIndex += 1;
  120. this.node.active = true;
  121. if (interactionType != undefined) {
  122. this.interactionType = interactionType;
  123. }
  124. if (this.interactionInfo.cnt >= 5) {
  125. this.interactionIsFull();
  126. return;
  127. }
  128. if (this.interactionIsCD() == true) {
  129. return;
  130. }
  131. this.hideAllNode();
  132. this.interactionSelectNode.active = true;
  133. this.showNodeAnimation(this.interactionSelectNode);
  134. if (this.interactionInfo.cnt) {
  135. this.interactionCountLabel.string = `剩余互动次数:${(5 - this.interactionInfo.cnt)}/5`;
  136. } else {
  137. this.interactionCountLabel.string = '剩余互动次数:5/5';
  138. }
  139. var originY = (this.interactionSelectNode.height / 2) - 120;
  140. switch (this.interactionType) {
  141. case UserInteractionType.Common:
  142. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>互 动</b></outline>";
  143. interactInfo.interactInfo5.forEach(item => {
  144. var itemNode = cc.instantiate(this.interactionItemPrefab);
  145. itemNode.parent = this.interactionSelectNode;
  146. itemNode.y = originY;
  147. originY -= (16 + 80);
  148. var itemMng = itemNode.getComponent('UserInteractionItem');
  149. itemMng.init(this);
  150. itemMng.configData(item);
  151. });
  152. break;
  153. case UserInteractionType.PlayUp:
  154. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>讨 好</b></outline>";
  155. interactInfo.interactInfo1.forEach(item => {
  156. var itemNode = cc.instantiate(this.interactionItemPrefab);
  157. itemNode.parent = this.interactionSelectNode;
  158. itemNode.y = originY;
  159. originY -= (16 + 80);
  160. var itemMng = itemNode.getComponent('UserInteractionItem');
  161. itemMng.init(this);
  162. itemMng.configData(item);
  163. });
  164. break;
  165. case UserInteractionType.Revolt:
  166. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>反 抗</b></outline>";
  167. interactInfo.interactInfo2.forEach(item => {
  168. var itemNode = cc.instantiate(this.interactionItemPrefab);
  169. itemNode.parent = this.interactionSelectNode;
  170. itemNode.y = originY;
  171. originY -= (16 + 80);
  172. var itemMng = itemNode.getComponent('UserInteractionItem');
  173. itemMng.init(this);
  174. itemMng.configData(item);
  175. });
  176. break;
  177. case UserInteractionType.Pacify:
  178. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>安 抚</b></outline>";
  179. interactInfo.interactInfo3.forEach(item => {
  180. var itemNode = cc.instantiate(this.interactionItemPrefab);
  181. itemNode.parent = this.interactionSelectNode;
  182. itemNode.y = originY;
  183. originY -= (16 + 80);
  184. var itemMng = itemNode.getComponent('UserInteractionItem');
  185. itemMng.init(this);
  186. itemMng.configData(item);
  187. });
  188. GameModule.homeGuide.getComponent('HomeGuide').handleState('state26');
  189. break;
  190. case UserInteractionType.Order:
  191. this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>使 唤</b></outline>";
  192. interactInfo.interactInfo4.forEach(item => {
  193. var itemNode = cc.instantiate(this.interactionItemPrefab);
  194. itemNode.parent = this.interactionSelectNode;
  195. itemNode.y = originY;
  196. originY -= (16 + 80);
  197. var itemMng = itemNode.getComponent('UserInteractionItem');
  198. itemMng.init(this);
  199. itemMng.configData(item);
  200. });
  201. break;
  202. }
  203. },
  204. selectInteraction() {
  205. UserInformationApi.postInteract(this.uid, this.interactionType, (responseData) => {
  206. this.interactionInfo = responseData;
  207. this.interactionResponse(responseData);
  208. this.userInformation.configIntimacy(responseData);
  209. }, (error) => {
  210. console.log('互动失败' + error);
  211. });
  212. },
  213. interactionResponse(responseData) {
  214. this.hideAllNode();
  215. switch (this.interactionType) {
  216. case UserInteractionType.Common:
  217. this.playUpSuccessNode.active = true;
  218. this.showNodeAnimation(this.playUpSuccessNode);
  219. break;
  220. case UserInteractionType.PlayUp:
  221. if (responseData.heart == UserIntimacyType.Full) {
  222. this.intimacyNode.active = true;
  223. this.showNodeAnimation(this.intimacyNode);
  224. this.intimacyMng = this.intimacyNode.getComponent('UserIntimacyInfo');
  225. this.intimacyMng.configIntimacyInfo(this.interactionInfo, true, false);
  226. } else {
  227. this.playUpSuccessNode.active = true;
  228. this.showNodeAnimation(this.playUpSuccessNode);
  229. }
  230. break;
  231. case UserInteractionType.Revolt:
  232. this.revoltSuccessNode.active = true;
  233. this.showNodeAnimation(this.revoltSuccessNode);
  234. break;
  235. case UserInteractionType.Pacify:
  236. if (responseData.heart == UserIntimacyType.Full) {
  237. this.intimacyNode.active = true;
  238. this.showNodeAnimation(this.intimacyNode);
  239. this.intimacyMng = this.intimacyNode.getComponent('UserIntimacyInfo');
  240. this.intimacyMng.configIntimacyInfo(this.interactionInfo, true, false);
  241. } else {
  242. this.playUpSuccessNode.active = true;
  243. this.showNodeAnimation(this.playUpSuccessNode);
  244. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state26', 'state27');
  245. }
  246. break;
  247. case UserInteractionType.Order:
  248. this.revoltSuccessNode.active = true;
  249. this.showNodeAnimation(this.revoltSuccessNode);
  250. break;
  251. }
  252. this.interactionInfo.cd = responseData.cd;
  253. this.interactionInfo.cdEnd = responseData.cdEnd;
  254. this.interactionInfo.cnt = responseData.cnt;
  255. this.interactionInfo.heart = responseData.heart;
  256. this.interactionInfo.intimacy = responseData.intimacy;
  257. this.playUpSuccessMng = this.playUpSuccessNode.getComponent('UserIntimacyInfo');
  258. let isCommon = this.interactionType == UserInteractionType.Common ? true : false;
  259. this.playUpSuccessMng.configIntimacyInfo(this.interactionInfo, false, isCommon);
  260. },
  261. //当天互动次数已满
  262. interactionIsFull() {
  263. this.hideAllNode();
  264. this.interactionFullNode.active = true;
  265. this.showNodeAnimation(this.interactionFullNode);
  266. },
  267. //正在CD中
  268. interactionIsCD() {
  269. this.hideAllNode();
  270. var curTimestamp = parseInt(new Date().getTime()); //当前时间戳
  271. this.cdEnd = this.interactionInfo.cdEnd;
  272. if ((this.cdEnd - curTimestamp) > 0) {
  273. this.interactionCDNode.active = true;
  274. this.showNodeAnimation(this.interactionCDNode);
  275. this.timeLabel.string = DWTool.calculateTime((this.cdEnd - curTimestamp) / 1000);
  276. return true;
  277. } else {
  278. return false;
  279. }
  280. },
  281. //消耗钻石加速CD时间
  282. speedCDAction() {
  283. UserInformationApi.postSpeedInteraction(this.uid, (responseData) => {
  284. this.interactionInfo.cnt += 1;
  285. this.interactionInfo.cdEnd = 0;
  286. GameModule.userInfo.diamond -= 20;
  287. this.showInteraction();
  288. }, (errCode, errMsg) => {
  289. if (CC_WECHATGAME) {
  290. wx.showModal({
  291. title: "提示",
  292. content: errMsg
  293. });
  294. } else {
  295. alert(errMsg);
  296. }
  297. });
  298. },
  299. //讨好、安抚成功分享
  300. playUpShare() {
  301. if (!CC_WECHATGAME) {
  302. return;
  303. }
  304. let shareItem = interactImage[Math.floor(Math.random() * 3)]
  305. var shareTitle = Global.user.nick + ':' + shareItem.title;
  306. window.wx.shareAppMessage({
  307. title: shareTitle,
  308. imageUrl: 'https://pub.dwstatic.com/wxgame/allstar/share/' + shareItem.image,
  309. success: (res) => {
  310. },
  311. fail: (res) => {
  312. }
  313. });
  314. },
  315. //反抗、使唤成功分享
  316. revoltShare() {
  317. if (!CC_WECHATGAME) {
  318. return;
  319. }
  320. var shareTitle = '真解气,一起来反抗老板的剥削!';
  321. if (this.interactionType == UserInteractionType.Order) {
  322. shareTitle = '真爽,一起来使唤艺人!';
  323. }
  324. window.wx.shareAppMessage({
  325. title: shareTitle,
  326. imageUrl: 'https://pub.dwstatic.com/wxgame/allstar/share/share1.jpg',
  327. });
  328. },
  329. //邀请互动
  330. inviteInteractionShare() {
  331. if (!CC_WECHATGAME) {
  332. return;
  333. }
  334. var shareTitle = this.userInformation.userInfo.nick + '你今天还没有和我进行互动~快来一起玩';
  335. window.wx.shareAppMessage({
  336. title: shareTitle,
  337. imageUrl: 'https://pub.dwstatic.com/wxgame/allstar/share/interact_0.png',
  338. });
  339. }
  340. });