Android Open Source - CircleWorldGDX Universe Object






From Project

Back to project page CircleWorldGDX.

License

The source code is released under:

MIT License

If you think the Android project CircleWorldGDX 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

package com.fdangelo.circleworld.universeengine.objects;
//  www  .j a v  a  2 s  .  c om
import com.badlogic.gdx.math.Vector2;
import com.fdangelo.circleworld.universeengine.tilemap.Planet;
import com.fdangelo.circleworld.universeengine.tilemap.TileDirection;
import com.fdangelo.circleworld.universeengine.tilemap.TileHitFlags;
import com.fdangelo.circleworld.universeengine.tilemap.TileHitInfo;
import com.fdangelo.circleworld.universeengine.tilemap.TilemapCircle;

public class UniverseObject {
  protected TilemapCircle parent;

  protected boolean useGravity = true;

  protected float positionX;
  protected float positionY;
  protected float scale = 1.0f;
  protected float rotation = 0.0f; // radians

  protected float sizeX = 1;
  protected float sizeY = 1;

  protected boolean visible = true;

  protected float velocityX;
  protected float velocityY;
  protected float rotationVelocity;

  // Uses TileHitFlags
  protected int hitFlags;

  protected float distanceInTilemapCircle;
  protected float angleInTilemapCirclePosition;

  protected IUniverseObjectListener listener;

  protected boolean parentFollowScale;
  protected boolean parentFollowRotation;
  protected boolean parentCheckCollisions;

  static private TileHitInfo tmpHitInfo = new TileHitInfo();

  public final float getPositionX() {
    return positionX;
  }

  public final float getPositionY() {
    return positionY;
  }

  public final float getScale() {
    return scale;
  }

  public final float getRotation() {
    return rotation;
  }

  public final float getSizeX() {
    return sizeX;
  }

  public final float getSizeY() {
    return sizeY;
  }

  public final float getVelocityX() {
    return velocityX;
  }

  public final float getVelocityY() {
    return velocityY;
  }

  public final void setVelocity(final float x, final float y) {
    velocityX = x;
    velocityY = y;
  }

  public final int getHitFlags() {
    return hitFlags;
  }

  public final IUniverseObjectListener getListener() {
    return listener;
  }

  public final void setListener(final IUniverseObjectListener value) {
    listener = value;
  }

  public final boolean getVisible() {
    return visible;
  }

  public final void setVisible(final boolean value) {
    visible = value;
    if (listener != null) {
      listener.onUniverseObjectUpdated(0.0f);
    }
  }

  public final TilemapCircle getParent() {
    return parent;
  }

  public void init(final float sizeX, final float sizeY, final TilemapCircle parent, final int followParentParameters, final float positionX,
      final float positionY, final float rotation) {
    this.sizeX = sizeX;
    this.sizeY = sizeY;

    setParent(parent, followParentParameters, positionX, positionY, rotation);
  }

  public final void setParent(final TilemapCircle parent, final int followParentParameters, final float positionX, final float positionY, final float rotation) {
    this.parent = parent;
    this.positionX = positionX;
    this.positionY = positionY;
    this.rotation = rotation;
    parentFollowScale = (followParentParameters & FollowParentParameters.FollowScale) != 0;
    parentFollowRotation = (followParentParameters & FollowParentParameters.FollowRotation) != 0;
    parentCheckCollisions = (followParentParameters & FollowParentParameters.CheckCollisions) != 0;

    if (parent != null) {
      if (parentFollowScale) {
        scale = parent.getScaleFromPosition(positionX, positionY);
      }

      if (parentFollowRotation) {
        this.rotation = parent.getAngleFromPosition(positionX, positionY);
      }

      distanceInTilemapCircle = parent.getDistanceFromPosition(positionX, positionY);
      angleInTilemapCirclePosition = parent.getAngleFromPosition(positionX, positionY);
    }

    if (listener != null) {
      listener.onParentChanged(parent);
    }
  }

  public final void update(final float deltaTime) {
    onUpdate(deltaTime);

    updatePosition(deltaTime);

    if (listener != null) {
      listener.onUniverseObjectUpdated(deltaTime);
    }
  }

  protected void onUpdate(final float deltaTime) {

  }

