import GameConfig from "../../configData/GameConfig"; import monsterManger from "../monster/monsterManger"; import monsterNode from "../monster/monsterNode"; import soldie from "../monster/soldie"; import towerManger from "./towerManger"; import DataObj from "../../configData/DataObj"; import gameUI from "../UIFace/gameUI"; const { ccclass, property } = cc._decorator; @ccclass export default class SkillSolate extends cc.Component { private _myPos: cc.Vec2 = null; private soldieThree: soldie[] = []; public towerType = 0; public myDataObj: DataObj = null; private doecount = 0; /**攻击范围内的怪物 */ private attackmonster: monsterNode[] = []; onLoad() { this.myDataObj = new DataObj(); this.node.opacity = 0; // this.initID(); } /**根据ID读取数据 */ public initID() { this._myPos = this.node.getPosition(); this.myDataObj.writeSkill_Config(61); this.loadSolde(this.myDataObj.SkillData.parameter_1); this.scheduleOnce(() => { towerManger.instance.delateTower(this); this.node.destroy(); }, Number(this.myDataObj.SkillData.parameter_2)); } /**加载兵 * @param num 3塔兵 2技能兵 */ private loadSolde(num: number) { let self = this; let arr: cc.Vec2[] = [cc.v2(-20, 0), cc.v2(20, 0), cc.v2(0, -20)]; let arrtemp = this.myDataObj.getArr(this.myDataObj.SkillData.parameter_3); for (let i = 0; i < num; i++) { let monid = arrtemp[Math.floor(Math.random() * arrtemp.length)]; this.myDataObj.writeMonster_Config(monid); cc.resources.load("prefabs/" + self.myDataObj.monsterData.name1, cc.Prefab, function (err, prefab: cc.Prefab) { var newNode = cc.instantiate(prefab); newNode.setPosition(self._myPos.add(arr[i])); monsterManger.instance.node.addChild(newNode, 3); let theSolde = newNode.getComponent(soldie); theSolde.setIsSkill(true); theSolde.initSkillMinter(self, monid); theSolde.setinitPos(self._myPos, self._myPos.add(arr[i])); self.soldieThree.push(theSolde); theSolde.setSche(); }); } } /**根据ID读取数据 */ public initMagicID(minpos: cc.Vec2, ID: number, Startpos: cc.Vec2) { this._myPos = minpos; this.myDataObj.writeSkill_Config(61); this.loadMagicSolde(ID,Startpos); } /**加载兵 * @param num 3塔兵 2技能兵 */ private loadMagicSolde(Id: number, Startpos: cc.Vec2) { let self = this; this.myDataObj.writeMonster_Config(Id); cc.resources.load("prefabs/" + self.myDataObj.monsterData.name1, cc.Prefab, function (err, prefab: cc.Prefab) { var newNode = cc.instantiate(prefab); let startpos = self.node.getPosition().add(cc.v2(0, -57)); if (self._myPos.y > Startpos.y) { startpos = Startpos.add(cc.v2(0, 37)); } newNode.setPosition(startpos); monsterManger.instance.node.addChild(newNode, 3); let theSolde = newNode.getComponent(soldie); theSolde.setIsSkill(true); theSolde.initLevelData(self, Id); theSolde.setinitPos(startpos, self._myPos); theSolde.initWalkToPoint(true); self.soldieThree.push(theSolde); theSolde.setSche(); }); } /**魔法塔获取石怪 */ public getsoldieThree():soldie { return this.soldieThree[0]; } /**兵死亡次数增加 */ public dieCount() { this.doecount++; if (this.doecount == this.soldieThree.length) { towerManger.instance.delateTower(this); this.node.destroy(); } } /**无怪物时兵归位 */ private attackOver() { for (let i = 0; i < this.soldieThree.length; i++) { const element = this.soldieThree[i]; element.initWalkToPoint(false); } } /**暂停动画 */ public isPaused(ispause: boolean) { for (let i = 0; i < this.soldieThree.length; i++) { const element = this.soldieThree[i]; element.ispaused(ispause); } } onDestroy() { for (let i = 0; i < this.soldieThree.length; i++) { const element = this.soldieThree[i]; element.node.destroy(); } this.Disassociate(); } /**接触怪和兵关联 */ private Disassociate() { for (let i = 0; i < this.soldieThree.length; i++) { const element = this.soldieThree[i]; if (element.getMymonster() != null) { if (element.getMymonster().node != null) { element.getMymonster().DelateAllBarracks(); element.getMymonster().animalwalk(); element.getMymonster().stopAni(false); } } } } /**刷新 */ public getIsInSqrt(pos: cc.Vec2) { let xiangl = this._myPos.sub(pos); let attack_range = 295; let a = attack_range; let b = attack_range * 5 / 6; let jieguo = xiangl.x * xiangl.x / (a * a) + xiangl.y * xiangl.y / (b * b); if (jieguo < 1) { return true; } else { return false; } } public getAttackmonster(): monsterNode[] { return this.attackmonster; } public addAttackmonster(monter: monsterNode) { let index = this.attackmonster.indexOf(monter); if (index < 0) { if (monter.myDataObj.monsterData.company_type == 2) { // if (this.myDataObj.TowerData.air_attack_switch == 'ON') { // this.attackmonster.push(monter); // this.moreMonsterAttack(); // } } else { this.attackmonster.push(monter); this.moreMonsterAttack(); } } } public DelateAllAttackmonster() { for (let i = 0; i < this.attackmonster.length; i++) { const element = this.attackmonster[i]; if (element.node == null) { this.attackmonster.splice(i, 1); i--; continue; } let isIn = this.getIsInSqrt(element.node.getPosition()); if (!isIn || element.AniStatus == 'ZhengDie') { this.attackmonster.splice(i, 1); i--; } } } /**调整兵围攻数量 */ private moreMonsterAttack() { if (this.attackmonster.length <= 1) { return; } if (this.attackmonster.length == 2) { for (let i = 0; i < this.attackmonster.length; i++) { const element = this.attackmonster[i]; element.DelateBarracksRetain(2); } } if (this.attackmonster.length >= 3) { for (let i = 0; i < this.attackmonster.length; i++) { const element = this.attackmonster[i]; element.DelateBarracksRetain(1); } } } /**开始攻击 */ public attckStart() { if (this.attackmonster.length == 0) { this.attackOver(); return; } let self = this; this.scheduleOnce(function () { self.DelateAllAttackmonster(); self.attckStart(); }, 0.3); } }