UserInteraction.js 12 KB

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