  protected final void updatePosition(final float deltaTime) {
    float normalX, normalY;
    float tangentX, tangentY;

    float deltaPositionX, deltaPositionY;
    float deltaRotation;

    if (parent != null) {
      final Vector2 positionInParent = parent.getPositionFromDistanceAndAngle(distanceInTilemapCircle, angleInTilemapCirclePosition);

      positionX = positionInParent.x;
      positionY = positionInParent.y;

      if (parentFollowRotation) {
        rotation = parent.getAngleFromPosition(positionX, positionY);
      }

      if (parentFollowScale) {
        scale = parent.getScaleFromPosition(positionX, positionY);
      }

      if (parent instanceof Planet && useGravity) {
        velocityY -= ((Planet) parent).getGravity() * deltaTime;
      }

      final Vector2 normal = parent.getNormalFromPosition(positionX, positionY); // doesn't
                                            // change
                                            // with
                                            // vertical
                                            // position
      normalX = normal.x;
      normalY = normal.y;

      final Vector2 tangent = parent.getTangentFromPosition(positionX, positionY); // doesn't
                                              // change
                                              // with
                                              // vertical
                                              // position
      tangentX = tangent.x;
      tangentY = tangent.y;

      deltaPositionX = velocityX * deltaTime * scale;
      deltaPositionY = velocityY * deltaTime * scale;

      if (parentFollowRotation) {
        deltaRotation = 0.0f;
      } else {
        deltaRotation = rotationVelocity * deltaTime;
      }
    } else {
      normalY = 1;
      normalX = 0;

      tangentY = 0;
      tangentX = 1;

      deltaPositionX = velocityX * deltaTime * scale;
      deltaPositionY = velocityY * deltaTime * scale;
      deltaRotation = rotationVelocity * deltaTime;
    }

    hitFlags = TileHitFlags.None;

    if (parent != null && parentCheckCollisions) {
      if (deltaPositionY > 0) {
        // Check against ceiling
        if (parent.raycastSquare(positionX + normalX * (sizeY * 0.5f * scale), positionY + normalY * (sizeY * 0.5f * scale), sizeX * scale,
            TileDirection.Up, deltaPositionY + (sizeY * 0.5f * scale), tmpHitInfo)) {
          deltaPositionY = -(tmpHitInfo.hitDistance - (sizeY * 0.5f * scale));
          velocityY = 0.0f;
          hitFlags |= TileHitFlags.Up;
        }
      } else if (deltaPositionY < 0) {
        // Check against floor
        if (parent.raycastSquare(positionX + normalX * (sizeY * 0.5f * scale), positionY + normalY * (sizeY * 0.5f * scale), sizeX * scale,
            TileDirection.Down, -deltaPositionY + (sizeY * 0.5f * scale), tmpHitInfo)) {
          deltaPositionY = -(tmpHitInfo.hitDistance - (sizeY * 0.5f * scale));
          velocityY = 0.0f;
          hitFlags |= TileHitFlags.Down;
        }
      }
    }

    if (deltaPositionY != 0) {
      positionY += normalY * deltaPositionY;
      positionX += normalX * deltaPositionY;
      if (parent != null && parentFollowScale) {
        scale = parent.getScaleFromPosition(positionX, positionY);
      }
    }

    if (parent != null && parentCheckCollisions) {
      if (deltaPositionX > 0) {
        // Check against right wall
        if (parent.raycastSquare(positionX + normalX * (sizeY * 0.5f * scale), positionY + normalY * (sizeY * 0.5f * scale), sizeY * scale,
            TileDirection.Right, deltaPositionX + (sizeX * 0.5f * scale), tmpHitInfo)) {
          deltaPositionX = (tmpHitInfo.hitDistance - (sizeX * 0.5f * scale));
          velocityX = 0.0f;
          hitFlags |= TileHitFlags.Right;
        }
      } else if (deltaPositionX < 0) {
        // Check against left wall
        if (parent.raycastSquare(positionX + normalX * (sizeY * 0.5f * scale), positionY + normalY * (sizeY * 0.5f * scale), sizeY * scale,
            TileDirection.Left, -deltaPositionX + (sizeX * 0.5f * scale), tmpHitInfo)) {
          deltaPositionX = -(tmpHitInfo.hitDistance - (sizeX * 0.5f * scale));
          velocityX = 0.0f;
          hitFlags |= TileHitFlags.Left;
        }
      }
    }

    if (deltaPositionX != 0) {
      positionX += tangentX * deltaPositionX;
      positionY += tangentY * deltaPositionX;
      if (parent != null) {
        final Vector2 normal = parent.getNormalFromPosition(positionX, positionY);
        normalX = normal.x;
        normalY = normal.y;
      }
    }

    if (parent != null) {
      if (parentFollowRotation) {
        rotation = parent.getAngleFromPosition(positionX, positionY);
      } else {
        rotation += deltaRotation;
      }

      distanceInTilemapCircle = parent.getDistanceFromPosition(positionX, positionY);
      angleInTilemapCirclePosition = parent.getAngleFromPosition(positionX, positionY);
    } else {
      rotation += deltaRotation;
    }
  }

  /*
   * public bool MoveTo(Vector2 position) { if (CanMoveTo(position)) {
   * this.position = position; return true; } return false; } public bool
   * CanMoveTo(Vector2 position) { float scale =
   * tilemapCircle.GetScaleFromPosition(position); int tileX, tileY; Vector2
   * right = transform.right; Vector2 up = transform.up; position += up *
   * 0.05f; for (int x = -1; x <= 1; x++) { for (int y = 0; y <= 2; y++) {
   * Vector2 pos = position + right * (size.x * 0.9f * x * 0.5f * scale) + up
   * * ((size.y * 0.9f / 2) * y * scale); if
   * (tilemapCircle.GetTileCoordinatesFromPosition(pos, out tileX, out tileY))
   * if (tilemapRicle.GetTile(tileX, tileY) != 0) return false; } } return
   * true; }
   */
}




