moveDest.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { GameLogic } from "./GameLogic";
  2. const {ccclass, property} = cc._decorator;
  3. @ccclass
  4. export default class MoveDest extends GameLogic {
  5. private _speed: number = 100;
  6. public get speed(): number {
  7. return this._speed;
  8. }
  9. public set speed(value: number) {
  10. this._speed = value;
  11. }
  12. private _destination: cc.Vec2 = cc.v2(0, 0);
  13. public get destination(): cc.Vec2 {
  14. return this._destination;
  15. }
  16. public set destination(value: cc.Vec2) {
  17. this._destination = value;
  18. }
  19. gameUpdate(dt: number): void {
  20. let offset = this.destination.sub(this.node.getPosition());
  21. if(offset.mag() <= this.speed * dt) {
  22. this.node.setPosition(this.destination);
  23. }
  24. else{
  25. let dirction = offset.normalize();
  26. this.node.x += this.speed * dt * dirction.x;
  27. this.node.y += this.speed * dt * dirction.y;
  28. }
  29. }
  30. isDone(): boolean {
  31. return this.node.x == this._destination.x && this.node.y == this._destination.y;
  32. }
  33. }