FenceItemRenderScript.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { _decorator, Component, Node, SpriteComponent, LabelComponent, ButtonComponent, loader, SpriteFrame } from 'cc';
  2. import { NoticeManager } from '../../../engines/notices/NoticeManager';
  3. import StringUtils from '../../../engines/utils/StringUtils';
  4. import { PlatformManager } from '../../../Platform/PlatformManager';
  5. import GameConfigManager from '../../models/GameConfigManager';
  6. import { GameModel } from '../../models/GameModel';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('FenceItemRenderScript')
  9. export class FenceItemRenderScript extends Component {
  10. @property({
  11. type:SpriteComponent
  12. })
  13. icon:SpriteComponent=null;
  14. @property({
  15. type:LabelComponent
  16. })
  17. hpLabel:LabelComponent=null;
  18. @property({
  19. type:LabelComponent
  20. })
  21. nameLabel:LabelComponent=null;
  22. @property({
  23. type:ButtonComponent
  24. })
  25. freeBuyButton:ButtonComponent=null;
  26. @property({
  27. type:LabelComponent
  28. })
  29. freeBuyButtonLabel:LabelComponent=null;
  30. @property({
  31. type:ButtonComponent
  32. })
  33. diamondBuyButton:ButtonComponent=null;
  34. @property({
  35. type:LabelComponent
  36. })
  37. diamondBuyButtonLabel:LabelComponent=null;
  38. @property({
  39. type:ButtonComponent
  40. })
  41. GlodBuyButton:ButtonComponent=null;
  42. @property({
  43. type:LabelComponent
  44. })
  45. GlodBuyButtonLabel:LabelComponent=null;
  46. private data:any;
  47. start () {
  48. // Your initialization goes here.
  49. }
  50. UpdateItemRender(data:any):void{
  51. this.data=data;
  52. let config:any=GameConfigManager.GetFenceConfig(data.id);
  53. //图标
  54. loader.loadRes(config.icon+"/spriteFrame",SpriteFrame,(err:Error,asset:SpriteFrame)=>{
  55. if(err!=null){
  56. console.log("加载图标出错:"+config.icon);
  57. return;
  58. }
  59. this.icon.spriteFrame=asset;
  60. })
  61. //等级
  62. this.hpLabel.string="HP:"+config.hp.toString();
  63. //名称
  64. this.nameLabel.string=config.name;
  65. let price:number;
  66. if(data.id<=GameModel.single.currentFenceId){
  67. this.freeBuyButton.node.active=this.diamondBuyButton.node.active=this.GlodBuyButton.node.active=false;
  68. }else{
  69. //buyType 购买方式 0金币购买 1宝石购买 2看广告购买
  70. if(data.buyType==0){//金币购买
  71. this.freeBuyButton.node.active=this.diamondBuyButton.node.active=false;
  72. this.GlodBuyButton.node.active=true;
  73. price=config.buyConsume;
  74. this.GlodBuyButtonLabel.string=StringUtils.numberUtilsEn(price);
  75. }else if(data.buyType==1){//宝石购买
  76. this.freeBuyButton.node.active=this.GlodBuyButton.node.active=false;
  77. this.diamondBuyButton.node.active=true;
  78. price=config.buyConsume;
  79. this.diamondBuyButtonLabel.string=StringUtils.numberUtilsEn(price);
  80. }else{//广告购买
  81. this.diamondBuyButton.node.active=this.GlodBuyButton.node.active=false;
  82. this.freeBuyButton.node.active=true;
  83. }
  84. }
  85. }
  86. /**
  87. * 金币购买
  88. */
  89. GlodBuy():void{
  90. if(this.BuyFence()){
  91. NoticeManager.ShowPrompt("购买成功。");
  92. }
  93. }
  94. /**
  95. * 钻石购买
  96. */
  97. DiamondBuy():void{
  98. if(this.BuyFence()){
  99. NoticeManager.ShowPrompt("购买成功。");
  100. }
  101. }
  102. /**
  103. * 视频购买
  104. */
  105. VideoBuy():void{
  106. PlatformManager.showRewardedVideo(()=>{
  107. if(this.BuyFence()){
  108. NoticeManager.ShowPrompt("购买成功。");
  109. }
  110. }, ()=>{
  111. NoticeManager.ShowPrompt("购买失败。");
  112. })
  113. }
  114. private BuyFence():boolean{
  115. //金钱购买
  116. if(this.data.buyType==0){
  117. if(GameModel.single.gold<this.data.buyConsume){
  118. NoticeManager.ShowPrompt("金币不足!");
  119. return false;
  120. }
  121. GameModel.single.gold-=this.data.buyConsume;
  122. }else if(this.data.buyType==1){//钻石购买
  123. if(GameModel.single.diamond<this.data.buyConsume){
  124. NoticeManager.ShowPrompt("钻石不足!");
  125. return false;
  126. }
  127. GameModel.single.diamond-=this.data.buyConsume;
  128. }
  129. GameModel.single.currentFenceId=this.data.id;
  130. return true;
  131. }
  132. // update (deltaTime: number) {
  133. // // Your update function goes here.
  134. // }
  135. }