import GameConfig from "../../configData/GameConfig"; import TowerHuan from "./TowerHuan"; import monsterNode from "../monster/monsterNode"; import taside from "./taside"; import towerManger from "./towerManger"; import DataObj from "../../configData/DataObj"; import monsterManger from "../monster/monsterManger"; import soldie from "../monster/soldie"; import gameUI from "../UIFace/gameUI"; import SkillSolate from "./SkillSolate"; const { ccclass, property } = cc._decorator; @ccclass export default class Magic extends cc.Component { @property(sp.Skeleton) private magicta: sp.Skeleton = null; @property(cc.Sprite) private guang_a: cc.Sprite = null; @property(cc.Sprite) private guang_b: cc.Sprite = null; /**地面激光烧过火光 */ private chongji: cc.Sprite = null; private clips: cc.Animation = null; private jiaodu = 0; private _taHuan: TowerHuan = null; public _taside: taside = null; private taHuanVec: cc.Vec2 = null; private interVec: cc.Vec2 = null; public myDataObj: DataObj = null; private isStartWait = true; public towerType = 0; /**左技能ID */ public SkillIDL = 0; /**右技能ID */ public SkillIDR = 0; private GoPos: cc.Vec3[] = null;//1路线坐标集合 /**石头人 */ private skillPeo:SkillSolate = null; private isdieLine = false; /**攻击范围内的怪物 */ private attackmonster: monsterNode[] = []; protected onLoad() { this.guang_a.node.opacity = 0; this.guang_b.node.opacity = 0; let poss = this.node.getPosition().add(this.guang_b.node.getPosition()); this.guang_b.node.removeFromParent(); this.node.parent.addChild(this.guang_b.node, 1008); this.guang_b.node.setPosition(poss); this.loadChongjiGuang(); this.myDataObj = new DataObj(); this.GoPos = gameUI.instance.getGoPos(); } /**加载冲击光 */ private loadChongjiGuang() { let self = this; cc.resources.load("prefabs/chongji", cc.Prefab, function (err, prefab: cc.Prefab) { var newNode = cc.instantiate(prefab); self.node.parent.addChild(newNode, 1008); newNode.opacity = 0; newNode.setPosition(cc.v2(0, 0)); self.chongji = newNode.getComponent(cc.Sprite); }); } /**根据ID读取数据 */ public initID(ID: number) { this.myDataObj.writeTower_Config(ID); this.myDataObj.writeBullet_Config(this.myDataObj.TowerData.bullet_id); if (towerManger.instance.talantData[10].length > 0) { this.myDataObj.TowerData.attack_range *= (1 + towerManger.instance.talantData[10][0]); } if (towerManger.instance.talantData[12].length > 0) { this.myDataObj.TowerData.up_gold[0] *= (1 - towerManger.instance.talantData[12][1]); this.myDataObj.TowerData.up_gold[0] = Math.floor(this.myDataObj.TowerData.up_gold[0]); } if (this.myDataObj.TowerData.tower_level == 1) { this.isStartWait = false; } else { this.scheduleOnce(function () { this.isStartWait = false; }, this.myDataObj.TowerData.attack_cd); } } /**设置或升级技能ID 技能类型 1左边 2右边*/ public setandUpSkillId(skillType: number) { if (skillType == 1) { if (this.SkillIDL == 0) { this.SkillIDL = this.myDataObj.TowerData.next_skill[0]; } else { this.myDataObj.writeSkill_Config(this.SkillIDL); this.SkillIDL = this.myDataObj.SkillData.next_skill; } } if (skillType == 2) { if (this.SkillIDR == 0) { this.SkillIDR = this.myDataObj.TowerData.next_skill[1]; } else { this.myDataObj.writeSkill_Config(this.SkillIDR); this.SkillIDR = this.myDataObj.SkillData.next_skill; } } // this.skillSpeattack(); } public setTaHuan(taHuan: TowerHuan) { this._taHuan = taHuan; this.taHuanVec = taHuan.node.getPosition(); this._taside.setRedHuanScale(this.myDataObj.TowerData.attack_range); this._taHuan.setRedHuanScale(this.myDataObj.TowerData.attack_range); this.guang_b.node.scaleX = this.myDataObj.BulletData.model_scaling; } public setNitu(mytaside: taside) { this._taside = mytaside; } onDestroy() { this._taHuan.node.destroy(); this.guang_b.node.destroy(); this.attackmonster = []; } public getIsInSqrt(pos: cc.Vec2) { let xiangl = this.taHuanVec.sub(pos); let a = this.myDataObj.TowerData.attack_range; let b = this.myDataObj.TowerData.attack_range * 5 / 6; if (xiangl.x * xiangl.x / (a * a) + xiangl.y * xiangl.y / (b * b) < 1) { return true; } else { return false; } } public getAttackmonster(): monsterNode[] { return this.attackmonster; } public addAttackmonster(monter: monsterNode) { if (!this.isStartWait) { if (monter.myDataObj.monsterData.company_type == 2) { if (this.myDataObj.TowerData.air_attack_switch == 'ON') { this.attackmonster.push(monter); } } else { this.attackmonster.push(monter); } } } public DelateAllAttackmonster() { for (let i = 0; i < this.attackmonster.length; i++) { const element = this.attackmonster[i]; if (element.node == null) { console.log("magic!!!!!!!!!!"); this.attackmonster.splice(i, 1); i--; continue; } if (!this.getIsInSqrt(element.node.getPosition()) || element.AniStatus == 'ZhengDie') { this.attackmonster.splice(i, 1); i--; } } } /**开始攻击 */ public attckStart() { if (this.attackmonster.length == 0) { return; } this.guang_a.node.opacity = 255; this.clips = this.guang_a.node.getComponent(cc.Animation); this.clips.play("laser_A").speed = GameConfig.instance().speed; this.clips.on('finished', this.startGuang, this); } private startGuang() { let self = this; this.interVec = this.attackmonster[0].getNewPos(); // this.interVec = this.interVec.add(this.attackmonster[0].getTimeSetDistance(0.4)); let danpos = this.guang_b.node.getPosition(); this.jiaodu = this.GetPointHAngle(this.interVec, danpos); // console.log("角度:"+this.jiaodu); this.guang_b.node.angle = this.jiaodu; this.guang_b.node.opacity = 255; this.guang_b.node.scaleY = 1; /**激光到怪的距离 */ let juli = this.interVec.sub(danpos).mag(); /** 设置激光长度*/ let leth = 1300; let xiangliang = this.interVec.sub(this._taside.node.getPosition()); let XiangLeth = xiangliang.mag(); let beishu = leth / XiangLeth; let endpos = this.interVec.add(cc.v2(xiangliang.x * beishu, xiangliang.y * beishu)); // let endAngle = this.GetPointHAngle(endpos,danpos); if (!this.isdieLine) { this.chongji.node.setPosition(this.interVec); this.chongji.node.opacity = 255; let clips = this.chongji.node.getComponent(cc.Animation); clips.play("chongji_1").speed = GameConfig.instance().speed; cc.tween(this.chongji.node) .delay(0.02) .to(this.myDataObj.BulletData.speed, { position: cc.v3(endpos) }) .to(0.02, { opacity: 0 }) .start(); let times = Math.floor(this.myDataObj.BulletData.speed / 0.02); this.schedule(function () { let chongjipos = self.chongji.node.getPosition(); let leetth = chongjipos.sub(danpos).mag(); self.guang_b.node.scaleY = leetth / 50; let sjortAngle = self.GetPointHAngle(chongjipos, danpos); self.guang_b.node.angle = sjortAngle; }, 0.02, times); } let attacknu = this.myDataObj.getRanm(1, this.myDataObj.BulletData.attack); if (towerManger.instance.talantData[13].length > 0) { attacknu *= (1 + towerManger.instance.talantData[13][0]); } let t = cc.tween; t(this.guang_b.node) .to(0.02, { scaleY: juli / 50 }) .call(() => { if (this.isdieLine) { /**伤害数值 */ this.myDataObj.writeSkill_Config(this.SkillIDL); let shanghai = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2); this.attackmonster[0].hpDown(shanghai); this.isdieLine = false; } else { towerManger.instance.MagicAttckMonster(this._taside.node.getPosition(), this.interVec, attacknu, this.myDataObj.BulletData.boom_range); } }) .delay(this.myDataObj.BulletData.speed) .to(0.3, { opacity: 0 }) .call(() => { this.guang_a.node.opacity = 0; }) .delay(this.myDataObj.TowerData.attack_cd) .call(() => { this.DelateAllAttackmonster(); this.attckStart(); }) .start(); } /**特殊技能 */ private skillSpeattack() { /**冷却时间 */ let delayTime = this.myDataObj.SkillData.parameter_4; if (this.SkillIDL >= 13 && this.SkillIDL <= 15) { this.schedule(this.skill13_15, delayTime); } if (this.SkillIDR >= 16 && this.SkillIDR <= 18) { this.schedule(this.skill16_18, delayTime); } if (this.SkillIDL >= 19 && this.SkillIDL <= 21) { this.schedule(this.skill19_21, delayTime); } if (this.SkillIDR >= 22 && this.SkillIDR <= 24) { this.scheduleOnce(this.skill22_24, delayTime); } } /**id 13-15技能 神秘射线 */ private skill13_15() { if (this.attackmonster.length == 0) { return; } this.isdieLine = true; } /**id 16-18技能 时空传送*/ private skill16_18() { if (this.attackmonster.length == 0) { return; } this.myDataObj.writeSkill_Config(this.SkillIDR); /**倒退步数 */ let step = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2); /**目标数量 */ let pointNum = this.myDataObj.SkillData.parameter_5; for (let i = 0; i < this.attackmonster.length; i++) { const element = this.attackmonster[i]; if (i < pointNum) { element.setMonterBackStep(step); } } } /**id 13-15技能 变羊术 */ private skill19_21() { if (this.attackmonster.length == 0) { return; } this.myDataObj.writeSkill_Config(this.SkillIDL); //变羊动画 let monter = this.attackmonster[0]; this.attackmonster[0].myDataObj.monsterData.active_attack_type == '3'; this.attackmonster[0].animalreGo(); let physics_re = this.attackmonster[0].myDataObj.monsterData.physics_reduction; let magic_reduction = this.attackmonster[0].myDataObj.monsterData.magic_reduction; this.attackmonster[0].myDataObj.monsterData.physics_reduction = 0; this.attackmonster[0].myDataObj.monsterData.magic_reduction = 0; /**持续时间 */ let totalcount = Number(this.myDataObj.SkillData.parameter_3); this.scheduleOnce(() => { if (monter.node != null) { if (monter.AniStatus != 'ZhengDie') { this.attackmonster[0].myDataObj.monsterData.physics_reduction = physics_re; this.attackmonster[0].myDataObj.monsterData.magic_reduction = magic_reduction; //还原 } } }, totalcount - 1); } /**id 13-15技能 召唤石头人 */ private skill22_24() { if (this.SkillIDR < 22 && this.SkillIDR > 24) { return; } this.myDataObj.writeSkill_Config(this.SkillIDR); let monid = Number(this.myDataObj.SkillData.parameter_2); this.myDataObj.writeMonster_Config(monid); if (this.skillPeo) { this.skillPeo.getsoldieThree().initLevelData(this,monid); return; } let self = this; let minPos = this.getMinPoint(); // cc.resources.load("prefabs/" + self.myDataObj.monsterData.name1, cc.Prefab, function (err, prefab: cc.Prefab) { // var newNode = cc.instantiate(prefab); // monsterManger.instance.node.addChild(newNode, 3); // let startpos = self.taHuanVec.add(cc.v2(0, -57)); // if (minPos.y > self.taHuanVec.y) { // startpos = self.taHuanVec.add(cc.v2(0, 37)); // } // let theSolde = newNode.getComponent(soldie); // theSolde.initLevelData(self,monid); // theSolde.setIsSkill(true); // theSolde.setinitPos(startpos, minPos); // theSolde.initWalkToPoint(true); // self.skillPeo = theSolde; // }); cc.resources.load("prefabs/skillSolate", cc.Prefab, function (err, prefab: cc.Prefab) { var newNode = cc.instantiate(prefab); newNode.setPosition(self.taHuanVec); towerManger.instance.node.addChild(newNode, 1006); let calssName = newNode.getComponent(SkillSolate); calssName.towerType = 4; calssName.initMagicID(minPos,monid,self.taHuanVec); self.skillPeo = calssName; towerManger.instance.towerArr.push(calssName); }); } /**路线距离兵营最近点坐标 */ private getMinPoint(): cc.Vec2 { let mylength = this.taHuanVec.sub(cc.v2(this.GoPos[0].x, this.GoPos[0].y)).mag(); let mindex = 0; for (let index = 1; index < this.GoPos.length; index++) { const element = this.GoPos[index]; let juli = this.taHuanVec.sub(cc.v2(element.x, element.y)).mag(); if (juli < mylength) { mindex = index; mylength = juli; } } return cc.v2(this.GoPos[mindex].x, this.GoPos[mindex].y); } private GetPointHAngle(p: cc.Vec2, ori: cc.Vec2): number//根据坐标x、y值计算其方位角 { let hAngle = 0; let dy = p.y - ori.y; let dx = p.x - ori.x; if (dx == 0 && dy > 0) { hAngle = 0; } else if (dx == 0 && dy < 0) { hAngle = 180; } else if (dy == 0 && dx > 0) { hAngle = 90; } else if (dy == 0 && dx < 0) { hAngle = 270; } else if (dx > 0 && dy > 0)//第一象限 { hAngle = Math.atan2(dx, dy) * 180 / Math.PI; } else if (dx > 0 && dy < 0)//第二象限 { hAngle = 180 - Math.atan2(dx, -dy) * 180 / Math.PI; } else if (dx < 0 && dy < 0)//第三象限 { hAngle = 180 + Math.atan2(-dx, -dy) * 180 / Math.PI; } else if (dx < 0 && dy > 0)//第四象限 { hAngle = 360 - Math.atan2(-dx, dy) * 180 / Math.PI; } return -hAngle + 360; } }