Skip to content

CostComponent

Rowan Gray edited this page Sep 13, 2023 · 1 revision

First see Components, Loading Resources.

The CostComponent is a component used to charge the user the given cost when the entity it is attached to is placed via the StructurePlacementService. It also refunds the cost when picked up. If there is also a CombatStatsComponent attached, it will refund an amount proportional to the percentage of health remaining.

It implements the Placeable interface for this functionality.

Constructors

The CostComponent takes in an ObjectMap<String, Integer> which contains a map from resource name to amount of resources required.

public CostComponent(ObjectMap<String, Integer> cost)

Usage

Before using the CostComponent you must first include it in your file.

import com.csse3200.game.components.structures.CostComponent;

The CostComponent can then be added to an entity as follows.

PlaceableEntity entity = new PlaceableEntity();

var cost = new ObjectMap<String, Integer>();
cost.put("resource1", 10);
cost.put("resource2", 25);

entity.addComponent(new CostComponent(cost));

You can then place the entity via the StructurePlacementService as follows.

var position = new GridPoint2(0,0);

ServiceLocator.getStructurePlacementService().placeStructureAt(entity, position);

When the entity is placed, the specified resources will be taken from the player.

Clone this wiki locally