106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
// Animation cues are derived by diffing two match snapshots, so they work for
|
|
// every client (including spectators) without adding anything to the wire format.
|
|
|
|
export interface AnimationCardSnapshot {
|
|
id: string
|
|
damage?: number
|
|
attacking?: string
|
|
}
|
|
|
|
export interface AnimationPlayerSnapshot {
|
|
id: string
|
|
life?: number
|
|
battlefield?: readonly AnimationCardSnapshot[]
|
|
}
|
|
|
|
export interface AnimationStateSnapshot {
|
|
id?: string
|
|
players?: readonly AnimationPlayerSnapshot[]
|
|
game?: {
|
|
manaPool?: Record<string, number>
|
|
}
|
|
}
|
|
|
|
export interface CardDamageAnimation {
|
|
cardId: string
|
|
amount: number
|
|
}
|
|
|
|
export interface LifeChangeAnimation {
|
|
playerId: string
|
|
amount: number
|
|
}
|
|
|
|
export interface ManaGainAnimation {
|
|
color: string
|
|
amount: number
|
|
}
|
|
|
|
export interface GameAnimations {
|
|
enteringCardIds: string[]
|
|
attackingCardIds: string[]
|
|
damagedCards: CardDamageAnimation[]
|
|
lifeChanges: LifeChangeAnimation[]
|
|
manaGains: ManaGainAnimation[]
|
|
}
|
|
|
|
function emptyAnimations(): GameAnimations {
|
|
return {
|
|
enteringCardIds: [],
|
|
attackingCardIds: [],
|
|
damagedCards: [],
|
|
lifeChanges: [],
|
|
manaGains: [],
|
|
}
|
|
}
|
|
|
|
function cardsByPlayer(state: AnimationStateSnapshot): Map<string, AnimationCardSnapshot> {
|
|
const cards = new Map<string, AnimationCardSnapshot>()
|
|
for (const player of state.players ?? []) {
|
|
for (const card of player.battlefield ?? []) cards.set(card.id, card)
|
|
}
|
|
return cards
|
|
}
|
|
|
|
/**
|
|
* Compare the previous and current match state and return the visual cues that
|
|
* should play for the current state. The first snapshot has nothing to compare
|
|
* against and produces no animations.
|
|
*/
|
|
export function diffGameState(
|
|
previous: AnimationStateSnapshot | null | undefined,
|
|
next: AnimationStateSnapshot,
|
|
): GameAnimations {
|
|
const animations = emptyAnimations()
|
|
if (!previous) return animations
|
|
if (previous.id && next.id && previous.id !== next.id) return animations
|
|
|
|
const previousCards = cardsByPlayer(previous)
|
|
const nextCards = cardsByPlayer(next)
|
|
for (const [id, card] of nextCards) {
|
|
const before = previousCards.get(id)
|
|
if (!before) animations.enteringCardIds.push(id)
|
|
else {
|
|
const amount = (card.damage ?? 0) - (before.damage ?? 0)
|
|
if (amount > 0) animations.damagedCards.push({ cardId: id, amount })
|
|
}
|
|
if (card.attacking && !before?.attacking) animations.attackingCardIds.push(id)
|
|
}
|
|
|
|
for (const player of next.players ?? []) {
|
|
const before = previous.players?.find(entry => entry.id === player.id)
|
|
if (!before || before.life === undefined || player.life === undefined) continue
|
|
const amount = player.life - before.life
|
|
if (amount) animations.lifeChanges.push({ playerId: player.id, amount })
|
|
}
|
|
|
|
const previousPool = previous.game?.manaPool ?? {}
|
|
const nextPool = next.game?.manaPool ?? {}
|
|
for (const color of new Set([...Object.keys(nextPool), ...Object.keys(previousPool)])) {
|
|
const amount = (nextPool[color] ?? 0) - (previousPool[color] ?? 0)
|
|
if (amount > 0) animations.manaGains.push({ color, amount })
|
|
}
|
|
|
|
return animations
|
|
}
|