evotars 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/.editorconfig +13 -0
  2. package/.prettierrc.json +3 -0
  3. package/README.md +3 -5
  4. package/index.html +13 -0
  5. package/package.json +2 -16
  6. package/playground/main.ts +48 -0
  7. package/playground/styles.css +17 -0
  8. package/public/evotars/agent/data.json +18 -0
  9. package/public/evotars/agent/sprite.json +166 -0
  10. package/public/evotars/agent/sprite.png +0 -0
  11. package/public/evotars/cat/data.json +18 -0
  12. package/public/evotars/cat/sprite.json +231 -0
  13. package/public/evotars/cat/sprite.png +0 -0
  14. package/public/evotars/dude/data.json +18 -0
  15. package/public/evotars/dude/sprite.json +231 -0
  16. package/public/evotars/dude/sprite.png +0 -0
  17. package/public/evotars/girl/data.json +18 -0
  18. package/public/evotars/girl/sprite.json +231 -0
  19. package/public/evotars/girl/sprite.png +0 -0
  20. package/public/evotars/nerd/data.json +18 -0
  21. package/public/evotars/nerd/sprite.json +231 -0
  22. package/public/evotars/nerd/sprite.png +0 -0
  23. package/public/evotars/senior/data.json +18 -0
  24. package/public/evotars/senior/sprite.json +231 -0
  25. package/public/evotars/senior/sprite.png +0 -0
  26. package/public/evotars/set.json +10 -0
  27. package/public/evotars/sith/data.json +18 -0
  28. package/public/evotars/sith/sprite.json +166 -0
  29. package/public/evotars/sith/sprite.png +0 -0
  30. package/public/evotars/sponge/data.json +18 -0
  31. package/public/evotars/sponge/sprite.json +231 -0
  32. package/public/evotars/sponge/sprite.png +0 -0
  33. package/public/fonts/Rubik.ttf +0 -0
  34. package/public/sounds/jump.mp3 +0 -0
  35. package/src/app.ts +71 -0
  36. package/src/config/constants.ts +1 -0
  37. package/src/entities/EmoteSpitter.ts +102 -0
  38. package/src/entities/Evotar.ts +468 -0
  39. package/src/entities/Message.ts +164 -0
  40. package/src/entities/Name.ts +33 -0
  41. package/src/entities/SpriteContainer.ts +65 -0
  42. package/src/entities/TrailEffect.ts +73 -0
  43. package/src/evotars.ts +75 -0
  44. package/src/evotarsManager.ts +313 -0
  45. package/src/helpers/timer.ts +43 -0
  46. package/src/helpers/types.ts +4 -0
  47. package/src/services/soundService.ts +30 -0
  48. package/src/services/spriteService.ts +167 -0
  49. package/src/types/dto/action.ts +79 -0
  50. package/{dist/types/dto/chatter.d.ts → src/types/dto/chatter.ts} +2 -2
  51. package/src/types/dto/event.ts +9 -0
  52. package/src/types/dto/message.ts +8 -0
  53. package/src/types/dto/settings.ts +5 -0
  54. package/src/types/dto/user.ts +13 -0
  55. package/tsconfig.json +23 -0
  56. package/vite.config.js +26 -0
  57. package/dist/app.d.ts +0 -24
  58. package/dist/config/constants.d.ts +0 -1
  59. package/dist/entities/EmoteSpitter.d.ts +0 -15
  60. package/dist/entities/Evotar.d.ts +0 -78
  61. package/dist/entities/Message.d.ts +0 -27
  62. package/dist/entities/Name.d.ts +0 -14
  63. package/dist/entities/SpriteContainer.d.ts +0 -16
  64. package/dist/entities/TrailEffect.d.ts +0 -15
  65. package/dist/evotars.d.ts +0 -15
  66. package/dist/evotars.js +0 -722
  67. package/dist/evotars.umd.cjs +0 -1
  68. package/dist/evotarsManager.d.ts +0 -30
  69. package/dist/helpers/timer.d.ts +0 -14
  70. package/dist/helpers/types.d.ts +0 -10
  71. package/dist/services/soundService.d.ts +0 -14
  72. package/dist/services/spriteService.d.ts +0 -63
  73. package/dist/types/dto/action.d.ts +0 -56
  74. package/dist/types/dto/event.d.ts +0 -9
  75. package/dist/types/dto/message.d.ts +0 -8
  76. package/dist/types/dto/settings.d.ts +0 -5
  77. package/dist/types/dto/user.d.ts +0 -12
  78. /package/{dist/index.d.ts → src/index.ts} +0 -0
  79. /package/{dist/types/dto/index.d.ts → src/types/dto/index.ts} +0 -0
  80. /package/{dist/types/index.d.ts → src/types/index.ts} +0 -0