Java Source Code List

.AssetsUpdater.java
com.fdangelo.circleworld.GameLogicState.java
com.fdangelo.circleworld.GameLogic.java
com.fdangelo.circleworld.MainActivity.java
com.fdangelo.circleworld.Main.java
com.fdangelo.circleworld.MyGdxGame.java
com.fdangelo.circleworld.RobovmLauncher.java
com.fdangelo.circleworld.client.GwtLauncher.java
com.fdangelo.circleworld.gui.AvatarEditControlScreen.java
com.fdangelo.circleworld.gui.AvatarMoveControlScreen.java
com.fdangelo.circleworld.gui.HudScreen.java
com.fdangelo.circleworld.gui.core.Gui.java
com.fdangelo.circleworld.gui.core.ScreenTable.java
com.fdangelo.circleworld.gui.core.Screen.java
com.fdangelo.circleworld.universeengine.IUniverseListener.java
com.fdangelo.circleworld.universeengine.ThingPosition.java
com.fdangelo.circleworld.universeengine.ThingType.java
com.fdangelo.circleworld.universeengine.Thing.java
com.fdangelo.circleworld.universeengine.UniverseFactory.java
com.fdangelo.circleworld.universeengine.UniverseGeneratorDefault.java
com.fdangelo.circleworld.universeengine.UniverseGenerator.java
com.fdangelo.circleworld.universeengine.Universe.java
com.fdangelo.circleworld.universeengine.objects.AvatarInput.java
com.fdangelo.circleworld.universeengine.objects.Avatar.java
com.fdangelo.circleworld.universeengine.objects.FollowParentParameters.java
com.fdangelo.circleworld.universeengine.objects.IUniverseObjectListener.java
com.fdangelo.circleworld.universeengine.objects.ShipInput.java
com.fdangelo.circleworld.universeengine.objects.Ship.java
com.fdangelo.circleworld.universeengine.objects.UniverseObject.java
com.fdangelo.circleworld.universeengine.tilemap.ITilemapCircleListener.java
com.fdangelo.circleworld.universeengine.tilemap.PlanetType.java
com.fdangelo.circleworld.universeengine.tilemap.PlanetTypes.java
com.fdangelo.circleworld.universeengine.tilemap.Planet.java
com.fdangelo.circleworld.universeengine.tilemap.TileDirection.java
com.fdangelo.circleworld.universeengine.tilemap.TileHitFlags.java
com.fdangelo.circleworld.universeengine.tilemap.TileHitInfo.java
com.fdangelo.circleworld.universeengine.tilemap.TileSubtype.java
com.fdangelo.circleworld.universeengine.tilemap.TileType.java
com.fdangelo.circleworld.universeengine.tilemap.TileTypes.java
com.fdangelo.circleworld.universeengine.tilemap.TilemapCircle.java
com.fdangelo.circleworld.universeengine.utils.DataPools.java
com.fdangelo.circleworld.universeengine.utils.PoolByte.java
com.fdangelo.circleworld.universeengine.utils.PoolColor.java
com.fdangelo.circleworld.universeengine.utils.PoolFloat.java
com.fdangelo.circleworld.universeengine.utils.PoolInt.java
com.fdangelo.circleworld.universeengine.utils.PoolVector2.java
com.fdangelo.circleworld.universeengine.utils.PoolVector3.java
com.fdangelo.circleworld.universeengine.utils.UEProfilerSample.java
com.fdangelo.circleworld.universeengine.utils.UEProfiler.java
com.fdangelo.circleworld.universeview.FollowCameraParameters.java
com.fdangelo.circleworld.universeview.UniverseViewCamera.java
com.fdangelo.circleworld.universeview.UniverseViewFactory.java
com.fdangelo.circleworld.universeview.UniverseView.java
com.fdangelo.circleworld.universeview.objects.AvatarInputEditTool.java
com.fdangelo.circleworld.universeview.objects.AvatarInputMode.java
com.fdangelo.circleworld.universeview.objects.AvatarViewInput.java
com.fdangelo.circleworld.universeview.objects.AvatarView.java
com.fdangelo.circleworld.universeview.objects.InputAreas.java
com.fdangelo.circleworld.universeview.objects.ShipInputMode.java
com.fdangelo.circleworld.universeview.objects.ShipViewInput.java
com.fdangelo.circleworld.universeview.objects.ShipView.java
com.fdangelo.circleworld.universeview.objects.UniverseObjectView.java
com.fdangelo.circleworld.universeview.tilemap.PlanetView.java
com.fdangelo.circleworld.universeview.tilemap.TilemapCircleViewBackgroundRenderer.java
com.fdangelo.circleworld.universeview.tilemap.TilemapCircleViewRenderer.java
com.fdangelo.circleworld.universeview.tilemap.TilemapCircleView.java
com.fdangelo.circleworld.utils.Mathf.java
com.fdangelo.circleworld.utils.Vector2I.java