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.timing.SystemClockComponent.TimePassedEvent;
public class AccelerationComponent extends VectorValueComponent implements EventListener{
public AccelerationComponent(Vector2f vector) {
super(vector, false);
EventSystem.getInstance().registerEventListener(this, "TimePassed", null);
}
public AccelerationComponent(CustomInputStream in, int baseID, byte versionNum) throws IOException {
super(in, baseID, versionNum);
EventSystem.getInstance().registerEventListener(this, "TimePassed", null);
}
public AccelerationComponent(float x, float y) {
this(new Vector2f(x, y));
}
@Override
public boolean allowSameTypedSiblings() {
return false;
}
@Override
public void setVector(Vector2f newVector) {
Vector2f oldVector = getVector();
super.setVector(newVector);
if(!oldVector.equals(newVector)){
EventSystem.getInstance().pushEvent(new AccelerationChangedEvent(this, oldVector, newVector));
}
}
public static final class AccelerationChangedEvent extends ValueChangedEvent<Vector2f>{
public AccelerationChangedEvent(EventSender sender, Vector2f from,Vector2f to) {
super(sender, from, to);
}
@Override
protected final String getEventTypeID() {
return "AccelerationChanged";
}
}
@Override
public void onEvent(Event e) {
if(e.getType().equals("TimePassed")){
VelocityComponent vel = (VelocityComponent) getSiblingByType(VelocityComponent.class.getName());
if(vel == null && getParent() != null){
vel = new VelocityComponent(0,0);
getParent().addComponent(vel);
}
TimePassedEvent time = (TimePassedEvent) e;
vel.addVector(this.getVector().scale(time.getDelta()));
}
}
}