makefuture / games / accept-the-cookie / game / entities / Doppelganger.js
107 words
 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
export class Doppelganger {
    constructor(scene, player, delay = 500) {
        this.scene = scene;
        this.player = player;
        this.delay = delay;
        this.history = [];
        this.sprite = scene.add.container(player.sprite.x, player.sprite.y).setAlpha(0.42);
        this.body = scene.add.ellipse(0, 0, 28, 42, 0x090909);
        this.head = scene.add.circle(0, -30, 13, 0x090909);
        this.glitch = scene.add.rectangle(5, -10, 30, 3, 0x61f3ff, 0.8);
        this.sprite.add([this.body, this.head, this.glitch]);
        scene.physics.add.existing(this.sprite);
        this.sprite.body.setSize(30, 70);
        this.sprite.body.setOffset(-15, -58);
        this.sprite.body.allowGravity = false;
    }

    update(time) {
        this.history.push({ time, x: this.player.sprite.x, y: this.player.sprite.y });
        while (this.history.length && time - this.history[0].time > this.delay + 1200) {
            this.history.shift();
        }
        const target = this.history.find((entry) => time - entry.time >= this.delay);
        if (!target) return;
        this.sprite.setPosition(target.x, target.y);
        this.glitch.x = 5 + Math.sin(time / 60) * 4;
    }
}

static mirror of HEAD · about · clone: git clone https://git.makefuture.ro/makefuture.git