Android Open Source - DolphinOES Position






From Project

Back to project page DolphinOES.

License

The source code is released under:

Apache License

If you think the Android project DolphinOES listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2015 See AUTHORS file./*w  w w  .j  a v a 2  s  .  co  m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.sidereal.dolphinoes.architecture.pos;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.sidereal.dolphinoes.architecture.GameObject;

/** Encapsulates local and hierarchial position of a  {@link GameObject}
 * object. Accessible from {@link GameObject#pos}
 * 
 * @author Claudiu Bele
 *
 */
public class Position
{
  // region fields
  
  private float x;
  private float y;
  private float z;
  private float localX;
  private float localY;
  private float localZ;
  private GameObject object;

  // endregion
  
  // region constructor
  
  public Position(GameObject object)
  {

    this.object = object;
  }
  
  // endregion
  
  // region methods

  // region local position

  public final float getLocalX()
  {

    return localX;
  }

  public final float getLocalY()
  {

    return localY;
  }

  public final float getLocalZ()
  {

    return localZ;
  }

  public final Vector2 getLocalXY()
  {

    return new Vector2(localX, localY);
  }

  public final Vector3 getLocal()
  {

    return new Vector3(localX, localY, localZ);
  }

  // region 1 param

  public final void setLocalX(float x)
  {

    updateChildren(setX(x, true), false, false);
  }

  public final void setLocalY(float y)
  {

    updateChildren(false, setY(y, true), false);
  }

  public final void setLocalZ(float z)
  {

    updateChildren(false, false, setZ(z, true));

  }

  // endregion

  // region 2 params

  public final void setLocal(Vector2 localXY)
  {

    setLocal(localXY.x, localXY.y);
  }

  public final void setLocal(float x, float y)
  {

    updateChildren(setX(x, true), setY(y, true), false);
  }

  // endregion

  // region 3 params

  public final void setLocal(Vector3 pos)
  {

    setLocal(pos.x, pos.y, pos.z);
  }

  /** Sets the local position of the Game Object without changing the object's
   * reference.
   * <p>
   * Sets the {@link #localPosition} object's x and y to match the parameters
   * given, after which the hierarchical position is updated
   * <p>
   * If the object does not have a {@link #parent}, the {@link #position}'s x
   * and y are set to the parameters given. Otherwise,
   * {@link #updatePositionInHierarchy(boolean, boolean, boolean)} is called
   * on the object whose local position we are changing.
   * <p>
   * After changing the object's variables, the position of all children will
   * be updated using
   * {@link #updatePositionInHierarchy(boolean, boolean, boolean)}.
   * 
   * @param x
   *            The x axis value
   * @param y
   *            The y axis value */
  public final void setLocal(float x, float y, float z)
  {

    updateChildren(setX(x, true), setY(y, true), setZ(z, true));
  }

  // endregion

  // endregion

  // region position

  // region getters

  public final float getX()
  {

    return x;
  }

  public final float getY()
  {

    return y;
  }

  public final float getZ()
  {

    return z;
  }

  public final Vector2 getXY()
  {

    return new Vector2(x, y);
  }

  public final Vector3 get()
  {

    return new Vector3(x, y, z);
  }

  // endregion

  // region setters

  // region 1 param

  public final void setX(float x)
  {

    updateChildren(setX(x, false), false, false);
  }

  public final void setY(float y)
  {

    updateChildren(false, setY(y, false), false);
  }

  public final void setZ(float z)
  {

    updateChildren(false, false, setZ(z, false));
  }

  // endregion

  // region 2 params

  public final void set(Vector2 XY)
  {

    set(XY.x, XY.y);
  }

  public final void set(float x, float y)
  {

    updateChildren(setX(x, false), setY(y, false), false);
  }

  // endregion

  // region 3 params

  public final void set(Vector3 pos)
  {

    set(pos.x, pos.y, pos.z);
  }

  public final void set(float x, float y, float z)
  {

    updateChildren(setX(x, false), setY(y, false), setZ(z, false));
  }
  
  public final void set(Position position)
  {
    set(position.x, position.y, position.z);
  }

  // endregion

  // endregion

  // endregion

  // region misc

  // region relative

  /** Sets the relative position of the Game Object, in regards to its' current
   * position.
   * <p>
   * The
   * 
   * @param x
   *            the value to be appended to the x-axis position of the Game
   *            Object
   * @param y
   *            the value to be appended to the y-axis position of the Game
   *            Object */
  public final void setRelative(float x, float y, float z)
  {

    setLocal(localX + x, localY + y, localZ + z);
  }

  public final void setRelative(float x, float y)
  {

    setRelative(x, y, 0);
  }

  // endregion

  // region internal methods

  // region setters

  private final boolean setX(float value, boolean isLocal)
  {

    if ((isLocal && value == localX) || (!isLocal && value == x))
      return false;

    if (isLocal)
      localX = value;
    else
      x = value;

    if (object.parent == null)
    {
      if (isLocal)
        x = value;
      else
        localX = value;
    } else
    {
      if (isLocal)
        x = value + object.parent.pos.getX();
      else
        localX = value - object.parent.pos.getX();
    }
    return true;
  }

  private final boolean setY(float value, boolean isLocal)
  {

    if ((isLocal && value == localY) || (!isLocal && value == y))
      return false;

    if (isLocal)
      localY = value;
    else
      y = value;

    if (object.parent == null)
    {
      if (isLocal)
        y = value;
      else
        localY = value;
    } else
    {
      if (isLocal)
        y = value + object.parent.pos.getY();
      else
        localY = value - object.parent.pos.getY();
    }

    return true;
  }

