SkillSolate.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import GameConfig from "../../configData/GameConfig";
  2. import monsterManger from "../monster/monsterManger";
  3. import monsterNode from "../monster/monsterNode";
  4. import soldie from "../monster/soldie";
  5. import towerManger from "./towerManger";
  6. import DataObj from "../../configData/DataObj";
  7. import gameUI from "../UIFace/gameUI";
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class SkillSolate extends cc.Component {
  11. private _myPos: cc.Vec2 = null;
  12. private soldieThree: soldie[] = [];
  13. public towerType = 0;
  14. public myDataObj: DataObj = null;
  15. private doecount = 0;
  16. /**攻击范围内的怪物 */
  17. private attackmonster: monsterNode[] = [];
  18. onLoad() {
  19. this.myDataObj = new DataObj();
  20. this.node.opacity = 0;
  21. // this.initID();
  22. }
  23. /**根据ID读取数据 */
  24. public initID() {
  25. this._myPos = this.node.getPosition();
  26. this.myDataObj.writeSkill_Config(61);
  27. this.loadSolde(this.myDataObj.SkillData.parameter_1);
  28. this.scheduleOnce(() => {
  29. towerManger.instance.delateTower(this);
  30. this.node.destroy();
  31. }, Number(this.myDataObj.SkillData.parameter_2));
  32. }
  33. /**加载兵
  34. * @param num 3塔兵 2技能兵
  35. */
  36. private loadSolde(num: number) {
  37. let self = this;
  38. let arr: cc.Vec2[] = [cc.v2(-20, 0), cc.v2(20, 0), cc.v2(0, -20)];
  39. let arrtemp = this.myDataObj.getArr(this.myDataObj.SkillData.parameter_3);
  40. for (let i = 0; i < num; i++) {
  41. let monid = arrtemp[Math.floor(Math.random() * arrtemp.length)];
  42. this.myDataObj.writeMonster_Config(monid);
  43. cc.resources.load("prefabs/" + self.myDataObj.monsterData.name1, cc.Prefab, function (err, prefab: cc.Prefab) {
  44. var newNode = cc.instantiate(prefab);
  45. newNode.setPosition(self._myPos.add(arr[i]));
  46. monsterManger.instance.node.addChild(newNode, 3);
  47. let theSolde = newNode.getComponent(soldie);
  48. theSolde.setIsSkill(true);
  49. theSolde.initSkillMinter(self, monid);
  50. theSolde.setinitPos(self._myPos, self._myPos.add(arr[i]));
  51. self.soldieThree.push(theSolde);
  52. theSolde.setSche();
  53. });
  54. }
  55. }
  56. /**根据ID读取数据 */
  57. public initMagicID(minpos: cc.Vec2, ID: number, Startpos: cc.Vec2) {
  58. this._myPos = minpos;
  59. this.myDataObj.writeSkill_Config(61);
  60. this.loadMagicSolde(ID,Startpos);
  61. }
  62. /**加载兵
  63. * @param num 3塔兵 2技能兵
  64. */
  65. private loadMagicSolde(Id: number, Startpos: cc.Vec2) {
  66. let self = this;
  67. this.myDataObj.writeMonster_Config(Id);
  68. cc.resources.load("prefabs/" + self.myDataObj.monsterData.name1, cc.Prefab, function (err, prefab: cc.Prefab) {
  69. var newNode = cc.instantiate(prefab);
  70. let startpos = self.node.getPosition().add(cc.v2(0, -57));
  71. if (self._myPos.y > Startpos.y) {
  72. startpos = Startpos.add(cc.v2(0, 37));
  73. }
  74. newNode.setPosition(startpos);
  75. monsterManger.instance.node.addChild(newNode, 3);
  76. let theSolde = newNode.getComponent(soldie);
  77. theSolde.setIsSkill(true);
  78. theSolde.initLevelData(self, Id);
  79. theSolde.setinitPos(startpos, self._myPos);
  80. theSolde.initWalkToPoint(true);
  81. self.soldieThree.push(theSolde);
  82. theSolde.setSche();
  83. });
  84. }
  85. /**魔法塔获取石怪 */
  86. public getsoldieThree():soldie
  87. {
  88. return this.soldieThree[0];
  89. }
  90. /**兵死亡次数增加 */
  91. public dieCount() {
  92. this.doecount++;
  93. if (this.doecount == this.soldieThree.length) {
  94. towerManger.instance.delateTower(this);
  95. this.node.destroy();
  96. }
  97. }
  98. /**无怪物时兵归位 */
  99. private attackOver() {
  100. for (let i = 0; i < this.soldieThree.length; i++) {
  101. const element = this.soldieThree[i];
  102. element.initWalkToPoint(false);
  103. }
  104. }
  105. /**暂停动画 */
  106. public isPaused(ispause: boolean) {
  107. for (let i = 0; i < this.soldieThree.length; i++) {
  108. const element = this.soldieThree[i];
  109. element.ispaused(ispause);
  110. }
  111. }
  112. onDestroy() {
  113. for (let i = 0; i < this.soldieThree.length; i++) {
  114. const element = this.soldieThree[i];
  115. element.node.destroy();
  116. }
  117. this.Disassociate();
  118. }
  119. /**接触怪和兵关联 */
  120. private Disassociate() {
  121. for (let i = 0; i < this.soldieThree.length; i++) {
  122. const element = this.soldieThree[i];
  123. if (element.getMymonster() != null) {
  124. if (element.getMymonster().node != null) {
  125. element.getMymonster().DelateAllBarracks();
  126. element.getMymonster().animalwalk();
  127. element.getMymonster().stopAni(false);
  128. }
  129. }
  130. }
  131. }
  132. /**刷新 */
  133. public getIsInSqrt(pos: cc.Vec2) {
  134. let xiangl = this._myPos.sub(pos);
  135. let attack_range = 295;
  136. let a = attack_range;
  137. let b = attack_range * 5 / 6;
  138. let jieguo = xiangl.x * xiangl.x / (a * a) + xiangl.y * xiangl.y / (b * b);
  139. if (jieguo < 1) {
  140. return true;
  141. }
  142. else {
  143. return false;
  144. }
  145. }
  146. public getAttackmonster(): monsterNode[] {
  147. return this.attackmonster;
  148. }
  149. public addAttackmonster(monter: monsterNode) {
  150. let index = this.attackmonster.indexOf(monter);
  151. if (index < 0) {
  152. if (monter.myDataObj.monsterData.company_type == 2) {
  153. // if (this.myDataObj.TowerData.air_attack_switch == 'ON') {
  154. // this.attackmonster.push(monter);
  155. // this.moreMonsterAttack();
  156. // }
  157. }
  158. else
  159. {
  160. this.attackmonster.push(monter);
  161. this.moreMonsterAttack();
  162. }
  163. }
  164. }
  165. public DelateAllAttackmonster() {
  166. for (let i = 0; i < this.attackmonster.length; i++) {
  167. const element = this.attackmonster[i];
  168. if (element.node == null) {
  169. this.attackmonster.splice(i, 1);
  170. i--;
  171. continue;
  172. }
  173. let isIn = this.getIsInSqrt(element.node.getPosition());
  174. if (!isIn || element.AniStatus == 'ZhengDie') {
  175. this.attackmonster.splice(i, 1);
  176. i--;
  177. }
  178. }
  179. }
  180. /**调整兵围攻数量 */
  181. private moreMonsterAttack() {
  182. if (this.attackmonster.length <= 1) {
  183. return;
  184. }
  185. if (this.attackmonster.length == 2) {
  186. for (let i = 0; i < this.attackmonster.length; i++) {
  187. const element = this.attackmonster[i];
  188. element.DelateBarracksRetain(2);
  189. }
  190. }
  191. if (this.attackmonster.length >= 3) {
  192. for (let i = 0; i < this.attackmonster.length; i++) {
  193. const element = this.attackmonster[i];
  194. element.DelateBarracksRetain(1);
  195. }
  196. }
  197. }
  198. /**开始攻击 */
  199. public attckStart() {
  200. if (this.attackmonster.length == 0) {
  201. this.attackOver();
  202. return;
  203. }
  204. let self = this;
  205. this.scheduleOnce(function () {
  206. self.DelateAllAttackmonster();
  207. self.attckStart();
  208. }, 0.3);
  209. }
  210. }