Magic.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import GameConfig from "../../configData/GameConfig";
  2. import TowerHuan from "./TowerHuan";
  3. import monsterNode from "../monster/monsterNode";
  4. import taside from "./taside";
  5. import towerManger from "./towerManger";
  6. import DataObj from "../../configData/DataObj";
  7. import monsterManger from "../monster/monsterManger";
  8. import soldie from "../monster/soldie";
  9. import gameUI from "../UIFace/gameUI";
  10. import SkillSolate from "./SkillSolate";
  11. const { ccclass, property } = cc._decorator;
  12. @ccclass
  13. export default class Magic extends cc.Component {
  14. @property(sp.Skeleton)
  15. private magicta: sp.Skeleton = null;
  16. @property(cc.Sprite)
  17. private guang_a: cc.Sprite = null;
  18. @property(cc.Sprite)
  19. private guang_b: cc.Sprite = null;
  20. /**地面激光烧过火光 */
  21. private chongji: cc.Sprite = null;
  22. private clips: cc.Animation = null;
  23. private jiaodu = 0;
  24. private _taHuan: TowerHuan = null;
  25. public _taside: taside = null;
  26. private taHuanVec: cc.Vec2 = null;
  27. private interVec: cc.Vec2 = null;
  28. public myDataObj: DataObj = null;
  29. private isStartWait = true;
  30. public towerType = 0;
  31. /**左技能ID */
  32. public SkillIDL = 0;
  33. /**右技能ID */
  34. public SkillIDR = 0;
  35. private GoPos: cc.Vec3[] = null;//1路线坐标集合
  36. /**石头人 */
  37. private skillPeo:SkillSolate = null;
  38. private isdieLine = false;
  39. /**攻击范围内的怪物 */
  40. private attackmonster: monsterNode[] = [];
  41. protected onLoad() {
  42. this.guang_a.node.opacity = 0;
  43. this.guang_b.node.opacity = 0;
  44. let poss = this.node.getPosition().add(this.guang_b.node.getPosition());
  45. this.guang_b.node.removeFromParent();
  46. this.node.parent.addChild(this.guang_b.node, 1008);
  47. this.guang_b.node.setPosition(poss);
  48. this.loadChongjiGuang();
  49. this.myDataObj = new DataObj();
  50. this.GoPos = gameUI.instance.getGoPos();
  51. }
  52. /**加载冲击光 */
  53. private loadChongjiGuang() {
  54. let self = this;
  55. cc.resources.load("prefabs/chongji", cc.Prefab, function (err, prefab: cc.Prefab) {
  56. var newNode = cc.instantiate(prefab);
  57. self.node.parent.addChild(newNode, 1008);
  58. newNode.opacity = 0;
  59. newNode.setPosition(cc.v2(0, 0));
  60. self.chongji = newNode.getComponent(cc.Sprite);
  61. });
  62. }
  63. /**根据ID读取数据 */
  64. public initID(ID: number) {
  65. this.myDataObj.writeTower_Config(ID);
  66. this.myDataObj.writeBullet_Config(this.myDataObj.TowerData.bullet_id);
  67. if (towerManger.instance.talantData[10].length > 0) {
  68. this.myDataObj.TowerData.attack_range *= (1 + towerManger.instance.talantData[10][0]);
  69. }
  70. if (towerManger.instance.talantData[12].length > 0) {
  71. this.myDataObj.TowerData.up_gold[0] *= (1 - towerManger.instance.talantData[12][1]);
  72. this.myDataObj.TowerData.up_gold[0] = Math.floor(this.myDataObj.TowerData.up_gold[0]);
  73. }
  74. if (this.myDataObj.TowerData.tower_level == 1) {
  75. this.isStartWait = false;
  76. }
  77. else {
  78. this.scheduleOnce(function () {
  79. this.isStartWait = false;
  80. }, this.myDataObj.TowerData.attack_cd);
  81. }
  82. }
  83. /**设置或升级技能ID 技能类型 1左边 2右边*/
  84. public setandUpSkillId(skillType: number) {
  85. if (skillType == 1) {
  86. if (this.SkillIDL == 0) {
  87. this.SkillIDL = this.myDataObj.TowerData.next_skill[0];
  88. }
  89. else {
  90. this.myDataObj.writeSkill_Config(this.SkillIDL);
  91. this.SkillIDL = this.myDataObj.SkillData.next_skill;
  92. }
  93. }
  94. if (skillType == 2) {
  95. if (this.SkillIDR == 0) {
  96. this.SkillIDR = this.myDataObj.TowerData.next_skill[1];
  97. }
  98. else {
  99. this.myDataObj.writeSkill_Config(this.SkillIDR);
  100. this.SkillIDR = this.myDataObj.SkillData.next_skill;
  101. }
  102. }
  103. // this.skillSpeattack();
  104. }
  105. public setTaHuan(taHuan: TowerHuan) {
  106. this._taHuan = taHuan;
  107. this.taHuanVec = taHuan.node.getPosition();
  108. this._taside.setRedHuanScale(this.myDataObj.TowerData.attack_range);
  109. this._taHuan.setRedHuanScale(this.myDataObj.TowerData.attack_range);
  110. this.guang_b.node.scaleX = this.myDataObj.BulletData.model_scaling;
  111. }
  112. public setNitu(mytaside: taside) {
  113. this._taside = mytaside;
  114. }
  115. onDestroy() {
  116. this._taHuan.node.destroy();
  117. this.guang_b.node.destroy();
  118. this.attackmonster = [];
  119. }
  120. public getIsInSqrt(pos: cc.Vec2) {
  121. let xiangl = this.taHuanVec.sub(pos);
  122. let a = this.myDataObj.TowerData.attack_range;
  123. let b = this.myDataObj.TowerData.attack_range * 5 / 6;
  124. if (xiangl.x * xiangl.x / (a * a) + xiangl.y * xiangl.y / (b * b) < 1) {
  125. return true;
  126. }
  127. else {
  128. return false;
  129. }
  130. }
  131. public getAttackmonster(): monsterNode[] {
  132. return this.attackmonster;
  133. }
  134. public addAttackmonster(monter: monsterNode) {
  135. if (!this.isStartWait) {
  136. if (monter.myDataObj.monsterData.company_type == 2) {
  137. if (this.myDataObj.TowerData.air_attack_switch == 'ON') {
  138. this.attackmonster.push(monter);
  139. }
  140. }
  141. else
  142. {
  143. this.attackmonster.push(monter);
  144. }
  145. }
  146. }
  147. public DelateAllAttackmonster() {
  148. for (let i = 0; i < this.attackmonster.length; i++) {
  149. const element = this.attackmonster[i];
  150. if (element.node == null) {
  151. console.log("magic!!!!!!!!!!");
  152. this.attackmonster.splice(i, 1);
  153. i--;
  154. continue;
  155. }
  156. if (!this.getIsInSqrt(element.node.getPosition()) || element.AniStatus == 'ZhengDie') {
  157. this.attackmonster.splice(i, 1);
  158. i--;
  159. }
  160. }
  161. }
  162. /**开始攻击 */
  163. public attckStart() {
  164. if (this.attackmonster.length == 0) {
  165. return;
  166. }
  167. this.guang_a.node.opacity = 255;
  168. this.clips = this.guang_a.node.getComponent(cc.Animation);
  169. this.clips.play("laser_A").speed = GameConfig.instance().speed;
  170. this.clips.on('finished', this.startGuang, this);
  171. }
  172. private startGuang() {
  173. let self = this;
  174. this.interVec = this.attackmonster[0].getNewPos();
  175. // this.interVec = this.interVec.add(this.attackmonster[0].getTimeSetDistance(0.4));
  176. let danpos = this.guang_b.node.getPosition();
  177. this.jiaodu = this.GetPointHAngle(this.interVec, danpos);
  178. // console.log("角度:"+this.jiaodu);
  179. this.guang_b.node.angle = this.jiaodu;
  180. this.guang_b.node.opacity = 255;
  181. this.guang_b.node.scaleY = 1;
  182. /**激光到怪的距离 */
  183. let juli = this.interVec.sub(danpos).mag();
  184. /** 设置激光长度*/
  185. let leth = 1300;
  186. let xiangliang = this.interVec.sub(this._taside.node.getPosition());
  187. let XiangLeth = xiangliang.mag();
  188. let beishu = leth / XiangLeth;
  189. let endpos = this.interVec.add(cc.v2(xiangliang.x * beishu, xiangliang.y * beishu));
  190. // let endAngle = this.GetPointHAngle(endpos,danpos);
  191. if (!this.isdieLine) {
  192. this.chongji.node.setPosition(this.interVec);
  193. this.chongji.node.opacity = 255;
  194. let clips = this.chongji.node.getComponent(cc.Animation);
  195. clips.play("chongji_1").speed = GameConfig.instance().speed;
  196. cc.tween(this.chongji.node)
  197. .delay(0.02)
  198. .to(this.myDataObj.BulletData.speed, { position: cc.v3(endpos) })
  199. .to(0.02, { opacity: 0 })
  200. .start();
  201. let times = Math.floor(this.myDataObj.BulletData.speed / 0.02);
  202. this.schedule(function () {
  203. let chongjipos = self.chongji.node.getPosition();
  204. let leetth = chongjipos.sub(danpos).mag();
  205. self.guang_b.node.scaleY = leetth / 50;
  206. let sjortAngle = self.GetPointHAngle(chongjipos, danpos);
  207. self.guang_b.node.angle = sjortAngle;
  208. }, 0.02, times);
  209. }
  210. let attacknu = this.myDataObj.getRanm(1, this.myDataObj.BulletData.attack);
  211. if (towerManger.instance.talantData[13].length > 0) {
  212. attacknu *= (1 + towerManger.instance.talantData[13][0]);
  213. }
  214. let t = cc.tween;
  215. t(this.guang_b.node)
  216. .to(0.02, { scaleY: juli / 50 })
  217. .call(() => {
  218. if (this.isdieLine) {
  219. /**伤害数值 */
  220. this.myDataObj.writeSkill_Config(this.SkillIDL);
  221. let shanghai = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2);
  222. this.attackmonster[0].hpDown(shanghai);
  223. this.isdieLine = false;
  224. }
  225. else {
  226. towerManger.instance.MagicAttckMonster(this._taside.node.getPosition(), this.interVec, attacknu, this.myDataObj.BulletData.boom_range);
  227. }
  228. })
  229. .delay(this.myDataObj.BulletData.speed)
  230. .to(0.3, { opacity: 0 })
  231. .call(() => {
  232. this.guang_a.node.opacity = 0;
  233. })
  234. .delay(this.myDataObj.TowerData.attack_cd)
  235. .call(() => {
  236. this.DelateAllAttackmonster();
  237. this.attckStart();
  238. })
  239. .start();
  240. }
  241. /**特殊技能 */
  242. private skillSpeattack() {
  243. /**冷却时间 */
  244. let delayTime = this.myDataObj.SkillData.parameter_4;
  245. if (this.SkillIDL >= 13 && this.SkillIDL <= 15) {
  246. this.schedule(this.skill13_15, delayTime);
  247. }
  248. if (this.SkillIDR >= 16 && this.SkillIDR <= 18) {
  249. this.schedule(this.skill16_18, delayTime);
  250. }
  251. if (this.SkillIDL >= 19 && this.SkillIDL <= 21) {
  252. this.schedule(this.skill19_21, delayTime);
  253. }
  254. if (this.SkillIDR >= 22 && this.SkillIDR <= 24) {
  255. this.scheduleOnce(this.skill22_24, delayTime);
  256. }
  257. }
  258. /**id 13-15技能 神秘射线 */
  259. private skill13_15() {
  260. if (this.attackmonster.length == 0) {
  261. return;
  262. }
  263. this.isdieLine = true;
  264. }
  265. /**id 16-18技能 时空传送*/
  266. private skill16_18() {
  267. if (this.attackmonster.length == 0) {
  268. return;
  269. }
  270. this.myDataObj.writeSkill_Config(this.SkillIDR);
  271. /**倒退步数 */
  272. let step = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2);
  273. /**目标数量 */
  274. let pointNum = this.myDataObj.SkillData.parameter_5;
  275. for (let i = 0; i < this.attackmonster.length; i++) {
  276. const element = this.attackmonster[i];
  277. if (i < pointNum) {
  278. element.setMonterBackStep(step);
  279. }
  280. }
  281. }
  282. /**id 13-15技能 变羊术 */
  283. private skill19_21() {
  284. if (this.attackmonster.length == 0) {
  285. return;
  286. }
  287. this.myDataObj.writeSkill_Config(this.SkillIDL);
  288. //变羊动画
  289. let monter = this.attackmonster[0];
  290. this.attackmonster[0].myDataObj.monsterData.active_attack_type == '3';
  291. this.attackmonster[0].animalreGo();
  292. let physics_re = this.attackmonster[0].myDataObj.monsterData.physics_reduction;
  293. let magic_reduction = this.attackmonster[0].myDataObj.monsterData.magic_reduction;
  294. this.attackmonster[0].myDataObj.monsterData.physics_reduction = 0;
  295. this.attackmonster[0].myDataObj.monsterData.magic_reduction = 0;
  296. /**持续时间 */
  297. let totalcount = Number(this.myDataObj.SkillData.parameter_3);
  298. this.scheduleOnce(() => {
  299. if (monter.node != null) {
  300. if (monter.AniStatus != 'ZhengDie') {
  301. this.attackmonster[0].myDataObj.monsterData.physics_reduction = physics_re;
  302. this.attackmonster[0].myDataObj.monsterData.magic_reduction = magic_reduction;
  303. //还原
  304. }
  305. }
  306. }, totalcount - 1);
  307. }
  308. /**id 13-15技能 召唤石头人 */
  309. private skill22_24() {
  310. if (this.SkillIDR < 22 && this.SkillIDR > 24) {
  311. return;
  312. }
  313. this.myDataObj.writeSkill_Config(this.SkillIDR);
  314. let monid = Number(this.myDataObj.SkillData.parameter_2);
  315. this.myDataObj.writeMonster_Config(monid);
  316. if (this.skillPeo) {
  317. this.skillPeo.getsoldieThree().initLevelData(this,monid);
  318. return;
  319. }
  320. let self = this;
  321. let minPos = this.getMinPoint();
  322. // cc.resources.load("prefabs/" + self.myDataObj.monsterData.name1, cc.Prefab, function (err, prefab: cc.Prefab) {
  323. // var newNode = cc.instantiate(prefab);
  324. // monsterManger.instance.node.addChild(newNode, 3);
  325. // let startpos = self.taHuanVec.add(cc.v2(0, -57));
  326. // if (minPos.y > self.taHuanVec.y) {
  327. // startpos = self.taHuanVec.add(cc.v2(0, 37));
  328. // }
  329. // let theSolde = newNode.getComponent(soldie);
  330. // theSolde.initLevelData(self,monid);
  331. // theSolde.setIsSkill(true);
  332. // theSolde.setinitPos(startpos, minPos);
  333. // theSolde.initWalkToPoint(true);
  334. // self.skillPeo = theSolde;
  335. // });
  336. cc.resources.load("prefabs/skillSolate", cc.Prefab, function (err, prefab: cc.Prefab) {
  337. var newNode = cc.instantiate(prefab);
  338. newNode.setPosition(self.taHuanVec);
  339. towerManger.instance.node.addChild(newNode, 1006);
  340. let calssName = newNode.getComponent(SkillSolate);
  341. calssName.towerType = 4;
  342. calssName.initMagicID(minPos,monid,self.taHuanVec);
  343. self.skillPeo = calssName;
  344. towerManger.instance.towerArr.push(calssName);
  345. });
  346. }
  347. /**路线距离兵营最近点坐标 */
  348. private getMinPoint(): cc.Vec2 {
  349. let mylength = this.taHuanVec.sub(cc.v2(this.GoPos[0].x, this.GoPos[0].y)).mag();
  350. let mindex = 0;
  351. for (let index = 1; index < this.GoPos.length; index++) {
  352. const element = this.GoPos[index];
  353. let juli = this.taHuanVec.sub(cc.v2(element.x, element.y)).mag();
  354. if (juli < mylength) {
  355. mindex = index;
  356. mylength = juli;
  357. }
  358. }
  359. return cc.v2(this.GoPos[mindex].x, this.GoPos[mindex].y);
  360. }
  361. private GetPointHAngle(p: cc.Vec2, ori: cc.Vec2): number//根据坐标x、y值计算其方位角
  362. {
  363. let hAngle = 0;
  364. let dy = p.y - ori.y;
  365. let dx = p.x - ori.x;
  366. if (dx == 0 && dy > 0) {
  367. hAngle = 0;
  368. }
  369. else if (dx == 0 && dy < 0) {
  370. hAngle = 180;
  371. }
  372. else if (dy == 0 && dx > 0) {
  373. hAngle = 90;
  374. }
  375. else if (dy == 0 && dx < 0) {
  376. hAngle = 270;
  377. }
  378. else if (dx > 0 && dy > 0)//第一象限
  379. {
  380. hAngle = Math.atan2(dx, dy) * 180 / Math.PI;
  381. }
  382. else if (dx > 0 && dy < 0)//第二象限
  383. {
  384. hAngle = 180 - Math.atan2(dx, -dy) * 180 / Math.PI;
  385. }
  386. else if (dx < 0 && dy < 0)//第三象限
  387. {
  388. hAngle = 180 + Math.atan2(-dx, -dy) * 180 / Math.PI;
  389. }
  390. else if (dx < 0 && dy > 0)//第四象限
  391. {
  392. hAngle = 360 - Math.atan2(-dx, dy) * 180 / Math.PI;
  393. }
  394. return -hAngle + 360;
  395. }
  396. }