-
Notifications
You must be signed in to change notification settings - Fork 9
PlaceableEntity
See also Entities.
PlaceableEntity
is a subclass of Entity
which specifies some functions for entities placed via the StructurePlacementService
.
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();
}
}
}
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 theStructurePlacementService
.
By default, these functions do nothing. This means that you only need to override the function you wish to use.
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);
}
}
}
Escape Earth Game
Interaction Controller and Interactable Components
Game and Entity Configuration Files
Loading Game Configuration Files