package framework.collision;
import main.L;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Vector2f;
import framework.spacial.Line;
public class LineCollisionMask implements CollisionMask {
private Line line = null;
public LineCollisionMask(Line line){
this.line = line;
}
@Override
public boolean isCollision(Vector2f thisOrigin, Vector2f otherOrigin, CollisionMask otherMask) {
if(otherMask != null && line != null && line.getFrom() != null && line.getTo() != null){
if(otherMask instanceof PointCollisionMask){
return getLine().translate(thisOrigin).isOnLine(otherOrigin.add(otherMask.getRelativePositionOfOrigin()));
}else if(otherMask instanceof CircularCollisionMask){
CircularCollisionMask c = (CircularCollisionMask) otherMask;
Vector2f centrePos = otherOrigin.add(c.getRelativePositionOfOrigin());
float distanceToCentre = getLine().translate(thisOrigin).getShortestDistanceTo(centrePos);
return distanceToCentre <= c.getRadius();
}else if(otherMask instanceof LineCollisionMask){
LineCollisionMask l = (LineCollisionMask) otherMask;
return getLine().translate(thisOrigin).intersetsWith(l.getLine().translate(otherOrigin));
}
}
return false;
}
@Override
public float getMaxWidth() {
return line != null ? line.getXWidth() : 0;
}
@Override
public float getMaxHeight() {
return line != null ? line.getYHeight() : 0;
}
@Override
public void rotate(float theta) {
if(line != null){
line.rotate(theta);
}
}
public Line getLine() {
return line.copy();
}
public void setLine(Line line) {
this.line = line;
}
@Override
public Vector2f getRelativePositionOfOrigin() {
return line != null ? line.getFrom() : null;
}
@Override
public void renderForDebug(Graphics g, Vector2f pos) {
Line l = getLine().translate(pos);
int x1 = Math.round(l.getFrom().getX());
int y1 = Math.round(l.getFrom().getY());
int x2 = Math.round(l.getTo().getX());
int y2 = Math.round(l.getTo().getY());
g.drawLine(x1, y1, x2, y2);
}
@Override
public void setRelativePositionOfOrigin(Vector2f newOrigin) {
//TODO fill this in
}
}