BuildingActions Component - UQdeco2800/2022-studio-3 GitHub Wiki

Explanation

This is a Component that is added to all building entities when spawn through the BuildingFactory

On create, it registers event handlers to listen to mouse events to provide functionality for placing the building in the GameArea. The building placement feature requires building entity to have the TouchPlayerInputComponent which triggers the various placement events when certain mouse events occur.

  • On Left mouse button down, the building will placed
  • When in placing mode, the Entity's position follows the mouse until placed or dispose.
  • Start placement is not triggered by anything - letting team 1 UI trigger through user interface

This component also registers a callable event to level up the building but is not currently being triggered by any event. This is just to demonstrate support for a building leveling system.

Implementation

public void create() {
    entity.getEvents().addListener("startPlacing", this::startPlacing); // Not triggerd by any event yet
    entity.getEvents().addListener("placing", this::placing);
    entity.getEvents().addListener("place", this::place);
    entity.getEvents().addListener("levelUp", this::addLevel); // Not triggered by any event yet
    physicsComponent = entity.getComponent(PhysicsComponent.class);
    placed = true;
}
public void startPlacing() {
    if (!placed) {return;}
    placed = false;
    physicsComponent.setEnabled(false);
}

public void placing(int screenX, int screenY) {
    if (placed) {return;}
    Vector2 position = mouseToGrid(screenX, screenY);
    entity.setPosition(position);
}

public void place(int screenX, int screenY) {
    if (placed) {return;}
    placed = true;
    physicsComponent.setEnabled(true);
    Vector2 position = mouseToGrid(screenX, screenY);
    entity.setPosition(position);
}