Target player creates target icon

This commit is contained in:
evanpelle
2024-10-02 16:25:13 -07:00
parent 8c81a75a53
commit 2223e40d53
13 changed files with 156 additions and 15 deletions
+7
View File
@@ -10,6 +10,13 @@ export class AllianceImpl implements MutableAlliance {
readonly createdAtTick_: Tick,
) { }
other(player: Player): PlayerImpl {
if (this.requestor_ == player) {
return this.recipient_
}
return this.requestor_
}
requestor(): MutablePlayer {
return this.requestor_
}
+10
View File
@@ -64,10 +64,12 @@ export interface Alliance {
requestor(): Player
recipient(): Player
createdAt(): Tick
other(player: Player): Player
}
export interface MutableAlliance extends Alliance {
expire(): void
other(player: Player): MutablePlayer
}
export class PlayerInfo {
@@ -148,6 +150,11 @@ export interface Player {
// Includes recent requests that are in cooldown
recentOrPendingAllianceRequestWith(other: Player): boolean
isTraitor(): boolean
canTarget(other: Player): boolean
// Targets for this player
targets(): Player[]
// Targets of player and all allies.
transitiveTargets(): Player[]
toString(): string
}
@@ -168,6 +175,9 @@ export interface MutablePlayer extends Player {
breakAlliance(alliance: Alliance): void
createAllianceRequest(recipient: Player): MutableAllianceRequest
addBoat(troops: number, tile: Tile, target: Player | TerraNullius): MutableBoat
target(other: Player): void
targets(): MutablePlayer[]
transitiveTargets(): MutablePlayer[]
}
export interface Game {
+34 -1
View File
@@ -1,4 +1,4 @@
import {MutablePlayer, Tile, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, MutableGame, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance} from "./Game";
import {MutablePlayer, Tile, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, MutableGame, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance, Tick} from "./Game";
import {ClientID} from "../Schemas";
import {simpleHash} from "../Util";
import {CellString, GameImpl} from "./GameImpl";
@@ -7,6 +7,10 @@ import {TileImpl} from "./TileImpl";
import {TerraNulliusImpl} from "./TerraNulliusImpl";
import {threadId} from "worker_threads";
interface Target {
tick: Tick
target: Player
}
export class PlayerImpl implements MutablePlayer {
isTraitor_ = false
@@ -20,6 +24,8 @@ export class PlayerImpl implements MutablePlayer {
public pastOutgoingAllianceRequests: AllianceRequest[] = []
private targets_: Target[] = []
constructor(private gs: GameImpl, private readonly playerInfo: PlayerInfo, private _troops) {
this._name = playerInfo.name;
}
@@ -172,6 +178,33 @@ export class PlayerImpl implements MutablePlayer {
return this.gs.createAllianceRequest(this, recipient)
}
canTarget(other: Player): boolean {
for (const t of this.targets_) {
if (t.target == other) {
if (this.gs.ticks() - t.tick < this.gs.config().targetCooldown()) {
return false
}
}
}
return true
}
target(other: Player): void {
this.targets_.push({tick: this.gs.ticks(), target: other})
}
targets(): PlayerImpl[] {
return this.targets_
.filter(t => this.gs.ticks() - t.tick < this.gs.config().targetDuration())
.map(t => t.target as PlayerImpl)
}
transitiveTargets(): MutablePlayer[] {
const ts = this.alliances().map(a => a.other(this)).flatMap(ally => ally.targets())
ts.push(...this.targets())
return [...new Set(ts)]
}
hash(): number {
return simpleHash(this.id()) * (this.troops() + this.numTilesOwned());
}