InvestItem.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /** 投资Item */
  2. import { HTTP_TYPE, MONEY_TYPE } from "../datas/CommonData";
  3. import GameM, { VIDEO_TYPE, AUDIO_TYPE } from "../manager/GameM";
  4. import { Utils } from "../utils/Utils";
  5. import UiM from "../manager/UiM";
  6. import EffectNode from "../ui/EffectNode";
  7. import RedPointM, { RedPointType } from "../manager/RedPointM";
  8. import Sciencen_M from "../utils/Sciencen_M";
  9. import Guide from "../ui/Guide";
  10. import AdM from "../manager/AdM";
  11. import Invest from "../ui/Invest";
  12. import RoleManageData from "../datas/RoleManageData";
  13. import LogUtil from "../utils/LogUtil";
  14. import InvestData from "../datas/InvestData";
  15. const { ccclass, property } = cc._decorator;
  16. @ccclass
  17. export default class InvestItem extends cc.Component {
  18. @property(cc.Label)
  19. labName: cc.Label = null;
  20. @property(cc.Label)
  21. labAward: cc.Label = null;
  22. @property(cc.Node)
  23. timeNode: cc.Node = null;
  24. @property(cc.Label)
  25. labTimeLeft: cc.Label = null;
  26. @property(cc.Sprite)
  27. icon: cc.Sprite = null;
  28. @property(cc.Sprite)
  29. moneyIcon: cc.Sprite = null;
  30. @property(cc.Node)
  31. videoIcon: cc.Node = null;
  32. @property(cc.Node)
  33. btnInvest: cc.Node = null;
  34. @property(cc.Node)
  35. btnGet: cc.Node = null;
  36. @property(cc.Node)
  37. btnSpeed: cc.Node = null;
  38. @property(cc.Node)
  39. cardInfo: cc.Node = null;
  40. @property(cc.Label)
  41. labCardTime: cc.Label = null;
  42. @property(cc.Node)
  43. videoInfo: cc.Node = null;
  44. @property(cc.Label)
  45. labVideoTime: cc.Label = null;
  46. @property(cc.Label)
  47. completeLabel: cc.Label = null;
  48. @property(sp.Skeleton)
  49. clockSk: sp.Skeleton = null
  50. /** 服务器数据 */
  51. data = null
  52. awardNum = 0
  53. /** 倒计时时间 */
  54. leftTime = 0
  55. realTime = 0
  56. interValNum = 0
  57. start() {
  58. }
  59. init(data) {
  60. this.data = data
  61. var self = this
  62. cc.loader.loadRes('gift/money_picture_' + this.data.index, cc.SpriteFrame, (err, assets) => {
  63. if (err) {
  64. cc.error(err);
  65. return;
  66. }
  67. self.icon.spriteFrame = assets;
  68. })
  69. this.freshVideoState()
  70. if (this.data.rewadr == MONEY_TYPE.gold) {
  71. let time = GameM.commonData.buyTimeTotal
  72. if (time == 0) {
  73. time = 1
  74. }
  75. this.awardNum = Math.floor(GameM.commonData.buyNumCfg[time.toString()].gold * this.data.rewardNum)
  76. cc.loader.loadRes('gift/goldIcon1', cc.SpriteFrame, (err, assets) => {
  77. if (err) {
  78. cc.error(err);
  79. return;
  80. }
  81. self.moneyIcon.spriteFrame = assets;
  82. })
  83. }
  84. else {
  85. this.awardNum = this.data.rewardNum
  86. cc.loader.loadRes('gift/redMoneyIcon1', cc.SpriteFrame, (err, assets) => {
  87. if (err) {
  88. cc.error(err);
  89. return;
  90. }
  91. self.moneyIcon.spriteFrame = assets;
  92. })
  93. }
  94. this.labAward.string = Sciencen_M.instance.format(this.awardNum.toString());// this.awardNum.toString()
  95. this.labCardTime.string = "-" + InvestData.Instance.investCardTime + "分钟"
  96. this.labVideoTime.string = "-" + Math.floor(InvestData.Instance.investVideoTime * 100) + "%时间"
  97. if (this.data.state == 0) {
  98. this.btnGet.active = false
  99. // this.timeNode.x = 1000
  100. this.leftTime = 0
  101. this.labTimeLeft.string = ""
  102. this.completeLabel.string = ""
  103. this.btnInvest.active = true
  104. this.btnSpeed.active = false
  105. }
  106. else if (this.data.state == 1) {
  107. if (this.leftTime <= 0) { //判断是否是刷新时间
  108. this.leftTime = this.data.investTime - GameM.commonData.gameTime * 0.001
  109. this.schedule(this.freshTime, 1)
  110. } else {
  111. this.realTime = this.data.investTime - GameM.commonData.gameTime * 0.001 - 1
  112. this.interValNum = Math.floor((this.leftTime - this.realTime) / 60)
  113. if (this.interValNum <= 0) {
  114. this.schedule(this.freshTime, 1)
  115. } else {
  116. this.unschedule(this.freshTime)
  117. this.clockSk.node.active = true
  118. this.clockSk.setAnimation(0, 'animation', false)
  119. this.schedule(this.fastScrollTime, 1 / 60)
  120. }
  121. }
  122. this.btnGet.active = false
  123. // this.timeNode.x = 180
  124. this.btnInvest.active = false
  125. this.btnSpeed.active = true
  126. }
  127. else if (this.data.state == 2) {
  128. this.leftTime = 0
  129. this.btnGet.active = true
  130. // this.timeNode.x = 1000
  131. this.btnInvest.active = false
  132. this.btnSpeed.active = false
  133. this.labTimeLeft.string = ""
  134. this.completeLabel.string = "已完成"
  135. }
  136. this.freshSpeedType()
  137. }
  138. freshVideoState() {
  139. if (InvestData.Instance.checkHasInvest()) {
  140. this.videoIcon.active = true
  141. }
  142. else {
  143. this.videoIcon.active = false
  144. }
  145. }
  146. freshSpeedType() {
  147. this.cardInfo.active = GameM.commonData.roleData.speedCard > 0
  148. this.videoInfo.active = GameM.commonData.roleData.speedCard <= 0
  149. }
  150. fastScrollTime() {
  151. LogUtil.logV('leftTime ', this.leftTime)
  152. this.leftTime -= this.interValNum
  153. if (this.leftTime <= this.realTime) {
  154. this.leftTime = this.realTime
  155. this.schedule(this.freshTime, 1)
  156. this.unschedule(this.fastScrollTime)
  157. this.clockSk.node.active = false
  158. // this.data.state = 2
  159. } else {
  160. // this.clockSk.node.active = false
  161. this.completeLabel.string = ""
  162. this.labTimeLeft.string = Utils.ParseTime2Format(this.leftTime)
  163. }
  164. }
  165. freshTime() {
  166. LogUtil.logV('leftTime ', this.leftTime)
  167. this.leftTime--
  168. if (this.leftTime <= 0) {
  169. this.leftTime = 0
  170. this.labTimeLeft.string = ""
  171. this.completeLabel.string = "已完成"
  172. this.unschedule(this.freshTime)
  173. this.btnGet.active = true
  174. // this.timeNode.x = 1000
  175. this.btnInvest.active = false
  176. this.btnSpeed.active = false
  177. InvestData.Instance.updateInvestProject(this.data.index, 2)
  178. // this.data.state = 2
  179. }
  180. this.completeLabel.string = ""
  181. this.labTimeLeft.string = Utils.ParseTime2Format(this.leftTime)
  182. }
  183. onDisable() {
  184. this.unschedule(this.freshTime)
  185. this.unschedule(this.fastScrollTime)
  186. }
  187. coolTime = false
  188. clickInvest() {
  189. GameM.audioM.playEffect(AUDIO_TYPE.button)
  190. if (GameM.commonData.roleData.investGuide == 1) {
  191. GameM.commonData.roleData.investGuide = 2
  192. GameM.commonData.updateRoleData()
  193. // UiM.Instance.guideNode.getComponent(Guide).nextInvest()
  194. }
  195. if (this.coolTime) {
  196. return
  197. }
  198. this.coolTime = true
  199. setTimeout(() => {
  200. this.coolTime = false
  201. }, 1000);
  202. if (InvestData.Instance.SurplusTimes <= 0) {
  203. EffectNode.instance.PlayTip("每日次数不足")
  204. return
  205. }
  206. else if (this.videoIcon.active) {
  207. InvestData.Instance.investItem = this.node
  208. GameM.adM.watchVideo(VIDEO_TYPE.invest)
  209. }
  210. else {
  211. this.btnInvest.active = false
  212. this.btnSpeed.active = true
  213. // this.timeNode.x = 198
  214. // this.data.investTime = this.data.time
  215. // this.data.state = 1
  216. // InvestData.Instance.updateInvestData()
  217. // UiM.Instance.InvestNode.getComponent(Invest).freshInvestItem()
  218. GameM.commonData.addDailyFinishTimesDataByType(4)
  219. // GameM.commonData.updateRoleData()
  220. InvestData.Instance.updateInvestProject(this.data.index, 1)
  221. RedPointM.instance.ActiveRedPoint(RedPointType.Invest, false)
  222. }
  223. }
  224. //播放视频成功
  225. onInvestVideoEnd() {
  226. this.btnInvest.active = false
  227. this.btnSpeed.active = true
  228. this.btnGet.active = false
  229. // this.timeNode.x = 198
  230. // this.data.investTime = this.data.time
  231. // this.data.state = 1
  232. // InvestData.Instance.updateInvestData()
  233. // UiM.Instance.InvestNode.getComponent(Invest).freshInvestItem()
  234. GameM.commonData.addDailyFinishTimesDataByType(4)
  235. // GameM.commonData.updateRoleData()
  236. InvestData.Instance.updateInvestProject(this.data.index, 1)
  237. RedPointM.instance.ActiveRedPoint(RedPointType.Invest, false)
  238. }
  239. // /** 投资返回 */
  240. // investBack() {
  241. // UiM.Instance.InvestNode.getComponent(Invest).updateTimes()
  242. // this.schedule(this.freshTime, 1)
  243. // }
  244. //播放加速成功
  245. onInvestVideoSpeedEnd() {
  246. InvestData.Instance.reduceTimeInvestProject(this.data.index, 2)
  247. let reduceTime = Math.floor(this.leftTime * InvestData.Instance.investVideoTime)
  248. this.leftTime = Math.floor(this.leftTime - reduceTime)
  249. if (this.leftTime < 0) {
  250. this.leftTime = 0
  251. }
  252. // InvestData.Instance.updateInvestData()
  253. let min = Math.floor(reduceTime / 60)
  254. let sec = reduceTime % 60
  255. let str = ""
  256. if (min <= 0) {
  257. str = sec + "秒"
  258. } else {
  259. if (min > 60 || sec < 10) {
  260. str = min + "分钟"
  261. } else {
  262. str = (reduceTime / 60).toFixed(1) + "分钟"
  263. }
  264. }
  265. EffectNode.instance.PlayTip(`视频加速成功,减少${str}`)
  266. // UiM.Instance.InvestNode.getComponent(Invest).freshInvestItem()
  267. if (this.leftTime <= 0) {
  268. // this.data.state = 2
  269. // UiM.Instance.InvestNode.getComponent(Invest).freshInvestItem()
  270. // GameM.httpM.sendDatas(HTTP_TYPE.updateInvestData, { 'investData': InvestData.Instance.investData })
  271. InvestData.Instance.updateInvestProject(this.data.index, 2)
  272. }
  273. }
  274. clickGet() {
  275. GameM.audioM.playEffect(AUDIO_TYPE.button)
  276. if (this.coolTime) {
  277. return
  278. }
  279. this.coolTime = true
  280. setTimeout(() => {
  281. this.coolTime = false
  282. }, 1000);
  283. this.btnInvest.active = true
  284. this.btnGet.active = false
  285. // this.data.state = 0
  286. // InvestData.Instance.updateInvestData()
  287. InvestData.Instance.updateInvestProject(this.data.index, 0)
  288. }
  289. getGold() {
  290. GameM.audioM.playEffect(AUDIO_TYPE.getGold, false)
  291. EffectNode.instance.PlayCoinAnim(0, 20, cc.v2(0, -300));
  292. let numStr = Sciencen_M.instance.format(String(this.awardNum))
  293. GameM.commonData.updateGold(String(this.awardNum))
  294. EffectNode.instance.PlayTip(`获得金币:${numStr}`)
  295. }
  296. speedClick() {
  297. GameM.audioM.playEffect(AUDIO_TYPE.button)
  298. if (this.coolTime) {
  299. return
  300. }
  301. this.coolTime = true
  302. setTimeout(() => {
  303. this.coolTime = false
  304. }, 1000);
  305. if (GameM.commonData.roleData.speedCard <= 0) {
  306. InvestData.Instance.investItem = this.node
  307. if (GameM.commonData.isVideoTest) {
  308. //测试
  309. InvestData.Instance.investItem.getComponent(InvestItem).onInvestVideoSpeedEnd()
  310. } else {
  311. GameM.adM.watchVideo(VIDEO_TYPE.investSpeed)
  312. }
  313. LogUtil.logV("speedClick", "没有加速卡")
  314. return
  315. }
  316. // 加速卡加速
  317. RoleManageData.Instance.updateInvestCard()
  318. EffectNode.instance.PlayTip(`加速卡使用成功,探索时间-10分钟`)
  319. InvestData.Instance.reduceTimeInvestProject(this.data.index, 1)
  320. AdM.onSendEvent(`item_${MONEY_TYPE.speedCard}`, `使用道具${MONEY_TYPE.speedCard}`, 'item')
  321. // this.leftTime = Math.floor(this.leftTime - InvestData.Instance.investCardTime * 60)
  322. // if (this.leftTime < 0) {
  323. // this.leftTime = 0
  324. // }
  325. // // InvestData.Instance.updateInvestData()
  326. // EffectNode.instance.PlayTip(`加速成功,减少${InvestData.Instance.investCardTime}分钟`)
  327. // AdM.onSendEvent("Investcakduse", "投资加速券使用")
  328. // // UiM.Instance.InvestNode.getComponent(Invest).freshInvestItem()
  329. // // UiM.Instance.InvestNode.getComponent(Invest).updateTimes()
  330. // if (this.leftTime <= 0) {
  331. // // this.data.state = 2
  332. // // UiM.Instance.InvestNode.getComponent(Invest).freshInvestItem()
  333. // // GameM.httpM.sendDatas(HTTP_TYPE.updateInvestData, { 'investData': InvestData.Instance.investData })
  334. // InvestData.Instance.updateInvestProject(this.data.index, 2)
  335. // }
  336. }
  337. }