package framework.spacial.translational;
import java.io.IOException;
import org.newdawn.slick.geom.Vector2f;
import framework.component.util.VectorValueComponent;
import framework.event.Event;
import framework.event.EventListener;
import framework.event.EventSender;
import framework.event.EventSystem;
import framework.event.util.ValueChangedEvent;
import framework.io.CustomInputStream;
import framework.spacial.PositionComponent;
import framework.timing.SystemClockComponent.TimePassedEvent;
public class VelocityComponent extends VectorValueComponent implements EventListener{
public VelocityComponent(CustomInputStream in, int baseID, byte versionNum) throws IOException {
super(in, baseID, versionNum);
EventSystem.getInstance().registerEventListener(this, "TimePassed", null);
}
public VelocityComponent(Vector2f vector, boolean relativeToParent) {
super(vector, relativeToParent);
EventSystem.getInstance().registerEventListener(this, "TimePassed", null);
}
public VelocityComponent(float x, float y) {
this(new Vector2f(x, y), false);
}
public VelocityComponent(float x, float y, boolean relativeToParent) {
this(new Vector2f(x, y), relativeToParent);
}
public VelocityComponent(Vector2f vector) {
this(vector, false);
}
@Override
public boolean allowSameTypedSiblings() {
return false;
}
@Override
public void setVector(Vector2f newVector) {
Vector2f oldVector = getVector();
super.setVector(newVector);
if(oldVector != null && !oldVector.equals(newVector)){
EventSystem.getInstance().pushEvent(new VelocityChangedEvent(this, oldVector, newVector));
}
}
@Override
public void onEvent(Event e) {
if(e != null && e.getType() == "TimePassed"){
PositionComponent pos = (PositionComponent) getSiblingByType(PositionComponent.class.getName());
if(pos != null){
TimePassedEvent time = (TimePassedEvent) e;
pos.addVector(this.getVector().scale(time.getDelta()));
}
}
}
public static final class VelocityChangedEvent extends ValueChangedEvent<Vector2f>{
public VelocityChangedEvent(EventSender sender, Vector2f from,Vector2f to) {
super(sender, from, to);
}
@Override
protected final String getEventTypeID() {
return "VelocityChanged";
}
}
}