PSprite.ts 8.7 KB

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