barracks.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import GameConfig from "../../configData/GameConfig";
  2. import monsterManger from "../monster/monsterManger";
  3. import TowerHuan from "./TowerHuan";
  4. import taside from "./taside";
  5. import monsterNode from "../monster/monsterNode";
  6. import soldie from "../monster/soldie";
  7. import towerManger from "./towerManger";
  8. import DataObj from "../../configData/DataObj";
  9. import gameUI from "../UIFace/gameUI";
  10. import gameData from "../../configData/gameData";
  11. const { ccclass, property } = cc._decorator;
  12. @ccclass
  13. export default class barracks extends cc.Component {
  14. private GoPos: cc.Vec3[] = null;//1路线坐标集合
  15. private minPos: cc.Vec2 = null;
  16. private _taHuan: TowerHuan = null;
  17. public _taside: taside = null;
  18. private taHuanVec: cc.Vec2 = null;
  19. private soldieThree: soldie[] = [];
  20. public towerType = 0;
  21. public myDataObj: DataObj = null;
  22. private isStartWait = true;
  23. private startPosArr: cc.Vec2[] = [];
  24. /**左技能ID */
  25. public SkillIDL = 0;
  26. /**右技能ID */
  27. public SkillIDR = 0;
  28. /**3技能ID */
  29. public SkillIDT = 0;
  30. /**攻击范围内的怪物 */
  31. private attackmonster: monsterNode[] = [];
  32. onLoad() {
  33. this.GoPos = gameUI.instance.getGoPos();
  34. this.myDataObj = new DataObj();
  35. }
  36. /**根据ID读取数据 */
  37. public initID(ID: number) {
  38. this.myDataObj.writeTower_Config(ID);
  39. if (towerManger.instance.talantData[7].length > 0) {
  40. this.myDataObj.TowerData.attack_range *= (1 + towerManger.instance.talantData[7][0]);
  41. }
  42. this._taside.setRedHuanScale(this.myDataObj.TowerData.attack_range);
  43. }
  44. /**设置或升级技能ID 技能类型 1左边 2右边*/
  45. public setandUpSkillId(skillType: number) {
  46. if (skillType == 1) {
  47. if (this.SkillIDL == 0) {
  48. this.SkillIDL = this.myDataObj.TowerData.next_skill[0];
  49. }
  50. else {
  51. this.myDataObj.writeSkill_Config(this.SkillIDL);
  52. this.SkillIDL = this.myDataObj.SkillData.next_skill;
  53. }
  54. }
  55. if (skillType == 2) {
  56. if (this.SkillIDR == 0) {
  57. this.SkillIDR = this.myDataObj.TowerData.next_skill[1];
  58. }
  59. else {
  60. this.myDataObj.writeSkill_Config(this.SkillIDR);
  61. this.SkillIDR = this.myDataObj.SkillData.next_skill;
  62. }
  63. }
  64. if (skillType == 3) {
  65. if (this.SkillIDT == 0) {
  66. this.SkillIDT = this.myDataObj.TowerData.next_skill[2];
  67. }
  68. else {
  69. this.myDataObj.writeSkill_Config(this.SkillIDT);
  70. this.SkillIDT = this.myDataObj.SkillData.next_skill;
  71. }
  72. }
  73. // this.skillSpeattack();
  74. }
  75. /**获取兵当前坐标 */
  76. public getSoldePosArr(): cc.Vec2[] {
  77. this.startPosArr = [];
  78. for (let i = 0; i < this.soldieThree.length; i++) {
  79. const element = this.soldieThree[i];
  80. this.startPosArr.push(element.node.getPosition());
  81. }
  82. return this.startPosArr.concat();
  83. }
  84. /**设置兵当前坐标 */
  85. public setSoldePosArr(arr: cc.Vec2[]) {
  86. this.startPosArr = arr;
  87. }
  88. /**加载兵
  89. * @param num 3塔兵 2技能兵
  90. */
  91. private loadSolde(num: number) {
  92. let self = this;
  93. let arr: cc.Vec2[] = [self.minPos.add(cc.v2(-20, 0)), self.minPos.add(cc.v2(20, 20)), self.minPos.add(cc.v2(20, -20))];
  94. cc.resources.load("prefabs/" + this.myDataObj.TowerData.name2, cc.Prefab, function (err, prefab: cc.Prefab) {
  95. for (let i = 0; i < num; i++) {
  96. var newNode = cc.instantiate(prefab);
  97. monsterManger.instance.node.addChild(newNode, 3);
  98. let startpos = self.taHuanVec.add(cc.v2(0, -57));
  99. if (self.minPos.y > self.taHuanVec.y) {
  100. startpos = self.taHuanVec.add(cc.v2(0, 37));
  101. }
  102. let theSolde = newNode.getComponent(soldie);
  103. theSolde.initLevelData(self,self.myDataObj.TowerData.monster_id[0]);
  104. theSolde.initTalant();
  105. theSolde.setIsSkill(false);
  106. theSolde.setinitPos(startpos, arr[i]);
  107. if (self.startPosArr.length > 0) {
  108. newNode.setPosition(self.startPosArr[i]);
  109. if (self.startPosArr[i] == cc.v2(0, 0)) {
  110. theSolde.initWalkToPoint(true);
  111. }
  112. }
  113. else {
  114. theSolde.initWalkToPoint(true);
  115. }
  116. self.soldieThree.push(theSolde);
  117. theSolde.setSche();
  118. }
  119. self.scheduleOnce(function () {
  120. self.isStartWait = false;
  121. }, 0);
  122. });
  123. }
  124. /**插旗 */
  125. public chaqiWalk(laca: cc.Vec2) {
  126. let self = this;
  127. this.minPos = laca;
  128. let arr: cc.Vec2[] = [self.minPos.add(cc.v2(-20, 0)), self.minPos.add(cc.v2(20, 20)), self.minPos.add(cc.v2(20, -20))];
  129. this.Disassociate();
  130. for (let i = 0; i < this.soldieThree.length; i++) {
  131. const element = this.soldieThree[i];
  132. element.setinitPos(null, arr[i]);
  133. // element.setisCanAttck(false);
  134. element.initWalkToPoint(false);
  135. }
  136. // this.scheduleOnce(function(){
  137. // for (let i = 0; i < this.soldieThree.length; i++) {
  138. // const element = this.soldieThree[i];
  139. // element.setisCanAttck(true);
  140. // }
  141. // },1);
  142. }
  143. /**无怪物时兵归位 */
  144. private attackOver() {
  145. for (let i = 0; i < this.soldieThree.length; i++) {
  146. const element = this.soldieThree[i];
  147. element.initWalkToPoint(false);
  148. }
  149. }
  150. /**暂停动画 */
  151. public isPaused(ispause: boolean) {
  152. for (let i = 0; i < this.soldieThree.length; i++) {
  153. const element = this.soldieThree[i];
  154. element.ispaused(ispause);
  155. }
  156. }
  157. /** 环*/
  158. public setTaHuan(taHuan: TowerHuan) {
  159. this._taHuan = taHuan;
  160. this.taHuanVec = taHuan.node.getPosition();
  161. this.minPos = this.getMinPoint();
  162. if (this.myDataObj.TowerData.tower_level == 1) {
  163. cc.tween(this.node)
  164. .delay(Number(gameData.instance().game_Const[1][2]) - 0.04)
  165. .call(() => {
  166. this.loadSolde(this.myDataObj.TowerData.monster_id[1]);
  167. })
  168. .start();
  169. }
  170. else
  171. {
  172. this.loadSolde(this.myDataObj.TowerData.monster_id[1]);
  173. }
  174. this._taHuan.setRedHuanScale(this.myDataObj.TowerData.attack_range);
  175. }
  176. /**设置泥土 */
  177. public setNitu(mytaside: taside) {
  178. this._taside = mytaside;
  179. }
  180. onDestroy() {
  181. this.attackmonster = [];
  182. for (let i = 0; i < this.soldieThree.length; i++) {
  183. const element = this.soldieThree[i];
  184. element.node.destroy();
  185. }
  186. this.Disassociate();
  187. this._taHuan.node.destroy();
  188. }
  189. /**接触怪和兵关联 */
  190. private Disassociate() {
  191. for (let i = 0; i < this.soldieThree.length; i++) {
  192. const element = this.soldieThree[i];
  193. if (element.getMymonster() != null) {
  194. if (element.getMymonster().node != null) {
  195. element.getMymonster().DelateAllBarracks();
  196. element.getMymonster().animalwalk();
  197. element.getMymonster().stopAni(false);
  198. }
  199. }
  200. }
  201. }
  202. /**刷新 */
  203. public getIsInSqrt(pos: cc.Vec2) {
  204. let xiangl = this.taHuanVec.sub(pos);
  205. let attack_range = this.myDataObj.TowerData.attack_range;
  206. let a = attack_range;
  207. let b = attack_range * 5 / 6;
  208. let jieguo = xiangl.x * xiangl.x / (a * a) + xiangl.y * xiangl.y / (b * b);
  209. if (xiangl.x * xiangl.x / (a * a) + xiangl.y * xiangl.y / (b * b) < 1) {
  210. return true;
  211. }
  212. else {
  213. return false;
  214. }
  215. }
  216. public getAttackmonster(): monsterNode[] {
  217. return this.attackmonster;
  218. }
  219. public addAttackmonster(monter: monsterNode) {
  220. if (!this.isStartWait) {
  221. let index = this.attackmonster.indexOf(monter);
  222. if (index < 0) {
  223. if (monter.myDataObj.monsterData.company_type == 2) {
  224. if (this.myDataObj.TowerData.air_attack_switch == 'ON') {
  225. this.attackmonster.push(monter);
  226. this.moreMonsterAttack();
  227. }
  228. }
  229. else
  230. {
  231. this.attackmonster.push(monter);
  232. this.moreMonsterAttack();
  233. }
  234. }
  235. }
  236. }
  237. public DelateAllAttackmonster() {
  238. for (let i = 0; i < this.attackmonster.length; i++) {
  239. const element = this.attackmonster[i];
  240. if (element.node == null) {
  241. this.attackmonster.splice(i, 1);
  242. i--;
  243. continue;
  244. }
  245. let isIn = this.getIsInSqrt(element.node.getPosition());
  246. if (!isIn || element.AniStatus == 'ZhengDie') {
  247. this.attackmonster.splice(i, 1);
  248. i--;
  249. }
  250. }
  251. }
  252. /**调整兵围攻数量 */
  253. private moreMonsterAttack() {
  254. if (this.attackmonster.length <= 1) {
  255. return;
  256. }
  257. if (this.attackmonster.length == 2) {
  258. for (let i = 0; i < this.attackmonster.length; i++) {
  259. const element = this.attackmonster[i];
  260. element.DelateBarracksRetain(2);
  261. }
  262. }
  263. if (this.attackmonster.length >= 3) {
  264. for (let i = 0; i < this.attackmonster.length; i++) {
  265. const element = this.attackmonster[i];
  266. element.DelateBarracksRetain(1);
  267. }
  268. }
  269. }
  270. /**开始攻击 */
  271. public attckStart() {
  272. if (this.attackmonster.length == 0) {
  273. this.attackOver();
  274. return;
  275. }
  276. let self = this;
  277. this.scheduleOnce(function () {
  278. self.DelateAllAttackmonster();
  279. self.attckStart();
  280. }, 0.3);
  281. }
  282. /**路线距离兵营最近点坐标 */
  283. private getMinPoint(): cc.Vec2 {
  284. let mylength = this.taHuanVec.sub(cc.v2(this.GoPos[0].x, this.GoPos[0].y)).mag();
  285. let mindex = 0;
  286. for (let index = 1; index < this.GoPos.length; index++) {
  287. const element = this.GoPos[index];
  288. let juli = this.taHuanVec.sub(cc.v2(element.x, element.y)).mag();
  289. if (juli < mylength) {
  290. mindex = index;
  291. mylength = juli;
  292. }
  293. }
  294. return cc.v2(this.GoPos[mindex].x, this.GoPos[mindex].y);
  295. }
  296. /**特殊技能 */
  297. private skillSpeattack() {
  298. /**冷却时间 */
  299. let delayTime = this.myDataObj.SkillData.parameter_4;
  300. if (this.SkillIDL >= 37 && this.SkillIDL <= 39) {
  301. this.schedule(this.skill37_39, delayTime);
  302. }
  303. if (this.SkillIDR >= 40 && this.SkillIDR <= 43) {
  304. this.skill40_42();
  305. }
  306. if (this.SkillIDT >= 52 && this.SkillIDT <= 54) {
  307. this.schedule(this.skill52_54, delayTime);
  308. }
  309. }
  310. /**id 37-39技能 治疗术 */
  311. private skill37_39() {
  312. if (this.attackmonster.length == 0) {
  313. return;
  314. }
  315. this.myDataObj.writeSkill_Config(this.SkillIDL);
  316. /**伤害数值 */
  317. let huixue = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2);
  318. for (let i = 0; i < this.soldieThree.length; i++) {
  319. const element = this.soldieThree[i];
  320. element.hpDown(-huixue);
  321. }
  322. }
  323. /**id 40-42技能 神圣护甲 */
  324. private skill40_42() {
  325. if (this.attackmonster.length == 0) {
  326. return;
  327. }
  328. this.myDataObj.writeSkill_Config(this.SkillIDR);
  329. /**物理免伤 */
  330. let reduce = Number(this.myDataObj.SkillData.parameter_3);
  331. for (let i = 0; i < this.soldieThree.length; i++) {
  332. const element = this.soldieThree[i];
  333. element.myDataObj.monsterData.physics_reduction += reduce * 100;
  334. }
  335. }
  336. /**id 43-45技能 神圣之力 */
  337. public skill43_45(attackSoldie: soldie) {
  338. if (this.attackmonster.length == 0) {
  339. return;
  340. }
  341. if (this.SkillIDT < 43 || this.SkillIDT > 45) {
  342. return;
  343. }
  344. this.myDataObj.writeSkill_Config(this.SkillIDT);
  345. /**伤害数值 */
  346. let shanghai = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2);
  347. /**几率 */
  348. let totalcount = Number(this.myDataObj.SkillData.parameter_3);
  349. /**范围 */
  350. let pointNum = this.myDataObj.SkillData.parameter_5;
  351. if (Math.random() < totalcount) {
  352. for (let i = 0; i < this.attackmonster.length; i++) {
  353. const element = this.attackmonster[i];
  354. if (attackSoldie.getMymonster() == element) {
  355. continue;
  356. }
  357. let monToDexLeth = attackSoldie.node.getPosition().sub(element.node.getPosition()).mag();
  358. if (monToDexLeth < pointNum) {
  359. element.hpDown(shanghai);
  360. }
  361. }
  362. }
  363. }
  364. /**id 46-48技能 双持战斧 */
  365. public skill46_48(attackSoldie: soldie) {
  366. if (this.attackmonster.length == 0) {
  367. return;
  368. }
  369. if (this.SkillIDL < 46 || this.SkillIDL > 48) {
  370. return;
  371. }
  372. this.myDataObj.writeSkill_Config(this.SkillIDL);
  373. /** */
  374. let monId = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2);
  375. /**伤害 */
  376. let shanghai = Number(this.myDataObj.SkillData.parameter_3);
  377. attackSoldie.getMymonster().hpDown(shanghai);
  378. }
  379. /**id 49-51技能 回旋斩 */
  380. public skill49_51(attackSoldie: soldie) {
  381. if (this.attackmonster.length == 0) {
  382. return;
  383. }
  384. if (this.SkillIDR < 49 || this.SkillIDR > 51) {
  385. return;
  386. }
  387. this.myDataObj.writeSkill_Config(this.SkillIDR);
  388. /**伤害数值 */
  389. let shanghai = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2);
  390. /**几率 */
  391. let totalcount = Number(this.myDataObj.SkillData.parameter_3);
  392. /**范围 */
  393. let pointNum = this.myDataObj.SkillData.parameter_5;
  394. if (Math.random() < totalcount) {
  395. for (let i = 0; i < this.attackmonster.length; i++) {
  396. const element = this.attackmonster[i];
  397. if (attackSoldie.getMymonster() == element) {
  398. continue;
  399. }
  400. let monToDexLeth = attackSoldie.node.getPosition().sub(element.node.getPosition()).mag();
  401. if (monToDexLeth < pointNum) {
  402. element.hpDown(shanghai);
  403. }
  404. }
  405. }
  406. }
  407. /**id 52-54技能 投掷领悟 */
  408. public skill52_54() {
  409. if (this.attackmonster.length == 0) {
  410. return;
  411. }
  412. if (this.SkillIDT < 52 || this.SkillIDT > 54) {
  413. return;
  414. }
  415. /**伤害数值 */
  416. let shanghai = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2);
  417. /**射程 */
  418. let totalcount = Number(this.myDataObj.SkillData.parameter_3);
  419. for (let k = 0; k < this.soldieThree.length; k++) {
  420. const element1 = this.soldieThree[k];
  421. for (let i = 0; i < this.attackmonster.length; i++) {
  422. const element = this.attackmonster[i];
  423. let monToDexLeth = element1.node.getPosition().sub(element.node.getPosition()).mag();
  424. if (monToDexLeth < totalcount) {
  425. element.hpDown(shanghai);
  426. break;
  427. }
  428. }
  429. }
  430. }
  431. }