Package com.palepail.TestGame.Actions

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

package com.palepail.TestGame.Actions;

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 SmoothMoveTo extends Action {
  Vector2 motion;
  Rectangle entityPosition = new Rectangle();
  Vector2 previousPosition;
  float speed;

  public SmoothMoveTo(MovableEntity entity, Vector2 destinationPoint, float speed) {
    setDone(false);
    this.destinationPoint = destinationPoint;
    this.entity = entity;
    this.speed = speed;
    this.destinationRec = new Rectangle(destinationPoint.x - (1f * Configuration.gameScale / 2), destinationPoint.y
        - (1f * Configuration.gameScale / 2), 1f * Configuration.gameScale, 1f * Configuration.gameScale);

  }

  public void update(int ticks) {
    entityPosition.set(entity.getPosition().x - entity.getWidth() / 2, entity.getPosition().y + entity.getHeight()
        / 2, 1f * Configuration.gameScale / 2, 1f * Configuration.gameScale / 2);

    motion = destinationPoint.cpy().sub(entity.getPosition());
    motion.nor();

    if (!entity.getPosition().epsilonEquals(destinationPoint, speed)) {
      moveTo();
    } else {
      entity.setVelocity(0, 0);
      setDone(true);
     
    }
  }

  private void moveTo() {

    entity.setPreviousPosition(entity.getPosition().cpy());
    entity.getPosition().lerp(destinationPoint, .001f);

    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.SmoothMoveTo

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.