package/dist/evotars.js DELETED
@@ -1,722 +0,0 @@
1
- var D = Object.defineProperty;
2
- var z = (r, t, e) => t in r ? D(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
- var s = (r, t, e) => (z(r, typeof t != "symbol" ? t + "" : t, e), e);
4
- import * as c from "pixi.js";
5
- import { Assets as R, Spritesheet as k, AnimatedSprite as M, SCALE_MODES as P } from "pixi.js";
6
- import F from "tinycolor2";
7
- import * as g from "@tweenjs/tween.js";
8
- import { Tween as T } from "@tweenjs/tween.js";
9
- import { AnimatedGIF as V } from "@pixi/gif";
10
- const m = 0.02 * 1e3;
11
- class f {
12
- constructor(t = 0, e) {
13
- s(this, "isCompleted", !1);
14
- this.current = t, this.onComplete = e;
15
- }
16
- tick(t = m) {
17
- var e;
18
- this.isCompleted || (this.current -= t, this.current < 0 && (this.isCompleted = !0, (e = this.onComplete) == null || e.call(this)));
19
- }
20
- }
21
- class L {
22
- constructor() {
23
- s(this, "timers", []);
24
- }
25
- add(t, e) {
26
- this.timers.push(new f(t, e));
27
- }
28
- tick(t = m) {
29
- const e = [];
30
- for (const i of this.timers)
31
- i.isCompleted || (i.tick(t), e.push(i));
32
- this.timers = e;
33
- }
34
- }
35
- const v = new L(), j = (r) => r.name == "jump", J = (r) => r.name == "color", W = (r) => r.name == "grow", O = (r) => r.name == "dash", U = (r) => r.name == "sprite";
36
- class X {
37
- constructor() {
38
- s(this, "assets", {});
39
- }
40
- async initialize(t) {
41
- this.assets = Object.fromEntries(
42
- Array.from(Object.entries(t ?? {})).map(([e, i]) => [e, new Audio(i.src)])
43
- );
44
- }
45
- play(t) {
46
- const e = this.assets[t];
47
- e && (e.volume = 0.2, e.pause(), e.currentTime = 0, e.play());
48
- }
49
- }
50
- const A = new X();
51
- var d = /* @__PURE__ */ ((r) => (r.Idle = "Idle", r.Jump = "Jump", r.Fall = "Fall", r.Land = "Land", r.Run = "Run", r.Die = "Die", r))(d || {});
52
- class N {
53
- constructor() {
54
- s(this, "spriteLoaderFn");
55
- s(this, "sprites", {});
56
- }
57
- initialize(t) {
58
- this.spriteLoaderFn = t;
59
- }
60
- async getSpriteData(t) {
61
- const e = await this.spriteLoaderFn(t);
62
- if (!e)
63
- return;
64
- if (this.sprites[t])
65
- return this.sprites[t];
66
- const i = await R.load(e.image), n = new k({
67
- texture: i,
68
- data: e.sprite
69
- });
70
- return await n.parse(), this.sprites[t] = {
71
- sheet: n,
72
- data: {
73
- ...e.data,
74
- scale: e.data.scale ?? 1
75
- }
76
- }, this.sprites[t];
77
- }
78
- getAnimatedSprites(t) {
79
- if (!t)
80
- throw Error("Sheet is not defined");
81
- if (!this.sprites[t])
82
- throw Error("Sprite is not loaded");
83
- const e = this.sprites[t].data, i = this.sprites[t].sheet, n = {};
84
- for (const a of i.data.meta.frameTags ?? []) {
85
- if (!i.data.meta.layers)
86
- throw Error("Layers are not defined");
87
- const o = {};
88
- for (const h of i.data.meta.layers) {
89
- const p = [];
90
- for (let u = a.from; u <= a.to; u++) {
91
- const b = u.toString(), C = e.name + "_" + h.name + "_" + b, E = i.textures[C], I = i.data.frames[C].duration;
92
- p.push({ texture: E, time: I });
93
- }
94
- const x = new M(p);
95
- x.texture.baseTexture.scaleMode = P.NEAREST, o[h.name] = x;
96
- }
97
- a.name && (n[a.name] = o);
98
- }
99
- return n;
100
- }
101
- }
102
- const S = new N();
103
- class _ {
104
- constructor() {
105
- s(this, "container", new c.Container());
106
- s(this, "emotes", []);
107
- s(this, "moveSpeed", 50);
108
- s(this, "alphaSpeed", 1);
109
- s(this, "scaleSpeed", 0.5);
110
- s(this, "timer");
111
- }
112
- async add(t) {
113
- const e = await fetch(t), i = e.headers.get("content-type");
114
- let n;
115
- switch (i) {
116
- case "image/gif": {
117
- const a = await e.arrayBuffer();
118
- n = V.fromBuffer(a);
119
- break;
120
- }
121
- case "image/png": {
122
- n = c.Sprite.from(t);
123
- break;
124
- }
125
- default: {
126
- console.warn("Unsupported content type: ", t);
127
- return;
128
- }
129
- }
130
- n.anchor.set(0.5, 0.5), n.scale.set(0, 0), this.emotes.push(n);
131
- }
132
- update(t) {
133
- var e;
134
- (e = this.timer) == null || e.tick(), (!this.timer || this.timer.isCompleted) && this.emotes.length > 0 && (this.timer = new f(2e3, () => {
135
- const i = this.emotes.shift();
136
- i && this.container.addChild(i);
137
- })), this.container.position.x = t.position.x ?? this.container.position.x, this.container.position.y = t.position.y ?? this.container.position.y;
138
- for (const i of this.container.children)
139
- i.position.y -= m * this.moveSpeed / 1e3, i.scale.x += m * this.scaleSpeed / 1e3, i.scale.y += m * this.scaleSpeed / 1e3, i.scale.x > 1 && (i.alpha -= m * this.alphaSpeed / 1e3), i.alpha <= 0 && this.container.removeChild(i);
140
- }
141
- }
142
- class G {
143
- constructor(t) {
144
- s(this, "container", new c.Container());
145
- s(this, "animated", new c.Container());
146
- s(this, "text", new c.Text(void 0, {
147
- fontFamily: "Rubik",
148
- fontSize: 20,
149
- fill: 2236962,
150
- align: "left",
151
- breakWords: !0,
152
- wordWrap: !0,
153
- wordWrapWidth: 200
154
- }));
155
- s(this, "padding", 10);
156
- s(this, "borderRadius", 10);
157
- s(this, "boxColor", 15658734);
158
- s(this, "fadeShift", 10);
159
- s(this, "isChecking", !0);
160
- s(this, "messageQueue", []);
161
- s(this, "showTween");
162
- s(this, "hideTween");
163
- s(this, "timer");
164
- this.beforeShow = t, this.container.addChild(this.animated), this.text.anchor.set(0.5, 1), this.text.position.set(0, -this.padding);
165
- }
166
- trim(t) {
167
- const e = c.TextMetrics.measureText(t.text, t.style);
168
- return e.lines.length > 4 ? e.lines.slice(0, 4).join(" ").slice(0, -3) + "..." : t.text;
169
- }
170
- update(t) {
171
- var i, n, a, o, h;
172
- if ((i = this.timer) == null || i.tick(), (n = this.showTween) == null || n.update(), (a = this.hideTween) == null || a.update(), this.isChecking && this.messageQueue.length > 0) {
173
- this.isChecking = !1;
174
- const p = this.messageQueue.shift();
175
- p && (this.beforeShow(), this.show(p));
176
- }
177
- this.container.position.x = t.position.x ?? this.container.position.x, this.container.position.y = t.position.y ?? this.container.position.y;
178
- const e = ((o = this.showTween) == null ? void 0 : o.isPlaying()) || ((h = this.hideTween) == null ? void 0 : h.isPlaying());
179
- !this.isChecking && !e && this.timer && (this.animated.position.y = Math.sin(this.timer.current * 25e-4) * 4 - 2);
180
- }
181
- add(t) {
182
- this.messageQueue.push(t);
183
- }
184
- drawBox(t) {
185
- const e = {
186
- x: t.x - this.padding - t.width * t.anchor.x,
187
- y: t.y - this.padding - t.height * t.anchor.y,
188
- w: t.width + this.padding * 2,
189
- h: t.height + this.padding * 2
190
- }, i = new c.Graphics();
191
- return i.beginFill(this.boxColor), i.drawRoundedRect(
192
- e.x,
193
- e.y,
194
- e.w,
195
- e.h,
196
- this.borderRadius
197
- ), i.drawPolygon(
198
- {
199
- x: e.x + e.w / 2 - 10,
200
- y: e.y + e.h
201
- },
202
- {
203
- x: e.x + e.w / 2 + 10,
204
- y: e.y + e.h
205
- },
206
- {
207
- x: e.x + e.w / 2,
208
- y: e.y + e.h + 10 - 4
209
- }
210
- ), i.endFill(), i;
211
- }
212
- show(t) {
213
- this.text.text = t, this.text.text = this.trim(this.text), this.animated.alpha = 0, this.animated.position.y = this.fadeShift;
214
- const e = this.drawBox(this.text);
215
- this.animated.removeChildren(), this.animated.addChild(e, this.text);
216
- const i = {
217
- alpha: 1,
218
- y: this.animated.y - this.fadeShift
219
- }, n = {
220
- alpha: 0,
221
- y: this.animated.y + this.fadeShift
222
- };
223
- this.hideTween = new T(this.animated).to(n, 500).onComplete(() => {
224
- this.isChecking = !0;
225
- }), this.showTween = new T(this.animated).to(i, 500).onComplete(() => {
226
- this.timer = new f(1e4, () => {
227
- var a;
228
- (a = this.hideTween) == null || a.start();
229
- });
230
- }), this.showTween.start();
231
- }
232
- }
233
- class B {
234
- constructor() {
235
- s(this, "text", new c.Text(void 0, {
236
- fontFamily: "Rubik",
237
- fontSize: 18,
238
- fill: 16777215,
239
- align: "center",
240
- lineJoin: "round",
241
- strokeThickness: 4,
242
- stroke: "black"
243
- }));
244
- s(this, "rect", () => ({
245
- x: this.text.x,
246
- y: this.text.y,
247
- w: this.text.width,
248
- h: this.text.height
249
- }));
250
- this.text.anchor.set(0.5, 1), this.text.zIndex = 100;
251
- }
252
- update(t) {
253
- this.text.text = t.name ?? "", this.text.visible = t.isVisible, this.text.position.x = t.position.x ?? this.text.position.x, this.text.position.y = t.position.y ?? this.text.position.y;
254
- }
255
- }
256
- class Y {
257
- constructor(t) {
258
- s(this, "container", new c.Container());
259
- s(this, "sprites");
260
- this.sprites = t, this.container.sortableChildren = !0;
261
- let e = 0;
262
- for (const i in this.sprites) {
263
- const n = this.sprites[i];
264
- this.container.addChild(n), n.zIndex = ++e, n.anchor.set(0.5, 0), n.autoUpdate = !1, n.play();
265
- }
266
- }
267
- update(t) {
268
- for (const e in this.sprites) {
269
- const i = this.sprites[e];
270
- if (t.play && i.update(m * 0.06), t.color) {
271
- for (const n in t.color)
272
- if (n == e) {
273
- const a = t.color[n];
274
- a && (i.alpha = a.alpha, i.tint = a);
275
- }
276
- }
277
- t.scale.x && (i.animationSpeed = 4 / Math.abs(t.scale.x));
278
- }
279
- this.container.scale.set(t.scale.x, t.scale.y);
280
- }
281
- }
282
- class Q {
283
- constructor() {
284
- s(this, "container", new c.Container());
285
- s(this, "timer");
286
- s(this, "sprite");
287
- s(this, "opacityStep", 0.1);
288
- s(this, "shadowStep", 50);
289
- l.stage.addChild(this.container);
290
- }
291
- setSprite(t) {
292
- this.sprite = t;
293
- }
294
- update(t) {
295
- for (const e of this.container.children)
296
- e.alpha > 0 && (e.alpha = e.alpha - this.opacityStep);
297
- if (this.timer && !this.timer.isCompleted)
298
- this.timer.tick();
299
- else if (t.play) {
300
- this.timer = new f(this.shadowStep);
301
- const e = new c.Container();
302
- if (this.sprite)
303
- for (const i in this.sprite.sprites) {
304
- const n = this.sprite.sprites[i], a = n.texture.clone(), o = new c.Sprite(a);
305
- o.anchor.set(0.5, 0), o.alpha = n.alpha, o.tint = n.tint, e.addChild(o), e.scale.set(
306
- this.sprite.container.scale.x,
307
- this.sprite.container.scale.y
308
- ), e.pivot.set(
309
- this.sprite.container.pivot.x,
310
- this.sprite.container.pivot.y
311
- );
312
- const h = this.sprite.container.getGlobalPosition();
313
- e.position.set(h.x, h.y);
314
- }
315
- this.container.addChild(e);
316
- }
317
- }
318
- }
319
- class y {
320
- constructor() {
321
- s(this, "container", new c.Container());
322
- s(this, "userState", {});
323
- s(this, "state", {
324
- name: "",
325
- sprite: "",
326
- color: new c.Color("#969696"),
327
- direction: 1,
328
- scale: 1,
329
- isAnonymous: !1,
330
- zIndex: 0
331
- });
332
- s(this, "animationState", d.Idle);
333
- // TODO: Refactor sprite initializing, move logic to separate file
334
- s(this, "sprite");
335
- s(this, "spriteData", {
336
- name: "evotar",
337
- collider: {
338
- x: 0,
339
- y: 0,
340
- w: 0,
341
- h: 0
342
- },
343
- size: { w: 0, h: 0 },
344
- scale: 1,
345
- flip: !1,
346
- colored: []
347
- });
348
- s(this, "animatedSprites");
349
- s(this, "trail", new Q());
350
- s(this, "name", new B());
351
- s(this, "message", new G(() => {
352
- this.state.zIndex = w.zIndexEvotarMax(this.container.zIndex);
353
- }));
354
- s(this, "emoteSpitter", new _());
355
- s(this, "velocity", {
356
- x: 0,
357
- y: 0
358
- });
359
- s(this, "runSpeed", 0.05);
360
- s(this, "gravity", 0.2);
361
- s(this, "dashAcc", 3);
362
- s(this, "isJumping", !1);
363
- s(this, "isDashing", !1);
364
- s(this, "isDespawned", !1);
365
- s(this, "landTimer");
366
- s(this, "stateTimer");
367
- s(this, "scaleTimer");
368
- s(this, "spawnTween");
369
- s(this, "fadeTween");
370
- s(this, "scaleTween");
371
- this.container.addChild(this.name.text), this.container.addChild(this.emoteSpitter.container), this.container.addChild(this.message.container), this.container.sortableChildren = !0;
372
- }
373
- spawn(t = { isFalling: !1 }) {
374
- const e = this.spriteData.collider, i = -(e.y + e.h - this.spriteData.size.h / 2), n = t.isFalling ? i : l.renderer.height, a = this.spriteData.size.w * this.state.scale * this.spriteData.scale, o = t.positionX != null ? t.positionX * l.renderer.width : Math.random() * (l.renderer.width - a) + a / 2, h = n * this.state.scale * this.spriteData.scale;
375
- if (this.container.x = o, this.container.y = h, this.state.direction = Math.random() > 0.5 ? 1 : -1, !t.isFalling) {
376
- const p = w.zIndexEvotarMin(this.container.zIndex);
377
- this.state.zIndex = p, this.container.alpha = 0, this.spawnTween = new g.Tween(this.container).to({ alpha: 1 }, 500).onComplete(t.onComplete).start();
378
- }
379
- this.stateTimer = new f(5e3, () => {
380
- this.isJumping || this.setAnimationState(d.Run);
381
- });
382
- }
383
- despawn(t = {}) {
384
- this.fadeTween && this.fadeTween.isPlaying() || (this.fadeTween = new g.Tween(this.container).to({ alpha: 0 }, 1e3).onComplete(() => {
385
- this.isDespawned = !0, t.onComplete && t.onComplete();
386
- }).start());
387
- }
388
- scale(t) {
389
- this.scaleTween && this.scaleTween.isPlaying() || this.scaleTimer && !this.scaleTimer.isCompleted || (this.scaleTween = new g.Tween(this).to({ state: { scale: t.value ?? 2 } }, 2e3).onComplete(() => {
390
- this.scaleTimer = new f((t.duration ?? 10) * 1e3, () => {
391
- this.scaleTween = new g.Tween(this).to({ state: { scale: 1 } }, 2e3).start();
392
- });
393
- }).start());
394
- }
395
- jump(t) {
396
- this.isDespawned || this.isJumping || (this.isJumping = !0, this.velocity.x = this.state.direction * (t.velocityX ?? 3.5), this.velocity.y = t.velocityY ?? -8, this.setAnimationState(d.Jump), A.play("jump"));
397
- }
398
- dash(t) {
399
- this.isDespawned || this.isDashing || (this.isDashing = !0, this.velocity.x = this.state.direction * (t.force ?? 14));
400
- }
401
- async setSprite(t) {
402
- if (t && t != this.state.sprite) {
403
- const e = await S.getSpriteData(t);
404
- if (!e)
405
- return;
406
- this.state.sprite = t, this.spriteData = e.data, this.animatedSprites = S.getAnimatedSprites(t), this.setAnimationState(this.animationState, !0);
407
- }
408
- }
409
- async setProps(t) {
410
- t.sprite && await this.setSprite(t.sprite), this.state = { ...this.state, ...t };
411
- }
412
- setUserProps(t) {
413
- this.userState = { ...this.userState, ...t };
414
- }
415
- move() {
416
- var i;
417
- const t = this.spriteData.collider, e = {
418
- x: this.container.position.x,
419
- y: this.container.position.y
420
- };
421
- if (this.isDashing) {
422
- const n = Math.sign(this.velocity.x);
423
- this.velocity.x = this.velocity.x - n * this.dashAcc / Math.abs(this.velocity.x), n != Math.sign(this.velocity.x) ? (this.isDashing = !1, this.velocity.x = 0) : e.x += this.velocity.x;
424
- } else {
425
- if ((i = this.stateTimer) != null && i.isCompleted && (this.stateTimer = new f(Math.random() * 5e3, () => {
426
- this.animationState == d.Idle ? this.setAnimationState(d.Run) : this.animationState == d.Run && this.setAnimationState(d.Idle);
427
- })), this.animationState == d.Run) {
428
- const n = this.runSpeed * this.state.direction;
429
- this.velocity.x = n * m;
430
- }
431
- this.velocity.y = this.velocity.y + this.gravity, e.x += this.velocity.x, e.y += this.velocity.y, this.isOnGround(e.y) && (this.velocity.y = 0, this.velocity.x = 0, e.y = l.renderer.height, this.animationState == d.Fall && (this.setAnimationState(d.Land), this.landTimer = new f(200, () => {
432
- this.setAnimationState(d.Idle), this.isJumping = !1;
433
- }))), this.velocity.y > 0 && this.setAnimationState(d.Fall);
434
- }
435
- if (this.animationState != d.Idle || this.isDashing) {
436
- const n = t.w / 2 * this.state.scale * this.spriteData.scale, a = this.container.x - n < 0, o = this.container.x + n > l.renderer.width;
437
- (a || o) && (this.state.direction = -this.state.direction, this.velocity.x = -this.velocity.x, a && (e.x = n), o && (e.x = l.renderer.width - n));
438
- }
439
- this.container.position.set(e.x, e.y);
440
- }
441
- update() {
442
- var e, i, n, a, o, h;
443
- if (this.isDespawned)
444
- return;
445
- (e = this.landTimer) == null || e.tick(), (i = this.stateTimer) == null || i.tick(), (n = this.scaleTimer) == null || n.tick(), (a = this.fadeTween) == null || a.update(), (o = this.spawnTween) == null || o.update(), (h = this.scaleTween) == null || h.update(), this.container.zIndex = this.state.zIndex;
446
- const t = this.spriteData.collider;
447
- if (this.name.update({
448
- name: this.state.name,
449
- isVisible: !this.state.isAnonymous,
450
- position: {
451
- y: -t.h * this.state.scale * this.spriteData.scale
452
- }
453
- }), this.message.update({
454
- position: {
455
- y: this.name.text.position.y - this.name.text.height - 6
456
- }
457
- }), this.emoteSpitter.update({
458
- position: {
459
- y: this.message.container.position.y - this.message.container.height
460
- }
461
- }), this.move(), this.sprite) {
462
- const p = Object.fromEntries(
463
- this.spriteData.colored.map((u) => [
464
- u,
465
- this.userState.color ? this.userState.color : this.state.color
466
- ])
467
- ), x = this.spriteData.flip ? this.state.direction : 1;
468
- this.sprite.update({
469
- color: p,
470
- scale: {
471
- x: x * this.state.scale * this.spriteData.scale,
472
- y: this.state.scale * this.spriteData.scale
473
- },
474
- play: !this.isDashing
475
- });
476
- }
477
- this.trail.update({
478
- play: this.isDashing
479
- });
480
- }
481
- isOnGround(t) {
482
- return t > l.renderer.height;
483
- }
484
- addMessage(t) {
485
- var e;
486
- this.message.add(t), this.state.isAnonymous = !1, (e = this.fadeTween) == null || e.stop(), this.fadeTween = void 0, this.container.alpha = 1;
487
- }
488
- spitEmotes(t) {
489
- for (const e of t)
490
- this.emoteSpitter.add(e);
491
- }
492
- async setAnimationState(t, e = !1) {
493
- if (this.animationState == t && !e || !this.animatedSprites)
494
- return;
495
- this.animationState = t, this.sprite && this.container.removeChild(this.sprite.container);
496
- const i = this.animatedSprites[this.animationState] ?? this.animatedSprites[d.Idle];
497
- this.sprite = new Y(i), this.trail.setSprite(this.sprite), this.sprite.container.pivot.set(
498
- 0,
499
- this.spriteData.collider.y + this.spriteData.collider.h
500
- ), this.container.addChild(this.sprite.container);
501
- }
502
- }
503
- const q = 1e3 * 60 * 5;
504
- class H {
505
- constructor() {
506
- s(this, "viewers", {});
507
- s(this, "raiders", []);
508
- s(this, "lastEvotarActivity", {});
509
- s(this, "subscriptions", []);
510
- s(this, "hasActivity", (t) => !!this.lastEvotarActivity[t]);
511
- }
512
- subscribe(t) {
513
- this.subscriptions.push(t);
514
- }
515
- update() {
516
- for (const t in this.viewers) {
517
- const e = this.viewers[t];
518
- e && e.update();
519
- }
520
- for (const t of this.raiders)
521
- t.update();
522
- }
523
- processRaid(t) {
524
- if (!l.settings.fallingRaiders)
525
- return;
526
- const e = 1 / t.viewers * 5e3;
527
- for (let o = 0; o < t.viewers; o++) {
528
- const h = new y();
529
- h.setProps({
530
- isAnonymous: !0,
531
- zIndex: -1,
532
- sprite: "agent",
533
- color: new c.Color(t.broadcaster.info.color)
534
- }), v.add(e * o, () => {
535
- this.addRaider(h), h.spawn({ isFalling: !0 });
536
- }), v.add(o * e + 3e4, () => {
537
- h.despawn({
538
- onComplete: () => {
539
- this.deleteRaider(h);
540
- }
541
- });
542
- });
543
- }
544
- const i = this.viewers[t.broadcaster.id], n = {
545
- ...this.prepareEvotarProps(
546
- t.broadcaster.info.displayName,
547
- t.broadcaster.info.color
548
- ),
549
- scale: 8
550
- }, a = () => {
551
- const o = new y();
552
- o.setProps(n), this.addViewer(t.broadcaster.id, o), o.spawn({ isFalling: !0, positionX: 0.5 });
553
- };
554
- this.lastEvotarActivity[t.broadcaster.id] = performance.now(), i ? i.despawn({
555
- onComplete: () => {
556
- a();
557
- }
558
- }) : a();
559
- }
560
- async processChatters(t) {
561
- for (const e in this.viewers) {
562
- const i = this.lastEvotarActivity[e], n = !!i && performance.now() - i < q;
563
- if (t.every((a) => a.userId != e) && !n) {
564
- const a = this.viewers[e];
565
- a && a.despawn({
566
- onComplete: () => {
567
- this.deleteViewer(e, a);
568
- }
569
- });
570
- }
571
- }
572
- if (l.settings.showAnonymousEvotars) {
573
- for (const e of t)
574
- if (!this.viewers[e.userId]) {
575
- const n = {
576
- name: e.name,
577
- isAnonymous: !this.hasActivity(e.userId)
578
- }, a = new y();
579
- await a.setProps(n), a.spawn(), this.addViewer(e.userId, a);
580
- }
581
- }
582
- }
583
- doAction(t, e) {
584
- if (j(t) && e.jump({
585
- velocityX: t.data.velocityX,
586
- velocityY: t.data.velocityY,
587
- cooldown: t.cooldown
588
- }), O(t) && e.dash({ force: t.data.force, cooldown: t.cooldown }), J(t)) {
589
- const i = F(t.data.color);
590
- i && i.isValid() && e.setUserProps({ color: new c.Color(t.data.color) });
591
- }
592
- W(t) && e.scale({
593
- value: t.data.scale,
594
- duration: t.data.duration,
595
- cooldown: t.cooldown
596
- }), U(t) && e.setSprite(t.data.sprite);
597
- }
598
- prepareEvotarProps(t, e, i) {
599
- const n = {
600
- name: t,
601
- isAnonymous: !1
602
- };
603
- if (i && (n.sprite = i), e)
604
- try {
605
- n.color = new c.Color(e);
606
- } catch {
607
- }
608
- return n;
609
- }
610
- async processAction(t) {
611
- const e = this.viewers[t.userId], i = this.prepareEvotarProps(
612
- t.info.displayName,
613
- t.info.color,
614
- t.info.sprite
615
- );
616
- if (e)
617
- await e.setProps(i), this.doAction(t, e);
618
- else {
619
- const n = new y();
620
- await n.setProps(i), n.spawn({
621
- onComplete: () => {
622
- const a = this.viewers[t.userId];
623
- a && this.doAction(t, a);
624
- }
625
- }), this.addViewer(t.userId, n);
626
- }
627
- }
628
- async processMessage(t) {
629
- const e = this.prepareEvotarProps(
630
- t.info.displayName,
631
- t.info.color,
632
- t.info.sprite
633
- );
634
- let i = this.viewers[t.userId];
635
- if (i)
636
- i.setProps(e);
637
- else {
638
- const n = l.settings.fallingEvotars ? !this.hasActivity(t.userId) : !1;
639
- i = new y(), await i.setProps(e), i.spawn({ isFalling: n }), this.addViewer(t.userId, i);
640
- }
641
- this.lastEvotarActivity[t.userId] = performance.now(), t.message && i.addMessage(t.message), t.emotes.length > 0 && i.spitEmotes(t.emotes);
642
- }
643
- addViewer(t, e) {
644
- this.subscriptions.forEach((i) => i.onAdd(e)), this.viewers[t] = e;
645
- }
646
- deleteViewer(t, e) {
647
- this.subscriptions.forEach((i) => i.onDelete(e)), delete this.viewers[t];
648
- }
649
- addRaider(t) {
650
- this.subscriptions.forEach((e) => e.onAdd(t)), this.raiders.push(t);
651
- }
652
- deleteRaider(t) {
653
- this.subscriptions.forEach((e) => e.onDelete(t)), this.raiders = this.raiders.filter((e) => e != t);
654
- }
655
- zIndexEvotarMax(t) {
656
- return Object.values(this.viewers).reduce((e, i) => i && e <= i.container.zIndex ? i.container.zIndex + 1 : e, t);
657
- }
658
- zIndexEvotarMin(t) {
659
- return Object.values(this.viewers).reduce((e, i) => i && e >= i.container.zIndex ? i.container.zIndex - 1 : e, t);
660
- }
661
- }
662
- const w = new H();
663
- class K {
664
- constructor() {
665
- s(this, "stage", new c.Container());
666
- s(this, "evotars", {});
667
- s(this, "chatterIds", []);
668
- s(this, "settings", {});
669
- s(this, "renderer");
670
- }
671
- async initialize(t, e) {
672
- S.initialize(e.spriteLoaderFn), A.initialize(e.sounds), this.renderer = new c.Renderer({
673
- width: t.clientWidth,
674
- height: t.clientHeight,
675
- backgroundAlpha: 0
676
- }), t.appendChild(this.renderer.view), window.onresize = () => {
677
- this.renderer.resize(t.clientWidth, t.clientHeight);
678
- }, this.stage.sortableChildren = !0, w.subscribe({
679
- onAdd: (i) => this.stage.addChild(i.container),
680
- onDelete: (i) => {
681
- this.stage.removeChild(i.container), this.stage.removeChild(i.trail.container);
682
- }
683
- });
684
- }
685
- updateSettings(t) {
686
- this.settings = t;
687
- }
688
- update() {
689
- v.tick(), w.update(), this.renderer.render(this.stage);
690
- }
691
- }
692
- const l = new K();
693
- class st {
694
- constructor(t, e) {
695
- s(this, "isRendered", !1);
696
- s(this, "processMessage", (t) => w.processMessage(t));
697
- s(this, "processAction", (t) => w.processAction(t));
698
- s(this, "processChatters", (t) => w.processChatters(t));
699
- s(this, "processRaid", (t) => w.processRaid(t));
700
- this.root = t, this.options = e;
701
- }
702
- updateSettings(t) {
703
- l.updateSettings(t);
704
- }
705
- async run() {
706
- if (this.isRendered)
707
- return;
708
- this.isRendered = !0, await (async () => {
709
- await l.initialize(this.root, this.options), performance.now();
710
- let e = -1;
711
- const n = 1e3 / 60;
712
- requestAnimationFrame(a);
713
- function a(o = performance.now()) {
714
- const h = o - e | 0;
715
- h > n && (e = o - h % n, l.update()), requestAnimationFrame(a);
716
- }
717
- })();
718
- }
719
- }
720
- export {
721
- st as Evotars
722
- };