2 min read
Destroying Castles & Losing

With castle persistence working, it’s only fair they can be destroyed.

When the player’s castle falls, the game ends.

Destroying castles

The PlayerPersistTag is just for the player’s castle. While persistence and castle structure are closely related, non-castle entities might need to be persisted (e.g. quest status, flags, etc).

Instead, I introduced a HasParent component with a parentId. This lets me tag any prefab parts (towers, sigils) for joint destruction and reuse the logic across multiple systems.

Every child entity in the WizardTower prefab now points back to the main castle entity.

The morbidly named ParentDeathSystem watches for parent entities marked with Death and flags all their children for destruction.

But that doesn’t cover everything. If a castle dies, so does everything inside.

The CastleDeathSystem checks for any entities in the castle’s Area and flags them with Death.

// CastleDeathSystem
for (Entity deadCastle : deadCastles) {
    // destroy any entities contained within the area
    destroyEntitiesInCastle(deadCastle);
    // check if this was a player castle, game over if it is.
    if (Comps.playerCastle.has(deadCastle)) {
        // TODO... game over!
    }
}

Though it seems excessive to add two new systems, concise systems make ECS easier to reason about.

Game Over

I added a you lost, restart? event using the new dialogue system.

When the player picks restart, restartMapWithNewWorld regenerates the world with a different seed and starts again.

Simple, now that map loading works.