-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.ts
139 lines (129 loc) · 3.37 KB
/
cell.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Game } from './game.ts';
import { User } from './user.ts';
export const enum Blast {
Square = 1,
Horizontal = 2,
Vertical = 3,
}
export class Cell {
#owner?: User;
get owner() {
return this.#owner;
}
#attacker?: User;
get attacker() {
return this.#attacker;
}
#ownedAt = 0;
#attackedAt = 0;
#takenAt = 0;
constructor(
readonly game: Game,
readonly x: number,
readonly y: number,
readonly gold: boolean,
readonly energy: boolean,
readonly blast: boolean
) {}
#adj(user: User) {
const { x, y } = this;
return (
[
this.game.cell(x, y - 1),
this.game.cell(x + 1, y),
this.game.cell(x, y + 1),
this.game.cell(x - 1, y),
].filter((c) => c && user.is(c.#owner)) as Cell[]
).length;
}
#time(user: User) {
return this.#owner
? this.game.min +
(this.game.max - this.game.min) *
2 ** (-(this.game.now - this.#ownedAt) / this.game.conv)
: this.game.idle *
(1 - 0.25 * Math.max(0, this.#adj(user) - 1)) *
this.game.energyRatio ** user.energy;
}
#blast(x: number, y: number, blast?: Blast) {
switch (blast) {
case Blast.Square:
return [
this.game.cell(x - 2, y),
this.game.cell(x - 1, y),
this.game.cell(x + 1, y),
this.game.cell(x + 2, y),
this.game.cell(x, y - 2),
this.game.cell(x, y - 1),
this.game.cell(x, y + 1),
this.game.cell(x, y + 2),
];
case Blast.Horizontal:
return [
this.game.cell(x - 4, y),
this.game.cell(x - 3, y),
this.game.cell(x - 2, y),
this.game.cell(x - 1, y),
this.game.cell(x + 1, y),
this.game.cell(x + 2, y),
this.game.cell(x + 3, y),
this.game.cell(x + 4, y),
];
case Blast.Vertical:
return [
this.game.cell(x, y - 4),
this.game.cell(x, y - 3),
this.game.cell(x, y - 2),
this.game.cell(x, y - 1),
this.game.cell(x, y + 1),
this.game.cell(x, y + 2),
this.game.cell(x, y + 3),
this.game.cell(x, y + 4),
];
}
return [];
}
update() {
if (this.#attacker && this.game.now > this.#takenAt) {
this.#owner = this.#attacker;
this.#attacker = undefined;
this.#ownedAt = this.#takenAt;
this.#attackedAt = 0;
this.#takenAt = 0;
}
}
attack(user: User, blast?: Blast) {
if (user.attacking || this.#attacker) return;
if (blast && this.blast) {
if (user.is(this.#owner)) {
const { x, y } = this;
for (const neighbor of this.#blast(x, y, blast).filter(
(c) => c && !c.#attacker
) as Cell[])
neighbor.#attack(user);
}
return;
}
if (!user.based || user.is(this.#owner) || this.#adj(user))
this.#attack(user);
}
#attack(user: User) {
this.#attacker = user;
this.#attackedAt = this.game.now;
this.#takenAt = this.#attackedAt + this.#time(user);
}
toJSON(): ICell {
return {
x: this.x,
y: this.y,
g: Number(this.gold) as binary,
e: Number(this.energy) as binary,
b: Number(this.blast) as binary,
o: this.#owner?.uuid ?? '',
a: this.#attacker?.uuid ?? '',
s: this.#ownedAt,
d: this.#attackedAt,
f: this.#takenAt,
};
}
}