Package framework.component.util

Source Code of framework.component.util.VectorValueComponent

package framework.component.util;

import java.io.IOException;

import org.newdawn.slick.geom.Vector2f;

import framework.component.Component;
import framework.component.ParentComponent;
import framework.component.list.SingleTypeComponentList;
import framework.io.CustomInputStream;
import framework.io.CustomOutputStream;

public abstract class VectorValueComponent extends Component {

  private Vector2f vector;

  public VectorValueComponent(float x, float y) {
    this(new Vector2f(x, y), false);
  }

  public VectorValueComponent(float x, float y, boolean relativeToParent) {
    this(new Vector2f(x, y), relativeToParent);
  }

  public VectorValueComponent(Vector2f vector, boolean relativeToParent) {
    super();
    if (!relativeToParent) {
      setVector(vector);
    } else {
      setRelativeToParent(vector);
    }
  }

  public VectorValueComponent(CustomInputStream in, int baseID,
      byte versionNum) throws IOException {
    super(in, baseID, versionNum);
    if (versionNum == 0) {
      vector = in.readVector();
    }
  }

  public Vector2f getVector() {
    return vector != null ? vector.copy() : null;
  }

  public void setVector(Vector2f newVector) {
    if (newVector != null) {
      Vector2f oldVector = getVector();
      this.vector = newVector;
      if (oldVector != null) {
        alterDescendents(newVector.copy().sub(oldVector),
            getSiblingsByType(ParentComponent.class.getName()));
      }
    }
  }

  private void alterDescendents(Vector2f deltaVector,
      SingleTypeComponentList descendents) {
    if (shouldUpdateDescendents() && descendents != null) {
      for (Component d : descendents) {
        ParentComponent p = (ParentComponent) d;
        VectorValueComponent v = (VectorValueComponent) p
            .getChildByType(getType());
        if (v != null && v.shouldUpdateWithAncestor()) {
          v.addVector(deltaVector);
        } else {
          alterDescendents(
              deltaVector,
              p.getChildrenByType(ParentComponent.class.getName()));
        }
      }
    }
  }

  public void setRelativeToParent(Vector2f newVector) {
    if (newVector != null) {
      ParentComponent p = this.getParent();
      if (p != null) {
        p = p.getParent();
        while (p != null && p.getChildByType(getType()) == null) {
          p = p.getParent();
        }
      }
      VectorValueComponent vecComp = null;
      if (p != null) {
        vecComp = (VectorValueComponent) p.getChildByType(getType());
      }
      Vector2f parentVector = null;
      if (vecComp != null) {
        parentVector = vecComp.getVector();
      }
      this.vector = parentVector == null ? newVector : parentVector
          .add(newVector);
    }
  }

  public void addVector(Vector2f newVector) {
    setVector(getVector().add(newVector));
  }

  public void subtractVector(Vector2f newVector) {
    setVector(getVector().sub(newVector));
  }

  @Override
  public void writeToStream(CustomOutputStream out) throws IOException {
    super.writeToStream(out);
    out.write(vector);
  }

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

  public boolean shouldUpdateDescendents() {
    return true;
  }

  public boolean shouldUpdateWithAncestor() {
    return true;
  }
}
TOP

Related Classes of framework.component.util.VectorValueComponent

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.