Spaceship Boss Code - UQdeco2800/2021-ext-studio-2 GitHub Wiki

Table of Contents

Creation
  Create Spaceship
  Create Missile
  Create Portal
Difficulty of attack

Creation

The code for creating spaceship and missile is in NPCFactory.java.

Spaceship

Unlike other enemies, the spaceship adds a SpaceshipAttackController. This component will be mentioned later.

Missile

The missile entity contains two animations, and one of them will be played when it is created. Since the original missile image has a large transparent part, this part should not trigger the collision effect. The code to adjust the collision edge is as follows:

        Vector2 boundingBox = missile.getScale().cpy().scl(0.5f, 0.3f);
        missile.getComponent(HitboxComponent.class).setAsBoxAligned(
                boundingBox, PhysicsComponent.AlignX.LEFT, PhysicsComponent.AlignY.CENTER);

For aesthetics, missiles are better generated above spaceship. This is achieved by adjusting zIndex. The engine first draws zIndex smaller entity. Corresponding code.

        missile.setZIndex(1); // Generate missile above spaceship

Portal

createPortal(Entity target, ObstacleEventHandler.ObstacleType type)in ObstacleFactory.java realizes the function of creating a portal. ObstacleEventHandler.ObstacleType type is used to specify the type of the portal to determine the event that the entity should listen to. The type on should be PortalEntrance or PortalExport. They respectively teleport the player to different locations. The animation starts when the portal is spawned.

Spaceship/missile attack

This part of the function is mainly realized by SpaceshipAttackController.java. This component completes the function of character movement and trigger generation of the missile. The related camera movement is implemented in MainGameScreen.java. This defines the type of spacecraft attack, the spacecraft attack state and the remaining time of the attack.

    /**
     * Spaceship attack state
     */
    public enum SpaceshipAttack {
        Off,
        Start, // Used once in render
        On,
        Finish;
    }

    /**
     * Spaceship attack type
     */
    public enum AttackType {
        Low,
        Middle,
        High,
        Player;

    /* Spaceship attack time */
    public float spaceshipTime;

When the character collides with the spaceship, change the attack state of the spaceship to Start. Every time reder(), judge the spacecraft's attack status and remaining time. That is, when the attack starts, other tasks are automatically performed by this component.

Difficulty of attack

This part of the code is implemented in easyAttack() and hardAttack() respectively. These two functions determine how long to generate the frequency and number of missiles. They are called by spaceshipAttackScene(). Each time the game render(), determine which type (easy, hard) of attack should be executed according to the current remaining time.

The following function determines where the missile is generated.

private Vector2 getRandomPosition(Vector2 spaceshipPosition, Vector2 playerPosition, AttackType type, int level)

According to different AttackType, the generated position is different. There are a total of four location options. High, Middle, and Low are to generate a missile at a fixed position, and the Player type generates a missile according to the current height of the player.