import DataObj from "../../configData/DataObj"; import {RoadType } from "../../configData/Enum"; import GameConfig from "../../configData/GameConfig"; import gameData from "../../configData/gameData"; import path from "../../configData/path"; import path1 from "../../configData/path1"; import path2 from "../../configData/path2"; import mainManager from "../../mainFace/mainManager"; import gameUI from "../UIFace/gameUI"; import towerManger from "../tower/towerManger"; import monsterManger from "./monsterManger"; import soldie from "./soldie"; import path3 from "../../configData/path3"; const {ccclass, property} = cc._decorator; enum EventAction { ZhengWalk = 'ZhengWalk', ZhengAttack = 'ZhengAttack', ZhengDie = 'ZhengDie', } @ccclass export default class monsterNode extends cc.Component { @property(cc.Sprite) private myHpbg: cc.Sprite = null; @property(cc.Sprite) public myHp: cc.Sprite = null; private animal :sp.Skeleton = null; private GoPos:cc.Vec3[] = null;//1路线坐标集合 private step:number = 0;//第几步 private isStop = false; public AniStatus:EventAction = EventAction.ZhengWalk; private _type:number = null; private mysoldie:soldie[] = []; public myDataObj:DataObj = null; /**攻击类型 1主动 2被动 3不攻击 */ public attackType:number = 2; private speedTemp = 0; private reduceTemp = 0; protected onLoad () { this.myDataObj = new DataObj(); this.myHpbg.node.opacity = 0; this.myHp.node.opacity = 0; } public initLevelData(way:number,road:RoadType,type:number) { if (way == 1) { this.GoPos = path.instance.getPathData().concat(); } if (way == 2) { this.GoPos = path1.instance.getpathData().concat(); } if (way == 3) { this.GoPos = path2.instance.getpathData().concat(); } if (way == 4) { this.GoPos = path3.instance.getpathData().concat(); } this._type = type; this.initMonsterReadData(); // console.log("this.GoPos:"+this.GoPos); this.changeRoad(road); this.node.setPosition(this.GoPos[this.step]); this.animal = this.node.getComponent(sp.Skeleton); this.animalGo(); } /**当图片展示 */ public iconShow() { this.animal = this.node.getComponent(sp.Skeleton); this.animal.paused = true; } /**读取csv怪物数据 */ private initMonsterReadData() { this.myDataObj.writeMonster_Config(this._type); this.node.scaleX = -this.myDataObj.monsterData.model_scaling; this.node.scaleY = this.myDataObj.monsterData.model_scaling; this.speedTemp = this.myDataObj.monsterData.Movespeed; this.reduceTemp = this.myDataObj.monsterData.physics_reduction; this.attackType = Number(this.myDataObj.monsterData.active_attack_type); } /**倒退步数 */ public setMonterBackStep(step:number) { this.step -= step; if (this.step < 0) { this.step = 0; } this.node.setPosition(this.GoPos[this.step]); } /**怪物中心点坐标 */ public getNewPos():cc.Vec2 { let v2temp = cc.v2(0,(this.node.getContentSize().height/2)*this.myDataObj.monsterData.model_scaling); switch (this._type) { case 2: v2temp = cc.v2(12,22*this.myDataObj.monsterData.model_scaling); break; default: break; } return this.node.getPosition().add(v2temp); } /**添加攻击自己的兵 */ public addBarracks(mysold:soldie) { let index = this.mysoldie.indexOf(mysold); if (index < 0) { this.mysoldie.push(mysold); } } /**删除攻击自己的兵 */ public DelateBarracks(mysole:soldie) { let index = this.mysoldie.indexOf(mysole); if (index >= 0) { this.mysoldie.splice(index, 1); } } /**删除所有攻击自己的兵 */ public DelateAllBarracks() { this.mysoldie = []; } /**删除攻击自己的兵保留num个 */ public DelateBarracksRetain(num:number) { for (let i = num; i < this.mysoldie.length; i++) { if (this.mysoldie[i].SolStatus == 'ZhengDie') { this.mysoldie.splice(i, 1); i--; } } let leth = this.mysoldie.length; if (leth > num) { for (let i = num; i < leth; i++) { this.mysoldie.pop().animalWalk(); } } } /**攻击自己的兵数组 */ public getBarracks():soldie[] { return this.mysoldie; } /**获取tt时间后增加的坐标向量 */ public getTimeSetDistance(tt:number):cc.Vec2 { let juli = tt*this.myDataObj.monsterData.Movespeed; let nowPos = this.node.getPosition(); let xiangliang = this.GoPos[this.step+1].sub(cc.v3(nowPos.x,nowPos.y)); let lenth = xiangliang.mag(); let laterPos = (cc.v2((juli/lenth)*xiangliang.x,(juli/lenth)*xiangliang.y)); return laterPos; } /**选择路线 */ private changeRoad(road:RoadType) { if (road == RoadType.OneRoad) { return; } let lukuang = Number(gameData.instance().game_Const[6][2]); let length1 = this.GoPos[1].sub(this.GoPos[0]).mag(); let sinX = Math.abs(this.GoPos[1].y-this.GoPos[0].y)/length1; let cosX = Math.abs(this.GoPos[1].x-this.GoPos[0].x)/length1; let pointUp = cc.v3(); let pointdw = cc.v3(); if (this.GoPos[1].y-this.GoPos[0].y >= 0) { if (road == RoadType.TwoUp||road == RoadType.TwoDown) { lukuang /= 2; } pointUp = cc.v3(this.GoPos[0].x-lukuang*sinX,this.GoPos[0].y+lukuang*cosX); pointdw = cc.v3(this.GoPos[0].x+lukuang*sinX,this.GoPos[0].y-lukuang*cosX); } if (this.GoPos[1].y-this.GoPos[0].y < 0) { pointUp = cc.v3(this.GoPos[0].x+lukuang*sinX,this.GoPos[0].y+lukuang*cosX); pointdw = cc.v3(this.GoPos[0].x-lukuang*sinX,this.GoPos[0].y-lukuang*cosX); } switch (road) { case RoadType.TwoUp: this.resetRoad(pointUp); break; case RoadType.TwoDown: this.resetRoad(pointdw); break; case RoadType.ThreeUp: this.resetRoad(pointUp); break; case RoadType.ThreeDown: this.resetRoad(pointdw); break; default: break; } } /**生成新路线 */ public resetRoad(startpos:cc.Vec3) { let postemp:cc.Vec3[] = []; for (let i = 0; i < this.GoPos.length; i++) { postemp.push(this.GoPos[i]); } postemp[0] = startpos; for (let i = this.step; i < this.GoPos.length-1; i++) { let newpos = postemp[i].add(this.GoPos[i+1].sub(this.GoPos[i])); postemp[i+1] = newpos; } for (let i = this.step; i < this.GoPos.length; i++) { this.GoPos[i] = postemp[i]; } } /**是否停止前行 true:停止 */ public stopAni(isStop:boolean) { this.isStop = isStop; if (isStop) { this.node.stopAllActions(); } else { this.GoPos[this.step] = cc.v3(this.node.getPosition().x,this.node.getPosition().y); this.animalGo(); this.node.scaleX = -this.myDataObj.monsterData.model_scaling; this.myHp.getComponent(cc.ProgressBar).reverse = true; } } /**设置怪物走路是否一直走 */ public SetWalk(isGo:boolean) { // if (this.AniStatus == EventAction.ZhengWalk) { // } this.animal.paused = isGo; // this.animal.setAnimation(0,"walk",false); } public pauseLaterGo() { } /**设置方向 */ private setScaleX(mypos:cc.Vec3,endpos:cc.Vec3) { let pross = this.myHp.getComponent(cc.ProgressBar); if (mypos.x > endpos.x) { this.node.scaleX = this.myDataObj.monsterData.model_scaling; pross.reverse = false; } else { this.node.scaleX = -this.myDataObj.monsterData.model_scaling; pross.reverse = true; } } private animalGo() { if (this.AniStatus != EventAction.ZhengWalk||this.isStop) { return; } if (this.step+1 >= this.GoPos.length) { gameUI.instance.HpCut(this.myDataObj.monsterData.hurt_level_hp); // cc.Device.vibrate(); monsterManger.instance.dieDelatemonster(this); this.node.destroy(); return; } let lenth = this.GoPos[this.step+1].sub(this.GoPos[this.step]).mag(); if (lenth > 600) { this.step++; this.node.setPosition(this.GoPos[this.step]); let mytime = this.myDataObj.getRanm(1,gameData.instance().game_Const[11][2]); this.scheduleOnce(function(){ this.animalGo(); }, mytime); return; } this.animalwalk(); if (Math.abs(this.GoPos[this.step].x-this.GoPos[this.step+1].x) > 10) { this.setScaleX(this.GoPos[this.step],this.GoPos[this.step+1]); } let dt = lenth/this.myDataObj.monsterData.Movespeed; this.node.stopAllActions(); cc.tween(this.node) .to(dt,{position:this.GoPos[this.step+1]}) .call(()=>{ this.step++; this.animalGo(); }) .start(); } /**被攻击血量减少 true:死*/ public hpDown(attackNum:number) { if (this.AniStatus == EventAction.ZhengDie) { return; } this.myHpbg.node.opacity = 255; this.myHp.node.opacity = 255; let myprogress = this.myHp.getComponent(cc.ProgressBar); this.myDataObj.monsterData.nowHp -= attackNum; if (this.myDataObj.monsterData.nowHp <= 0) { this.myDataObj.monsterData.nowHp = 0; this.animalDie(); } if (this.myDataObj.monsterData.nowHp > this.myDataObj.monsterData.totalHp) { this.myDataObj.monsterData.nowHp = this.myDataObj.monsterData.totalHp; } myprogress.progress = this.myDataObj.monsterData.nowHp/this.myDataObj.monsterData.totalHp; } public getIsdie():boolean { if (this.AniStatus == EventAction.ZhengDie) { return true; } else { return false; } } /**被魔法塔攻击后 魔力侵蚀 */ public MagicCutPhysicsreduction() { let self = this; if (towerManger.instance.talantData[11].length > 0&&this.myDataObj.monsterData.physics_reduction == self.reduceTemp) { this.myDataObj.monsterData.physics_reduction -= towerManger.instance.talantData[11][0]; if (this.myDataObj.monsterData.physics_reduction < 0) { this.myDataObj.monsterData.physics_reduction = 0; } this.scheduleOnce(function(){ self.myDataObj.monsterData.physics_reduction = self.reduceTemp; }, towerManger.instance.talantData[11][1]); } if (towerManger.instance.talantData[14].length > 0&&this.myDataObj.monsterData.Movespeed == this.speedTemp) { this.myDataObj.monsterData.Movespeed *= (1-towerManger.instance.talantData[14][0]); this.GoPos[this.step] = cc.v3(this.node.getPosition().x,this.node.getPosition().y); this.animalGo(); this.scheduleOnce(function(){ self.myDataObj.monsterData.Movespeed = self.speedTemp; }, towerManger.instance.talantData[14][1]); } } public animalDie() { this.AniStatus = EventAction.ZhengDie; this.node.stopAllActions(); this.animal.setAnimation(0,"die",false).timeScale = GameConfig.instance().speed; monsterManger.instance.dieDelatemonster(this); gameUI.instance.AddGold(this.myDataObj.monsterData.drop_group_1); gameUI.instance.addDiamind(this.myDataObj.getRanm(1,this.myDataObj.monsterData.drop_group_2),this.myDataObj.monsterData.percentage); let self = this; this.scheduleOnce(function(){ self.node.opacity = 0; }, 0.6); this.scheduleOnce(function(){ this.node.destroy(); // console.log("销毁:"+monsterManger.instance.GetMonsterArr().length); }, 8); } public animalwalk() { if (this.animal.animation != "walk") { this.animal.setAnimation(0,"walk",true).timeScale = GameConfig.instance().speed; } this.AniStatus = EventAction.ZhengWalk; } /**放弃攻击离开 */ public animalreGo() { this.animalwalk(); this.stopAni(false); this.mysoldie = []; } public animalAttack() { if (this.AniStatus == EventAction.ZhengDie) { return; } if (this.mysoldie.length == 0) { return; } this.animal.setAnimation(0,"attack",false).timeScale = GameConfig.instance().speed; this.AniStatus = EventAction.ZhengAttack; this.animal.setCompleteListener((trackEntry, loopCount) => { let name = trackEntry.animation ? trackEntry.animation.name : ''; if (name === 'attack') { let attack = this.myDataObj.getRanm(1,this.myDataObj.monsterData.attack); let diff = this.myDataObj.monsterData.attack_diff_coefficient; let reduction = this.mysoldie[0].myDataObj.monsterData.physics_reduction; if (towerManger.instance.talantData[6].length > 0&&!this.mysoldie[0].getIsSkill()) { reduction += towerManger.instance.talantData[6][0]; } let shanghai = attack*diff*(1-reduction); this.mysoldie[0].hpDown(shanghai); if (towerManger.instance.talantData[9].length > 0) { this.hpDown(attack*diff*towerManger.instance.talantData[9][0]); } if (this.mysoldie[0].SolStatus == 'ZhengDie') { this.mysoldie.shift(); if (this.mysoldie.length == 0) { this.animalwalk(); this.stopAni(false); } } } }); this.scheduleOnce(function(){ this.animalAttack(); }, this.myDataObj.monsterData.attack_speed); } }