makefuture / games / accept-the-cookie / game / entities / Player.js
210 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
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
import { recordJump, recordMove } from '../systems/behavioral.js?v=20260427c';

export class Player {
    constructor(scene, x, y) {
        this.scene = scene;
        this.sprite = scene.add.container(x, y);
        this.body = scene.add.ellipse(0, 0, 28, 42, 0xffffff);
        this.head = scene.add.circle(0, -30, 13, 0xffffff);
        this.eye = scene.add.rectangle(5, -32, 7, 2, 0x111111);
        this.sprite.add([this.body, this.head, this.eye]);
        scene.physics.add.existing(this.sprite);
        this.sprite.body.setSize(28, 70);
        this.sprite.body.setOffset(-14, -58);
        this.sprite.body.setCollideWorldBounds(true);
        this.sprite.body.setMaxVelocity(380, 900);
        this.cursors = scene.input.keyboard.createCursorKeys();
        this.keys = scene.input.keyboard.addKeys('W,A,S,D,SPACE');
        this.jumpWasDown = false;
        this.pointerWasDown = false;
        this.jumpSuppressed = false;
    }

    suppressJumpUntilRelease() {
        this.jumpSuppressed = true;
        this.jumpWasDown = true;
        this.pointerWasDown = true;
    }

    setTint(color) {
        this.body.setFillStyle(color);
        this.head.setFillStyle(color);
    }

    update(session) {
        const body = this.sprite.body;
        const pointerDown = this.scene.input.activePointer.isDown;
        if (this.jumpSuppressed && !pointerDown && !this.cursors.up.isDown && !this.keys.W.isDown && !this.keys.SPACE.isDown) {
            this.jumpSuppressed = false;
        }
        const left = this.cursors.left.isDown || this.keys.A.isDown;
        const right = this.cursors.right.isDown || this.keys.D.isDown;
        const jumpKey = this.cursors.up.isDown || this.keys.W.isDown || this.keys.SPACE.isDown;
        const jump = this.jumpSuppressed ? false : (jumpKey || pointerDown);
        const speed = 245 + session.currencies.convenience * 18;

        if (left) {
            body.setVelocityX(-speed);
            recordMove(session, -1);
        } else if (right) {
            body.setVelocityX(speed);
            recordMove(session, 1);
        } else {
            body.setVelocityX(body.velocity.x * 0.82);
        }

        const touching = body.blocked.down || body.touching.down;
        if (jump && !this.jumpWasDown && touching) {
            body.setVelocityY(-560 - session.currencies.convenience * 18);
            recordJump(session);
            this.scene.soundTick?.('jump');
        }

        if (pointerDown && !this.pointerWasDown && !touching && !this.jumpSuppressed) {
            this.scene.onAirTap?.();
        }

        this.jumpWasDown = jump;
        this.pointerWasDown = pointerDown;
    }
}

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