PSprite.ts 8.6 KB

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