Attack Cooldown Function - UQdeco2800/2022-studio-2 GitHub Wiki

Summary

This wiki page will explain the code implementation which ensures there is a reasonable time interval between a attack instance and subsequent attack instance.

PhysicalWeaponStatsComponent

Located in: source/core/src/main/com/deco2800/game/components/CombatItemsComponents/PhysicalWeaponStatsComponent.java

When this component is initialised and tied to a weapon entity in WeaponFactory, the cooldown duration is defined for each weapon with its duration values taken from the weapon config files under configs/Weapons.json.

Below is an example of a method under WeaponFactory that initialises a new weapon and associate a PhysicalWeaponStatsComponent to it.

   public static Entity createHera() {
        Entity hera = createBaseWeapon();
        hera.setEntityType(EntityTypes.CRAFTABLE);
        WeaponConfig config = configs.heraDag;
        PhysicalWeaponStatsComponent weaponStats = new PhysicalWeaponStatsComponent(config.damage, config.coolDown, config.materials, config.weight, "hera");
        hera
                .addComponent(weaponStats)
                .addComponent(new TextureRenderComponent("images/CombatItems/Sprint-1/Level 2 Dagger 2png.png"));
        hera.getComponent(TextureRenderComponent.class).scaleEntity();
        hera.scaleHeight(5f);
        hera.setEntityType(EntityTypes.MELEE);
        return hera;
    }

And the code below shows the constructor for PhysicalWeaponStatsComponent that takes in a coolDown value.

    public PhysicalWeaponStatsComponent(double damage, double coolDown, HashMap<Materials, Integer> materials, double weight, String description) {
       super(damage, coolDown, materials, description);
        setWeight(weight);
    }

PlayerTouchAttackComponent

Located in: source/core/src/main/com/deco2800/game/components/player/PlayerTouchAttackComponent.java_

Now, to the fun part. This is the component responsible for the attack of the player, with reference to (https://github.com/UQdeco2800/2022-studio-2/wiki/Attack-function).

In the attack() method, when there is a weapon equipped, a variable defined in the class, called cooldownEnd is updated to be the current time added with the cooldown time of the weapon equipped, both in milliseconds.

cooldownEnd = (long) (System.currentTimeMillis() + weaponEquipped.getComponent(PhysicalWeaponStatsComponent.class).getCoolDown());

If there is no weapon equipped however, the cooldown of the player's attack is defaulted be 4 seconds,

cooldownEnd = (System.currentTimeMillis() + 4000); //cooldown when no weapon equipped

The update() method is overwritten from the Component parent class to run checkCanAttack() after every frame instance to see if the cooldownEnd time is passed.

    @Override
    public void update() {
        checkCanAttack();
    }

Check can attack method. If the current time is passed the cooldownEnd variable, the canAttack boolean becomes true and allows the player to have a attack instance the next time spacebar is pressed.

    public void checkCanAttack() {
        if (System.currentTimeMillis() > cooldownEnd) {
            canAttack = true;
            cooldownEnd = 0;
        }
    }

Below is first four lines of attack() method, indicating that the contents of the functions are only ran when canAttack is true, and are changed to false when the player attacks.

    void attack() {
        if (canAttack) {
            Sound attackSound = ServiceLocator.getResourceService().getAsset("sounds/Impact4.ogg", Sound.class);
            attackSound.play();
            canAttack = false;
...
...

Justification of Integration

This function is created as it makes the gameplay more reasonable for the player. Imagine a gameplay where the player can just kill the enemies by spamming the spacebar to attack as there is no cooldown functionality for the attack instance. With this functionality, it sets the limit of how fast enemies can be killed and also introduces a skill factor which requires the player to actually try their best to kill the enemy, rather then just spamming the space bar. This functionality also complements the other stats of the weapons such as the damage, as the weapon's damage dealt will not be overshadowed by the fact that the damage will not mean anything if the player can just spam the space bar to deal the same amount of damage over the same time.

Back to Combat Items Contents Page

Author

  • Eugene Chong
  • GitHub: @eugene2341
  • Discord: gene#0005
  • Slack: Eugene Chong