talantNode.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { AudioManager } from "../configData/AudioManager";
  2. import DataObj from "../configData/DataObj";
  3. import GameConfig from "../configData/GameConfig";
  4. import viewComtent from "../game/UIFace/viewComtent";
  5. import mainManager from "./mainManager";
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class talantNode extends cc.Component {
  9. /**背景 */
  10. @property(cc.Prefab)
  11. private zhehzao: cc.Prefab = null;
  12. /**背景 */
  13. @property(cc.Sprite)
  14. private mainbg: cc.Sprite = null;
  15. /**名字 */
  16. @property(cc.Sprite)
  17. private title: cc.Sprite = null;
  18. /**滚轮 */
  19. @property(cc.ScrollView)
  20. private scroll: cc.ScrollView = null;
  21. /**可视图 */
  22. @property(cc.Node)
  23. private view: cc.Node = null;
  24. /**容器 */
  25. @property(cc.Node)
  26. private content: cc.Node = null;
  27. /**升级按钮 */
  28. @property(cc.Button)
  29. private upbtn: cc.Button = null;
  30. // /**关闭按钮 */
  31. // @property(cc.Button)
  32. // private closebtn: cc.Button = null;
  33. /**天赋名字 */
  34. @property(cc.Label)
  35. private skillname: cc.Label = null;
  36. /**描述 */
  37. @property(cc.Label)
  38. private skillword: cc.Label = null;
  39. /**星星总数 */
  40. @property(cc.Label)
  41. private total: cc.Label = null;
  42. /**单个天赋价格 */
  43. @property(cc.Label)
  44. private saleNum: cc.Label = null;
  45. /**天赋图标 */
  46. @property(cc.Sprite)
  47. private talantIcon: cc.Sprite = null;
  48. /**天赋icon数组 */
  49. private skillIcon: cc.SpriteFrame[] = [];
  50. /**选择ID */
  51. private seleId = 1;
  52. /**icon */
  53. private iconSpr:cc.Sprite[] = [];
  54. private norMaterial:cc.Material = cc.Material.getBuiltinMaterial('2d-sprite');
  55. private grayMaterial:cc.Material = cc.Material.getBuiltinMaterial('2d-gray-sprite');
  56. public myDataObj: DataObj = null;
  57. public static instance: talantNode = null;
  58. protected onLoad() {
  59. talantNode.instance = this;
  60. this.node.opacity = 255;
  61. this.mainbg.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  62. this.myDataObj = new DataObj();
  63. this.initPos();
  64. this.loadSprite();
  65. this.scroll.scrollToOffset(cc.v2());
  66. }
  67. /**初始化坐标 */
  68. private initPos() {
  69. let hetgt = viewComtent.instance.getWinsize().y;
  70. // let closepos = this.closebtn.node.getPosition();
  71. // this.closebtn.node.setPosition(closepos.x, hetgt/2 - 62);
  72. this.title.node.setPosition(0, hetgt/2 - 72);
  73. this.total.string = '' + GameConfig.instance().storageData.starNum;
  74. }
  75. /**加载图片 */
  76. private loadSprite() {
  77. let self = this;
  78. self.skillIcon = [];
  79. cc.resources.load("talant/itembg", cc.SpriteFrame, function (err, myspriteFrame: cc.SpriteFrame) {
  80. for (let i = 0; i < 6; i++) {
  81. let mynode = new cc.Node();
  82. let spr = mynode.addComponent(cc.Sprite);
  83. spr.spriteFrame = myspriteFrame;
  84. mynode.setPosition(0, self.content.getContentSize().height / 2 - 300 - 207 * i);
  85. self.content.addChild(mynode);
  86. }
  87. });
  88. let strArr = ["sele_tower_build_4","sele_tower_build_1","sele_tower_build_2","sele_tower_build_3","yunshiicon","yuanbinicon"];
  89. for (let i = 0; i < 6; i++) {
  90. cc.resources.load("game/"+strArr[i], cc.SpriteFrame, function (err, myspriteFrame: cc.SpriteFrame) {
  91. let mynode = new cc.Node();
  92. let spr = mynode.addComponent(cc.Sprite);
  93. spr.spriteFrame = myspriteFrame;
  94. mynode.setPosition(-500, self.content.getContentSize().height / 2 - 300 - 207 * i);
  95. self.content.addChild(mynode);
  96. });
  97. }
  98. for (let i = 0; i < 30; i++) {
  99. self.skillIcon.push(null);
  100. }
  101. let coint = 0;
  102. for (let i = 0; i < 30; i++) {
  103. this.myDataObj.writeTalent_Config(i+1);
  104. cc.resources.load("talant/" + this.myDataObj.talentData.talent_icon, cc.SpriteFrame, function (err, myspriteFrame: cc.SpriteFrame) {
  105. let mynode = new cc.Node();
  106. let spr = mynode.addComponent(cc.Sprite);
  107. spr.spriteFrame = myspriteFrame;
  108. self.skillIcon[i] = myspriteFrame;
  109. mynode.setPosition(-322 + (i % 5) * 200, self.content.getContentSize().height / 2 - 300 - 207 * Math.floor((i / 5)));
  110. self.content.addChild(mynode);
  111. mynode.on(cc.Node.EventType.TOUCH_START, function () {
  112. self.tapSkillId(i + 1);
  113. }, self);
  114. coint++;
  115. if (coint == 30) {
  116. self.tapSkillId(self.seleId);
  117. }
  118. let isKaiqi = GameConfig.instance().storageData.myTalant[i];
  119. if (isKaiqi == 0) {
  120. spr.setMaterial(0, self.grayMaterial);
  121. }
  122. self.iconSpr.push(spr);
  123. });
  124. }
  125. }
  126. private loadTowerPic()
  127. {
  128. }
  129. /**点击各个天赋标志 */
  130. private tapSkillId(ID: number) {
  131. // console.log('-----------:' + ID);
  132. this.seleId = ID;
  133. this.myDataObj.writeTalent_Config(ID);
  134. this.skillname.string = this.myDataObj.talentData.name;
  135. this.skillword.string = this.myDataObj.talentData.talent_text;
  136. this.talantIcon.spriteFrame = this.skillIcon[ID - 1];
  137. this.saleNum.string = '' + this.myDataObj.talentData.star_required;
  138. this.saleNum.node.color = cc.color(142,105,52);
  139. this.setIsShowUpBtn(ID);
  140. }
  141. /**判断升级按钮是否能显示 */
  142. private setIsShowUpBtn(ID: number) {
  143. let isKaiqi = GameConfig.instance().storageData.myTalant[ID - 1];
  144. this.upbtn.node.active = false;
  145. if (isKaiqi == 0) {
  146. if ((ID - 1) % 5 == 0) {
  147. this.upbtn.node.active = true;
  148. }
  149. else {
  150. if (GameConfig.instance().storageData.myTalant[ID - 2] == 1) {
  151. this.upbtn.node.active = true;
  152. }
  153. }
  154. let starNum = GameConfig.instance().storageData.starNum;
  155. if (starNum < this.myDataObj.talentData.star_required) {
  156. this.upbtn.node.active = false;
  157. this.saleNum.node.color = cc.color(255,0,0);
  158. }
  159. }
  160. }
  161. private onTouchStart(e: cc.Event.EventTouch): void {
  162. }
  163. /**升级天赋 */
  164. private upTalant() {
  165. AudioManager.palyAudio(6);
  166. let starNum = GameConfig.instance().storageData.starNum;
  167. if (starNum >= this.myDataObj.talentData.star_required) {
  168. GameConfig.instance().storageData.starNum -= this.myDataObj.talentData.star_required;
  169. GameConfig.instance().storageData.myTalant[this.seleId - 1] = 1;
  170. GameConfig.instance().setStorage();
  171. this.total.string = '' + GameConfig.instance().storageData.starNum;
  172. this.iconSpr[this.seleId - 1].setMaterial(0, this.norMaterial);
  173. this.upbtn.node.active = false;
  174. }
  175. }
  176. /**关闭界面 */
  177. private closed() {
  178. AudioManager.palyAudio(6);
  179. mainManager.instance.initData();
  180. this.node.destroy();
  181. }
  182. }