evotars 0.0.6 → 0.0.8
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.
- package/dist/entities/EmoteSpitter.d.ts +1 -1
- package/dist/evotars.js +53 -36
- package/dist/evotars.umd.cjs +1 -1
- package/package.json +4 -1
package/dist/evotars.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
var
|
|
2
|
-
var z = (r, t, e) => t in r ?
|
|
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
3
|
var s = (r, t, e) => (z(r, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
4
|
import * as c from "pixi.js";
|
|
5
|
-
import { Assets as R, Spritesheet as
|
|
5
|
+
import { Assets as R, Spritesheet as k, AnimatedSprite as M, SCALE_MODES as P } from "pixi.js";
|
|
6
6
|
import F from "tinycolor2";
|
|
7
7
|
import * as g from "@tweenjs/tween.js";
|
|
8
8
|
import { Tween as T } from "@tweenjs/tween.js";
|
|
9
|
+
import { AnimatedGIF as V } from "@pixi/gif";
|
|
9
10
|
const m = 0.02 * 1e3;
|
|
10
11
|
class f {
|
|
11
12
|
constructor(t = 0, e) {
|
|
@@ -17,7 +18,7 @@ class f {
|
|
|
17
18
|
this.isCompleted || (this.current -= t, this.current < 0 && (this.isCompleted = !0, (e = this.onComplete) == null || e.call(this)));
|
|
18
19
|
}
|
|
19
20
|
}
|
|
20
|
-
class
|
|
21
|
+
class L {
|
|
21
22
|
constructor() {
|
|
22
23
|
s(this, "timers", []);
|
|
23
24
|
}
|
|
@@ -31,8 +32,8 @@ class V {
|
|
|
31
32
|
this.timers = e;
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
|
-
const v = new
|
|
35
|
-
class
|
|
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 {
|
|
36
37
|
constructor() {
|
|
37
38
|
s(this, "assets", {});
|
|
38
39
|
}
|
|
@@ -46,9 +47,9 @@ class U {
|
|
|
46
47
|
e && (e.volume = 0.2, e.pause(), e.currentTime = 0, e.play());
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
|
-
const A = new
|
|
50
|
+
const A = new X();
|
|
50
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 || {});
|
|
51
|
-
class
|
|
52
|
+
class N {
|
|
52
53
|
constructor() {
|
|
53
54
|
s(this, "spriteLoaderFn");
|
|
54
55
|
s(this, "sprites", {});
|
|
@@ -62,7 +63,7 @@ class X {
|
|
|
62
63
|
return;
|
|
63
64
|
if (this.sprites[t])
|
|
64
65
|
return this.sprites[t];
|
|
65
|
-
const i = await R.load(e.image), n = new
|
|
66
|
+
const i = await R.load(e.image), n = new k({
|
|
66
67
|
texture: i,
|
|
67
68
|
data: e.sprite
|
|
68
69
|
});
|
|
@@ -87,19 +88,19 @@ class X {
|
|
|
87
88
|
for (const h of i.data.meta.layers) {
|
|
88
89
|
const p = [];
|
|
89
90
|
for (let u = a.from; u <= a.to; u++) {
|
|
90
|
-
const
|
|
91
|
-
p.push({ texture:
|
|
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 });
|
|
92
93
|
}
|
|
93
|
-
const x = new
|
|
94
|
-
x.texture.baseTexture.scaleMode =
|
|
94
|
+
const x = new M(p);
|
|
95
|
+
x.texture.baseTexture.scaleMode = P.NEAREST, o[h.name] = x;
|
|
95
96
|
}
|
|
96
97
|
a.name && (n[a.name] = o);
|
|
97
98
|
}
|
|
98
99
|
return n;
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
|
-
const S = new
|
|
102
|
-
class
|
|
102
|
+
const S = new N();
|
|
103
|
+
class _ {
|
|
103
104
|
constructor() {
|
|
104
105
|
s(this, "container", new c.Container());
|
|
105
106
|
s(this, "emotes", []);
|
|
@@ -108,9 +109,25 @@ class N {
|
|
|
108
109
|
s(this, "scaleSpeed", 0.5);
|
|
109
110
|
s(this, "timer");
|
|
110
111
|
}
|
|
111
|
-
add(t) {
|
|
112
|
-
const e =
|
|
113
|
-
|
|
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);
|
|
114
131
|
}
|
|
115
132
|
update(t) {
|
|
116
133
|
var e;
|
|
@@ -122,7 +139,7 @@ class N {
|
|
|
122
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);
|
|
123
140
|
}
|
|
124
141
|
}
|
|
125
|
-
class
|
|
142
|
+
class G {
|
|
126
143
|
constructor(t) {
|
|
127
144
|
s(this, "container", new c.Container());
|
|
128
145
|
s(this, "animated", new c.Container());
|
|
@@ -213,7 +230,7 @@ class _ {
|
|
|
213
230
|
}), this.showTween.start();
|
|
214
231
|
}
|
|
215
232
|
}
|
|
216
|
-
class
|
|
233
|
+
class B {
|
|
217
234
|
constructor() {
|
|
218
235
|
s(this, "text", new c.Text(void 0, {
|
|
219
236
|
fontFamily: "Rubik",
|
|
@@ -330,11 +347,11 @@ class y {
|
|
|
330
347
|
});
|
|
331
348
|
s(this, "animatedSprites");
|
|
332
349
|
s(this, "trail", new Q());
|
|
333
|
-
s(this, "name", new
|
|
334
|
-
s(this, "message", new
|
|
350
|
+
s(this, "name", new B());
|
|
351
|
+
s(this, "message", new G(() => {
|
|
335
352
|
this.state.zIndex = w.zIndexEvotarMax(this.container.zIndex);
|
|
336
353
|
}));
|
|
337
|
-
s(this, "emoteSpitter", new
|
|
354
|
+
s(this, "emoteSpitter", new _());
|
|
338
355
|
s(this, "velocity", {
|
|
339
356
|
x: 0,
|
|
340
357
|
y: 0
|
|
@@ -386,7 +403,7 @@ class y {
|
|
|
386
403
|
const e = await S.getSpriteData(t);
|
|
387
404
|
if (!e)
|
|
388
405
|
return;
|
|
389
|
-
this.spriteData = e.data, this.animatedSprites = S.getAnimatedSprites(t), this.setAnimationState(this.animationState, !0);
|
|
406
|
+
this.state.sprite = t, this.spriteData = e.data, this.animatedSprites = S.getAnimatedSprites(t), this.setAnimationState(this.animationState, !0);
|
|
390
407
|
}
|
|
391
408
|
}
|
|
392
409
|
async setProps(t) {
|
|
@@ -483,8 +500,8 @@ class y {
|
|
|
483
500
|
), this.container.addChild(this.sprite.container);
|
|
484
501
|
}
|
|
485
502
|
}
|
|
486
|
-
const
|
|
487
|
-
class
|
|
503
|
+
const q = 1e3 * 60 * 5;
|
|
504
|
+
class H {
|
|
488
505
|
constructor() {
|
|
489
506
|
s(this, "viewers", {});
|
|
490
507
|
s(this, "raiders", []);
|
|
@@ -542,7 +559,7 @@ class q {
|
|
|
542
559
|
}
|
|
543
560
|
async processChatters(t) {
|
|
544
561
|
for (const e in this.viewers) {
|
|
545
|
-
const i = this.lastEvotarActivity[e], n = !!i && performance.now() - i <
|
|
562
|
+
const i = this.lastEvotarActivity[e], n = !!i && performance.now() - i < q;
|
|
546
563
|
if (t.every((a) => a.userId != e) && !n) {
|
|
547
564
|
const a = this.viewers[e];
|
|
548
565
|
a && a.despawn({
|
|
@@ -564,19 +581,19 @@ class q {
|
|
|
564
581
|
}
|
|
565
582
|
}
|
|
566
583
|
doAction(t, e) {
|
|
567
|
-
if (
|
|
584
|
+
if (j(t) && e.jump({
|
|
568
585
|
velocityX: t.data.velocityX,
|
|
569
586
|
velocityY: t.data.velocityY,
|
|
570
587
|
cooldown: t.cooldown
|
|
571
|
-
}),
|
|
588
|
+
}), O(t) && e.dash({ force: t.data.force, cooldown: t.cooldown }), J(t)) {
|
|
572
589
|
const i = F(t.data.color);
|
|
573
590
|
i && i.isValid() && e.setUserProps({ color: new c.Color(t.data.color) });
|
|
574
591
|
}
|
|
575
|
-
|
|
592
|
+
W(t) && e.scale({
|
|
576
593
|
value: t.data.scale,
|
|
577
594
|
duration: t.data.duration,
|
|
578
595
|
cooldown: t.cooldown
|
|
579
|
-
}),
|
|
596
|
+
}), U(t) && e.setSprite(t.data.sprite);
|
|
580
597
|
}
|
|
581
598
|
prepareEvotarProps(t, e, i) {
|
|
582
599
|
const n = {
|
|
@@ -642,8 +659,8 @@ class q {
|
|
|
642
659
|
return Object.values(this.viewers).reduce((e, i) => i && e >= i.container.zIndex ? i.container.zIndex - 1 : e, t);
|
|
643
660
|
}
|
|
644
661
|
}
|
|
645
|
-
const w = new
|
|
646
|
-
class
|
|
662
|
+
const w = new H();
|
|
663
|
+
class K {
|
|
647
664
|
constructor() {
|
|
648
665
|
s(this, "stage", new c.Container());
|
|
649
666
|
s(this, "evotars", {});
|
|
@@ -672,8 +689,8 @@ class H {
|
|
|
672
689
|
v.tick(), w.update(), this.renderer.render(this.stage);
|
|
673
690
|
}
|
|
674
691
|
}
|
|
675
|
-
const l = new
|
|
676
|
-
class
|
|
692
|
+
const l = new K();
|
|
693
|
+
class st {
|
|
677
694
|
constructor(t, e) {
|
|
678
695
|
s(this, "isRendered", !1);
|
|
679
696
|
s(this, "processMessage", (t) => w.processMessage(t));
|
|
@@ -701,5 +718,5 @@ class et {
|
|
|
701
718
|
}
|
|
702
719
|
}
|
|
703
720
|
export {
|
|
704
|
-
|
|
721
|
+
st as Evotars
|
|
705
722
|
};
|
package/dist/evotars.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(l,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("pixi.js"),require("tinycolor2"),require("@tweenjs/tween.js")):typeof define=="function"&&define.amd?define(["exports","pixi.js","tinycolor2","@tweenjs/tween.js"],d):(l=typeof globalThis<"u"?globalThis:l||self,d(l.evotars={},l.PIXI,l.tinycolor,l.TWEEN))})(this,function(l,d,x,T){"use strict";var B=Object.defineProperty;var H=(l,d,x)=>d in l?B(l,d,{enumerable:!0,configurable:!0,writable:!0,value:x}):l[d]=x;var s=(l,d,x)=>(H(l,typeof d!="symbol"?d+"":d,x),x);function E(r){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const e in r)if(e!=="default"){const i=Object.getOwnPropertyDescriptor(r,e);Object.defineProperty(t,e,i.get?i:{enumerable:!0,get:()=>r[e]})}}return t.default=r,Object.freeze(t)}const c=E(d),S=E(T),f=.02*1e3;class w{constructor(t=0,e){s(this,"isCompleted",!1);this.current=t,this.onComplete=e}tick(t=f){var e;this.isCompleted||(this.current-=t,this.current<0&&(this.isCompleted=!0,(e=this.onComplete)==null||e.call(this)))}}class z{constructor(){s(this,"timers",[])}add(t,e){this.timers.push(new w(t,e))}tick(t=f){const e=[];for(const i of this.timers)i.isCompleted||(i.tick(t),e.push(i));this.timers=e}}const A=new z,M=r=>r.name=="jump",R=r=>r.name=="color",k=r=>r.name=="grow",P=r=>r.name=="dash",F=r=>r.name=="sprite";class j{constructor(){s(this,"assets",{})}async initialize(t){this.assets=Object.fromEntries(Array.from(Object.entries(t??{})).map(([e,i])=>[e,new Audio(i.src)]))}play(t){const e=this.assets[t];e&&(e.volume=.2,e.pause(),e.currentTime=0,e.play())}}const D=new j;var p=(r=>(r.Idle="Idle",r.Jump="Jump",r.Fall="Fall",r.Land="Land",r.Run="Run",r.Die="Die",r))(p||{});class O{constructor(){s(this,"spriteLoaderFn");s(this,"sprites",{})}initialize(t){this.spriteLoaderFn=t}async getSpriteData(t){const e=await this.spriteLoaderFn(t);if(!e)return;if(this.sprites[t])return this.sprites[t];const i=await d.Assets.load(e.image),n=new d.Spritesheet({texture:i,data:e.sprite});return await n.parse(),this.sprites[t]={sheet:n,data:{...e.data,scale:e.data.scale??1}},this.sprites[t]}getAnimatedSprites(t){if(!t)throw Error("Sheet is not defined");if(!this.sprites[t])throw Error("Sprite is not loaded");const e=this.sprites[t].data,i=this.sprites[t].sheet,n={};for(const a of i.data.meta.frameTags??[]){if(!i.data.meta.layers)throw Error("Layers are not defined");const o={};for(const h of i.data.meta.layers){const u=[];for(let v=a.from;v<=a.to;v++){const X=v.toString(),I=e.name+"_"+h.name+"_"+X,Y=i.textures[I],Q=i.data.frames[I].duration;u.push({texture:Y,time:Q})}const C=new d.AnimatedSprite(u);C.texture.baseTexture.scaleMode=d.SCALE_MODES.NEAREST,o[h.name]=C}a.name&&(n[a.name]=o)}return n}}const b=new O;class V{constructor(){s(this,"container",new c.Container);s(this,"emotes",[]);s(this,"moveSpeed",50);s(this,"alphaSpeed",1);s(this,"scaleSpeed",.5);s(this,"timer")}add(t){const e=c.Sprite.from(t);e.anchor.set(.5,.5),e.scale.set(0,0),this.emotes.push(e)}update(t){var e;(e=this.timer)==null||e.tick(),(!this.timer||this.timer.isCompleted)&&this.emotes.length>0&&(this.timer=new w(2e3,()=>{const i=this.emotes.shift();i&&this.container.addChild(i)})),this.container.position.x=t.position.x??this.container.position.x,this.container.position.y=t.position.y??this.container.position.y;for(const i of this.container.children)i.position.y-=f*this.moveSpeed/1e3,i.scale.x+=f*this.scaleSpeed/1e3,i.scale.y+=f*this.scaleSpeed/1e3,i.scale.x>1&&(i.alpha-=f*this.alphaSpeed/1e3),i.alpha<=0&&this.container.removeChild(i)}}class _{constructor(t){s(this,"container",new c.Container);s(this,"animated",new c.Container);s(this,"text",new c.Text(void 0,{fontFamily:"Rubik",fontSize:20,fill:2236962,align:"left",breakWords:!0,wordWrap:!0,wordWrapWidth:200}));s(this,"padding",10);s(this,"borderRadius",10);s(this,"boxColor",15658734);s(this,"fadeShift",10);s(this,"isChecking",!0);s(this,"messageQueue",[]);s(this,"showTween");s(this,"hideTween");s(this,"timer");this.beforeShow=t,this.container.addChild(this.animated),this.text.anchor.set(.5,1),this.text.position.set(0,-this.padding)}trim(t){const e=c.TextMetrics.measureText(t.text,t.style);return e.lines.length>4?e.lines.slice(0,4).join(" ").slice(0,-3)+"...":t.text}update(t){var i,n,a,o,h;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){this.isChecking=!1;const u=this.messageQueue.shift();u&&(this.beforeShow(),this.show(u))}this.container.position.x=t.position.x??this.container.position.x,this.container.position.y=t.position.y??this.container.position.y;const e=((o=this.showTween)==null?void 0:o.isPlaying())||((h=this.hideTween)==null?void 0:h.isPlaying());!this.isChecking&&!e&&this.timer&&(this.animated.position.y=Math.sin(this.timer.current*.0025)*4-2)}add(t){this.messageQueue.push(t)}drawBox(t){const e={x:t.x-this.padding-t.width*t.anchor.x,y:t.y-this.padding-t.height*t.anchor.y,w:t.width+this.padding*2,h:t.height+this.padding*2},i=new c.Graphics;return i.beginFill(this.boxColor),i.drawRoundedRect(e.x,e.y,e.w,e.h,this.borderRadius),i.drawPolygon({x:e.x+e.w/2-10,y:e.y+e.h},{x:e.x+e.w/2+10,y:e.y+e.h},{x:e.x+e.w/2,y:e.y+e.h+10-4}),i.endFill(),i}show(t){this.text.text=t,this.text.text=this.trim(this.text),this.animated.alpha=0,this.animated.position.y=this.fadeShift;const e=this.drawBox(this.text);this.animated.removeChildren(),this.animated.addChild(e,this.text);const i={alpha:1,y:this.animated.y-this.fadeShift},n={alpha:0,y:this.animated.y+this.fadeShift};this.hideTween=new T.Tween(this.animated).to(n,500).onComplete(()=>{this.isChecking=!0}),this.showTween=new T.Tween(this.animated).to(i,500).onComplete(()=>{this.timer=new w(1e4,()=>{var a;(a=this.hideTween)==null||a.start()})}),this.showTween.start()}}class L{constructor(){s(this,"text",new c.Text(void 0,{fontFamily:"Rubik",fontSize:18,fill:16777215,align:"center",lineJoin:"round",strokeThickness:4,stroke:"black"}));s(this,"rect",()=>({x:this.text.x,y:this.text.y,w:this.text.width,h:this.text.height}));this.text.anchor.set(.5,1),this.text.zIndex=100}update(t){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}}class J{constructor(t){s(this,"container",new c.Container);s(this,"sprites");this.sprites=t,this.container.sortableChildren=!0;let e=0;for(const i in this.sprites){const n=this.sprites[i];this.container.addChild(n),n.zIndex=++e,n.anchor.set(.5,0),n.autoUpdate=!1,n.play()}}update(t){for(const e in this.sprites){const i=this.sprites[e];if(t.play&&i.update(f*.06),t.color){for(const n in t.color)if(n==e){const a=t.color[n];a&&(i.alpha=a.alpha,i.tint=a)}}t.scale.x&&(i.animationSpeed=4/Math.abs(t.scale.x))}this.container.scale.set(t.scale.x,t.scale.y)}}class W{constructor(){s(this,"container",new c.Container);s(this,"timer");s(this,"sprite");s(this,"opacityStep",.1);s(this,"shadowStep",50);m.stage.addChild(this.container)}setSprite(t){this.sprite=t}update(t){for(const e of this.container.children)e.alpha>0&&(e.alpha=e.alpha-this.opacityStep);if(this.timer&&!this.timer.isCompleted)this.timer.tick();else if(t.play){this.timer=new w(this.shadowStep);const e=new c.Container;if(this.sprite)for(const i in this.sprite.sprites){const n=this.sprite.sprites[i],a=n.texture.clone(),o=new c.Sprite(a);o.anchor.set(.5,0),o.alpha=n.alpha,o.tint=n.tint,e.addChild(o),e.scale.set(this.sprite.container.scale.x,this.sprite.container.scale.y),e.pivot.set(this.sprite.container.pivot.x,this.sprite.container.pivot.y);const h=this.sprite.container.getGlobalPosition();e.position.set(h.x,h.y)}this.container.addChild(e)}}}class g{constructor(){s(this,"container",new c.Container);s(this,"userState",{});s(this,"state",{name:"",sprite:"",color:new c.Color("#969696"),direction:1,scale:1,isAnonymous:!1,zIndex:0});s(this,"animationState",p.Idle);s(this,"sprite");s(this,"spriteData",{name:"evotar",collider:{x:0,y:0,w:0,h:0},size:{w:0,h:0},scale:1,flip:!1,colored:[]});s(this,"animatedSprites");s(this,"trail",new W);s(this,"name",new L);s(this,"message",new _(()=>{this.state.zIndex=y.zIndexEvotarMax(this.container.zIndex)}));s(this,"emoteSpitter",new V);s(this,"velocity",{x:0,y:0});s(this,"runSpeed",.05);s(this,"gravity",.2);s(this,"dashAcc",3);s(this,"isJumping",!1);s(this,"isDashing",!1);s(this,"isDespawned",!1);s(this,"landTimer");s(this,"stateTimer");s(this,"scaleTimer");s(this,"spawnTween");s(this,"fadeTween");s(this,"scaleTween");this.container.addChild(this.name.text),this.container.addChild(this.emoteSpitter.container),this.container.addChild(this.message.container),this.container.sortableChildren=!0}spawn(t={isFalling:!1}){const e=this.spriteData.collider,i=-(e.y+e.h-this.spriteData.size.h/2),n=t.isFalling?i:m.renderer.height,a=this.spriteData.size.w*this.state.scale*this.spriteData.scale,o=t.positionX!=null?t.positionX*m.renderer.width:Math.random()*(m.renderer.width-a)+a/2,h=n*this.state.scale*this.spriteData.scale;if(this.container.x=o,this.container.y=h,this.state.direction=Math.random()>.5?1:-1,!t.isFalling){const u=y.zIndexEvotarMin(this.container.zIndex);this.state.zIndex=u,this.container.alpha=0,this.spawnTween=new S.Tween(this.container).to({alpha:1},500).onComplete(t.onComplete).start()}this.stateTimer=new w(5e3,()=>{this.isJumping||this.setAnimationState(p.Run)})}despawn(t={}){this.fadeTween&&this.fadeTween.isPlaying()||(this.fadeTween=new S.Tween(this.container).to({alpha:0},1e3).onComplete(()=>{this.isDespawned=!0,t.onComplete&&t.onComplete()}).start())}scale(t){this.scaleTween&&this.scaleTween.isPlaying()||this.scaleTimer&&!this.scaleTimer.isCompleted||(this.scaleTween=new S.Tween(this).to({state:{scale:t.value??2}},2e3).onComplete(()=>{this.scaleTimer=new w((t.duration??10)*1e3,()=>{this.scaleTween=new S.Tween(this).to({state:{scale:1}},2e3).start()})}).start())}jump(t){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(p.Jump),D.play("jump"))}dash(t){this.isDespawned||this.isDashing||(this.isDashing=!0,this.velocity.x=this.state.direction*(t.force??14))}async setSprite(t){if(t&&t!=this.state.sprite){const e=await b.getSpriteData(t);if(!e)return;this.spriteData=e.data,this.animatedSprites=b.getAnimatedSprites(t),this.setAnimationState(this.animationState,!0)}}async setProps(t){t.sprite&&await this.setSprite(t.sprite),this.state={...this.state,...t}}setUserProps(t){this.userState={...this.userState,...t}}move(){var i;const t=this.spriteData.collider,e={x:this.container.position.x,y:this.container.position.y};if(this.isDashing){const n=Math.sign(this.velocity.x);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}else{if((i=this.stateTimer)!=null&&i.isCompleted&&(this.stateTimer=new w(Math.random()*5e3,()=>{this.animationState==p.Idle?this.setAnimationState(p.Run):this.animationState==p.Run&&this.setAnimationState(p.Idle)})),this.animationState==p.Run){const n=this.runSpeed*this.state.direction;this.velocity.x=n*f}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=m.renderer.height,this.animationState==p.Fall&&(this.setAnimationState(p.Land),this.landTimer=new w(200,()=>{this.setAnimationState(p.Idle),this.isJumping=!1}))),this.velocity.y>0&&this.setAnimationState(p.Fall)}if(this.animationState!=p.Idle||this.isDashing){const n=t.w/2*this.state.scale*this.spriteData.scale,a=this.container.x-n<0,o=this.container.x+n>m.renderer.width;(a||o)&&(this.state.direction=-this.state.direction,this.velocity.x=-this.velocity.x,a&&(e.x=n),o&&(e.x=m.renderer.width-n))}this.container.position.set(e.x,e.y)}update(){var e,i,n,a,o,h;if(this.isDespawned)return;(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;const t=this.spriteData.collider;if(this.name.update({name:this.state.name,isVisible:!this.state.isAnonymous,position:{y:-t.h*this.state.scale*this.spriteData.scale}}),this.message.update({position:{y:this.name.text.position.y-this.name.text.height-6}}),this.emoteSpitter.update({position:{y:this.message.container.position.y-this.message.container.height}}),this.move(),this.sprite){const u=Object.fromEntries(this.spriteData.colored.map(v=>[v,this.userState.color?this.userState.color:this.state.color])),C=this.spriteData.flip?this.state.direction:1;this.sprite.update({color:u,scale:{x:C*this.state.scale*this.spriteData.scale,y:this.state.scale*this.spriteData.scale},play:!this.isDashing})}this.trail.update({play:this.isDashing})}isOnGround(t){return t>m.renderer.height}addMessage(t){var e;this.message.add(t),this.state.isAnonymous=!1,(e=this.fadeTween)==null||e.stop(),this.fadeTween=void 0,this.container.alpha=1}spitEmotes(t){for(const e of t)this.emoteSpitter.add(e)}async setAnimationState(t,e=!1){if(this.animationState==t&&!e||!this.animatedSprites)return;this.animationState=t,this.sprite&&this.container.removeChild(this.sprite.container);const i=this.animatedSprites[this.animationState]??this.animatedSprites[p.Idle];this.sprite=new J(i),this.trail.setSprite(this.sprite),this.sprite.container.pivot.set(0,this.spriteData.collider.y+this.spriteData.collider.h),this.container.addChild(this.sprite.container)}}const U=1e3*60*5;class N{constructor(){s(this,"viewers",{});s(this,"raiders",[]);s(this,"lastEvotarActivity",{});s(this,"subscriptions",[]);s(this,"hasActivity",t=>!!this.lastEvotarActivity[t])}subscribe(t){this.subscriptions.push(t)}update(){for(const t in this.viewers){const e=this.viewers[t];e&&e.update()}for(const t of this.raiders)t.update()}processRaid(t){if(!m.settings.fallingRaiders)return;const e=1/t.viewers*5e3;for(let o=0;o<t.viewers;o++){const h=new g;h.setProps({isAnonymous:!0,zIndex:-1,sprite:"agent",color:new c.Color(t.broadcaster.info.color)}),A.add(e*o,()=>{this.addRaider(h),h.spawn({isFalling:!0})}),A.add(o*e+3e4,()=>{h.despawn({onComplete:()=>{this.deleteRaider(h)}})})}const i=this.viewers[t.broadcaster.id],n={...this.prepareEvotarProps(t.broadcaster.info.displayName,t.broadcaster.info.color),scale:8},a=()=>{const o=new g;o.setProps(n),this.addViewer(t.broadcaster.id,o),o.spawn({isFalling:!0,positionX:.5})};this.lastEvotarActivity[t.broadcaster.id]=performance.now(),i?i.despawn({onComplete:()=>{a()}}):a()}async processChatters(t){for(const e in this.viewers){const i=this.lastEvotarActivity[e],n=!!i&&performance.now()-i<U;if(t.every(a=>a.userId!=e)&&!n){const a=this.viewers[e];a&&a.despawn({onComplete:()=>{this.deleteViewer(e,a)}})}}if(m.settings.showAnonymousEvotars){for(const e of t)if(!this.viewers[e.userId]){const n={name:e.name,isAnonymous:!this.hasActivity(e.userId)},a=new g;await a.setProps(n),a.spawn(),this.addViewer(e.userId,a)}}}doAction(t,e){if(M(t)&&e.jump({velocityX:t.data.velocityX,velocityY:t.data.velocityY,cooldown:t.cooldown}),P(t)&&e.dash({force:t.data.force,cooldown:t.cooldown}),R(t)){const i=x(t.data.color);i&&i.isValid()&&e.setUserProps({color:new c.Color(t.data.color)})}k(t)&&e.scale({value:t.data.scale,duration:t.data.duration,cooldown:t.cooldown}),F(t)&&e.setSprite(t.data.sprite)}prepareEvotarProps(t,e,i){const n={name:t,isAnonymous:!1};if(i&&(n.sprite=i),e)try{n.color=new c.Color(e)}catch{}return n}async processAction(t){const e=this.viewers[t.userId],i=this.prepareEvotarProps(t.info.displayName,t.info.color,t.info.sprite);if(e)await e.setProps(i),this.doAction(t,e);else{const n=new g;await n.setProps(i),n.spawn({onComplete:()=>{const a=this.viewers[t.userId];a&&this.doAction(t,a)}}),this.addViewer(t.userId,n)}}async processMessage(t){const e=this.prepareEvotarProps(t.info.displayName,t.info.color,t.info.sprite);let i=this.viewers[t.userId];if(i)i.setProps(e);else{const n=m.settings.fallingEvotars?!this.hasActivity(t.userId):!1;i=new g,await i.setProps(e),i.spawn({isFalling:n}),this.addViewer(t.userId,i)}this.lastEvotarActivity[t.userId]=performance.now(),t.message&&i.addMessage(t.message),t.emotes.length>0&&i.spitEmotes(t.emotes)}addViewer(t,e){this.subscriptions.forEach(i=>i.onAdd(e)),this.viewers[t]=e}deleteViewer(t,e){this.subscriptions.forEach(i=>i.onDelete(e)),delete this.viewers[t]}addRaider(t){this.subscriptions.forEach(e=>e.onAdd(t)),this.raiders.push(t)}deleteRaider(t){this.subscriptions.forEach(e=>e.onDelete(t)),this.raiders=this.raiders.filter(e=>e!=t)}zIndexEvotarMax(t){return Object.values(this.viewers).reduce((e,i)=>i&&e<=i.container.zIndex?i.container.zIndex+1:e,t)}zIndexEvotarMin(t){return Object.values(this.viewers).reduce((e,i)=>i&&e>=i.container.zIndex?i.container.zIndex-1:e,t)}}const y=new N;class q{constructor(){s(this,"stage",new c.Container);s(this,"evotars",{});s(this,"chatterIds",[]);s(this,"settings",{});s(this,"renderer")}async initialize(t,e){b.initialize(e.spriteLoaderFn),D.initialize(e.sounds),this.renderer=new c.Renderer({width:t.clientWidth,height:t.clientHeight,backgroundAlpha:0}),t.appendChild(this.renderer.view),window.onresize=()=>{this.renderer.resize(t.clientWidth,t.clientHeight)},this.stage.sortableChildren=!0,y.subscribe({onAdd:i=>this.stage.addChild(i.container),onDelete:i=>{this.stage.removeChild(i.container),this.stage.removeChild(i.trail.container)}})}updateSettings(t){this.settings=t}update(){A.tick(),y.update(),this.renderer.render(this.stage)}}const m=new q;class G{constructor(t,e){s(this,"isRendered",!1);s(this,"processMessage",t=>y.processMessage(t));s(this,"processAction",t=>y.processAction(t));s(this,"processChatters",t=>y.processChatters(t));s(this,"processRaid",t=>y.processRaid(t));this.root=t,this.options=e}updateSettings(t){m.updateSettings(t)}async run(){if(this.isRendered)return;this.isRendered=!0,await(async()=>{await m.initialize(this.root,this.options),performance.now();let e=-1;const n=1e3/60;requestAnimationFrame(a);function a(o=performance.now()){const h=o-e|0;h>n&&(e=o-h%n,m.update()),requestAnimationFrame(a)}})()}}l.Evotars=G,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(l,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("pixi.js"),require("tinycolor2"),require("@tweenjs/tween.js"),require("@pixi/gif")):typeof define=="function"&&define.amd?define(["exports","pixi.js","tinycolor2","@tweenjs/tween.js","@pixi/gif"],d):(l=typeof globalThis<"u"?globalThis:l||self,d(l.evotars={},l.PIXI,l.tinycolor,l.TWEEN,l.gif))})(this,function(l,d,x,T,z){"use strict";var H=Object.defineProperty;var K=(l,d,x)=>d in l?H(l,d,{enumerable:!0,configurable:!0,writable:!0,value:x}):l[d]=x;var s=(l,d,x)=>(K(l,typeof d!="symbol"?d+"":d,x),x);function E(r){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const e in r)if(e!=="default"){const i=Object.getOwnPropertyDescriptor(r,e);Object.defineProperty(t,e,i.get?i:{enumerable:!0,get:()=>r[e]})}}return t.default=r,Object.freeze(t)}const c=E(d),S=E(T),u=.02*1e3;class w{constructor(t=0,e){s(this,"isCompleted",!1);this.current=t,this.onComplete=e}tick(t=u){var e;this.isCompleted||(this.current-=t,this.current<0&&(this.isCompleted=!0,(e=this.onComplete)==null||e.call(this)))}}class M{constructor(){s(this,"timers",[])}add(t,e){this.timers.push(new w(t,e))}tick(t=u){const e=[];for(const i of this.timers)i.isCompleted||(i.tick(t),e.push(i));this.timers=e}}const A=new M,R=r=>r.name=="jump",k=r=>r.name=="color",P=r=>r.name=="grow",F=r=>r.name=="dash",j=r=>r.name=="sprite";class O{constructor(){s(this,"assets",{})}async initialize(t){this.assets=Object.fromEntries(Array.from(Object.entries(t??{})).map(([e,i])=>[e,new Audio(i.src)]))}play(t){const e=this.assets[t];e&&(e.volume=.2,e.pause(),e.currentTime=0,e.play())}}const D=new O;var p=(r=>(r.Idle="Idle",r.Jump="Jump",r.Fall="Fall",r.Land="Land",r.Run="Run",r.Die="Die",r))(p||{});class V{constructor(){s(this,"spriteLoaderFn");s(this,"sprites",{})}initialize(t){this.spriteLoaderFn=t}async getSpriteData(t){const e=await this.spriteLoaderFn(t);if(!e)return;if(this.sprites[t])return this.sprites[t];const i=await d.Assets.load(e.image),n=new d.Spritesheet({texture:i,data:e.sprite});return await n.parse(),this.sprites[t]={sheet:n,data:{...e.data,scale:e.data.scale??1}},this.sprites[t]}getAnimatedSprites(t){if(!t)throw Error("Sheet is not defined");if(!this.sprites[t])throw Error("Sprite is not loaded");const e=this.sprites[t].data,i=this.sprites[t].sheet,n={};for(const a of i.data.meta.frameTags??[]){if(!i.data.meta.layers)throw Error("Layers are not defined");const o={};for(const h of i.data.meta.layers){const f=[];for(let v=a.from;v<=a.to;v++){const X=v.toString(),I=e.name+"_"+h.name+"_"+X,Y=i.textures[I],Q=i.data.frames[I].duration;f.push({texture:Y,time:Q})}const C=new d.AnimatedSprite(f);C.texture.baseTexture.scaleMode=d.SCALE_MODES.NEAREST,o[h.name]=C}a.name&&(n[a.name]=o)}return n}}const b=new V;class _{constructor(){s(this,"container",new c.Container);s(this,"emotes",[]);s(this,"moveSpeed",50);s(this,"alphaSpeed",1);s(this,"scaleSpeed",.5);s(this,"timer")}async add(t){const e=await fetch(t),i=e.headers.get("content-type");let n;switch(i){case"image/gif":{const a=await e.arrayBuffer();n=z.AnimatedGIF.fromBuffer(a);break}case"image/png":{n=c.Sprite.from(t);break}default:{console.warn("Unsupported content type: ",t);return}}n.anchor.set(.5,.5),n.scale.set(0,0),this.emotes.push(n)}update(t){var e;(e=this.timer)==null||e.tick(),(!this.timer||this.timer.isCompleted)&&this.emotes.length>0&&(this.timer=new w(2e3,()=>{const i=this.emotes.shift();i&&this.container.addChild(i)})),this.container.position.x=t.position.x??this.container.position.x,this.container.position.y=t.position.y??this.container.position.y;for(const i of this.container.children)i.position.y-=u*this.moveSpeed/1e3,i.scale.x+=u*this.scaleSpeed/1e3,i.scale.y+=u*this.scaleSpeed/1e3,i.scale.x>1&&(i.alpha-=u*this.alphaSpeed/1e3),i.alpha<=0&&this.container.removeChild(i)}}class L{constructor(t){s(this,"container",new c.Container);s(this,"animated",new c.Container);s(this,"text",new c.Text(void 0,{fontFamily:"Rubik",fontSize:20,fill:2236962,align:"left",breakWords:!0,wordWrap:!0,wordWrapWidth:200}));s(this,"padding",10);s(this,"borderRadius",10);s(this,"boxColor",15658734);s(this,"fadeShift",10);s(this,"isChecking",!0);s(this,"messageQueue",[]);s(this,"showTween");s(this,"hideTween");s(this,"timer");this.beforeShow=t,this.container.addChild(this.animated),this.text.anchor.set(.5,1),this.text.position.set(0,-this.padding)}trim(t){const e=c.TextMetrics.measureText(t.text,t.style);return e.lines.length>4?e.lines.slice(0,4).join(" ").slice(0,-3)+"...":t.text}update(t){var i,n,a,o,h;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){this.isChecking=!1;const f=this.messageQueue.shift();f&&(this.beforeShow(),this.show(f))}this.container.position.x=t.position.x??this.container.position.x,this.container.position.y=t.position.y??this.container.position.y;const e=((o=this.showTween)==null?void 0:o.isPlaying())||((h=this.hideTween)==null?void 0:h.isPlaying());!this.isChecking&&!e&&this.timer&&(this.animated.position.y=Math.sin(this.timer.current*.0025)*4-2)}add(t){this.messageQueue.push(t)}drawBox(t){const e={x:t.x-this.padding-t.width*t.anchor.x,y:t.y-this.padding-t.height*t.anchor.y,w:t.width+this.padding*2,h:t.height+this.padding*2},i=new c.Graphics;return i.beginFill(this.boxColor),i.drawRoundedRect(e.x,e.y,e.w,e.h,this.borderRadius),i.drawPolygon({x:e.x+e.w/2-10,y:e.y+e.h},{x:e.x+e.w/2+10,y:e.y+e.h},{x:e.x+e.w/2,y:e.y+e.h+10-4}),i.endFill(),i}show(t){this.text.text=t,this.text.text=this.trim(this.text),this.animated.alpha=0,this.animated.position.y=this.fadeShift;const e=this.drawBox(this.text);this.animated.removeChildren(),this.animated.addChild(e,this.text);const i={alpha:1,y:this.animated.y-this.fadeShift},n={alpha:0,y:this.animated.y+this.fadeShift};this.hideTween=new T.Tween(this.animated).to(n,500).onComplete(()=>{this.isChecking=!0}),this.showTween=new T.Tween(this.animated).to(i,500).onComplete(()=>{this.timer=new w(1e4,()=>{var a;(a=this.hideTween)==null||a.start()})}),this.showTween.start()}}class J{constructor(){s(this,"text",new c.Text(void 0,{fontFamily:"Rubik",fontSize:18,fill:16777215,align:"center",lineJoin:"round",strokeThickness:4,stroke:"black"}));s(this,"rect",()=>({x:this.text.x,y:this.text.y,w:this.text.width,h:this.text.height}));this.text.anchor.set(.5,1),this.text.zIndex=100}update(t){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}}class U{constructor(t){s(this,"container",new c.Container);s(this,"sprites");this.sprites=t,this.container.sortableChildren=!0;let e=0;for(const i in this.sprites){const n=this.sprites[i];this.container.addChild(n),n.zIndex=++e,n.anchor.set(.5,0),n.autoUpdate=!1,n.play()}}update(t){for(const e in this.sprites){const i=this.sprites[e];if(t.play&&i.update(u*.06),t.color){for(const n in t.color)if(n==e){const a=t.color[n];a&&(i.alpha=a.alpha,i.tint=a)}}t.scale.x&&(i.animationSpeed=4/Math.abs(t.scale.x))}this.container.scale.set(t.scale.x,t.scale.y)}}class W{constructor(){s(this,"container",new c.Container);s(this,"timer");s(this,"sprite");s(this,"opacityStep",.1);s(this,"shadowStep",50);m.stage.addChild(this.container)}setSprite(t){this.sprite=t}update(t){for(const e of this.container.children)e.alpha>0&&(e.alpha=e.alpha-this.opacityStep);if(this.timer&&!this.timer.isCompleted)this.timer.tick();else if(t.play){this.timer=new w(this.shadowStep);const e=new c.Container;if(this.sprite)for(const i in this.sprite.sprites){const n=this.sprite.sprites[i],a=n.texture.clone(),o=new c.Sprite(a);o.anchor.set(.5,0),o.alpha=n.alpha,o.tint=n.tint,e.addChild(o),e.scale.set(this.sprite.container.scale.x,this.sprite.container.scale.y),e.pivot.set(this.sprite.container.pivot.x,this.sprite.container.pivot.y);const h=this.sprite.container.getGlobalPosition();e.position.set(h.x,h.y)}this.container.addChild(e)}}}class g{constructor(){s(this,"container",new c.Container);s(this,"userState",{});s(this,"state",{name:"",sprite:"",color:new c.Color("#969696"),direction:1,scale:1,isAnonymous:!1,zIndex:0});s(this,"animationState",p.Idle);s(this,"sprite");s(this,"spriteData",{name:"evotar",collider:{x:0,y:0,w:0,h:0},size:{w:0,h:0},scale:1,flip:!1,colored:[]});s(this,"animatedSprites");s(this,"trail",new W);s(this,"name",new J);s(this,"message",new L(()=>{this.state.zIndex=y.zIndexEvotarMax(this.container.zIndex)}));s(this,"emoteSpitter",new _);s(this,"velocity",{x:0,y:0});s(this,"runSpeed",.05);s(this,"gravity",.2);s(this,"dashAcc",3);s(this,"isJumping",!1);s(this,"isDashing",!1);s(this,"isDespawned",!1);s(this,"landTimer");s(this,"stateTimer");s(this,"scaleTimer");s(this,"spawnTween");s(this,"fadeTween");s(this,"scaleTween");this.container.addChild(this.name.text),this.container.addChild(this.emoteSpitter.container),this.container.addChild(this.message.container),this.container.sortableChildren=!0}spawn(t={isFalling:!1}){const e=this.spriteData.collider,i=-(e.y+e.h-this.spriteData.size.h/2),n=t.isFalling?i:m.renderer.height,a=this.spriteData.size.w*this.state.scale*this.spriteData.scale,o=t.positionX!=null?t.positionX*m.renderer.width:Math.random()*(m.renderer.width-a)+a/2,h=n*this.state.scale*this.spriteData.scale;if(this.container.x=o,this.container.y=h,this.state.direction=Math.random()>.5?1:-1,!t.isFalling){const f=y.zIndexEvotarMin(this.container.zIndex);this.state.zIndex=f,this.container.alpha=0,this.spawnTween=new S.Tween(this.container).to({alpha:1},500).onComplete(t.onComplete).start()}this.stateTimer=new w(5e3,()=>{this.isJumping||this.setAnimationState(p.Run)})}despawn(t={}){this.fadeTween&&this.fadeTween.isPlaying()||(this.fadeTween=new S.Tween(this.container).to({alpha:0},1e3).onComplete(()=>{this.isDespawned=!0,t.onComplete&&t.onComplete()}).start())}scale(t){this.scaleTween&&this.scaleTween.isPlaying()||this.scaleTimer&&!this.scaleTimer.isCompleted||(this.scaleTween=new S.Tween(this).to({state:{scale:t.value??2}},2e3).onComplete(()=>{this.scaleTimer=new w((t.duration??10)*1e3,()=>{this.scaleTween=new S.Tween(this).to({state:{scale:1}},2e3).start()})}).start())}jump(t){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(p.Jump),D.play("jump"))}dash(t){this.isDespawned||this.isDashing||(this.isDashing=!0,this.velocity.x=this.state.direction*(t.force??14))}async setSprite(t){if(t&&t!=this.state.sprite){const e=await b.getSpriteData(t);if(!e)return;this.state.sprite=t,this.spriteData=e.data,this.animatedSprites=b.getAnimatedSprites(t),this.setAnimationState(this.animationState,!0)}}async setProps(t){t.sprite&&await this.setSprite(t.sprite),this.state={...this.state,...t}}setUserProps(t){this.userState={...this.userState,...t}}move(){var i;const t=this.spriteData.collider,e={x:this.container.position.x,y:this.container.position.y};if(this.isDashing){const n=Math.sign(this.velocity.x);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}else{if((i=this.stateTimer)!=null&&i.isCompleted&&(this.stateTimer=new w(Math.random()*5e3,()=>{this.animationState==p.Idle?this.setAnimationState(p.Run):this.animationState==p.Run&&this.setAnimationState(p.Idle)})),this.animationState==p.Run){const n=this.runSpeed*this.state.direction;this.velocity.x=n*u}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=m.renderer.height,this.animationState==p.Fall&&(this.setAnimationState(p.Land),this.landTimer=new w(200,()=>{this.setAnimationState(p.Idle),this.isJumping=!1}))),this.velocity.y>0&&this.setAnimationState(p.Fall)}if(this.animationState!=p.Idle||this.isDashing){const n=t.w/2*this.state.scale*this.spriteData.scale,a=this.container.x-n<0,o=this.container.x+n>m.renderer.width;(a||o)&&(this.state.direction=-this.state.direction,this.velocity.x=-this.velocity.x,a&&(e.x=n),o&&(e.x=m.renderer.width-n))}this.container.position.set(e.x,e.y)}update(){var e,i,n,a,o,h;if(this.isDespawned)return;(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;const t=this.spriteData.collider;if(this.name.update({name:this.state.name,isVisible:!this.state.isAnonymous,position:{y:-t.h*this.state.scale*this.spriteData.scale}}),this.message.update({position:{y:this.name.text.position.y-this.name.text.height-6}}),this.emoteSpitter.update({position:{y:this.message.container.position.y-this.message.container.height}}),this.move(),this.sprite){const f=Object.fromEntries(this.spriteData.colored.map(v=>[v,this.userState.color?this.userState.color:this.state.color])),C=this.spriteData.flip?this.state.direction:1;this.sprite.update({color:f,scale:{x:C*this.state.scale*this.spriteData.scale,y:this.state.scale*this.spriteData.scale},play:!this.isDashing})}this.trail.update({play:this.isDashing})}isOnGround(t){return t>m.renderer.height}addMessage(t){var e;this.message.add(t),this.state.isAnonymous=!1,(e=this.fadeTween)==null||e.stop(),this.fadeTween=void 0,this.container.alpha=1}spitEmotes(t){for(const e of t)this.emoteSpitter.add(e)}async setAnimationState(t,e=!1){if(this.animationState==t&&!e||!this.animatedSprites)return;this.animationState=t,this.sprite&&this.container.removeChild(this.sprite.container);const i=this.animatedSprites[this.animationState]??this.animatedSprites[p.Idle];this.sprite=new U(i),this.trail.setSprite(this.sprite),this.sprite.container.pivot.set(0,this.spriteData.collider.y+this.spriteData.collider.h),this.container.addChild(this.sprite.container)}}const N=1e3*60*5;class q{constructor(){s(this,"viewers",{});s(this,"raiders",[]);s(this,"lastEvotarActivity",{});s(this,"subscriptions",[]);s(this,"hasActivity",t=>!!this.lastEvotarActivity[t])}subscribe(t){this.subscriptions.push(t)}update(){for(const t in this.viewers){const e=this.viewers[t];e&&e.update()}for(const t of this.raiders)t.update()}processRaid(t){if(!m.settings.fallingRaiders)return;const e=1/t.viewers*5e3;for(let o=0;o<t.viewers;o++){const h=new g;h.setProps({isAnonymous:!0,zIndex:-1,sprite:"agent",color:new c.Color(t.broadcaster.info.color)}),A.add(e*o,()=>{this.addRaider(h),h.spawn({isFalling:!0})}),A.add(o*e+3e4,()=>{h.despawn({onComplete:()=>{this.deleteRaider(h)}})})}const i=this.viewers[t.broadcaster.id],n={...this.prepareEvotarProps(t.broadcaster.info.displayName,t.broadcaster.info.color),scale:8},a=()=>{const o=new g;o.setProps(n),this.addViewer(t.broadcaster.id,o),o.spawn({isFalling:!0,positionX:.5})};this.lastEvotarActivity[t.broadcaster.id]=performance.now(),i?i.despawn({onComplete:()=>{a()}}):a()}async processChatters(t){for(const e in this.viewers){const i=this.lastEvotarActivity[e],n=!!i&&performance.now()-i<N;if(t.every(a=>a.userId!=e)&&!n){const a=this.viewers[e];a&&a.despawn({onComplete:()=>{this.deleteViewer(e,a)}})}}if(m.settings.showAnonymousEvotars){for(const e of t)if(!this.viewers[e.userId]){const n={name:e.name,isAnonymous:!this.hasActivity(e.userId)},a=new g;await a.setProps(n),a.spawn(),this.addViewer(e.userId,a)}}}doAction(t,e){if(R(t)&&e.jump({velocityX:t.data.velocityX,velocityY:t.data.velocityY,cooldown:t.cooldown}),F(t)&&e.dash({force:t.data.force,cooldown:t.cooldown}),k(t)){const i=x(t.data.color);i&&i.isValid()&&e.setUserProps({color:new c.Color(t.data.color)})}P(t)&&e.scale({value:t.data.scale,duration:t.data.duration,cooldown:t.cooldown}),j(t)&&e.setSprite(t.data.sprite)}prepareEvotarProps(t,e,i){const n={name:t,isAnonymous:!1};if(i&&(n.sprite=i),e)try{n.color=new c.Color(e)}catch{}return n}async processAction(t){const e=this.viewers[t.userId],i=this.prepareEvotarProps(t.info.displayName,t.info.color,t.info.sprite);if(e)await e.setProps(i),this.doAction(t,e);else{const n=new g;await n.setProps(i),n.spawn({onComplete:()=>{const a=this.viewers[t.userId];a&&this.doAction(t,a)}}),this.addViewer(t.userId,n)}}async processMessage(t){const e=this.prepareEvotarProps(t.info.displayName,t.info.color,t.info.sprite);let i=this.viewers[t.userId];if(i)i.setProps(e);else{const n=m.settings.fallingEvotars?!this.hasActivity(t.userId):!1;i=new g,await i.setProps(e),i.spawn({isFalling:n}),this.addViewer(t.userId,i)}this.lastEvotarActivity[t.userId]=performance.now(),t.message&&i.addMessage(t.message),t.emotes.length>0&&i.spitEmotes(t.emotes)}addViewer(t,e){this.subscriptions.forEach(i=>i.onAdd(e)),this.viewers[t]=e}deleteViewer(t,e){this.subscriptions.forEach(i=>i.onDelete(e)),delete this.viewers[t]}addRaider(t){this.subscriptions.forEach(e=>e.onAdd(t)),this.raiders.push(t)}deleteRaider(t){this.subscriptions.forEach(e=>e.onDelete(t)),this.raiders=this.raiders.filter(e=>e!=t)}zIndexEvotarMax(t){return Object.values(this.viewers).reduce((e,i)=>i&&e<=i.container.zIndex?i.container.zIndex+1:e,t)}zIndexEvotarMin(t){return Object.values(this.viewers).reduce((e,i)=>i&&e>=i.container.zIndex?i.container.zIndex-1:e,t)}}const y=new q;class G{constructor(){s(this,"stage",new c.Container);s(this,"evotars",{});s(this,"chatterIds",[]);s(this,"settings",{});s(this,"renderer")}async initialize(t,e){b.initialize(e.spriteLoaderFn),D.initialize(e.sounds),this.renderer=new c.Renderer({width:t.clientWidth,height:t.clientHeight,backgroundAlpha:0}),t.appendChild(this.renderer.view),window.onresize=()=>{this.renderer.resize(t.clientWidth,t.clientHeight)},this.stage.sortableChildren=!0,y.subscribe({onAdd:i=>this.stage.addChild(i.container),onDelete:i=>{this.stage.removeChild(i.container),this.stage.removeChild(i.trail.container)}})}updateSettings(t){this.settings=t}update(){A.tick(),y.update(),this.renderer.render(this.stage)}}const m=new G;class B{constructor(t,e){s(this,"isRendered",!1);s(this,"processMessage",t=>y.processMessage(t));s(this,"processAction",t=>y.processAction(t));s(this,"processChatters",t=>y.processChatters(t));s(this,"processRaid",t=>y.processRaid(t));this.root=t,this.options=e}updateSettings(t){m.updateSettings(t)}async run(){if(this.isRendered)return;this.isRendered=!0,await(async()=>{await m.initialize(this.root,this.options),performance.now();let e=-1;const n=1e3/60;requestAnimationFrame(a);function a(o=performance.now()){const h=o-e|0;h>n&&(e=o-h%n,m.update()),requestAnimationFrame(a)}})()}}l.Evotars=B,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.8",
|
|
3
3
|
"name": "evotars",
|
|
4
4
|
"description": "Show animated characters on stream",
|
|
5
5
|
"author": "Mikhail Morozov",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"LICENSE",
|
|
25
25
|
"README.md"
|
|
26
26
|
],
|
|
27
|
+
"source": "./src/index.ts",
|
|
27
28
|
"main": "./dist/evotars.umd.cjs",
|
|
29
|
+
"module": "./dist/evotars.js",
|
|
28
30
|
"exports": {
|
|
29
31
|
".": {
|
|
30
32
|
"import": "./dist/evotars.js",
|
|
@@ -40,6 +42,7 @@
|
|
|
40
42
|
"vite-plugin-dts": "^3.8.3"
|
|
41
43
|
},
|
|
42
44
|
"dependencies": {
|
|
45
|
+
"@pixi/gif": "^2",
|
|
43
46
|
"@tweenjs/tween.js": "^23.1.1",
|
|
44
47
|
"pixi.js": "^7.4.2",
|
|
45
48
|
"tinycolor2": "^1.6.0"
|