Monster.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { _decorator, tween, Vec3 } from 'cc';
  2. import { GameMgr } from './GameFrameWork/GameMgr';
  3. import { ModulerBase } from './GameFrameWork/ModulerBase';
  4. import { MapMgr } from './GameFrameWork/MapMgr';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('Monster')
  7. export class Monster extends ModulerBase {
  8. //private _gameMgr: GameMgr = null;
  9. private _speed: number = 0;
  10. private _points: Vec3[] = null;
  11. protected onLoad(): void {
  12. //this._gameMgr = this.node.parent.parent.getComponent(GameMgr);
  13. //this._points = this._gameMgr.getPath();
  14. this._points = GameMgr.Instance.getModuler(MapMgr).getPath();
  15. }
  16. start() {
  17. const point = this._points[0];
  18. this.node.setPosition(point.x, point.y);
  19. this._speed = 100;
  20. this.move(this._points, 1)
  21. }
  22. move(points: Vec3[],index: number){
  23. if(index >= points.length - 1){
  24. return;
  25. }
  26. const endPos = points[index];
  27. const curPos = points[index - 1];
  28. //计算向量差
  29. const dis: number = endPos.clone().subtract(curPos).length();
  30. const t: number = dis / this._speed;
  31. tween(this.node)
  32. .to(t,{position:points[index]})
  33. .call(()=>{
  34. this.move(points, index + 1)
  35. })
  36. .start();
  37. }
  38. private _destroyMonster(points: Vec3[]) {
  39. if(this.node.getPosition().x === points[points.length - 2].x
  40. && this.node.getPosition().y === points[points.length - 2].y){
  41. this.schedule(()=>{
  42. this.node.destroy();
  43. },1)
  44. }
  45. }
  46. update(deltaTime: number) {
  47. this._destroyMonster(this._points)
  48. }
  49. }