Skip to content

Healing Tool

Yugansh-Uq edited this page Sep 13, 2023 · 8 revisions

Introduction

The Healing Tool is a component of the game that allows players to interact with entities and heal them. This document provides an in-depth explanation of the Healing Tool code.

Table of Contents

Overview

The Healing Tool is a Java class designed for a game development project. It 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

The Healing class is the main component responsible for healing entities. It is a subclass of the Tool class.

Constructor

The Healing class has a constructor that accepts a map of costs. The cost map is used to specify the resource cost of using this healing tool.

public Healing(ObjectMap<String, Integer> cost) {
    super(cost);
}

Interact Method

The interact method allows the player to interact with entities in the game world. It checks if the player has clicked on an entity, and if so, it attempts to heal the entity by setting its health to its maximum health value.

@Override
public boolean interact(Entity player, GridPoint2 position) {
    Entity clickedEntity = determineSelectedEntity(position);

    if (clickedEntity == null) {
        return false;
    }

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

    if (combatStats == null) {
        return false;
    }

    combatStats.setHealth(combatStats.getMaxHealth());
    return true;
}

determineSelectedEntity Method

The determineSelectedEntity method is a private helper function that determines which entity the player has clicked on. It uses the ServiceLocator to access the StructurePlacementService and retrieve the entity at the specified position.

private Entity determineSelectedEntity(GridPoint2 position) {
    return ServiceLocator.getStructurePlacementService().getStructureAt(position);
}

Usage

Clone this wiki locally