arrow.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import DataObj from "../../configData/DataObj";
  2. import GameConfig from "../../configData/GameConfig";
  3. import monsterNode from "../monster/monsterNode";
  4. import Archer from "./Archer";
  5. import taside from "./taside";
  6. import TowerHuan from "./TowerHuan";
  7. import towerManger from "./towerManger";
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class arrow extends cc.Component {
  11. private _taHuan: TowerHuan = null;
  12. public _taside: taside = null;
  13. public taHuanVec: cc.Vec2 = null;
  14. public myDataObj: DataObj = null;
  15. /**攻击范围内的怪物 */
  16. private attackmonster: monsterNode[] = [];
  17. private archer: Archer[] = [];
  18. public towerType = 0;
  19. /**左技能ID */
  20. public SkillIDL = 0;
  21. /**右技能ID */
  22. public SkillIDR = 0;
  23. private timeCount = 0;
  24. private isStartWait = true;
  25. public talantData = {
  26. };
  27. protected onLoad() {
  28. this.myDataObj = new DataObj();
  29. }
  30. /**根据ID读取数据 */
  31. public initID(ID: number) {
  32. this.myDataObj.writeTower_Config(ID);
  33. this.myDataObj.writeBullet_Config(this.myDataObj.TowerData.bullet_id);
  34. if (towerManger.instance.talantData[1].length > 0 && towerManger.instance.talantData[3].length > 0) {
  35. this.myDataObj.TowerData.attack_range *= (1 + towerManger.instance.talantData[3][0] + towerManger.instance.talantData[1][0]);
  36. }
  37. else {
  38. if (towerManger.instance.talantData[1].length > 0) {
  39. this.myDataObj.TowerData.attack_range *= (1 + towerManger.instance.talantData[1][0]);
  40. }
  41. if (towerManger.instance.talantData[3].length > 0) {
  42. this.myDataObj.TowerData.attack_range *= (1 + towerManger.instance.talantData[3][0]);
  43. }
  44. }
  45. this.loadArcher(this.myDataObj.TowerData.monster_id[1]);
  46. }
  47. /**设置或升级技能ID 技能类型 1左边 2右边*/
  48. public setandUpSkillId(skillType:number)
  49. {
  50. if (skillType == 1) {
  51. if (this.SkillIDL == 0) {
  52. this.SkillIDL = this.myDataObj.TowerData.next_skill[0];
  53. }
  54. else
  55. {
  56. this.myDataObj.writeSkill_Config(this.SkillIDL);
  57. this.SkillIDL = this.myDataObj.SkillData.next_skill;
  58. }
  59. }
  60. if (skillType == 2) {
  61. if (this.SkillIDR == 0) {
  62. this.SkillIDR = this.myDataObj.TowerData.next_skill[1];
  63. }
  64. else
  65. {
  66. this.myDataObj.writeSkill_Config(this.SkillIDR);
  67. this.SkillIDR = this.myDataObj.SkillData.next_skill;
  68. }
  69. }
  70. // this.skillSpeattack();
  71. }
  72. private loadArcher(num: number) {
  73. let self = this;
  74. let arr = [];
  75. if (num == 1) {
  76. arr = [cc.v2(0, 20)];
  77. }
  78. if (num == 2) {
  79. arr = [cc.v2(-21, 20), cc.v2(11, 20)];
  80. if (this.myDataObj.TowerData.id == 3) {
  81. arr = [cc.v2(-19, 5), cc.v2(13, 5)];
  82. }
  83. if (this.myDataObj.TowerData.id == 4) {
  84. arr = [cc.v2(-19, -5), cc.v2(13, -5)];
  85. }
  86. if (this.myDataObj.TowerData.id == 7) {
  87. arr = [cc.v2(-19-10, 8), cc.v2(13+10, 8)];
  88. }
  89. }
  90. if (num == 3) {
  91. arr = [cc.v2(-30, 20), cc.v2(0, 20), cc.v2(30, 20)];
  92. }
  93. cc.resources.load("prefabs/" + this.myDataObj.TowerData.name2, cc.Prefab, function (err, prefab: cc.Prefab) {
  94. for (let i = 0; i < num; i++) {
  95. var newNode = cc.instantiate(prefab);
  96. self.node.addChild(newNode);
  97. newNode.setPosition(arr[i]);
  98. let theSolde = newNode.getComponent(Archer);
  99. theSolde.initTower(self);
  100. self.archer.push(theSolde);
  101. }
  102. if (self.myDataObj.TowerData.tower_level == 1) {
  103. self.isStartWait = false;
  104. }
  105. else
  106. {
  107. self.scheduleOnce(function(){
  108. self.isStartWait = false;
  109. },self.myDataObj.TowerData.attack_cd);
  110. }
  111. });
  112. }
  113. public setTaHuan(taHuan: TowerHuan) {
  114. this._taHuan = taHuan;
  115. this.taHuanVec = taHuan.node.getPosition();
  116. this._taHuan.setRedHuanScale(this.myDataObj.TowerData.attack_range);
  117. this._taside.setRedHuanScale(this.myDataObj.TowerData.attack_range);
  118. }
  119. public setNitu(mytaside: taside) {
  120. this._taside = mytaside;
  121. }
  122. onDestroy() {
  123. this._taHuan.node.destroy();
  124. this.attackmonster = [];
  125. }
  126. public getIsInSqrt(pos: cc.Vec2): boolean {
  127. let xiangl = this.taHuanVec.sub(pos);
  128. let attack_range = this.myDataObj.TowerData.attack_range;
  129. let a = attack_range;
  130. let b = attack_range * 5 / 6;
  131. if (xiangl.x * xiangl.x / (a * a) + xiangl.y * xiangl.y / (b * b) < 1) {
  132. return true;
  133. }
  134. else {
  135. return false;
  136. }
  137. }
  138. public getAttackmonster(): monsterNode[] {
  139. return this.attackmonster;
  140. }
  141. public addAttackmonster(monter: monsterNode) {
  142. if (!this.isStartWait) {
  143. if (monter.myDataObj.monsterData.company_type == 2) {
  144. if (this.myDataObj.TowerData.air_attack_switch == 'ON') {
  145. this.attackmonster.push(monter);
  146. }
  147. }
  148. else
  149. {
  150. this.attackmonster.push(monter);
  151. }
  152. }
  153. }
  154. private DelateAllAttackmonster() {
  155. for (let i = 0; i < this.attackmonster.length; i++) {
  156. const element = this.attackmonster[i];
  157. if (element.node == null) {
  158. console.log("arraw!!!!!!!!!!");
  159. this.attackmonster.splice(i, 1);
  160. i--;
  161. continue;
  162. }
  163. if (!this.getIsInSqrt(element.node.getPosition()) || element.AniStatus == 'ZhengDie') {
  164. this.attackmonster.splice(i, 1);
  165. i--;
  166. }
  167. }
  168. }
  169. /**开始攻击 */
  170. public attckStart() {
  171. if (this.attackmonster.length == 0) {
  172. return;
  173. }
  174. for (let i = 0; i < this.archer.length; i++) {
  175. const element = this.archer[i];
  176. this.scheduleOnce(function () {
  177. element.attack(this.attackmonster[0]);
  178. }, i * 0.3);
  179. }
  180. }
  181. public oneAccack(myArcher: Archer) {
  182. this.DelateAllAttackmonster();
  183. if (this.attackmonster.length == 0) {
  184. return;
  185. }
  186. myArcher.attack(this.attackmonster[0]);
  187. }
  188. /**特殊技能 */
  189. private skillSpeattack() {
  190. /**冷却时间 */
  191. let delayTime = this.myDataObj.SkillData.parameter_4;
  192. if (this.SkillIDR >= 4 && this.SkillIDR <= 6) {
  193. this.schedule(this.skill4_6, delayTime);
  194. }
  195. if (this.SkillIDR >= 10 && this.SkillIDR <= 12) {
  196. this.schedule(this.skill10_12, delayTime);
  197. }
  198. }
  199. /**id 4-6技能 藤蔓缠绕*/
  200. private skill4_6() {
  201. if (this.attackmonster.length == 0) {
  202. return;
  203. }
  204. this.myDataObj.writeSkill_Config(this.SkillIDR);
  205. this.timeCount = 0;
  206. /**伤害数值 */
  207. let shanghai = Number(this.myDataObj.SkillData.parameter_2);
  208. /**持续时间 */
  209. let totalcount = Number(this.myDataObj.SkillData.parameter_3);
  210. /**目标数量 */
  211. let pointNum = this.myDataObj.SkillData.parameter_5;
  212. for (let i = 0; i < this.attackmonster.length; i++) {
  213. const element = this.attackmonster[i];
  214. if (i < pointNum) {
  215. element.SetWalk(true);
  216. }
  217. }
  218. this.schedule(() => {
  219. this.timeCount++;
  220. for (let i = 0; i < this.attackmonster.length; i++) {
  221. const element = this.attackmonster[i];
  222. if (i < pointNum) {
  223. element.hpDown(shanghai);
  224. if (this.timeCount >= totalcount) {
  225. element.SetWalk(false);
  226. }
  227. }
  228. }
  229. }, 1, totalcount - 1);
  230. }
  231. /**id 10-12技能 弹幕射击*/
  232. private skill10_12() {
  233. if (this.attackmonster.length == 0) {
  234. return;
  235. }
  236. this.myDataObj.writeSkill_Config(this.SkillIDR);
  237. this.timeCount = 0;
  238. /**伤害数值 */
  239. let shanghai = this.myDataObj.getRanm(1, this.myDataObj.SkillData.parameter_2);
  240. /**射程半径 */
  241. let totalcount = Number(this.myDataObj.SkillData.parameter_3);
  242. /**目标数量 */
  243. let pointNum = this.myDataObj.SkillData.parameter_5;
  244. let dex = 0;
  245. let juli = 10000;
  246. for (let i = 0; i < this.attackmonster.length; i++) {
  247. const element = this.attackmonster[i];
  248. let monTotaLeth = this.taHuanVec.sub(element.node.getPosition()).mag();
  249. if (monTotaLeth < juli) {
  250. juli = monTotaLeth;
  251. dex = i;
  252. }
  253. }
  254. let dexPos = this.attackmonster[dex].node.getPosition();
  255. let arrTemp: monsterNode[] = [];
  256. for (let i = 0; i < this.attackmonster.length; i++) {
  257. const element = this.attackmonster[i];
  258. let monToDexLeth = dexPos.sub(element.node.getPosition()).mag();
  259. if (monToDexLeth < totalcount) {
  260. arrTemp.push(element);
  261. }
  262. }
  263. for (let i = 0; i < arrTemp.length; i++) {
  264. const element = arrTemp[i];
  265. if (i < pointNum) {
  266. element.hpDown(shanghai);
  267. }
  268. }
  269. }
  270. }