FPatrol.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import CMath from "../../../../util/CMath";
  2. import FF from "../../FF";
  3. import FSprite from "../../object/FSprite";
  4. /**
  5. * 巡逻兵
  6. */
  7. const {ccclass, property} = cc._decorator;
  8. @ccclass
  9. export default class FPatrol extends cc.Component {
  10. @property({
  11. displayName: '起点到终点时间(秒)'
  12. })
  13. public time = 2;
  14. @property({
  15. displayName: '每次巡逻停留时间(秒)'
  16. })
  17. public interval = 0.5;
  18. @property({
  19. type:cc.Node,
  20. displayName: '巡逻目的地'
  21. })
  22. public target:cc.Node = null;
  23. @property({
  24. type:cc.Node,
  25. displayName: '警示区域'
  26. })
  27. public warn:cc.Node = null;
  28. @property({
  29. type:sp.Skeleton,
  30. displayName: '精灵动画'
  31. })
  32. public spine:sp.Skeleton = null;
  33. private initPos:cc.Vec2;
  34. private targetPos:cc.Vec2;
  35. public ff:FF;
  36. onLoad () {
  37. this.initPos = this.node.getPosition();
  38. this.targetPos = this.target.getPosition();
  39. this.moveToB();
  40. }
  41. private moveToB(){
  42. cc.tween(this.node).sequence(
  43. cc.delayTime(this.interval),
  44. cc.callFunc(()=>{
  45. // if(this.targetPos.x > 0){
  46. // this.node.scaleX = Math.abs(this.node.scaleX);
  47. // }else{
  48. // this.node.scaleX = -Math.abs(this.node.scaleX);
  49. // }
  50. }),
  51. cc.moveBy(this.time,this.targetPos),
  52. cc.callFunc(()=>{
  53. this.moveToA();
  54. })
  55. ).start();
  56. cc.tween(this.warn).sequence(
  57. cc.delayTime(this.interval),
  58. cc.callFunc(()=>{
  59. let angle = CMath.getAngle(cc.Vec2.ZERO_R,this.targetPos)*180/Math.PI;
  60. this.warn.angle = angle;
  61. }),
  62. cc.moveBy(this.time,this.targetPos),
  63. cc.callFunc(()=>{
  64. })
  65. ).start();
  66. }
  67. private moveToA(){
  68. let pos = this.targetPos.clone();
  69. pos.x = -pos.x;
  70. pos.y = -pos.y;
  71. cc.tween(this.node).sequence(
  72. cc.delayTime(this.interval),
  73. cc.callFunc(()=>{
  74. // if(pos.x > 0){
  75. // this.node.scaleX = Math.abs(this.node.scaleX);
  76. // }else{
  77. // this.node.scaleX = -Math.abs(this.node.scaleX);
  78. // }
  79. }),
  80. cc.moveBy(this.time,pos),
  81. cc.callFunc(()=>{
  82. this.moveToB();
  83. })
  84. ).start();
  85. cc.tween(this.warn).sequence(
  86. cc.delayTime(this.interval),
  87. cc.callFunc(()=>{
  88. let angle = CMath.getAngle(cc.Vec2.ZERO_R,pos)*180/Math.PI;
  89. this.warn.angle = angle;
  90. }),
  91. cc.moveBy(this.time,pos),
  92. cc.callFunc(()=>{
  93. })
  94. ).start();
  95. }
  96. }