Skip to content

Commit

Permalink
Use transactions for clearing tiles (e.g. when changing spawns)
Browse files Browse the repository at this point in the history
  • Loading branch information
platz1de committed Oct 13, 2024
1 parent 8db30dd commit 992e6f2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
6 changes: 2 additions & 4 deletions src/game/TerritoryManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {playerManager} from "./player/PlayerManager";
import {territoryRenderingManager} from "../renderer/manager/TerritoryRenderingManager";
import {playerNameRenderingManager} from "../renderer/manager/PlayerNameRenderingManager";
import {gameMap} from "./GameData";
import {TerritoryTransaction} from "./transaction/TerritoryTransaction";
Expand Down Expand Up @@ -105,15 +104,14 @@ class TerritoryManager {
* Clears a tile.
* @see TerritoryManager.conquer
* @param tile The tile to clear
* @param transaction The transaction to apply the clearing to
* @param transaction The transaction to apply the clearing to, requires to have null as the attacker
*/
clear(tile: number, transaction: TerritoryTransaction): void {
const owner = this.tileOwners[tile];
if (owner !== this.OWNER_NONE) {
this.tileOwners[tile] = this.OWNER_NONE;
playerManager.getPlayer(owner).removeTile(tile, transaction);
//TODO: integrate this with the transaction system
territoryRenderingManager.clear(tile);
transaction.setTerritory(tile);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/game/player/SpawnManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {clientPlayer, playerManager} from "./PlayerManager";
import {SpawnRequestPacket} from "../../network/protocol/packet/game/SpawnRequestPacket";
import {gameTicker} from "../GameTicker";
import {TerritoryTransaction} from "../transaction/TerritoryTransaction";
import {playerNameRenderingManager} from "../../renderer/manager/PlayerNameRenderingManager";

class SpawnManager {
spawnPoints: number[];
Expand Down Expand Up @@ -162,13 +161,17 @@ class SpawnManager {
if (pixels.length === 0) {
return false; //Invalid spawn point
}
const transaction = new TerritoryTransaction(playerManager.getPlayer(player), null);

if (this.spawnData[player]) {
this.spawnData[player].pixels.forEach(pixel => territoryManager.clear(pixel, transaction));
const clearTransaction = new TerritoryTransaction(null, playerManager.getPlayer(player));
this.spawnData[player].pixels.forEach(pixel => territoryManager.clear(pixel, clearTransaction));
this.spawnPoints.push(...this.spawnData[player].blockedPoints);
this.spawnData[player].blockedPoints.forEach(point => this.backupPoints.splice(this.backupPoints.indexOf(point), 1));
clearTransaction.apply();
}

const transaction = new TerritoryTransaction(playerManager.getPlayer(player), null);

const data = new SpawnData();
data.blockedPoints = this.spawnPoints.filter(point => Math.abs(point % gameMap.width - tile % gameMap.width) <= 4 && Math.abs(Math.floor(point / gameMap.width) - Math.floor(tile / gameMap.width)) <= 4);
data.pixels = pixels;
Expand All @@ -177,9 +180,6 @@ class SpawnManager {
this.backupPoints.push(...data.blockedPoints);
this.spawnData[player] = data;

transaction.addExecutor(function (this: TerritoryTransaction) {
playerNameRenderingManager.getPlayerData(this.player).validatePosition(); //Old name position is invalid, but wasn't captured by another player
});
transaction.apply(); //This intentionally ignores the "target" pixels
return true;
}
Expand Down
17 changes: 15 additions & 2 deletions src/game/transaction/TerritoryTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {Transaction} from "./Transaction";
import {Player} from "../player/Player";
import {getTransactionExecutors, registerTransactionType} from "./TransactionExecutors";
import {InvalidArgumentException} from "../../util/Exceptions";

//TODO: Do border calculations when transaction is applied (we currently paint some border tiles multiple times)
export class TerritoryTransaction extends Transaction {
protected readonly attacker: Player | null;
protected readonly defendant: Player | null;
protected readonly territoryQueue: Array<number> = [];
protected readonly borderQueue: Array<number> = [];
Expand All @@ -12,8 +15,18 @@ export class TerritoryTransaction extends Transaction {
protected defendantNamePos: number = 0;
protected defendantNamePosSize: number = -1;

constructor(attacker: Player, defendant: Player | null) {
super(attacker);
/**
* Create a new territory transaction.
* @param attacker The player that is attacking, or null if the territory is being cleared.
* @param defendant The player that is being attacked, or null if the territory is being claimed.
* @throws InvalidArgumentException if both attacker and defendant are null
*/
constructor(attacker: Player | null, defendant: Player | null) {
if (!attacker && !defendant) {
throw new InvalidArgumentException("Both attacker and defendant are null");
}
super(attacker ?? defendant as Player);
this.attacker = attacker;
this.defendant = defendant;
this.addExecutor(...getTransactionExecutors(TerritoryTransaction));
}
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/layer/TerritoryRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ class TerritoryRenderer extends CachedLayer {
export const territoryRenderer = new TerritoryRenderer();

mapTransformHandler.scale.register(territoryRenderer.onMapScale);
mapTransformHandler.move.register(territoryRenderer.onMapMove);
mapTransformHandler.move.register(territoryRenderer.onMapMove);

import("../manager/TerritoryRenderingManager");
4 changes: 2 additions & 2 deletions src/renderer/manager/PlayerNameRenderingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ export class PlayerNameRenderingData {
export const playerNameRenderingManager = new PlayerNameRenderingManager();

registerTransactionExecutor(TerritoryTransaction, function (this: TerritoryTransaction) {
if (this.namePosSize > 0) {
playerNameRenderingManager.getPlayerData(this.player).addPosition(this.namePosSize, this.namePos);
if (this.attacker && this.namePosSize > 0) {
playerNameRenderingManager.getPlayerData(this.attacker).addPosition(this.namePosSize, this.namePos);
}

if (this.defendant && this.defendantNamePosSize > -1) {
Expand Down
20 changes: 13 additions & 7 deletions src/renderer/manager/TerritoryRenderingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import {TerritoryTransaction} from "../../game/transaction/TerritoryTransaction"
* 2. The tile can become an inner tile of the player's territory.
* 3. A neighboring tile can become a border tile of the player's territory.
*/
class TerritoryRenderingManager {
export class TerritoryRenderingManager {
/**
* Clear the tile at the given index.
* @param tile index of the tile
* Clear the tiles at the given indices.
* @param tiles the tiles to clear
*/
clear(tile: number): void {
territoryRenderer.context.clearRect(tile % gameMap.width, Math.floor(tile / gameMap.width), 1, 1);
clearTiles(tiles: number[]): void {
for (const tile of tiles) {
territoryRenderer.context.clearRect(tile % gameMap.width, Math.floor(tile / gameMap.width), 1, 1);
}
}

/**
Expand Down Expand Up @@ -76,6 +78,10 @@ registerTransactionExecutor(TerritoryTransaction, function (this: TerritoryTrans
territoryRenderingManager.paintTiles(this.defendantBorderQueue, getSetting("theme").getBorderColor(this.defendant.baseColor));
}

territoryRenderingManager.paintTiles(this.borderQueue, getSetting("theme").getBorderColor(this.player.baseColor));
territoryRenderingManager.paintTiles(this.territoryQueue, getSetting("theme").getTerritoryColor(this.player.baseColor));
if (this.attacker) {
territoryRenderingManager.paintTiles(this.borderQueue, getSetting("theme").getBorderColor(this.attacker.baseColor));
territoryRenderingManager.paintTiles(this.territoryQueue, getSetting("theme").getTerritoryColor(this.attacker.baseColor));
} else {
territoryRenderingManager.clearTiles(this.territoryQueue);
}
});

0 comments on commit 992e6f2

Please sign in to comment.