package raiding.engine.handler;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
public abstract class MotionHandler {
private int shooterId;
private float x1,y1 = 0;
protected float x2,y2 = 0;
protected Vector2f current;
private final float SPEEDFACTOR = 0.3f;
private int moveProgress = 0;
private double angle;
private int delay;
private int delayElapsed = 0;
private Animation image;
private Rectangle area;
private boolean active = false;
private boolean ended = false;
public MotionHandler(int shooterId, Animation image, Vector2f to, int delay) {
x2 = to.getX();
y2 = to.getY();
this.delay = delay;
this.image = image;
area = new Rectangle(x1, y1, 14, 14);
this.shooterId = shooterId;
}
public Vector2f getCurrentPosition() {
return current;
}
public void updateCurrentPosition(int delta) {
if (!active)
return;
if (endRules()) {
//if (current.getX() > 765 || current.getX() < -5 || current.getY() > 605 || current.getY() < -5 ) {
ended = true;
}
int xMod = (x2 > x1)?+1:-1;
int yMod = (y2 > y1)?+1:-1;
moveProgress += (delta * SPEEDFACTOR);
float xStep = x1 + xMod * (int) (moveProgress * Math.cos(this.angle));
float yStep = y1 + yMod * (int) (moveProgress * Math.sin(this.angle));
//System.out.println(" x:"+x1+" y:"+y1);
//System.out.println("step "+currentTime+ " x:"+xStep+" y:"+yStep);
current = new Vector2f(xStep, yStep);
}
public boolean isActivating(Vector2f from, int delta) {
delayElapsed +=delta;
if (active || delayElapsed < delay)
return false;
active = true;
x1 = from.getX();
y1 = from.getY();
startTrajectory();
return true;
}
private void startTrajectory() {
float catAd = Math.abs(x2 - x1);
float catOp = Math.abs(y2 - y1);
//double hip = Math.sqrt(Math.pow(catAd,2) + Math.pow(catOp,2));
angle = Math.atan(catOp/catAd);
current = new Vector2f(x1, y1);
}
public void drawBullet(Graphics g) {
image.draw(getCurrentPosition().getX(), getCurrentPosition().getY());
area.setLocation(getCurrentPosition().getX(), getCurrentPosition().getY());
//g.draw(area);
}
public Rectangle getArea() {
return area;
}
public int getShooterId() {
return shooterId;
}
public boolean isEnded() {
return ended;
}
public abstract boolean endRules();
}