ShopItem.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /** 商店Item */
  2. import GameM, { AUDIO_TYPE } from "../manager/GameM";
  3. import UiM, { PANEL_NAME } from "../manager/UiM";
  4. import Main from "../Main";
  5. import Sciencen_M from "../utils/Sciencen_M";
  6. import Shop from "../ui/Shop";
  7. import EffectNode from "../ui/EffectNode";
  8. import LocalManagerData from "../datas/LocalManagerData";
  9. import ShopData from "../datas/ShopData";
  10. const { ccclass, property } = cc._decorator;
  11. @ccclass
  12. export default class ShopItem extends cc.Component {
  13. @property(cc.Button)
  14. btnShopBuy: cc.Button = null;
  15. @property(cc.Node)
  16. lock: cc.Node = null;
  17. @property(cc.Node)
  18. free: cc.Node = null;
  19. @property(cc.Node)
  20. price: cc.Node = null;
  21. @property(cc.Node)
  22. iconCarEmpty: cc.Node = null;
  23. @property(cc.Node)
  24. car: cc.Node = null;
  25. @property(cc.Label)
  26. labLevel: cc.Label = null;
  27. @property(cc.Label)
  28. labPrice: cc.Label = null;
  29. @property(cc.Node)
  30. iconGold: cc.Node = null;
  31. @property(cc.Node)
  32. iconMoney: cc.Node = null;
  33. @property(cc.Label)
  34. label_lockTip: cc.Label = null;
  35. @property(cc.Node)
  36. desNode: cc.Node = null;
  37. @property(cc.Label)
  38. labCircle: cc.Label = null;
  39. @property(cc.Label)
  40. labLeftTimes: cc.Label = null;
  41. //类型
  42. type = 0
  43. //当前车价格
  44. curPrice = '0'
  45. normalPrice = '0'
  46. percent = 0
  47. resetTimes = 0
  48. start() {
  49. }
  50. init(type) {
  51. this.type = type
  52. cc.loader.loadRes('carPic/head/head_' + this.type.toString(), cc.SpriteFrame, (err, assets) => {
  53. if (err) {
  54. cc.error(err);
  55. return;
  56. }
  57. this.car.getComponent(cc.Sprite).spriteFrame = assets
  58. })
  59. this.labLevel.string = this.type.toString()
  60. this.refreshPrice(true)
  61. }
  62. updateLeftTimes() {
  63. let times = LocalManagerData.Instance.getBuyCarData(this.type)
  64. let maxbuycarnum = GameM.commonData.globalCfg.maxbuycarnum
  65. if (maxbuycarnum == null || maxbuycarnum == undefined) {
  66. maxbuycarnum = 0
  67. }
  68. if (maxbuycarnum == 0) {
  69. this.labLeftTimes.string = ""
  70. return
  71. }
  72. let leftTimes = maxbuycarnum - times
  73. if (leftTimes < 0) {
  74. leftTimes = 0
  75. }
  76. this.labLeftTimes.string = "购买次数:" + leftTimes
  77. }
  78. refreshPrice(init = false) {
  79. let max = Math.max(GameM.commonData.maxCarLevel - 6, 4);
  80. this.labLeftTimes.string = ""
  81. if (this.type <= max) {
  82. this.iconCarEmpty.active = false
  83. this.desNode.active = true
  84. this.btnShopBuy.interactable = true
  85. this.desNode.active = true
  86. this.curPrice = GameM.commonData.getPriceByType(this.type)
  87. this.labCircle.node.active = true
  88. this.labCircle.string = Sciencen_M.instance.format(GameM.commonData.getFristPriceByType(this.type), false)
  89. this.iconMoney.active = false
  90. this.iconGold.active = true
  91. this.labPrice.string = Sciencen_M.instance.format(this.curPrice)
  92. if (!this.car.active) {
  93. cc.loader.loadRes('carPic/head/head_' + this.type.toString(), cc.SpriteFrame, (err, assets) => {
  94. if (err) {
  95. cc.error(err);
  96. return;
  97. }
  98. this.car.getComponent(cc.Sprite).spriteFrame = assets
  99. this.car.active = true
  100. })
  101. }
  102. this.price.active = true
  103. this.free.active = false
  104. this.updateLeftTimes()
  105. }
  106. else if (this.type <= max + 2) {
  107. if (!GameM.commonData.redSwitch) {
  108. this.iconCarEmpty.active = false
  109. this.desNode.active = true
  110. this.btnShopBuy.interactable = true
  111. this.labCircle.node.active = true
  112. this.labCircle.string = Sciencen_M.instance.format(GameM.commonData.getFristPriceByType(this.type), false)
  113. this.lock.active = false
  114. this.free.active = false
  115. this.curPrice = GameM.commonData.getMoneyPriceBuyType(this.type)
  116. this.iconMoney.active = true
  117. this.iconGold.active = false
  118. this.labPrice.string = Sciencen_M.instance.format(this.curPrice)
  119. if (!this.car.active) {
  120. cc.loader.loadRes('carPic/head/head_' + this.type.toString(), cc.SpriteFrame, (err, assets) => {
  121. if (err) {
  122. cc.error(err);
  123. return;
  124. }
  125. this.car.getComponent(cc.Sprite).spriteFrame = assets
  126. this.car.active = true
  127. })
  128. }
  129. this.price.active = true
  130. this.updateLeftTimes()
  131. }
  132. else {
  133. this.labCircle.node.active = false
  134. this.iconCarEmpty.active = true
  135. this.desNode.active = false
  136. this.car.active = false
  137. this.btnShopBuy.interactable = false
  138. this.price.active = false
  139. this.free.active = false
  140. this.lock.active = true
  141. this.label_lockTip.string = `${this.type + 6}级武将解锁`;
  142. cc.loader.loadRes('carPic/head/head_' + this.type.toString(), cc.SpriteFrame, (err, assets) => {
  143. if (err) {
  144. cc.error(err);
  145. return;
  146. }
  147. this.iconCarEmpty.getComponent(cc.Sprite).spriteFrame = assets
  148. })
  149. }
  150. }
  151. else {
  152. this.labCircle.node.active = false
  153. this.iconCarEmpty.active = true
  154. this.desNode.active = false
  155. this.car.active = false
  156. this.btnShopBuy.interactable = false
  157. this.price.active = false
  158. this.free.active = false
  159. this.lock.active = true
  160. }
  161. }
  162. /** 点击购买 */
  163. clickBuyCar() {
  164. GameM.audioM.playEffect(AUDIO_TYPE.button)
  165. if (GameM.commonData.roleData.shop == 0) {
  166. ShopData.Instance.shopGuideTimes++
  167. if (ShopData.Instance.shopGuideTimes >= 2) {
  168. ShopData.Instance.updateShop()
  169. UiM.Instance.shopNode.getComponent(Shop).hideGuide()
  170. let main = UiM.Instance.hallNode.getComponent(Main)
  171. main.hideAllHand()
  172. main.CheckNormalGuide()
  173. }
  174. }
  175. let times = LocalManagerData.Instance.getBuyCarData(this.type)
  176. let maxbuycarnum = GameM.commonData.globalCfg.maxbuycarnum
  177. if (maxbuycarnum == null || maxbuycarnum == undefined) {
  178. maxbuycarnum = 0
  179. }
  180. if (maxbuycarnum != 0) {
  181. if (times >= maxbuycarnum) {
  182. EffectNode.instance.PlayTip('今日次数用尽,可降伏其他武将')
  183. return
  184. }
  185. }
  186. UiM.Instance.hallNode.getComponent(Main).onBuyTypeCar(this.type, 0)
  187. }
  188. }