Package framework.spacial.rotational

Source Code of framework.spacial.rotational.OrbitalVelocityComponent

package framework.spacial.rotational;

import java.io.IOException;

import org.newdawn.slick.geom.Vector2f;

import framework.component.Component;
import framework.component.ComponentSystem;
import framework.component.filter.ComponentPathFilter;
import framework.component.path.ComponentPath;
import framework.event.Event;
import framework.event.EventListener;
import framework.event.EventSystem;
import framework.event.filter.EventSenderComponentFilter;
import framework.io.CustomInputStream;
import framework.spacial.PositionComponent;
import framework.spacial.VectorUtil;
import framework.spacial.PositionComponent.PositionChangedEvent;
import framework.timing.SystemClockComponent.TimePassedEvent;

public class OrbitalVelocityComponent extends Component implements EventListener{

  private float orbitalVelocity;
  private Vector2f centreOfOrbit;
  private boolean isOrbitingPositionComponent = false;
  private ComponentPath centreOfOrbitPath = null;

  public OrbitalVelocityComponent(CustomInputStream in, int baseID, byte versionNum) throws IOException {
    super(in, baseID, versionNum);
  }

  public OrbitalVelocityComponent(float orbitalVelocity, Vector2f centreOfOrbit) {
    super();
    this.centreOfOrbit = centreOfOrbit;
    this.orbitalVelocity = orbitalVelocity;
    this.isOrbitingPositionComponent = false;
    EventSystem.getInstance().registerEventListener(this, "TimePassed", null);
  }

  public OrbitalVelocityComponent(float orbitalVelocity, ComponentPath path){
    super();
    this.orbitalVelocity = orbitalVelocity;
    setCentreOfOrbit(path);
    EventSystem.getInstance().registerEventListener(this, "TimePassed", null);
  }

  public void setCentreOfOrbit(ComponentPath path){
    this.centreOfOrbitPath = path;
    this.isOrbitingPositionComponent = true;
    Component c = ComponentSystem.getInstance().getComponent(path);
    if(c != null && c.isType(PositionComponent.class)){
      centreOfOrbit = ((PositionComponent) c).getVector();
      EventSystem.getInstance().registerEventListener(this, "PositionChanged", new EventSenderComponentFilter(new ComponentPathFilter(path)));
    }
  }

  public void setCentreOfOrbit(Vector2f centre){
    this.centreOfOrbit = centre;
    this.isOrbitingPositionComponent = false;
  }

  @Override
  public boolean allowSameTypedSiblings() {
    return false;
  }

  @Override
  protected byte getDataFormatVersion() {
    return 0;
  }

  @Override
  public void onEvent(Event e) {
    if(e != null){
      if(e.getType().equals("PositionChanged")){
        PositionChangedEvent p = (PositionChangedEvent) e;
        this.centreOfOrbit = p.getTo();
      }else if(e.getType().equals("TimePassed")){
        PositionComponent posComp = (PositionComponent) getSiblingByType(PositionComponent.class.getName());
        if(posComp != null){
          Vector2f pos = posComp.getVector();
          if(pos != null && centreOfOrbit != null && !pos.equals(centreOfOrbit)){
            TimePassedEvent t = (TimePassedEvent) e;
            posComp.setVector(VectorUtil.rotateAround(pos.copy(), centreOfOrbit, t.getDelta()*orbitalVelocity));
          }
        }
      }
    }
  } 
}
 
TOP

Related Classes of framework.spacial.rotational.OrbitalVelocityComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.