import gameData from "../../configData/gameData"; import gameUI from "../UIFace/gameUI"; import towerManger from "./towerManger"; const { ccclass, property } = cc._decorator; /** * 塔类型 */ export enum TasideType { Enum_nor = 1, Enum_show, } @ccclass export default class taside extends cc.Component { @property(cc.Node) private nitubg: cc.Node = null; /**塔攻击范围 */ @property(cc.Sprite) private quan: cc.Sprite = null; /**选择建塔的环 */ private huan: cc.Sprite = null; private diban: cc.Node[] = []; private coin: cc.Label[] = []; private sele: cc.Sprite[] = []; private iconSpr: cc.Sprite[] = []; private norMaterial:cc.Material = cc.Material.getBuiltinMaterial('2d-sprite'); private grayMaterial:cc.Material = cc.Material.getBuiltinMaterial('2d-gray-sprite'); private isCanSele = [true, true, true, true]; private seleScale: number[] = [2, 2, 2, 2]; /**4个选项是否打勾 */ private isGou: boolean[] = []; /**圆环是否打开 */ public isOpen = false; /**建塔、升级花费金币 */ private towerGold = 0; /**打勾地板 */ private _diban:cc.Node[] = []; private towerID = [28, 10, 19, 1]; private towerModel:cc.Node[] = []; private first_gold: number[] = [0, 0, 0, 0]; onLoad() { this.quan.node.setPosition(this.quan.node.getPosition().add(cc.v2(0,45))); this.huan = this.node.getChildByName("sele_tower_huan").getComponent(cc.Sprite); this.initData(); this.initSpr(); this.openTouchOn(); this.loadTowerModel(); } private loadTowerModel() { let self = this; for (let i = 1; i <= 4; i++) { let bb = i; if (i == 1) { bb = 4; } if (i == 4) { bb = 1; } cc.resources.load('prefabs/tower_model'+bb,cc.Prefab, function (err, prefab: cc.Prefab) { var newNode = cc.instantiate(prefab); self.node.addChild(newNode); newNode.active = false; self.towerModel.push(newNode); newNode.scale = 0.7; switch (bb) { case 1: newNode.setPosition(cc.v2(-1, 20)); break; case 2: newNode.setPosition(cc.v2(-1, -19)); break; case 3: newNode.setPosition(cc.v2(-1, -24)); break; case 4: newNode.setPosition(cc.v2(5, 15)); break; default: break; } }); } } /**花费金币增加 */ public addtowerGold(num: number) { this.towerGold += num; } /**获取所花费金币 */ public gettowerGold(): number { return this.towerGold; } /**设置所花费金币 */ public settowerGold(num: number) { this.towerGold = num; } /**根据ID获取数据 */ private initData() { let nameDex = 0; let resdex = 0; let oneData = []; for (let i = 0; i < gameData.instance().tower_Config.length; i++) { const arr = gameData.instance().tower_Config[i]; if (i == 2) { for (let k = 0; k < arr.length; k++) { const element = arr[k]; if (element == 'first_gold') { nameDex = k; } if (element == 'attack_range') { resdex = k; } } } if (Number(arr[0]) == this.towerID[0]) { this.first_gold[0] = (Number(arr[nameDex])); this.seleScale[0] = (Number(arr[resdex])); if (towerManger.instance.talantData[7].length > 0) { this.seleScale[0] *= (1 + towerManger.instance.talantData[7][0]); } } if (Number(arr[0]) == this.towerID[1]) { this.first_gold[1] = (Number(arr[nameDex])); if (towerManger.instance.talantData[12].length > 0) { this.first_gold[1] *= (1 - towerManger.instance.talantData[12][0]); } this.first_gold[1] = Math.floor(this.first_gold[1]); this.seleScale[1] = (Number(arr[resdex])); if (towerManger.instance.talantData[10].length > 0) { this.seleScale[1] *= (1 + towerManger.instance.talantData[10][0]); } } if (Number(arr[0]) == this.towerID[2]) { this.first_gold[2] = (Number(arr[nameDex])); if (towerManger.instance.talantData[17].length > 0 && towerManger.instance.talantData[18].length > 0) { this.first_gold[2] *= (1 - towerManger.instance.talantData[17][0] - towerManger.instance.talantData[18][0]); } else { if (towerManger.instance.talantData[17].length > 0) { this.first_gold[2] *= (1 - towerManger.instance.talantData[17][0]); } if (towerManger.instance.talantData[18].length > 0) { this.first_gold[2] *= (1 - towerManger.instance.talantData[18][0]); } } this.first_gold[2] = Math.floor(this.first_gold[2]); this.seleScale[2] = (Number(arr[resdex])); if (towerManger.instance.talantData[16].length > 0) { this.seleScale[2] *= (1+towerManger.instance.talantData[16][0]); } } if (Number(arr[0]) == this.towerID[3]) { this.first_gold[3] = (Number(arr[nameDex])); this.seleScale[3] = (Number(arr[resdex])); if (towerManger.instance.talantData[1].length > 0 && towerManger.instance.talantData[3].length > 0) { this.seleScale[3] *= (1 + towerManger.instance.talantData[3][0] + towerManger.instance.talantData[1][0]); } else { if (towerManger.instance.talantData[1].length > 0) { this.seleScale[3] *= (1 + towerManger.instance.talantData[1][0]); } if (towerManger.instance.talantData[3].length > 0) { this.seleScale[3] *= (1 + towerManger.instance.talantData[3][0]); } } } } } /**开启触摸 */ public openTouchOn() { this.nitubg.on(cc.Node.EventType.TOUCH_START, this.touchNode, this); } /**关闭触摸 */ public ClosedTouch() { this.nitubg.off(cc.Node.EventType.TOUCH_START, this.touchNode, this); } private initSpr() { this.quan.node.active = false; this.huan.node.active = false; let self = this; this._diban = []; for (let i = 1; i <= 4; i++) { let diban = this.huan.node.getChildByName("sele_tower_build_bg" + i); this._diban.push(diban); diban.on(cc.Node.EventType.TOUCH_START, function () { self.touchDiban(i); }, this); let mycoin: cc.Label = diban.getChildByName("coin1").getComponent(cc.Label); mycoin.string = this.first_gold[i - 1].toString(); let mysele: cc.Sprite = diban.getChildByName("build_1").getComponent(cc.Sprite); let myicon: cc.Sprite = diban.getChildByName("iconspr").getComponent(cc.Sprite); this.isGou.push(false); this.diban.push(diban); this.coin.push(mycoin); this.sele.push(mysele); this.iconSpr.push(myicon); } } /**显示金币不足时状态 */ private showNosele() { for (let i = 0; i < 4; i++) { if (gameUI.instance.getGold() >= this.first_gold[i]) { this.isCanSele[i] = true; this.iconSpr[i].node.getComponent(cc.Sprite).setMaterial(0,this.norMaterial); this.coin[i].node.color = cc.color(235,255,6); } else { this.isCanSele[i] = false; this.iconSpr[i].node.getComponent(cc.Sprite).setMaterial(0,this.grayMaterial); this.coin[i].node.color = cc.color(102,91,91); } } } /**打开圆环 */ private turnbig() { this.showNosele(); this.node.zIndex = 1007; towerManger.instance.closedHuan(); this.huan.node.scale = 0.5; this.huan.node.opacity = 0; this.huan.node.active = true; this.huan.node.stopAllActions(); cc.tween(this.huan.node) .to(0.1, { scale: 1, opacity: 255 }) .call(() => { this.isOpen = true; }) .start(); } /**关闭圆环 */ public turnsmall() { if (this.isOpen) { this.node.zIndex = 1006; this.huan.node.stopAllActions(); cc.tween(this.huan.node) .to(0.1, { scale: 0.5, opacity: 0 }) .call(() => { this.huan.node.active = false; this.isOpen = false; this.delateGou(); this.quan.node.active = false; this.showSeleModel(100); }) .start(); } } /**点击泥土 */ private touchNode(e: cc.Event.EventTouch) { this.turnbig(); } /**取消勾 */ private delateGou() { for (let i = 0; i < this.isGou.length; i++) { if (this.isGou[i]) { this.isGou[i] = false; this.sele[i].node.opacity = 0; } } } private showSeleModel(num:number) { for (let i = 0; i < this.towerModel.length; i++) { const element = this.towerModel[i]; if (i == num) { element.active = true; } else { element.active = false; } } } private touchDiban(type: number) { if (!this.isCanSele[type - 1]) { return; } if (!this.isGou[type - 1]) { this.delateGou(); this.isGou[type - 1] = true; this.sele[type - 1].node.opacity = 255; this.quan.node.active = true; this.setRedHuanScale(this.seleScale[type - 1]); this.showSeleModel(type - 1); } else { this.seleTowerBuild(type, this.node.getPosition()); this.nitubg.off(cc.Node.EventType.TOUCH_START, this.touchNode, this); this.turnsmall(); } } /**设置环的大小 */ public setRedHuanScale(scale: number) { this.quan.node.scale = scale / 275; } /**修建塔 type 1兵塔 2魔法 3炮 4箭塔*/ public seleTowerBuild(type: number, vecpos: cc.Vec2) { let salegold = this.first_gold[type - 1]; gameUI.instance.AddGold(-salegold); towerManger.instance.buildTower(this, this.towerID[type - 1],[],true); this.addtowerGold(this.first_gold[type - 1]); } }