Shop.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import EventName, { EventLabel, PopType } from "./EventName/EventName";
  8. import Hall from "./Hall";
  9. import LocalData from "./LocalData";
  10. import MyComponent from "./Template/MyComponent";
  11. const { ccclass, property } = cc._decorator;
  12. type CharacterSkinType = {
  13. name: string,
  14. introduce: string,
  15. image: string,
  16. imageMain: string,
  17. imageSp: cc.SpriteFrame,
  18. imageSpMain: cc.SpriteFrame,
  19. price: number,
  20. id: number,
  21. }
  22. type SkinType = {
  23. CharacterSkin: CharacterSkinType[],
  24. ParabolaSkin: CharacterSkinType[],
  25. }
  26. @ccclass
  27. export default class Shop extends MyComponent {
  28. @property(cc.Node)
  29. content: cc.Node = null;
  30. @property(cc.Node)
  31. CharacterSkinPrefab: cc.Node = null;
  32. @property(cc.Label)
  33. ShowCharacterName: cc.Label = null;
  34. @property(cc.Label)
  35. ShowCharacterintroduce: cc.Label = null;
  36. @property(cc.Sprite)
  37. ShowCharacterSprite: cc.Sprite = null;
  38. @property(cc.Node)
  39. ParabolaSkin: cc.Node = null;
  40. @property(cc.Node)
  41. CharacterSkin: cc.Node = null;
  42. @property(cc.Node)
  43. Select: cc.Node = null;
  44. @property(cc.Label)
  45. Gold: cc.Label = null;
  46. SelectCharacterSkinType: CharacterSkinType = null
  47. SelectParabolaSkinType: CharacterSkinType = null
  48. Click_CharacterSkin: Boolean = true
  49. onLoad(): void {
  50. super.onLoad()
  51. this.regEvent(EventName.changeGold, this.UpdataGold, this)
  52. // cc.resources.load("json/Skin", cc.JsonAsset, this.LoadDone.bind(this));
  53. let bundle = cc.assetManager.getBundle("sub");
  54. bundle.load("res/json/Skin", cc.JsonAsset, this.LoadDone.bind(this));
  55. }
  56. start() {
  57. this.scheduleOnce(() => {
  58. this.SetScrollViewSize()
  59. this.UpdataGold()
  60. }, 0)
  61. }
  62. jsonData: SkinType = null
  63. LoadDone(error: Error, resource: cc.JsonAsset) {
  64. if (error) {
  65. console.log(error.name);
  66. console.log(error.message);
  67. return;
  68. }
  69. this.jsonData = resource.json;
  70. // console.log(jsonData);
  71. // this.clickCharacterSkin()
  72. this.initSetScrollView()
  73. }
  74. initSetScrollView() {
  75. let jsonData = this.jsonData
  76. console.log(jsonData);
  77. if (this.Click_CharacterSkin) {
  78. for (let index = 0; index < jsonData.CharacterSkin.length; index++) {
  79. let element = jsonData.CharacterSkin[index];
  80. let temp_node = cc.instantiate(this.CharacterSkinPrefab)
  81. this.content.addChild(temp_node)
  82. let Skin = temp_node.getChildByName("Skin").getChildByName("Skin")
  83. //加入发光节点
  84. this.LightNodes.push(temp_node.getChildByName("checkbox"))
  85. //是否拥有了这个皮肤
  86. let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
  87. if (LocalData.getInstance().getHasCharacterSkin().includes(element.id)) {
  88. let has = temp_node.getChildByName("has")
  89. temp_node.getComponent(cc.Sprite).spriteFrame =
  90. has.getComponent(cc.Sprite).spriteFrame
  91. if (LocalData.getInstance().getCurrentCharacterSkin() == element.id) {
  92. NodeLabel.string = '使用中'
  93. //设置当前选择的皮肤类型
  94. this.SelectCharacterSkinType = element
  95. } else {
  96. NodeLabel.string = '选择'
  97. }
  98. } else {
  99. NodeLabel.node.active = false
  100. let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
  101. Gold.active = true
  102. let Label = Gold.getChildByName("Label").getComponent(cc.Label)
  103. Label.string = element.price.toString()
  104. }
  105. //加载图
  106. const url = element.image
  107. let bundle = cc.assetManager.getBundle("sub");
  108. bundle.load(url, cc.SpriteFrame, (err: Error, spriteframe) => {
  109. if (!err) {
  110. element.imageSp = spriteframe
  111. Skin.getComponent(cc.Sprite).spriteFrame = spriteframe
  112. temp_node['CharacterSkin'] = element
  113. }
  114. });
  115. bundle.load(element.imageMain, cc.SpriteFrame, (err: Error, spriteframe) => {
  116. if (!err) {
  117. element.imageSpMain = spriteframe
  118. }
  119. });
  120. //添加点击事件
  121. temp_node.on('touchstart', () => {
  122. let temp = temp_node['CharacterSkin'] as CharacterSkinType
  123. this.ShowCharacterSprite.spriteFrame = temp.imageSpMain
  124. this.ShowCharacterintroduce.string = temp.introduce
  125. this.ShowCharacterName.string = temp.name
  126. this.setLight(temp_node.getChildByName("checkbox"))
  127. //设置当前选择的皮肤类型
  128. this.SelectCharacterSkinType = temp
  129. })
  130. }
  131. } else {
  132. for (let index = 0; index < jsonData.ParabolaSkin.length; index++) {
  133. let element = jsonData.ParabolaSkin[index];
  134. let temp_node = cc.instantiate(this.CharacterSkinPrefab)
  135. this.content.addChild(temp_node)
  136. let Skin = temp_node.getChildByName("Skin").getChildByName("Skin")
  137. //加入发光节点
  138. this.LightNodes.push(temp_node.getChildByName("checkbox"))
  139. //是否拥有了这个皮肤
  140. let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
  141. if (LocalData.getInstance().getHasParabolaSkin().includes(element.id)) {
  142. let has = temp_node.getChildByName("has")
  143. temp_node.getComponent(cc.Sprite).spriteFrame =
  144. has.getComponent(cc.Sprite).spriteFrame
  145. if (LocalData.getInstance().getCurrentParabolaSkin() == element.id) {
  146. NodeLabel.string = '使用中'
  147. //设置当前选择的皮肤类型
  148. this.SelectCharacterSkinType = element
  149. } else {
  150. NodeLabel.string = '选择'
  151. }
  152. } else {
  153. NodeLabel.node.active = false
  154. let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
  155. Gold.active = true
  156. let Label = Gold.getChildByName("Label").getComponent(cc.Label)
  157. Label.string = element.price.toString()
  158. }
  159. //加载图
  160. const url = element.image
  161. // cc.resources.load(url, cc.SpriteFrame, (err, spriteframe) => {
  162. // if (!err) {
  163. // element.imageSp = spriteframe
  164. // Skin.getComponent(cc.Sprite).spriteFrame = spriteframe
  165. // temp_node['ParabolaSkin'] = element
  166. // }
  167. // });
  168. let bundle = cc.assetManager.getBundle("sub");
  169. bundle.load(url, cc.SpriteFrame, (err: Error, spriteframe) => {
  170. if (!err) {
  171. element.imageSp = spriteframe
  172. Skin.getComponent(cc.Sprite).spriteFrame = spriteframe
  173. temp_node['ParabolaSkin'] = element
  174. }
  175. });
  176. bundle.load(element.imageMain, cc.SpriteFrame, (err: Error, spriteframe) => {
  177. if (!err) {
  178. element.imageSpMain = spriteframe
  179. }
  180. });
  181. //添加点击事件
  182. temp_node.on('touchstart', () => {
  183. let temp = temp_node['ParabolaSkin'] as CharacterSkinType
  184. this.ShowCharacterSprite.spriteFrame = temp.imageSpMain
  185. this.ShowCharacterintroduce.string = temp.introduce
  186. this.ShowCharacterName.string = temp.name
  187. this.setLight(temp_node.getChildByName("checkbox"))
  188. //设置当前选择的皮肤类型
  189. this.SelectParabolaSkinType = temp
  190. })
  191. }
  192. }
  193. }
  194. updataSetScrollView() {
  195. if (this.Click_CharacterSkin) {
  196. for (let index = 0; index < this.content.children.length; index++) {
  197. const temp_node = this.content.children[index];
  198. let element = this.jsonData.CharacterSkin[index];
  199. //是否拥有了这个皮肤
  200. let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
  201. let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
  202. if (LocalData.getInstance().getHasCharacterSkin().includes(element.id)) {
  203. Gold.active = false
  204. NodeLabel.node.active = true
  205. let has = temp_node.getChildByName("has")
  206. temp_node.getComponent(cc.Sprite).spriteFrame =
  207. has.getComponent(cc.Sprite).spriteFrame
  208. if (LocalData.getInstance().getCurrentCharacterSkin() == element.id) {
  209. NodeLabel.string = '使用中'
  210. //设置当前选择的皮肤类型
  211. this.SelectCharacterSkinType = element
  212. } else {
  213. NodeLabel.string = '选择'
  214. }
  215. } else {
  216. NodeLabel.node.active = false
  217. Gold.active = true
  218. let Label = Gold.getChildByName("Label").getComponent(cc.Label)
  219. Label.string = element.price.toString()
  220. }
  221. }
  222. } else {
  223. for (let index = 0; index < this.content.children.length; index++) {
  224. const temp_node = this.content.children[index];
  225. let element = this.jsonData.ParabolaSkin[index];
  226. //是否拥有了这个皮肤
  227. let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
  228. let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
  229. if (LocalData.getInstance().getHasParabolaSkin().includes(element.id)) {
  230. Gold.active = false
  231. NodeLabel.node.active = true
  232. let has = temp_node.getChildByName("has")
  233. temp_node.getComponent(cc.Sprite).spriteFrame =
  234. has.getComponent(cc.Sprite).spriteFrame
  235. if (LocalData.getInstance().getCurrentParabolaSkin() == element.id) {
  236. NodeLabel.string = '使用中'
  237. //设置当前选择的皮肤类型
  238. this.SelectParabolaSkinType = element
  239. } else {
  240. NodeLabel.string = '选择'
  241. }
  242. } else {
  243. NodeLabel.node.active = false
  244. Gold.active = true
  245. let Label = Gold.getChildByName("Label").getComponent(cc.Label)
  246. Label.string = element.price.toString()
  247. }
  248. }
  249. }
  250. }
  251. SetScrollViewSize() {
  252. let content_width = this.content.getContentSize().width
  253. let spacingX = (content_width - 4 * this.CharacterSkinPrefab.getContentSize().width) / 5
  254. this.content.getComponent(cc.Layout).spacingX = spacingX
  255. this.content.getComponent(cc.Layout).paddingLeft = spacingX
  256. }
  257. clickParabolaSkin() {
  258. this.ParabolaSkin.color = new cc.Color(200, 200, 200, 255);
  259. this.CharacterSkin.color = new cc.Color(255, 255, 255, 255);
  260. this.Select.setPosition(this.ParabolaSkin.position)
  261. this.Click_CharacterSkin = false
  262. this.content.children.forEach(e => e.destroy())
  263. this.initSetScrollView()
  264. }
  265. clickCharacterSkin() {
  266. this.ParabolaSkin.color = new cc.Color(255, 255, 255, 255);
  267. this.CharacterSkin.color = new cc.Color(200, 200, 200, 255);
  268. this.Select.setPosition(this.CharacterSkin.position)
  269. this.Click_CharacterSkin = true
  270. this.content.children.forEach(e => e.destroy())
  271. this.initSetScrollView()
  272. }
  273. clickExit() {
  274. cc.Canvas.instance.node.getComponent(Hall).init()
  275. this.node.destroy()
  276. }
  277. //发光节点数组
  278. LightNodes: cc.Node[] = []
  279. setLight(selectNode: cc.Node) {
  280. this.LightNodes.forEach(e => {
  281. if (e == selectNode) {
  282. e.active = true
  283. } else {
  284. e.active = false
  285. }
  286. })
  287. }
  288. // update (dt) {}
  289. UpdataGold() {
  290. this.Gold.string = LocalData.getInstance().getGold().toString()
  291. }
  292. UseSkin() {
  293. if (this.Click_CharacterSkin == false) {
  294. //是否拥有这个皮肤
  295. if (LocalData.getInstance().getHasParabolaSkin().includes(this.SelectParabolaSkinType.id)) {
  296. //拥有
  297. LocalData.getInstance().setCurrentParabolaSkin(this.SelectParabolaSkinType.id)
  298. //更新展示
  299. this.updataSetScrollView()
  300. } else {
  301. //没拥有
  302. if (LocalData.getInstance().getGold() >= this.SelectParabolaSkinType.price) {
  303. //购买
  304. LocalData.getInstance().setGold(this.SelectParabolaSkinType.price, '-')
  305. //拥有
  306. LocalData.getInstance().setHasParabolaSkin(this.SelectParabolaSkinType.id)
  307. //当前用的
  308. LocalData.getInstance().setCurrentParabolaSkin(this.SelectParabolaSkinType.id)
  309. //更新展示
  310. this.updataSetScrollView()
  311. } else {
  312. let temp: PopType = new PopType()
  313. temp.string = EventLabel.OpenVideo
  314. temp.Title = '提示'
  315. temp.OK = () => {
  316. this.OpenAD()
  317. }
  318. temp.Fail = () => {
  319. }
  320. this.Pop(temp)
  321. }
  322. }
  323. } else {
  324. //是否拥有这个皮肤
  325. if (LocalData.getInstance().getHasCharacterSkin().includes(this.SelectCharacterSkinType.id)) {
  326. //拥有
  327. LocalData.getInstance().setCurrentCharacterSkin(this.SelectCharacterSkinType.id)
  328. //更新展示
  329. this.updataSetScrollView()
  330. } else {
  331. //没拥有
  332. if (LocalData.getInstance().getGold() >= this.SelectCharacterSkinType.price) {
  333. //购买
  334. LocalData.getInstance().setGold(this.SelectCharacterSkinType.price, '-')
  335. //拥有
  336. LocalData.getInstance().setHasCharacterSkin(this.SelectCharacterSkinType.id)
  337. //当前用的
  338. LocalData.getInstance().setCurrentCharacterSkin(this.SelectCharacterSkinType.id)
  339. //更新展示
  340. this.updataSetScrollView()
  341. } else {
  342. let temp: PopType = new PopType()
  343. temp.string = EventLabel.OpenVideo
  344. temp.Title = '提示'
  345. temp.OK = () => {
  346. this.OpenAD()
  347. }
  348. temp.Fail = () => {
  349. }
  350. this.Pop(temp)
  351. }
  352. }
  353. }
  354. }
  355. //看广告
  356. OpenAD() {
  357. LocalData.getInstance().setGold(1100, '+')
  358. }
  359. }