Package com.palepail.TestGame.Actions

Source Code of com.palepail.TestGame.Actions.MoveTo

package com.palepail.TestGame.Actions;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.palepail.TestGame.Model.MovableEntity;
import com.palepail.TestGame.Utilities.Configuration;

public class MoveTo extends Action {

  float time = 0;

  public MoveTo(MovableEntity entity, Vector2 destinationPoint) {
    setDone(false);
    this.destinationPoint = destinationPoint;
    this.entity = entity;
    this.destinationRec = new Rectangle(destinationPoint.x, destinationPoint.y, 1f * Configuration.gameScale,
        1f * Configuration.gameScale);
  }

  public void update(int ticks) {

    if (!entity.getBounds().overlaps(destinationRec)) {

      moveTo(destinationPoint);

    } else {
      entity.getVelocity().x = 0;
      entity.getVelocity().y = 0;
      setDone(true);
    }

  }

  private void moveTo(Vector2 destinationPoint) {

    entity.setPreviousPosition(entity.getPosition().cpy());
    entity.getPosition().lerp(destinationPoint, Gdx.graphics.getDeltaTime() * 1.5f);

    if (entity.getPreviousPosition() != null) {
      if (entity.getPreviousPosition().x - entity.getPosition().x < -.002f) {
        entity.setMOVE_LEFT(false);
        entity.setMOVE_RIGHT(true);

      } else if (entity.getPreviousPosition().x - entity.getPosition().x > .002f) {
        entity.setMOVE_RIGHT(false);
        entity.setMOVE_LEFT(true);

      } else {
        entity.setMOVE_LEFT(false);
        entity.setMOVE_RIGHT(false);
      }
    }
  }

}
TOP

Related Classes of com.palepail.TestGame.Actions.MoveTo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.