Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
296 lines (276 loc) · 9.89 KB
/
Copy pathutils.js
File metadata and controls
296 lines (276 loc) · 9.89 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// swing motion: a real cut instead of a snap-and-drift. the blade PULLS BACK
// (wind-up), whips THROUGH the arc in a few violent frames, then a heavy
// follow-through eases back to guard. t counts frames since the blow landed;
// returns the weapon's angle offset (negative = wound back).
function swingCurve(t, peak, windup, snap, recover) {
if (t === undefined || t < 0) return 0
if (t < windup) return -peak * 0.35 * (t / windup)
if (t < windup + snap) return -peak * 0.35 + peak * 1.35 * ((t - windup) / snap)
const r = (t - windup - snap) / recover
if (r >= 1) return 0
return peak * (1 - r) * (1 - r)
}
// swing juice: a fading streak along the arc the weapon just carved. call
// inside the facing-rotated frame, BEFORE rotate(attackRotate). rot decays
// from ±peak back to 0 as the swing recovers; restAngle is where the blade
// points at rest; radius is roughly mid-blade. spans over 2π (reaper spins)
// render as full blur rings.
function drawSwingStreak(size, rot, peak, radius, restAngle) {
const energy = peak ? abs(rot / peak) : 0
if (energy <= 0.08) return
const a0 = restAngle + min(rot, peak)
const a1 = restAngle + max(rot, peak)
noFill()
stroke(255, 110 * energy)
strokeWeight(size / 7)
arc(0, 0, radius * 2, radius * 2, a0, a1)
stroke(255, 45 * energy)
strokeWeight(size / 12)
arc(0, 0, radius * 2.3, radius * 2.3, a0, a1)
}
// troops STAND on the field: the body is drawn a little above its ground
// position with a shadow at the feet — the same ground/air separation the
// ballistic arrows use, so bows loose into the plane the bodies live in
function bodyLift(size) {
return size * 0.28
}
function drawUnitShadow(size) {
noStroke()
fill(0, 100)
ellipse(0, size * 0.35, size * 1.15, size * 0.5)
}
// shadow pass: in a massed line, every shadow goes down BEFORE any body is
// drawn, so a unit's shadow never lands on top of a neighbor's back. troops
// stamped here skip their own inline shadow this frame; structures draw
// their own footprint shading.
function drawTroopShadows(troops) {
for (let i = 0; i < troops.length; i++) {
const t = troops[i]
if (!t || isStructure(t)) continue
push()
translate(t.pos.x, t.pos.y)
drawUnitShadow(t.size)
pop()
t._shadowFrame = frameCount
}
}
function drawSettings(color, tranparency, size) {
// drawingContext.setLineDash([])
if (size) {
strokeWeight(size / 10)
} else {
strokeWeight(width / 1000)
}
colorMode(RGB)
switch (color) {
case 'red':
if (tranparency) {
fill(255, 120, 120, tranparency)
stroke(255, 150, 150, tranparency)
} else {
fill(255, 120, 120, 150)
stroke(255, 150, 150)
// fill(0, 150)
// stroke(0)
}
break;
case 'blue':
if (tranparency) {
fill(120, 120, 255, tranparency)
stroke(150, 150, 255, tranparency)
} else {
fill(120, 120, 255, 150)
stroke(150, 150, 255)
// fill(255, 150)
// stroke(255)
}
break;
}
}
function distSquaredVal(x, y, x1, y1) { //faster than sqrt
let dx = x1 - x;
let dy = y1 - y;
return dx * dx + dy * dy;
}
// vectors — no allocation, this is the hottest function in the game
function distSquared(a, b) {
let dx = a.x - b.x;
let dy = a.y - b.y;
return dx * dx + dy * dy;
}
function sqr(n) {
return n * n
}
function cb(n) {
return n * n * n
}
function lerp1(start, end, amt) {
return (1 - amt) * start + amt * end;
}
function clearTroops(team) {
battling = false;
if (team) {
if (team == 'red') {
redTroops = []
redProjectiles = []
redToRemove = []
} else if (team == 'blue') {
blueTroops = []
blueProjectiles = []
blueToRemove = []
}
} else {
clearTroops('red')
clearTroops('blue')
}
}
// single factory for every troop type — used by menus, shops, ghosts, and spawners
function makeTroop(name, x, y, team) {
let troop
switch (name) {
case 'soldier': troop = new Soldier(x, y, team); break
case 'archer': troop = new Archer(x, y, team); break
case 'spear': troop = new Spear(x, y, team); break
case 'phalanx': troop = new Phalanx(x, y, team); break
case 'knight': troop = new Knight(x, y, team); break
case 'necromancer': troop = new Necromancer(x, y, team); break
case 'summoner': troop = new Summoner(x, y, team); break
case 'ewizard': troop = new EWizard(x, y, team); break
case 'fwizard': troop = new FWizard(x, y, team); break
case 'warlock': troop = new Warlock(x, y, team); break
case 'shield': troop = new Shield(x, y, team); break
case 'charger': troop = new Charger(x, y, team); break
case 'reaver': troop = new Reaver(x, y, team); break
case 'reaper': troop = new Reaper(x, y, team); break
case 'zombie': troop = new Zombie(x, y, team); break
case 'castlewall': troop = new CastleWall(x, y, team); break
case 'keep': troop = new Keep(x, y, team); break
}
if (troop && typeof applyUnitBuffs === 'function') {
applyUnitBuffs(troop) // autochess: red troops pick up owned buffs (covers mid-battle summons too)
}
return troop
}
function weightedPick(list) {
if (!list.length) return undefined
let total = 0
list.forEach(e => total += e.weight)
let roll = random(0, total)
for (const e of list) {
roll -= e.weight
if (roll <= 0) return e
}
return list[list.length - 1]
}
function randomTroops(team) {
let buffer = width / 30
let offset = width / 2
let mult = team == 'red' ? -1 : 1
troops = team == 'red' ? redTroops : blueTroops
for (let i = 0; i < 200; i++) {
troops.push(new Soldier(offset + mult * random(buffer, offset - buffer), random(buffer, height - buffer), team))
}
for (let i = 0; i < 5; i++) {
troops.push(new CastleWall(offset + mult * random(buffer, offset - buffer), random(buffer, height - buffer), team))
}
for (let i = 0; i < 40; i++) {
troops.push(new Shield(offset + mult * random(buffer / 2, buffer), random(buffer, height - buffer), team))
}
for (let i = 0; i < 7; i++) {
troops.push(new Spear(offset + mult * random(buffer, buffer * 3 / 2), random(buffer, height - buffer), team))
}
for (let i = 0; i < 15; i++) {
troops.push(new Phalanx(offset + mult * random(buffer / 2, buffer * 3 / 2), random(buffer, height - buffer), team))
}
for (let i = 0; i < 10; i++) {
troops.push(new Reaper(offset + mult * random(buffer, buffer * 2), random(buffer, height - buffer), team))
}
for (let i = 0; i < 25; i++) {
troops.push(new Archer(offset + mult * random(offset - 3 * buffer, offset - buffer), random(buffer, height - buffer), team))
}
for (let i = 0; i < 2; i++) {
troops.push(new EWizard(offset + mult * random(offset - 3 * buffer, offset - buffer), random(buffer, height - buffer), team))
}
for (let i = 0; i < 2; i++) {
troops.push(new FWizard(offset + mult * random(offset - 3 * buffer, offset - buffer), random(buffer, height - buffer), team))
}
for (let i = 0; i < 2; i++) {
troops.push(new Summoner(offset + mult * random(offset - 4 * buffer, offset - 3 * buffer), random(buffer, height - buffer), team))
}
for (let i = 0; i < 1; i++) {
troops.push(new Necromancer(offset + mult * random(offset - 6 * buffer, offset - 4 * buffer), random(buffer, height - buffer), team))
}
}
// full-screen banner for round results — shared by campaign, autochess, siege
function drawBanner(title, subtitle) {
rectMode(CORNER)
noStroke()
fill(0, 130)
rect(0, 0, width, height)
fill(255)
textAlign(CENTER, CENTER)
textSize(width / 12)
text(title, width / 2, height / 2 - width / 40)
if (subtitle) {
textSize(width / 40)
fill(255, 200)
text(subtitle, width / 2, height / 2 + width / 20)
}
}
// ---------- persistence ----------
const SAVE_KEY = 'battledots_save'
function saveProgress() {
try {
localStorage.setItem(SAVE_KEY, JSON.stringify({
levelsComplete: levels.map(l => l.complete),
autochessBest: autochessBestStage,
siegeBest: siege.bestWave,
}))
} catch (e) { /* storage unavailable (private mode etc.) — play on without saving */ }
}
function loadProgress() {
try {
const data = JSON.parse(localStorage.getItem(SAVE_KEY))
if (!data) return
if (data.levelsComplete) {
for (let i = 0; i < levels.length; i++) {
levels[i].complete = !!data.levelsComplete[i]
if (levels[i].complete) {
levels[i].locked = false
if (levels[i + 1]) levels[i + 1].locked = false
}
}
}
if (data.autochessBest) {
autochessBestStage = data.autochessBest
}
if (data.siegeBest) {
siege.bestWave = data.siegeBest
}
} catch (e) { /* corrupt or unavailable save — start fresh */ }
}
function changeScreen(newScreen) {
clearTroops()
switch (newScreen) {
case 'title':
break
case 'level_select':
break
case 'versus_lobby':
lobbyName = ""
break
case 'game':
menu = new Menu()
newTroopId = undefined
erasing = false
// randomSeed(1)
if (mode == 'autochess') {
autochessEngine = new AutochessEngine()
}
if (mode == 'siege') {
initSiege()
}
break
}
screen = newScreen
}