Skip to content

PlaceableEntity

Rowan Gray edited this page Sep 13, 2023 · 3 revisions

See also Entities.

PlaceableEntity is a subclass of Entity which specifies some functions for entities placed via the StructurePlacementService.

Functions

The additional functions defined within PlaceableEntity are:

  • void placed()
  • void removed()
  • void willPlace()
  • void willRemove()

These functions allow actions to be taken when the structure is placed or removed via the StructurePlacementService. They each loop through the entity's component and, if the component implements the Placeable interface, calls the corresponding method in the component. For instance:

    public void placed() {
        for (var component : components.values()) {
            if (component instanceof Placeable) {
                ((Placeable) component).placed();
            }
        }
    }

Placeable Interface

The placeable interface defines:

  • void placed()
  • void removed()
  • void willPlace()
  • void willRemove() It can be implemented by components when an action should be taken when the structure is placed or removed via the StructurePlacementService.

By default, these functions do nothing. This means that you only need to override the function you wish to use.

Example

Here is an example of the placed and removed interface functions being implemented in the CostComponent.

    /**
     * Charges the player the specified cost of the structure.
     */
    @Override
    public void placed() {
        for (var elementCost : cost.entries()) {
            addResource(elementCost.key, -elementCost.value);
        }
    }

    /**
     * Refunds the player the specified cost of the structure proportional to the
     * percentage of health remaining.
     */
    @Override
    public void removed() {
        var combatStatsComponent = entity.getComponent(CombatStatsComponent.class);

        if (combatStatsComponent == null) {
            for (var elementCost : cost.entries()) {
                addResource(elementCost.key, elementCost.value);
            }
        } else {
            var healthPercentage = combatStatsComponent.getHealth() / combatStatsComponent.getMaxHealth();
            // if there is a health component, returns the cost in proportion to the health
            for (var elementCost : cost.entries()) {
                addResource(elementCost.key, elementCost.value * healthPercentage);
            }
        }
    }
Clone this wiki locally