package com.palepail.TestGame.Actions;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.CatmullRomSpline;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.palepail.TestGame.Model.MovableEntity;
import com.palepail.TestGame.Utilities.Configuration;
public class SplineMove extends Action {
Vector2 previousPosition;
Vector3[] path;
float time = 0;
int point = 0;
CatmullRomSpline spline;
int pointsPerSegment;
public SplineMove(MovableEntity entity, Array<Vector2> path) {
this(entity, path, 35);
}
public SplineMove(MovableEntity entity, Array<Vector2> path, int pointsPerSegment) {
this.pointsPerSegment = pointsPerSegment;
setDone(false);
this.entity = entity;
spline = new CatmullRomSpline();
// adds the points given to the spline as control points
// the mandatory length is 4 and the first and last are not used as
// control points
for (Vector2 point : path) {
spline.add(new Vector3(point.x, point.y, 0));
}
// creates a new vector3 array that has enough space for all points in
// the spline.
this.path = new Vector3[(path.size - 2) * pointsPerSegment];
for (int i = 0; i < this.path.length; i++) {
this.path[i] = new Vector3();
}
// creates and inserts new points between the control points into the
// given Vector3 array at the rate of pointsPerSegment
spline.getPath(this.path, pointsPerSegment);
}
public void update(int ticks) {
time += (Gdx.graphics.getDeltaTime() * Configuration.frameRate);
while (time >= 1) {
time--;
point++;
Vector3 point1 = this.path[point];
if (previousPosition != null) {
if (previousPosition.x - point1.x < -.002f) {
entity.setMOVE_LEFT(false);
entity.setMOVE_RIGHT(true);
} else if (previousPosition.x - point1.x > .002f) {
entity.setMOVE_RIGHT(false);
entity.setMOVE_LEFT(true);
}else{
entity.setMOVE_LEFT(false);
entity.setMOVE_RIGHT(false);
}
}
if (point1.x == 0 && point1.y == 0) {
setDone(true);
} else {
entity.setPosition(point1.x, point1.y);
previousPosition = entity.getPosition();
}
}
if (point > 90000) {
point = 0;
}
}
}