taside.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import gameData from "../../configData/gameData";
  2. import gameUI from "../UIFace/gameUI";
  3. import towerManger from "./towerManger";
  4. const { ccclass, property } = cc._decorator;
  5. /**
  6. * 塔类型
  7. */
  8. export enum TasideType {
  9. Enum_nor = 1,
  10. Enum_show,
  11. }
  12. @ccclass
  13. export default class taside extends cc.Component {
  14. @property(cc.Node)
  15. private nitubg: cc.Node = null;
  16. /**塔攻击范围 */
  17. @property(cc.Sprite)
  18. private quan: cc.Sprite = null;
  19. /**选择建塔的环 */
  20. private huan: cc.Sprite = null;
  21. private diban: cc.Node[] = [];
  22. private coin: cc.Label[] = [];
  23. private sele: cc.Sprite[] = [];
  24. private iconSpr: cc.Sprite[] = [];
  25. private norMaterial:cc.Material = cc.Material.getBuiltinMaterial('2d-sprite');
  26. private grayMaterial:cc.Material = cc.Material.getBuiltinMaterial('2d-gray-sprite');
  27. private isCanSele = [true, true, true, true];
  28. private seleScale: number[] = [2, 2, 2, 2];
  29. /**4个选项是否打勾 */
  30. private isGou: boolean[] = [];
  31. /**圆环是否打开 */
  32. public isOpen = false;
  33. /**建塔、升级花费金币 */
  34. private towerGold = 0;
  35. /**打勾地板 */
  36. private _diban:cc.Node[] = [];
  37. private towerID = [28, 10, 19, 1];
  38. private towerModel:cc.Node[] = [];
  39. private first_gold: number[] = [0, 0, 0, 0];
  40. onLoad() {
  41. this.quan.node.setPosition(this.quan.node.getPosition().add(cc.v2(0,45)));
  42. this.huan = this.node.getChildByName("sele_tower_huan").getComponent(cc.Sprite);
  43. this.initData();
  44. this.initSpr();
  45. this.openTouchOn();
  46. this.loadTowerModel();
  47. }
  48. private loadTowerModel()
  49. {
  50. let self = this;
  51. for (let i = 1; i <= 4; i++) {
  52. let bb = i;
  53. if (i == 1) {
  54. bb = 4;
  55. }
  56. if (i == 4) {
  57. bb = 1;
  58. }
  59. cc.resources.load('prefabs/tower_model'+bb,cc.Prefab, function (err, prefab: cc.Prefab) {
  60. var newNode = cc.instantiate(prefab);
  61. self.node.addChild(newNode);
  62. newNode.active = false;
  63. self.towerModel.push(newNode);
  64. newNode.scale = 0.7;
  65. switch (bb) {
  66. case 1:
  67. newNode.setPosition(cc.v2(-1, 20));
  68. break;
  69. case 2:
  70. newNode.setPosition(cc.v2(-1, -19));
  71. break;
  72. case 3:
  73. newNode.setPosition(cc.v2(-1, -24));
  74. break;
  75. case 4:
  76. newNode.setPosition(cc.v2(5, 15));
  77. break;
  78. default:
  79. break;
  80. }
  81. });
  82. }
  83. }
  84. /**花费金币增加 */
  85. public addtowerGold(num: number) {
  86. this.towerGold += num;
  87. }
  88. /**获取所花费金币 */
  89. public gettowerGold(): number {
  90. return this.towerGold;
  91. }
  92. /**设置所花费金币 */
  93. public settowerGold(num: number) {
  94. this.towerGold = num;
  95. }
  96. /**根据ID获取数据 */
  97. private initData() {
  98. let nameDex = 0;
  99. let resdex = 0;
  100. let oneData = [];
  101. for (let i = 0; i < gameData.instance().tower_Config.length; i++) {
  102. const arr = gameData.instance().tower_Config[i];
  103. if (i == 2) {
  104. for (let k = 0; k < arr.length; k++) {
  105. const element = arr[k];
  106. if (element == 'first_gold') {
  107. nameDex = k;
  108. }
  109. if (element == 'attack_range') {
  110. resdex = k;
  111. }
  112. }
  113. }
  114. if (Number(arr[0]) == this.towerID[0]) {
  115. this.first_gold[0] = (Number(arr[nameDex]));
  116. this.seleScale[0] = (Number(arr[resdex]));
  117. if (towerManger.instance.talantData[7].length > 0) {
  118. this.seleScale[0] *= (1 + towerManger.instance.talantData[7][0]);
  119. }
  120. }
  121. if (Number(arr[0]) == this.towerID[1]) {
  122. this.first_gold[1] = (Number(arr[nameDex]));
  123. if (towerManger.instance.talantData[12].length > 0) {
  124. this.first_gold[1] *= (1 - towerManger.instance.talantData[12][0]);
  125. }
  126. this.first_gold[1] = Math.floor(this.first_gold[1]);
  127. this.seleScale[1] = (Number(arr[resdex]));
  128. if (towerManger.instance.talantData[10].length > 0) {
  129. this.seleScale[1] *= (1 + towerManger.instance.talantData[10][0]);
  130. }
  131. }
  132. if (Number(arr[0]) == this.towerID[2]) {
  133. this.first_gold[2] = (Number(arr[nameDex]));
  134. if (towerManger.instance.talantData[17].length > 0 && towerManger.instance.talantData[18].length > 0) {
  135. this.first_gold[2] *= (1 - towerManger.instance.talantData[17][0] - towerManger.instance.talantData[18][0]);
  136. }
  137. else {
  138. if (towerManger.instance.talantData[17].length > 0) {
  139. this.first_gold[2] *= (1 - towerManger.instance.talantData[17][0]);
  140. }
  141. if (towerManger.instance.talantData[18].length > 0) {
  142. this.first_gold[2] *= (1 - towerManger.instance.talantData[18][0]);
  143. }
  144. }
  145. this.first_gold[2] = Math.floor(this.first_gold[2]);
  146. this.seleScale[2] = (Number(arr[resdex]));
  147. if (towerManger.instance.talantData[16].length > 0) {
  148. this.seleScale[2] *= (1+towerManger.instance.talantData[16][0]);
  149. }
  150. }
  151. if (Number(arr[0]) == this.towerID[3]) {
  152. this.first_gold[3] = (Number(arr[nameDex]));
  153. this.seleScale[3] = (Number(arr[resdex]));
  154. if (towerManger.instance.talantData[1].length > 0 && towerManger.instance.talantData[3].length > 0) {
  155. this.seleScale[3] *= (1 + towerManger.instance.talantData[3][0] + towerManger.instance.talantData[1][0]);
  156. }
  157. else {
  158. if (towerManger.instance.talantData[1].length > 0) {
  159. this.seleScale[3] *= (1 + towerManger.instance.talantData[1][0]);
  160. }
  161. if (towerManger.instance.talantData[3].length > 0) {
  162. this.seleScale[3] *= (1 + towerManger.instance.talantData[3][0]);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. /**开启触摸 */
  169. public openTouchOn() {
  170. this.nitubg.on(cc.Node.EventType.TOUCH_START, this.touchNode, this);
  171. }
  172. /**关闭触摸 */
  173. public ClosedTouch() {
  174. this.nitubg.off(cc.Node.EventType.TOUCH_START, this.touchNode, this);
  175. }
  176. private initSpr() {
  177. this.quan.node.active = false;
  178. this.huan.node.active = false;
  179. let self = this;
  180. this._diban = [];
  181. for (let i = 1; i <= 4; i++) {
  182. let diban = this.huan.node.getChildByName("sele_tower_build_bg" + i);
  183. this._diban.push(diban);
  184. diban.on(cc.Node.EventType.TOUCH_START, function () {
  185. self.touchDiban(i);
  186. }, this);
  187. let mycoin: cc.Label = diban.getChildByName("coin1").getComponent(cc.Label);
  188. mycoin.string = this.first_gold[i - 1].toString();
  189. let mysele: cc.Sprite = diban.getChildByName("build_1").getComponent(cc.Sprite);
  190. let myicon: cc.Sprite = diban.getChildByName("iconspr").getComponent(cc.Sprite);
  191. this.isGou.push(false);
  192. this.diban.push(diban);
  193. this.coin.push(mycoin);
  194. this.sele.push(mysele);
  195. this.iconSpr.push(myicon);
  196. }
  197. }
  198. /**显示金币不足时状态 */
  199. private showNosele() {
  200. for (let i = 0; i < 4; i++) {
  201. if (gameUI.instance.getGold() >= this.first_gold[i]) {
  202. this.isCanSele[i] = true;
  203. this.iconSpr[i].node.getComponent(cc.Sprite).setMaterial(0,this.norMaterial);
  204. this.coin[i].node.color = cc.color(235,255,6);
  205. }
  206. else {
  207. this.isCanSele[i] = false;
  208. this.iconSpr[i].node.getComponent(cc.Sprite).setMaterial(0,this.grayMaterial);
  209. this.coin[i].node.color = cc.color(102,91,91);
  210. }
  211. }
  212. }
  213. /**打开圆环 */
  214. private turnbig() {
  215. this.showNosele();
  216. this.node.zIndex = 1007;
  217. towerManger.instance.closedHuan();
  218. this.huan.node.scale = 0.5;
  219. this.huan.node.opacity = 0;
  220. this.huan.node.active = true;
  221. this.huan.node.stopAllActions();
  222. cc.tween(this.huan.node)
  223. .to(0.1, { scale: 1, opacity: 255 })
  224. .call(() => {
  225. this.isOpen = true;
  226. })
  227. .start();
  228. }
  229. /**关闭圆环 */
  230. public turnsmall() {
  231. if (this.isOpen) {
  232. this.node.zIndex = 1006;
  233. this.huan.node.stopAllActions();
  234. cc.tween(this.huan.node)
  235. .to(0.1, { scale: 0.5, opacity: 0 })
  236. .call(() => {
  237. this.huan.node.active = false;
  238. this.isOpen = false;
  239. this.delateGou();
  240. this.quan.node.active = false;
  241. this.showSeleModel(100);
  242. })
  243. .start();
  244. }
  245. }
  246. /**点击泥土 */
  247. private touchNode(e: cc.Event.EventTouch) {
  248. this.turnbig();
  249. }
  250. /**取消勾 */
  251. private delateGou() {
  252. for (let i = 0; i < this.isGou.length; i++) {
  253. if (this.isGou[i]) {
  254. this.isGou[i] = false;
  255. this.sele[i].node.opacity = 0;
  256. }
  257. }
  258. }
  259. private showSeleModel(num:number)
  260. {
  261. for (let i = 0; i < this.towerModel.length; i++) {
  262. const element = this.towerModel[i];
  263. if (i == num) {
  264. element.active = true;
  265. }
  266. else
  267. {
  268. element.active = false;
  269. }
  270. }
  271. }
  272. private touchDiban(type: number) {
  273. if (!this.isCanSele[type - 1]) {
  274. return;
  275. }
  276. if (!this.isGou[type - 1]) {
  277. this.delateGou();
  278. this.isGou[type - 1] = true;
  279. this.sele[type - 1].node.opacity = 255;
  280. this.quan.node.active = true;
  281. this.setRedHuanScale(this.seleScale[type - 1]);
  282. this.showSeleModel(type - 1);
  283. }
  284. else {
  285. this.seleTowerBuild(type, this.node.getPosition());
  286. this.nitubg.off(cc.Node.EventType.TOUCH_START, this.touchNode, this);
  287. this.turnsmall();
  288. }
  289. }
  290. /**设置环的大小 */
  291. public setRedHuanScale(scale: number) {
  292. this.quan.node.scale = scale / 275;
  293. }
  294. /**修建塔 type 1兵塔 2魔法 3炮 4箭塔*/
  295. public seleTowerBuild(type: number, vecpos: cc.Vec2) {
  296. let salegold = this.first_gold[type - 1];
  297. gameUI.instance.AddGold(-salegold);
  298. towerManger.instance.buildTower(this, this.towerID[type - 1],[],true);
  299. this.addtowerGold(this.first_gold[type - 1]);
  300. }
  301. }