Archer.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import DataObj from "../../configData/DataObj";
  2. import GameConfig from "../../configData/GameConfig";
  3. import monsterNode from "../monster/monsterNode";
  4. import arrow from "./arrow";
  5. import towerManger from "./towerManger";
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class Archer extends cc.Component {
  9. @property(cc.Sprite)
  10. private jiantou: cc.Sprite = null;
  11. private animal: sp.Skeleton = null;
  12. private minter: monsterNode = null;
  13. private interVec: cc.Vec2 = null;
  14. private paodanstartPos: cc.Vec2 = null;
  15. /**是否开始计时 */
  16. private timeUp = false;
  17. /**计时数 */
  18. private timeCount = 0;
  19. private _arraw: arrow = null;
  20. protected onLoad() {
  21. this.jiantou.node.opacity = 0;
  22. this.animal = this.node.getComponent(sp.Skeleton);
  23. this.jiantou.node.setPosition(1, 45);
  24. this.paodanstartPos = this.jiantou.node.getPosition();
  25. }
  26. public initTower(myarraw: arrow) {
  27. this._arraw = myarraw;
  28. this.jiantou.node.scale = this._arraw.myDataObj.BulletData.model_scaling;
  29. if (this._arraw.myDataObj.TowerData.id != 7) {
  30. this.archerAngleUpdate();
  31. }
  32. }
  33. public attack(aminter: monsterNode) {
  34. this.minter = aminter;
  35. this.animal.setAnimation(0, "animation", false).timeScale = GameConfig.instance().speed;
  36. this.interVec = this.minter.getNewPos().sub(cc.v2(0, -45));
  37. let xianglin = this.minter.getTimeSetDistance(this._arraw.myDataObj.BulletData.speed+0.4);
  38. this.interVec = this.interVec.add(xianglin);
  39. this.scheduleOnce(function () {
  40. this.initFeiData();
  41. }, 0.4);
  42. }
  43. private initFeiData() {
  44. this.jiantou.node.setPosition(this.paodanstartPos);
  45. let arrawpos = this._arraw.node.getPosition();
  46. let mypos = arrawpos.add(this.node.getPosition());
  47. let danpos = mypos.add(this.jiantou.node.getPosition());
  48. this.paodanUpAction(this.interVec, danpos);
  49. // console.log("this.interVec:"+this.interVec+" danpos:"+danpos);
  50. }
  51. private paodanUpAction(pos: cc.Vec2, danpos: cc.Vec2) {
  52. // this.jiantou.node.angle = 0;
  53. let hetght = this._arraw.myDataObj.BulletData.parabola[0];
  54. if (pos.y > danpos.y) {
  55. hetght = 50+Math.abs(pos.y - danpos.y)*0.5 + this._arraw.myDataObj.BulletData.parabola[0];
  56. }
  57. let endpos = pos.sub(danpos);
  58. if (this.interVec.x > this._arraw.taHuanVec.x) {
  59. this.animal.node.scaleX = -1;
  60. endpos = cc.v2(-endpos.x, endpos.y);
  61. }
  62. else {
  63. this.animal.node.scaleX = 1;
  64. }
  65. if (this._arraw.myDataObj.TowerData.id == 7) {
  66. cc.tween(this.jiantou.node)
  67. .by(this._arraw.myDataObj.BulletData.speed,{position:cc.v3(endpos.sub(cc.v2(0,25)))})
  68. .start();
  69. this.jiantou.node.opacity = 255;
  70. }
  71. else {
  72. this.ParabolaTo(this._arraw.myDataObj.BulletData.speed, cc.v2(0, 0), endpos, hetght, this._arraw.myDataObj.BulletData.parabola[1]);
  73. this.scheduleOnce(function () {
  74. this.jiantou.node.opacity = 255;
  75. }, 0.03);
  76. }
  77. this.scheduleOnce(function () {
  78. this.paodanhuanyuan();
  79. }, this._arraw.myDataObj.BulletData.speed);
  80. this.scheduleOnce(function () {
  81. this._arraw.oneAccack(this);
  82. }, this._arraw.myDataObj.TowerData.attack_cd);
  83. }
  84. /**即时更新箭头角度 */
  85. private archerAngleUpdate() {
  86. let self = this;
  87. let lastP = this.jiantou.node.getPosition();
  88. this.schedule(function () {
  89. let curP = self.jiantou.node.getPosition();
  90. if (curP.x != lastP.x && curP.y != lastP.y) {
  91. let dirVec = curP.sub(lastP);//获得从startPos指向endPos的方向向量
  92. let comVec = new cc.Vec2(0, 1);//计算夹角的参考方向,这里选择x轴正方向
  93. let radian = cc.v2(dirVec).signAngle(comVec);// --向量夹角弧度(相对于y轴正向)
  94. let angle = radian * 180 / Math.PI; //-- 弧度值转换为角度 公式:弧度=度*π/180
  95. self.jiantou.node.angle = -angle;
  96. }
  97. lastP = curP;
  98. }, 0);
  99. }
  100. /**抛物线运动 */
  101. private ParabolaTo(t: number, startPoint: cc.Vec2, endPoint: cc.Vec2, height: number, angle: number) {
  102. // 把角度转换为弧度
  103. // let radian = angle ;
  104. let radian = angle * 3.14159 / 180.0;
  105. // 第一个控制点为抛物线左半弧的中点
  106. let q1x = startPoint.x + (endPoint.x - startPoint.x) / 2.0;
  107. let q1 = cc.v2(q1x, height + startPoint.y + Math.cos(radian) * q1x);
  108. // 第二个控制点为整个抛物线的中点
  109. let q2x = startPoint.x + (endPoint.x - startPoint.x) / 2.0;
  110. let q2 = cc.v2(q2x, height + startPoint.y + Math.cos(radian) * q2x);
  111. //曲线配置
  112. var bezier = [q1, q2, endPoint];
  113. // console.log("endPoint:"+endPoint);
  114. //使用CCEaseInOut让曲线运动有一个由慢到快的变化,显得更自然
  115. let bezerby = cc.bezierTo(t, bezier);
  116. this.jiantou.node.runAction(bezerby);
  117. // this.jiantou.node.runAction(bezerby.easing(cc.easeInOut(t)));
  118. }
  119. private paodanhuanyuan() {
  120. if (this.minter.AniStatus == 'ZhengDie'&&this._arraw.myDataObj.TowerData.id != 7) {
  121. this.scheduleOnce(function () {
  122. this.jiantou.node.opacity = 0;
  123. this.jiantou.node.setPosition(this.paodanstartPos);
  124. }, this._arraw.myDataObj.TowerData.attack_cd - this._arraw.myDataObj.BulletData.speed-0.1);
  125. }
  126. else {
  127. this.jiantou.node.opacity = 0;
  128. this.jiantou.node.setPosition(this.paodanstartPos);
  129. }
  130. let attck = this._arraw.myDataObj.getRanm(1, this._arraw.myDataObj.BulletData.attack);
  131. let reduction = this.minter.myDataObj.monsterData.physics_reduction;
  132. if (towerManger.instance.talantData[2].length > 0) {
  133. reduction -= towerManger.instance.talantData[2][0];
  134. if (reduction < 0) {
  135. reduction = 0;
  136. }
  137. }
  138. if (towerManger.instance.talantData[4].length > 0) {
  139. if (Math.random() < towerManger.instance.talantData[4][0]) {
  140. attck *= towerManger.instance.talantData[4][1];
  141. }
  142. }
  143. let realHurt = attck * (1 - reduction);
  144. //技能7-9 爆头一击
  145. // if (this._arraw.SkillIDL >= 7 && this._arraw.SkillIDL <= 9) {
  146. // this._arraw.myDataObj.writeSkill_Config(this._arraw.SkillIDL);
  147. // let jilv = Number(this._arraw.myDataObj.SkillData.parameter_2);
  148. // if (Math.random() < jilv) {
  149. // realHurt *= 2;
  150. // }
  151. // }
  152. this.minter.hpDown(realHurt);
  153. // this.skillNorattack(this._arraw.SkillIDL);
  154. }
  155. /**普攻技能 */
  156. private skillNorattack(ID: number) {
  157. if (ID <= 3 && ID > 0) {
  158. if (!this.timeUp) {
  159. this.timeUp = true;
  160. this.skill1_3();
  161. }
  162. else {
  163. this.timeCount = 0;
  164. }
  165. }
  166. }
  167. /**id 1-3技能 剧毒箭矢*/
  168. private skill1_3() {
  169. this.scheduleOnce(() => {
  170. this._arraw.myDataObj.writeSkill_Config(this._arraw.SkillIDL);
  171. let shanghai = Number(this._arraw.myDataObj.SkillData.parameter_2);
  172. let totalcount = Number(this._arraw.myDataObj.SkillData.parameter_3);
  173. this.minter.hpDown(shanghai);
  174. this.timeCount++;
  175. if (this.timeCount <= totalcount) {
  176. this.skill1_3();
  177. }
  178. else {
  179. this.timeUp = false;
  180. }
  181. }, 1);
  182. }
  183. }