Skip to content

Obstacle Minigame ‐ Sprint 2

Anshul Dadhwal edited this page Oct 6, 2023 · 4 revisions

Sprint 2

Player Guide

The player can do basic movements with the ship using the arrow keys as follows:

D and -- Move Right
A and -- Move Left
W and -- Move Up
A and -- Move Down
B -- Brake (Adds linear damping and stops the ship)
V -- Deactivate Brake

Assets/Design Decisions

Design of Wormhole

Wormhole

Design of Map tile

Map

Code Documentation

The playAnimation method adjusts the spaceship's animation based on its movement direction:

  1. Determine Orientation: It calculates the spaceship's current orientation based on the given direction vector.
  2. Choose Animation:
    • If moving up, play "Ship_UpStill".
    • If moving down, play "Ship_DownStill".
    • If moving right, play "Ship_RightStill".
    • If moving left (with two possible left values), play "Ship_LeftStill".
    • For diagonal upward-right movement, play "Ship_RightUp".
    • For diagonal upward-left movement, play "Ship_LeftUp".
    • For diagonal downward-left movement, play "Ship_LeftDown".
    • For diagonal downward-right movement, play "Ship_RightDown".

In essence, it determines the spaceship's orientation from its velocity and plays the corresponding animation.

void playAnimation(Vector2 direction) {
        double currentOrientation = Vector2Utils.angleTo(direction);

        if (currentOrientation == up) {
            //
            animator.startAnimation("Ship_UpStill");
        } else if (currentOrientation == down) {
            //
            animator.startAnimation("Ship_DownStill");
        } else if (currentOrientation == right) {
            //
            animator.startAnimation("Ship_RightStill");
        } else if (currentOrientation == left || currentOrientation == left2) {
            //
            animator.startAnimation("Ship_LeftStill");
        } else if (currentOrientation < 90 && currentOrientation > 0) {
            //
            animator.startAnimation("Ship_RightUp");
        } else if (currentOrientation > 90 && currentOrientation < 180) {
            //
            animator.startAnimation("Ship_LeftUp");
        } else if (currentOrientation > -180 && currentOrientation < -90) {
            //
            animator.startAnimation("Ship_LeftDown");
        } else if (currentOrientation > -90 && currentOrientation < 0) {
            //
            animator.startAnimation("Ship_RightDown");
        }


}

The spawnAsteroids method spawns a predetermined number of asteroids at random positions on the map:

  1. Boundaries Set: Minimum position is (1,1) and maximum position is determined by the map's bounds minus (2,2).
  2. Asteroid Creation: For each of the NUM_ASTEROIDS, it:
    • Generates a random position within the defined boundaries.
    • Creates an asteroid of size ASTEROID_SIZE.
    • Spawns the asteroid at the generated position.

In essence, it randomly places NUM_ASTEROIDS of a specific size across the map within defined boundaries.

private void spawnAsteroids() {
        GridPoint2 minPos = new GridPoint2(1, 1);
        GridPoint2 maxPos = terrain.getMapBounds(0).sub(2, 2);
        for (int i = 0; i < NUM_ASTEROIDS; i++) {
            GridPoint2 randomPos = RandomUtils.random(minPos, maxPos);
            Entity asteroid = ObstacleFactory.createAsteroid(ASTEROID_SIZE,ASTEROID_SIZE);
            spawnEntityAt(asteroid, randomPos, false, false);
        }
}

The followShip method adjusts the game camera to follow the ship while ensuring it stays within set boundaries:

  1. Boundaries Calculation:
    • maxX is the rightmost camera boundary.
    • maxY is the uppermost camera boundary.
  2. Camera Positioning:
    • For the X-axis, it ensures the camera doesn't go beyond the left or right boundaries while centering on the ship.
    • Similarly, for the Y-axis, it ensures the camera doesn't exceed the top or bottom boundaries while focusing on the ship.
  3. Set Camera Position: Updates the camera's position based on the calculated cameraX and cameraY.

In essence, it ensures the camera follows the ship but stays within the game's boundaries.

private void followShip() {
        float maxX = 59 * 1f - renderer.getCamera().getCamera().viewportWidth * 0.5f;
        float maxY = 29 * 1f - renderer.getCamera().getCamera().viewportHeight * 0.5f;
        float cameraX = Math.min(maxX, Math.max(renderer.getCamera().getCamera().viewportWidth * 0.5f, ship.getPosition().x));
        float cameraY = Math.min(maxY, Math.max(renderer.getCamera().getCamera().viewportHeight * 0.5f, ship.getPosition().y));
        renderer.getCamera().getEntity().setPosition(cameraX, cameraY);
}

The calculateDistance method finds the distance between two entities:

  1. Retrieve Positions: Gets the center positions of entity1 and entity2.
  2. Distance Calculation: Uses the dst method of Vector2 to calculate the distance between these two positions.
  3. Return: Outputs the calculated distance.

In essence, it returns the distance between the centers of two given entities.

public static float calculateDistance(Entity entity1, Entity entity2) {
        // Get the center positions of both entities
        Vector2 position1 = entity1.getCenterPosition();
        Vector2 position2 = entity2.getCenterPosition();

        // Calculate the distance between the two positions
        float distance = position1.dst(position2);

        return distance;
}

The updateDistanceUI method:

  1. Format Distance: Converts the distance value to a string with two decimal places, prefixed by "Distance:".
  2. Update UI: Sets this formatted string as the text for the distanceLabel.

In essence, it updates the distanceLabel UI element to display the provided distance in a formatted manner.

public void updateDistanceUI(float distance) {
        CharSequence text = String.format("Distance: %.2f", distance);
        distanceLabel.setText(text);
}

The Exitonc method:

  1. Check Distance: If the given value d is less than 1.0,
  2. Screen Transition: It switches the game's screen to the main menu.

In essence, if the provided distance d is less than 1.0, the method exits the minigame and returns to the main menu.

public void Exitonc(float d)
    {
        if(d < 1.0) {
            game.setScreen(GdxGame.ScreenType.MAIN_MENU);
        }
}
Clone this wiki locally