monsterNode.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. import DataObj from "../../configData/DataObj";
  2. import {RoadType } from "../../configData/Enum";
  3. import GameConfig from "../../configData/GameConfig";
  4. import gameData from "../../configData/gameData";
  5. import path from "../../configData/path";
  6. import path1 from "../../configData/path1";
  7. import path2 from "../../configData/path2";
  8. import mainManager from "../../mainFace/mainManager";
  9. import gameUI from "../UIFace/gameUI";
  10. import towerManger from "../tower/towerManger";
  11. import monsterManger from "./monsterManger";
  12. import soldie from "./soldie";
  13. import path3 from "../../configData/path3";
  14. const {ccclass, property} = cc._decorator;
  15. enum EventAction {
  16. ZhengWalk = 'ZhengWalk',
  17. ZhengAttack = 'ZhengAttack',
  18. ZhengDie = 'ZhengDie',
  19. }
  20. @ccclass
  21. export default class monsterNode extends cc.Component {
  22. @property(cc.Sprite)
  23. private myHpbg: cc.Sprite = null;
  24. @property(cc.Sprite)
  25. public myHp: cc.Sprite = null;
  26. private animal :sp.Skeleton = null;
  27. private GoPos:cc.Vec3[] = null;//1路线坐标集合
  28. private step:number = 0;//第几步
  29. private isStop = false;
  30. public AniStatus:EventAction = EventAction.ZhengWalk;
  31. private _type:number = null;
  32. private mysoldie:soldie[] = [];
  33. public myDataObj:DataObj = null;
  34. /**攻击类型 1主动 2被动 3不攻击 */
  35. public attackType:number = 2;
  36. private speedTemp = 0;
  37. private reduceTemp = 0;
  38. protected onLoad () {
  39. this.myDataObj = new DataObj();
  40. this.myHpbg.node.opacity = 0;
  41. this.myHp.node.opacity = 0;
  42. }
  43. public initLevelData(way:number,road:RoadType,type:number)
  44. {
  45. if (way == 1) {
  46. this.GoPos = path.instance.getPathData().concat();
  47. }
  48. if (way == 2)
  49. {
  50. this.GoPos = path1.instance.getpathData().concat();
  51. }
  52. if (way == 3)
  53. {
  54. this.GoPos = path2.instance.getpathData().concat();
  55. }
  56. if (way == 4)
  57. {
  58. this.GoPos = path3.instance.getpathData().concat();
  59. }
  60. this._type = type;
  61. this.initMonsterReadData();
  62. // console.log("this.GoPos:"+this.GoPos);
  63. this.changeRoad(road);
  64. this.node.setPosition(this.GoPos[this.step]);
  65. this.animal = this.node.getComponent(sp.Skeleton);
  66. this.animalGo();
  67. }
  68. /**当图片展示 */
  69. public iconShow()
  70. {
  71. this.animal = this.node.getComponent(sp.Skeleton);
  72. this.animal.paused = true;
  73. }
  74. /**读取csv怪物数据 */
  75. private initMonsterReadData()
  76. {
  77. this.myDataObj.writeMonster_Config(this._type);
  78. this.node.scaleX = -this.myDataObj.monsterData.model_scaling;
  79. this.node.scaleY = this.myDataObj.monsterData.model_scaling;
  80. this.speedTemp = this.myDataObj.monsterData.Movespeed;
  81. this.reduceTemp = this.myDataObj.monsterData.physics_reduction;
  82. this.attackType = Number(this.myDataObj.monsterData.active_attack_type);
  83. }
  84. /**倒退步数 */
  85. public setMonterBackStep(step:number)
  86. {
  87. this.step -= step;
  88. if (this.step < 0) {
  89. this.step = 0;
  90. }
  91. this.node.setPosition(this.GoPos[this.step]);
  92. }
  93. /**怪物中心点坐标 */
  94. public getNewPos():cc.Vec2
  95. {
  96. let v2temp = cc.v2(0,(this.node.getContentSize().height/2)*this.myDataObj.monsterData.model_scaling);
  97. switch (this._type) {
  98. case 2:
  99. v2temp = cc.v2(12,22*this.myDataObj.monsterData.model_scaling);
  100. break;
  101. default:
  102. break;
  103. }
  104. return this.node.getPosition().add(v2temp);
  105. }
  106. /**添加攻击自己的兵 */
  107. public addBarracks(mysold:soldie)
  108. {
  109. let index = this.mysoldie.indexOf(mysold);
  110. if (index < 0) {
  111. this.mysoldie.push(mysold);
  112. }
  113. }
  114. /**删除攻击自己的兵 */
  115. public DelateBarracks(mysole:soldie)
  116. {
  117. let index = this.mysoldie.indexOf(mysole);
  118. if (index >= 0) {
  119. this.mysoldie.splice(index, 1);
  120. }
  121. }
  122. /**删除所有攻击自己的兵 */
  123. public DelateAllBarracks()
  124. {
  125. this.mysoldie = [];
  126. }
  127. /**删除攻击自己的兵保留num个 */
  128. public DelateBarracksRetain(num:number)
  129. {
  130. for (let i = num; i < this.mysoldie.length; i++) {
  131. if (this.mysoldie[i].SolStatus == 'ZhengDie') {
  132. this.mysoldie.splice(i, 1);
  133. i--;
  134. }
  135. }
  136. let leth = this.mysoldie.length;
  137. if (leth > num)
  138. {
  139. for (let i = num; i < leth; i++) {
  140. this.mysoldie.pop().animalWalk();
  141. }
  142. }
  143. }
  144. /**攻击自己的兵数组 */
  145. public getBarracks():soldie[]
  146. {
  147. return this.mysoldie;
  148. }
  149. /**获取tt时间后增加的坐标向量 */
  150. public getTimeSetDistance(tt:number):cc.Vec2
  151. {
  152. let juli = tt*this.myDataObj.monsterData.Movespeed;
  153. let nowPos = this.node.getPosition();
  154. let xiangliang = this.GoPos[this.step+1].sub(cc.v3(nowPos.x,nowPos.y));
  155. let lenth = xiangliang.mag();
  156. let laterPos = (cc.v2((juli/lenth)*xiangliang.x,(juli/lenth)*xiangliang.y));
  157. return laterPos;
  158. }
  159. /**选择路线 */
  160. private changeRoad(road:RoadType)
  161. {
  162. if (road == RoadType.OneRoad) {
  163. return;
  164. }
  165. let lukuang = Number(gameData.instance().game_Const[6][2]);
  166. let length1 = this.GoPos[1].sub(this.GoPos[0]).mag();
  167. let sinX = Math.abs(this.GoPos[1].y-this.GoPos[0].y)/length1;
  168. let cosX = Math.abs(this.GoPos[1].x-this.GoPos[0].x)/length1;
  169. let pointUp = cc.v3();
  170. let pointdw = cc.v3();
  171. if (this.GoPos[1].y-this.GoPos[0].y >= 0) {
  172. if (road == RoadType.TwoUp||road == RoadType.TwoDown) {
  173. lukuang /= 2;
  174. }
  175. pointUp = cc.v3(this.GoPos[0].x-lukuang*sinX,this.GoPos[0].y+lukuang*cosX);
  176. pointdw = cc.v3(this.GoPos[0].x+lukuang*sinX,this.GoPos[0].y-lukuang*cosX);
  177. }
  178. if (this.GoPos[1].y-this.GoPos[0].y < 0) {
  179. pointUp = cc.v3(this.GoPos[0].x+lukuang*sinX,this.GoPos[0].y+lukuang*cosX);
  180. pointdw = cc.v3(this.GoPos[0].x-lukuang*sinX,this.GoPos[0].y-lukuang*cosX);
  181. }
  182. switch (road) {
  183. case RoadType.TwoUp:
  184. this.resetRoad(pointUp);
  185. break;
  186. case RoadType.TwoDown:
  187. this.resetRoad(pointdw);
  188. break;
  189. case RoadType.ThreeUp:
  190. this.resetRoad(pointUp);
  191. break;
  192. case RoadType.ThreeDown:
  193. this.resetRoad(pointdw);
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. /**生成新路线 */
  200. public resetRoad(startpos:cc.Vec3)
  201. {
  202. let postemp:cc.Vec3[] = [];
  203. for (let i = 0; i < this.GoPos.length; i++) {
  204. postemp.push(this.GoPos[i]);
  205. }
  206. postemp[0] = startpos;
  207. for (let i = this.step; i < this.GoPos.length-1; i++) {
  208. let newpos = postemp[i].add(this.GoPos[i+1].sub(this.GoPos[i]));
  209. postemp[i+1] = newpos;
  210. }
  211. for (let i = this.step; i < this.GoPos.length; i++) {
  212. this.GoPos[i] = postemp[i];
  213. }
  214. }
  215. /**是否停止前行 true:停止 */
  216. public stopAni(isStop:boolean)
  217. {
  218. this.isStop = isStop;
  219. if (isStop) {
  220. this.node.stopAllActions();
  221. }
  222. else
  223. {
  224. this.GoPos[this.step] = cc.v3(this.node.getPosition().x,this.node.getPosition().y);
  225. this.animalGo();
  226. this.node.scaleX = -this.myDataObj.monsterData.model_scaling;
  227. this.myHp.getComponent(cc.ProgressBar).reverse = true;
  228. }
  229. }
  230. /**设置怪物走路是否一直走 */
  231. public SetWalk(isGo:boolean)
  232. {
  233. // if (this.AniStatus == EventAction.ZhengWalk) {
  234. // }
  235. this.animal.paused = isGo;
  236. // this.animal.setAnimation(0,"walk",false);
  237. }
  238. public pauseLaterGo()
  239. {
  240. }
  241. /**设置方向 */
  242. private setScaleX(mypos:cc.Vec3,endpos:cc.Vec3)
  243. {
  244. let pross = this.myHp.getComponent(cc.ProgressBar);
  245. if (mypos.x > endpos.x) {
  246. this.node.scaleX = this.myDataObj.monsterData.model_scaling;
  247. pross.reverse = false;
  248. }
  249. else {
  250. this.node.scaleX = -this.myDataObj.monsterData.model_scaling;
  251. pross.reverse = true;
  252. }
  253. }
  254. private animalGo()
  255. {
  256. if (this.AniStatus != EventAction.ZhengWalk||this.isStop) {
  257. return;
  258. }
  259. if (this.step+1 >= this.GoPos.length) {
  260. gameUI.instance.HpCut(this.myDataObj.monsterData.hurt_level_hp);
  261. // cc.Device.vibrate();
  262. monsterManger.instance.dieDelatemonster(this);
  263. this.node.destroy();
  264. return;
  265. }
  266. let lenth = this.GoPos[this.step+1].sub(this.GoPos[this.step]).mag();
  267. if (lenth > 600) {
  268. this.step++;
  269. this.node.setPosition(this.GoPos[this.step]);
  270. let mytime = this.myDataObj.getRanm(1,gameData.instance().game_Const[11][2]);
  271. this.scheduleOnce(function(){
  272. this.animalGo();
  273. }, mytime);
  274. return;
  275. }
  276. this.animalwalk();
  277. if (Math.abs(this.GoPos[this.step].x-this.GoPos[this.step+1].x) > 10) {
  278. this.setScaleX(this.GoPos[this.step],this.GoPos[this.step+1]);
  279. }
  280. let dt = lenth/this.myDataObj.monsterData.Movespeed;
  281. this.node.stopAllActions();
  282. cc.tween(this.node)
  283. .to(dt,{position:this.GoPos[this.step+1]})
  284. .call(()=>{
  285. this.step++;
  286. this.animalGo();
  287. })
  288. .start();
  289. }
  290. /**被攻击血量减少 true:死*/
  291. public hpDown(attackNum:number)
  292. {
  293. if (this.AniStatus == EventAction.ZhengDie) {
  294. return;
  295. }
  296. this.myHpbg.node.opacity = 255;
  297. this.myHp.node.opacity = 255;
  298. let myprogress = this.myHp.getComponent(cc.ProgressBar);
  299. this.myDataObj.monsterData.nowHp -= attackNum;
  300. if (this.myDataObj.monsterData.nowHp <= 0) {
  301. this.myDataObj.monsterData.nowHp = 0;
  302. this.animalDie();
  303. }
  304. if (this.myDataObj.monsterData.nowHp > this.myDataObj.monsterData.totalHp) {
  305. this.myDataObj.monsterData.nowHp = this.myDataObj.monsterData.totalHp;
  306. }
  307. myprogress.progress = this.myDataObj.monsterData.nowHp/this.myDataObj.monsterData.totalHp;
  308. }
  309. public getIsdie():boolean
  310. {
  311. if (this.AniStatus == EventAction.ZhengDie) {
  312. return true;
  313. }
  314. else
  315. {
  316. return false;
  317. }
  318. }
  319. /**被魔法塔攻击后 魔力侵蚀 */
  320. public MagicCutPhysicsreduction()
  321. {
  322. let self = this;
  323. if (towerManger.instance.talantData[11].length > 0&&this.myDataObj.monsterData.physics_reduction == self.reduceTemp) {
  324. this.myDataObj.monsterData.physics_reduction -= towerManger.instance.talantData[11][0];
  325. if (this.myDataObj.monsterData.physics_reduction < 0) {
  326. this.myDataObj.monsterData.physics_reduction = 0;
  327. }
  328. this.scheduleOnce(function(){
  329. self.myDataObj.monsterData.physics_reduction = self.reduceTemp;
  330. }, towerManger.instance.talantData[11][1]);
  331. }
  332. if (towerManger.instance.talantData[14].length > 0&&this.myDataObj.monsterData.Movespeed == this.speedTemp) {
  333. this.myDataObj.monsterData.Movespeed *= (1-towerManger.instance.talantData[14][0]);
  334. this.GoPos[this.step] = cc.v3(this.node.getPosition().x,this.node.getPosition().y);
  335. this.animalGo();
  336. this.scheduleOnce(function(){
  337. self.myDataObj.monsterData.Movespeed = self.speedTemp;
  338. }, towerManger.instance.talantData[14][1]);
  339. }
  340. }
  341. public animalDie()
  342. {
  343. this.AniStatus = EventAction.ZhengDie;
  344. this.node.stopAllActions();
  345. this.animal.setAnimation(0,"die",false).timeScale = GameConfig.instance().speed;
  346. monsterManger.instance.dieDelatemonster(this);
  347. gameUI.instance.AddGold(this.myDataObj.monsterData.drop_group_1);
  348. gameUI.instance.addDiamind(this.myDataObj.getRanm(1,this.myDataObj.monsterData.drop_group_2),this.myDataObj.monsterData.percentage);
  349. let self = this;
  350. this.scheduleOnce(function(){
  351. self.node.opacity = 0;
  352. }, 0.6);
  353. this.scheduleOnce(function(){
  354. this.node.destroy();
  355. // console.log("销毁:"+monsterManger.instance.GetMonsterArr().length);
  356. }, 8);
  357. }
  358. public animalwalk()
  359. {
  360. if (this.animal.animation != "walk") {
  361. this.animal.setAnimation(0,"walk",true).timeScale = GameConfig.instance().speed;
  362. }
  363. this.AniStatus = EventAction.ZhengWalk;
  364. }
  365. /**放弃攻击离开 */
  366. public animalreGo()
  367. {
  368. this.animalwalk();
  369. this.stopAni(false);
  370. this.mysoldie = [];
  371. }
  372. public animalAttack()
  373. {
  374. if (this.AniStatus == EventAction.ZhengDie) {
  375. return;
  376. }
  377. if (this.mysoldie.length == 0) {
  378. return;
  379. }
  380. this.animal.setAnimation(0,"attack",false).timeScale = GameConfig.instance().speed;
  381. this.AniStatus = EventAction.ZhengAttack;
  382. this.animal.setCompleteListener((trackEntry, loopCount) => {
  383. let name = trackEntry.animation ? trackEntry.animation.name : '';
  384. if (name === 'attack') {
  385. let attack = this.myDataObj.getRanm(1,this.myDataObj.monsterData.attack);
  386. let diff = this.myDataObj.monsterData.attack_diff_coefficient;
  387. let reduction = this.mysoldie[0].myDataObj.monsterData.physics_reduction;
  388. if (towerManger.instance.talantData[6].length > 0&&!this.mysoldie[0].getIsSkill()) {
  389. reduction += towerManger.instance.talantData[6][0];
  390. }
  391. let shanghai = attack*diff*(1-reduction);
  392. this.mysoldie[0].hpDown(shanghai);
  393. if (towerManger.instance.talantData[9].length > 0) {
  394. this.hpDown(attack*diff*towerManger.instance.talantData[9][0]);
  395. }
  396. if (this.mysoldie[0].SolStatus == 'ZhengDie') {
  397. this.mysoldie.shift();
  398. if (this.mysoldie.length == 0) {
  399. this.animalwalk();
  400. this.stopAni(false);
  401. }
  402. }
  403. }
  404. });
  405. this.scheduleOnce(function(){
  406. this.animalAttack();
  407. }, this.myDataObj.monsterData.attack_speed);
  408. }
  409. }