PSprite.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * 跟随者
  3. */
  4. import CMath from "../../../util/CMath";
  5. import FSprite,{SpriteActionType} from "./FSprite";
  6. const {ccclass, property} = cc._decorator;
  7. @ccclass
  8. export default class PSprite extends FSprite {
  9. /**
  10. * 是否跟随
  11. */
  12. private isFollow = false;
  13. /**
  14. * 前面的
  15. */
  16. public inFront: FSprite;
  17. public zt:boolean = false//暂停
  18. /**
  19. * 行走路径记录
  20. */
  21. private PosList: Array<cc.Vec2> = [];
  22. private RecordGap = 3; //目标移动多远记录一次距离
  23. private StopCount = 7; //记录还剩多少时停止移动
  24. private isFloowStart = false;//是否开始跟随
  25. /**
  26. * 设置领队
  27. * @param sprite
  28. */
  29. public setLeader(sprite: FSprite) {
  30. this.inFront = sprite;
  31. this.node.x = this.inFront.node.x
  32. this.node.y = this.inFront.node.y
  33. this.isFollow = true;
  34. }
  35. /**
  36. * 开始跟随
  37. */
  38. public startFollow() {
  39. if (this.isFloowStart) {
  40. return;
  41. }
  42. if (!this.isFollow) {
  43. this.setShooting(false);
  44. this.isFloowStart = true;
  45. this.PosList = [];
  46. let p1 = this.ff.mainSprite.node.getPosition();
  47. let pos = cc.v2();
  48. pos.x = p1.x;
  49. pos.y = p1.y;
  50. this.playAction(SpriteActionType.run, true);
  51. cc.tween(this.node).sequence(
  52. cc.moveTo(0.5, pos),
  53. cc.callFunc(() => {
  54. this.playAction(SpriteActionType.stand, true);
  55. this.tmpActionType = null;
  56. this.isFollow = true;
  57. this.isFloowStart = false;
  58. })
  59. ).start()
  60. }
  61. }
  62. /**
  63. * 结束跟随
  64. */
  65. public stopFollow() {
  66. this.playAction(SpriteActionType.stand, true);
  67. this.isFollow = false;
  68. }
  69. public update(dt) {
  70. if(this.zt){
  71. return
  72. }
  73. if (this.gamePause) {
  74. return;
  75. }
  76. if(!this.inFront){
  77. return;
  78. }
  79. if (this.PosList.length > 0) {
  80. //跟随
  81. if (this.isFollow) {
  82. //添加当前Target位置
  83. let fp = this.inFront.node.getPosition();
  84. let lp = this.PosList[this.PosList.length - 1];
  85. let distance = cc.Vec2.distance(fp, lp);
  86. if(distance > this.RecordGap){
  87. this.PosList.push(fp);
  88. }
  89. if (this.PosList.length > this.StopCount) {
  90. let p0 = this.node.getPosition();
  91. let p1 = this.PosList[0];
  92. if (this.updateSpineF(p0, p1)) {
  93. // cc.log('切换了动作');
  94. this.tmpActionType = null;
  95. }
  96. this.moveTo(p1, dt);
  97. this.isWalk = true;
  98. this.playAction(SpriteActionType.run, true);
  99. }
  100. else {
  101. this.isWalk = this.inFront.isWalk;
  102. if (this.isWalk) {
  103. this.playAction(SpriteActionType.run, true);
  104. } else {
  105. this.playAction(SpriteActionType.stand, true);
  106. }
  107. }
  108. //清除已经到达的点
  109. while (this.PosList.length > 0) {
  110. let distance = cc.Vec2.distance(this.node.getPosition(), this.PosList[0]);
  111. if (distance <= this.RecordGap) {
  112. this.PosList.shift();
  113. } else {
  114. break;
  115. }
  116. }
  117. } else {
  118. this.tmpActionType = null;
  119. if (!this.isFloowStart) {
  120. this.updateMove(dt)
  121. }
  122. }
  123. } else {
  124. if (!this.isFloowStart) {
  125. this.PosList.push(this.inFront.node.getPosition());
  126. }
  127. }
  128. }
  129. /**
  130. * 更新跟随角色的方向
  131. */
  132. private updateSpineF(p1: cc.Vec2, p2: cc.Vec2): boolean {
  133. let tmpSpine = this.spine;
  134. //计算弧度
  135. // let angle = Math.floor(CMath.getAngle(p1, p2) * (180 / Math.PI));
  136. // this.updateWeaponAngle(angle);
  137. let x0 = Math.floor(p1.x);
  138. let x1 = Math.floor(p2.x);
  139. if (Math.abs(x0-x1) < 2) {//不改变方向
  140. } else if (x0 - x1 > 0) {
  141. this.setLR(-1);
  142. } else {
  143. this.setLR(1);
  144. }
  145. if (tmpSpine == this.spine) {
  146. return false;
  147. } else {
  148. if (tmpSpine) {
  149. tmpSpine.node.active = false;
  150. }
  151. this.spine.node.active = true;
  152. }
  153. return true;
  154. }
  155. /**
  156. * 设置坐标和方向
  157. */
  158. public setPosition(pos) {
  159. this.node.setPosition(pos);
  160. this.PosList = [];
  161. }
  162. public removeSelf() {
  163. this.playAction2(SpriteActionType.dead, false, () => {
  164. //替换为幽灵
  165. this.setGhost();
  166. });
  167. }
  168. /**
  169. * 死亡后设置为幽灵
  170. */
  171. private setGhost() {
  172. cc.resources.load('prefab/monter/ghost', cc.Prefab, (err, prefab: cc.Prefab) => {
  173. if (err) {
  174. cc.error(err);
  175. } else {
  176. let node = cc.instantiate(prefab);
  177. let spine = node.getComponent(sp.Skeleton);
  178. node.parent = this.node.getChildByName('juese01');
  179. this.spine.node.active = false;
  180. this.spine = spine;
  181. for (let i = 0; i < this.mPanels.length; i++) {
  182. const element = this.mPanels[i];
  183. element.setClose()
  184. }
  185. }
  186. });
  187. }
  188. /**
  189. * 目标位置的切线方向移动
  190. * @param target
  191. */
  192. public tangentMove(target:cc.Node){
  193. let p1 = this.node.getPosition();
  194. let p2 = target.getPosition();
  195. let tan = CMath.getAngle(p1,p2);
  196. let angle = tan*180/Math.PI;
  197. let rand = CMath.getRandom(0,1);
  198. if(rand > 0){
  199. angle += 90
  200. }else{
  201. angle -= 90;
  202. }
  203. if(angle > 180){
  204. angle -= 180;
  205. }
  206. if(angle < -180){
  207. angle += 180;
  208. }
  209. let dir = this.getDir(angle);
  210. cc.tween(this.node).sequence(
  211. cc.delayTime(0.5),
  212. cc.callFunc(()=>{
  213. this.setDir(dir);
  214. })
  215. ).start();
  216. }
  217. private getDir(angle){
  218. if(angle >= -30 && angle < 30){
  219. return {x:1.4,y:0}
  220. }else if(angle >= 30 && angle < 60){
  221. return {x:1,y:1}
  222. }else if(angle >= 60 && angle < 120){
  223. return {x:0,y:1.4}
  224. }else if(angle >= 120 && angle < 150){
  225. return {x:-1,y:1}
  226. }else if(angle >= 150 && angle < 180){
  227. return {x:-1.4,y:0}
  228. }else if(angle >= -180 && angle < -150){
  229. return {x:-1.4,y:0}
  230. }else if(angle >= -150 && angle < -120){
  231. return {x:-1,y:-1}
  232. }else if(angle >= -120 && angle < -60){
  233. return {x:0,y:-1.4}
  234. }else if(angle >= -60 && angle < -30){
  235. return {x:1,y:-1}
  236. }
  237. }
  238. }