  private final boolean setZ(float value, boolean isLocal)
  {

    if ((isLocal && value == localZ) || (!isLocal && value == z))
      return false;

    if (isLocal)
      localZ = value;
    else
      z = value;

    if (object.parent == null)
    {
      if (isLocal)
        z = value;
      else
        localZ = value;
    } else
    {
      if (isLocal)
        z = value + object.parent.pos.getZ();
      else
        localZ = value - object.parent.pos.getZ();
    }
    return true;
  }

  // endregion

  // region hierarchy update
  
  public final void ensureHierarchialMatch()
  {
    object.pos.updateHierarchialX();
    object.pos.updateHierarchialY();
    object.pos.updateHierarchialZ();
  }

  private final void updateChildren(boolean x, boolean y, boolean z)
  {

    for (int i = 0; i < object.children.size(); i++)
    {
      if (x)
        object.children.get(i).pos.updateHierarchialX();
      if (y)
        object.children.get(i).pos.updateHierarchialY();
      if (z)
        object.children.get(i).pos.updateHierarchialZ();

      object.children.get(i).pos.updateChildren(x, y, z);
    }
  }

  private final void updateHierarchialX()
  {

    setX(object.parent.pos.x + localX, false);
  }

  private final void updateHierarchialY()
  {

    setY(object.parent.pos.y + localY, false);
  }

  private final void updateHierarchialZ()
  {

    setZ(object.parent.pos.z + localZ, false);
  }

  // endregion

  // endregion

  // endregion

  // endregion
}




Java Source Code List

com.sidereal.dolphinoes.architecture.AbstractEvent.java
com.sidereal.dolphinoes.architecture.DebugHandler.java
com.sidereal.dolphinoes.architecture.DolphinOES.java
com.sidereal.dolphinoes.architecture.GameBatch.java
com.sidereal.dolphinoes.architecture.GameBehavior.java
com.sidereal.dolphinoes.architecture.GameObject.java
com.sidereal.dolphinoes.architecture.GameScene.java
com.sidereal.dolphinoes.architecture.Module.java
com.sidereal.dolphinoes.architecture.core.Assets.java
com.sidereal.dolphinoes.architecture.core.Configurable.java
com.sidereal.dolphinoes.architecture.core.Debug.java
com.sidereal.dolphinoes.architecture.core.DolphinOESConfiguration.java
com.sidereal.dolphinoes.architecture.core.GameData.java
com.sidereal.dolphinoes.architecture.core.MouseMovedEvent.java
com.sidereal.dolphinoes.architecture.core.Time.java
com.sidereal.dolphinoes.architecture.core.assetload.AssetLoadHandler.java
com.sidereal.dolphinoes.architecture.core.assetload.AssetLoader.java
com.sidereal.dolphinoes.architecture.core.assetload.ClassFileHandleResolver.java
com.sidereal.dolphinoes.architecture.core.assetload.LoadingPercentage.java
com.sidereal.dolphinoes.architecture.core.input.ActionData.java
com.sidereal.dolphinoes.architecture.core.input.ActionEventWrapper.java
com.sidereal.dolphinoes.architecture.core.input.ActionEvent.java
com.sidereal.dolphinoes.architecture.core.input.Input.java
com.sidereal.dolphinoes.architecture.core.input.KeyTypedEvent.java
com.sidereal.dolphinoes.architecture.core.input.ScrollEvent.java
com.sidereal.dolphinoes.architecture.core.input.TouchData.java
com.sidereal.dolphinoes.architecture.core.input.TouchEventWrapper.java
com.sidereal.dolphinoes.architecture.core.input.TouchEvent.java
com.sidereal.dolphinoes.architecture.pos.Position.java
com.sidereal.dolphinoes.architecture.pos.Positions.java
com.sidereal.dolphinoes.backend.ConcreteGametester.java
com.sidereal.dolphinoes.backend.GameTester.java
com.sidereal.dolphinoes.backend.SceneGameTester.java
com.sidereal.dolphinoes.behaviors.audio.AudioListener.java
com.sidereal.dolphinoes.behaviors.audio.AudioPlayer.java
com.sidereal.dolphinoes.behaviors.events.EventTimer.java
com.sidereal.dolphinoes.behaviors.events.RecurringEvent.java
com.sidereal.dolphinoes.behaviors.input.Clickable.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleEmitter.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleHandler.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleSpriteLayout.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleSpriteObject.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingHandler.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingMap.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingNode.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingRoute.java
com.sidereal.dolphinoes.behaviors.renderer.DrawerBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.Drawer.java
com.sidereal.dolphinoes.behaviors.renderer.Renderer.java
com.sidereal.dolphinoes.behaviors.renderer.ninepatch.NinepatchBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.ninepatch.NinepatchDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.scml.SCMLBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.scml.SCMLDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.sprite.SpriteBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.sprite.SpriteDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.spritesequence.SpriteSequenceBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.spritesequence.SpriteSequenceDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.spritesequence.SpriteSequencePreference.java
com.sidereal.dolphinoes.behaviors.renderer.texture.TextureBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.texture.TextureDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.tilemap.TileMapBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.tilemap.TileMapDrawer.java
com.sidereal.dolphinoes.behaviors.triggers.Activatable.java
com.sidereal.dolphinoes.behaviors.triggers.Collider.java
com.sidereal.dolphinoes.behaviors.triggers.Hoverable.java
com.sidereal.dolphinoes.ui.MessageBubble.java
com.sidereal.dolphinoes.ui.TextBuilder.java
com.sidereal.dolphinoes.util.BooleanWrapper.java
com.sidereal.dolphinoes.util.DolphinOESException.java
com.sidereal.dolphinoes.util.FloatWrapper.java
com.sidereal.dolphinoes.util.IntWrapper.java
com.sidereal.dolphinoes.util.Utility.java