Skip to content

PlaceableEntity

Rowan Gray edited this page Sep 14, 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);
            }
        }
    }

Diagrams

This diagram shows how the PlaceableEntity interacts with the StructurePlacementService and its components which implement the Placeable interface.

classDiagram
    class PlaceableEntity {
        <<inherits>>
        - components : Array<Component>
        + void created()
        + void willCreate()
        + void removed()
        + void willRemove()
    }
    class Entity

    class Placeable {
        <<interface>>
        + void created()
        + void willCreate()
        + void removed()
        + void willRemove()
    }

    Entity <|-- PlaceableEntity
    PlaceableEntity --o Placeable
    PlaceableEntity --o Component
    Component --o Placeable
    StructurePlacementService --o PlaceableEntity
Loading

This diagram shows how the PlaceableEntity interacts with the StructurePlacementService, StructureToolPicker, and PlaceableTool after a player action is invoked.

sequenceDiagram
    PlayerActions ->>+ StructureToolPicker : interact(position)
    StructureToolPicker ->>+ PlacementTool: boolean = interact(player, position)
    PlacementTool ->>+ StructurePlacementService: placeStructureAt(placeableEntity, position)
    StructurePlacementService ->>+ PlaceableEntity: willPlace()
    StructurePlacementService ->>+ EntityService: register(placeableEntity)
    StructurePlacementService ->>+ PlaceableEntity: placed()
    PlayerActions ->>+ StructurePlacementService: removeStructureAt(position)
    StructurePlacementService ->>+ PlaceableEntity: willRemove()
    StructurePlacementService ->>+ EntityService: unregister(placeableEntity)
    StructurePlacementService ->>+ PlaceableEntity: removed()
Loading
Clone this wiki locally