Projectile bug fixes - UQdeco2800/2022-studio-1 GitHub Wiki

Link back to main page

During Sprint 4 implementation of projectiles was polished. Some major changes include

  • Making projectile size bigger
  • Projectiles disappearing when hitting target
  • Speed of projectiles reduced

Projectiles Disappearing

To get the projectiles to disappear came in 2 steps. The first was when collision occurred. This was done through the UGS grid system. The second was when projectiles did not collide but needed to despawn due to time. A timer component was created below to keep track of how long each projectile had existed.

package com.deco2800.game.components;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.deco2800.game.entities.Entity;
import com.deco2800.game.physics.BodyUserData;
import com.deco2800.game.physics.PhysicsLayer;
import com.deco2800.game.physics.components.HitboxComponent;
import com.deco2800.game.physics.components.PhysicsComponent;
import com.deco2800.game.services.ServiceLocator;

public class TimerComponent extends Component {
    private int timer;


    @Override
    public void update(){
        timer++;
        if (timer > 420) {
            ServiceLocator.getEntityService().addToDestroyEntities(this.entity);
        }
    }
}

In the entity service, when the time has run over and the projectile has been entered into the destroyed entities list, the below function removes the entity from the game. This stops errors from occuring as previously to many projectiles were spawning and multiple of the same projectile were being removed, causing crashes.

public void setDestroyEntire(ArrayList<Entity> newEntities) {
    this.toDestroyEntities = newEntities;
  }

  /**
   *
   * @param e entity to destroy
   */
  public void addToDestroyEntities(Entity e) {
    this.toDestroyEntities.add(e);
    if (!this.toDestroyEntities.contains(e)) {
      this.toDestroyEntities.add(e);
      unregister(e);
    }


  }

Change of Size

The following code was used to edit the size of the projectile sprite

projectile.setPosition(sourcePosition);
PhysicsUtils.setScaledCollider(projectile, 2f, 2f);
projectile.scaleHeight(20f);

Speed of Projectiles and targeting

The following code was used to update the way that the targets for the projectile is calculated and the speed of the projectiles.

destination.y -= ServiceLocator.getEntityService().getNamedEntity("terrain")
                .getComponent(TerrainComponent.class).getTileSize() / 2;

        projectile.getComponent(ProjectileMovementComponent.class).setTarget(destination);
        projectile.getComponent(ProjectileMovementComponent.class).setMoving(true);
        projectile.getComponent(ProjectileMovementComponent.class).setNewSpeed(new Vector2(4, 4));

Furthermore in general bug fixes surrounding the ranged enemy were fixed according to SonarCloud.

⚠️ **GitHub.com Fallback** ⚠️