Android Open Source - CircleWorldGDX Ship






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 av  a  2 s  .c o  m*/
import com.badlogic.gdx.math.MathUtils;
import com.fdangelo.circleworld.GameLogic;
import com.fdangelo.circleworld.GameLogicState;
import com.fdangelo.circleworld.universeengine.tilemap.Planet;
import com.fdangelo.circleworld.utils.Mathf;

public class Ship extends UniverseObject {
  public ShipInput input = new ShipInput();

  private final float movementSpeedMax = 100.0f;
  private final float movementAcceleration = 100.0f;
  private final float movementFriction = 200.0f;

  private final float rotationSpeedMax = 135.0f * MathUtils.degreesToRadians;
  private final float rotationAcceleration = 360.0f * MathUtils.degreesToRadians;
  private final float rotationFriction = 360.0f * MathUtils.degreesToRadians;

  public Ship() {
    useGravity = false;
  }

  @Override
  protected void onUpdate(final float deltaTime) {
    if (GameLogic.getInstace().getState() == GameLogicState.PlayingShip) {
      if (input.rotateDirection != 0) {
        rotationVelocity += input.rotateDirection * rotationAcceleration * deltaTime;
      } else {
        rotationVelocity -= Mathf.Sign(rotationVelocity) * MathUtils.clamp(rotationFriction * deltaTime, 0, Math.abs(rotationVelocity));
      }

      rotationVelocity = MathUtils.clamp(rotationVelocity, -rotationSpeedMax, rotationSpeedMax);

      float currentSpeed = (float) Math.sqrt(velocityX * velocityX + velocityY * velocityY);

      if (input.moveDirection > 0) {
        currentSpeed += input.moveDirection * movementAcceleration * deltaTime;
      } else {
        currentSpeed -= Mathf.Sign(currentSpeed) * MathUtils.clamp(movementFriction * deltaTime, 0, Math.abs(currentSpeed));
      }

      currentSpeed = MathUtils.clamp(currentSpeed, 0, movementSpeedMax);

      velocityX = Mathf.sin(getRotation()) * currentSpeed;
      velocityY = Mathf.cos(getRotation()) * currentSpeed;
    } else if (GameLogic.getInstace().getState() == GameLogicState.PlayingAvatar) {
      // Orbit planet!
      if (parent != null) {
        final float orbitDistance = parent.getDistanceFromTileY(parent.getHeight() + 7);

        if (distanceInTilemapCircle > orbitDistance + 1.0f) {
          velocityY -= movementAcceleration * deltaTime;
        } else if (distanceInTilemapCircle < orbitDistance - 1.0f) {
          velocityY += movementAcceleration * deltaTime;
        } else {
          velocityY -= Mathf.Sign(velocityY) * MathUtils.clamp(movementFriction * deltaTime, 0, Math.abs(velocityY));
        }

        velocityY = MathUtils.clamp(velocityY, 0, movementSpeedMax * 0.1f);

        velocityX = movementSpeedMax * 0.05f;

        float orbitRotationDiff = (parent.getAngleFromPosition(positionX, positionY) + MathUtils.PI * 0.5f) - rotation;

        if (orbitRotationDiff > MathUtils.PI) {
          orbitRotationDiff -= MathUtils.PI2;
        } else if (orbitRotationDiff < -MathUtils.PI) {
          orbitRotationDiff += MathUtils.PI2;
        }

        if (orbitRotationDiff < 0) {
          rotationVelocity -= rotationAcceleration * deltaTime;
        } else if (orbitRotationDiff > 0) {
          rotationVelocity += rotationAcceleration * deltaTime;
        } else {
          rotationVelocity -= Mathf.Sign(rotationVelocity) * MathUtils.clamp(rotationFriction * deltaTime, 0, Math.abs(rotationVelocity));
        }

        rotationVelocity = MathUtils.clamp(rotationVelocity, -rotationSpeedMax * 0.1f, rotationSpeedMax * 0.1f);
      }
    }

    input.reset();
  }

  public final void beamDownAvatar(final Avatar avatar, final Planet planet) {
    rotationVelocity = 0;
    velocityX = 0;
    velocityY = 0;

    setParent(planet, FollowParentParameters.None, positionX, positionY, rotation);

    avatar.travelToPlanet(planet);
  }

  public final void beamUpAvatar(final Avatar avatar) {
    setParent(null, FollowParentParameters.None, positionX, positionY, rotation);

    avatar.boardShip(this);
  }
}




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