Skip to content

Healing Tool

Rowan Gray edited this page Oct 15, 2023 · 8 revisions

Introduction

The Healing Tool is a component of the game that allows players to interact with entities and heal them. To heal any entity such as turret or wall after damage, we need to press 3 and then T-Key on keyboard respectively. A toolbox opens where we can see the heal icon. We click on the heal icon to activate the healing component and then click on either wall or turret to heal the health. As per the updated functionality of heal tool, the heal tool now works in accordance with resources available with the player. So for healing any entity by 1%, the player required 2 Durasteels and 2 Solstites.

Table of Contents

Overview

The Healing Tool is a subclass of the Tool class and is responsible for healing entities in the game. Below, we'll dive into the details of the code.

Code Structure

Healing Class

This is the healing class declaration that extends another class tool. In this class, there are two private integer variables declared, namely "requiredSolstite" and "requiredDurasteel".

public class Healing extends Tool {
    private int requiredSolstite;
    private int requiredDurasteel;

Constructor

This is the constructor of the Healing class. It takes an ObjectMap as a parameter named cost. The constructor of the parent class Tool is called using super(cost).

    public Healing(ObjectMap<String, Integer> cost) {
        super(cost);
        // healingSound = Gdx.audio.newSound(Gdx.files.internal("sounds/healing_sound.wav"));
    }

}

Method Definition

    @Override
    protected ToolResponse canInteract(Entity player, GridPoint2 position) {

This is a method definition. Here, a method is being override called canInteract from the parent class Tool. It takes two parameters: player, an Entity object representing the player, and position, a GridPoint2 object representing the position of the interaction. The method is declared with the protected access modifier.

determineSelectedEntity Method

In code below, determineSelectedEntity method is called to determine the entity that the player has clicked on based on the provided position.

        Entity clickedEntity = determineSelectedEntity(position);

Further Code

        // If no entity is clicked, return false
        if (clickedEntity == null) {
            return new ToolResponse(PlacementValidity.INVALID, "No structure to heal");
        }

This is an if statement that checks if clickedEntity is null, meaning no entity was clicked. If that's the case, it returns a ToolResponse with PlacementValidity.INVALID and a message indicating that there's no structure to heal.

        CombatStatsComponent combatStats = clickedEntity.getComponent(CombatStatsComponent.class);

Attempting to get a component of type CombatStatsComponent from the clickedEntity. This component is used to represent the combat statistics of the clicked entity.

        // If the clicked entity does not have a CombatStatsComponent, return false
        if (combatStats == null) {
            return new ToolResponse(PlacementValidity.INVALID, "Cannot be healed");
        }

Another if statement checks if combatStats is null, which means the clicked entity does not have a CombatStatsComponent. In this case, it returns a ToolResponse with PlacementValidity.INVALID and a message indicating that the entity cannot be healed.

        // Calculate the healing cost based on the current health deficit
        int currentHealth = combatStats.getHealth();
        int maxHealth = combatStats.getMaxHealth();
        double healthDeficit = 1 - ((double) currentHealth / maxHealth);
        requiredDurasteel = (int) Math.ceil(healthDeficit * 100 * 2); // 2 Durasteel per 1%
        requiredSolstite = (int) Math.ceil(healthDeficit * 100 * 2); // 2 Solstite per 1%

Calculating the healing cost based on the current health deficit of the entity. You calculate the healthDeficit as a percentage and then convert it into the required amount of requiredDurasteel and requiredSolstite resources.

        // Check if the player has enough resources for healing
        if (!playerHasEnoughResources(requiredDurasteel, requiredSolstite)) {
            return new ToolResponse(PlacementValidity.INVALID, "Not enough resources");
        }

Checking if the player has enough resources (Durasteel and Solstite) for healing. If they don't have enough, it returns a ToolResponse with PlacementValidity.INVALID and a message indicating that there are not enough resources.

        return ToolResponse.valid();
    }

If all the checks pass, return a ToolResponse with ToolResponse.valid(), indicating that the interaction is valid.

    @Override
    public void performInteraction(Entity player, GridPoint2 position) {

This method is also an override from the parent class Tool. It's called performInteraction and takes two parameters: player, an Entity object representing the player, and position, a GridPoint2 object representing the position of the interaction.

        // Deduct the required resources
        deductResources(requiredDurasteel, requiredSolstite);

Calling the deductResources method to deduct the required resources (Durasteel and Solstite) from the player's inventory.

        Entity clickedEntity = determineSelectedEntity(position);

Similar to the canInteract method, calling the determineSelectedEntity method to determine the entity that the player has clicked on based on the provided position.

        entity.getEvents().trigger("playSound","wallHeal");

Triggering an event on the entity object, which is "playing a sound" with the identifier "wallHeal."

        CombatStatsComponent combatStats = clickedEntity.getComponent(CombatStatsComponent.class);

Getting the CombatStatsComponent from the clickedEntity, which is the entity that the player clicked on. This component is used to manage combat-related statistics.

        // For setting the health of the clicked entity to 100
        combatStats.setHealth(combatStats.getMaxHealth());
    }

Setting the health of the clickedEntity to its maximum health value. This presumably represents the healing action, restoring the entity's health to full.

    // Determine the selected entity based on the player's click position
    private Entity determineSelectedEntity(GridPoint2 position) {
        return ServiceLocator.getStructurePlacementService().getStructureAt(position);
    }

This method, determineSelectedEntity, takes a GridPoint2 object as input, representing a position, and returns the selected entity at that position. It relies on a service (ServiceLocator.getStructurePlacementService()) to determine the entity.

    // Check if the player has enough Durasteel and Solstite for healing
    private boolean playerHasEnoughResources(int requiredDurasteel, int requiredSolstite) {
        try {
            int playerDurasteel = (int) ServiceLocator.getGameStateObserverService()
                    .getStateData("resource/" + Resource.Durasteel);
            int playerSolstite = (int) ServiceLocator.getGameStateObserverService()
                    .getStateData("resource/" + Resource.Solstite);

            return playerDurasteel >= requiredDurasteel && playerSolstite >= requiredSolstite;
        } catch (NullPointerException e) {
            return false;
        }
    }

This method, playerHasEnoughResources, checks if the player has enough Durasteel and Solstite for healing. It retrieves the player's resource amounts from a game state observer service (ServiceLocator.getGameStateObserverService()) and compares them with the required amounts. If any exception occurs (e.g., NullPointerException), it returns false.

    // Deduct the required Durasteel and Solstite from the player's resources
    private void deductResources(int requiredDurasteel, int requiredSolstite) {
        ServiceLocator.getGameStateObserverService().trigger("resourceAdd", Resource.Durasteel.toString(), -requiredDurasteel);
        ServiceLocator.getGameStateObserverService().trigger("resourceAdd", Resource.Solstite.toString(), -requiredSolstite);
    }
}

This method, deductResources, deducts the required amounts of Durasteel and Solstite from the player's resources using the game state observer service. It triggers a resource deduction event with the specified resource type and amount (negative value to deduct).

Purpose

The purpose of adding healing feature in the game is to basically enable players to strengthen their defence during gameplay. Be it preventing the walls from collapsing or the turrets from getting damaged, the healing tool handles it all.

Testing

The healing feature was tested by placing the gates and turrets near enemies and luring them to attack those. Since enemies' attack reduced the health of both of these entities, we were able to heal these entities to maximum by clicking 3 and T-Key respectively, then clicking on the heal icon, and finally clicking on the entity that we wanted to heal. We had sufficient resources while doing so.

Clone this wiki locally