summaryrefslogtreecommitdiffstats
path: root/site/game.mjs
blob: 0b1de90f8d332c0cbcfd7a3a39af6593fa00831e (plain)
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
import Arena from './arena.mjs';
import Inspector from './inspector.mjs';

const ARENA_SELECTOR = 'x-arena';
const TICK_BUTTON_SELECTOR = '#tick';
const TICK_RATE_SELECTOR = '#tick-rate-select';
const RUN_BUTTON_SELECTOR = '#run';

export default class extends HTMLElement {
    static name = 'x-game';
    static register() {
        console.debug('registering custom element', this.name, this);
        self.customElements.define(this.name, this);
    }

    #running = false;
    #robos = [];
    #inspectors = [];
    #arena;
    #tickButton;
    #runButton;
    #tickRate;

    constructor() {
        super();
    }

    connectedCallback() {
        console.debug('connectedCallback()', this);
        this.#arena = this.querySelector(ARENA_SELECTOR);
        this.#tickButton = this.querySelector(TICK_BUTTON_SELECTOR);
        this.#runButton = this.querySelector(RUN_BUTTON_SELECTOR);
        this.#tickRate = this.querySelector(TICK_RATE_SELECTOR);
        this.#inspectors = this.querySelectorAll(Inspector.name);

        this.#tickButton.onclick = this.#tickHandler.bind(this);
        this.#runButton.onclick = this.#blinkenHandler.bind(this);
        this.#tickRate.onchange = this.#tickRateHandler.bind(this);

        this.#tickRate.onchange({ target: this.#tickRate });
        this.addRobo();
    }

    addRobo() {
        const { x, y } = this.#arena.randStart();
        const robo = {
            worker: new Worker('robo.mjs', { type: 'module' }),
            lastTick: undefined,
            heading: 0,
            speed: 0,
            speedx: 0,
            speedy: 0,
            x,
            y,
        }
        robo.worker.onmessage = msg => this.#messageHandler(i, msg);
        robo.worker.onerror = msg => this.#errorHander(i, msg);
        this.#robos.push(robo);
        const i = this.#robos.length - 1;
        this.#inspectors[i].addEventListener(Inspector.compileRequest, e => {
            console.debug('compiling for worker', i, e.detail.text, this.#robos[i].worker);
            this.#robos[i].worker.postMessage({ kind: 'compile', text: e.detail.text });
        });
    }

    #messageHandler(i, e) {
        const { kind, res, trans } = e.data;
        switch (kind) {
        case 'compile':
            if (res) {
                this.#arena.render(this.#robos);
                this.#inspectors[i].render(trans, true);
            }
            break;
        case 'tick':
            const robo = this.#robos[i];
            robo.lastTick = document.timeline.currentTime;

            [robo.x, robo.y] = this.#arena.clamp(Arena.radius, robo.x + robo.speedx, robo.y + robo.speedy);
            this.#arena.render(this.#robos, robo.lastTick);

            robo.heading = trans.vars.heading;
            robo.speed = trans.vars.speed;
            [robo.speedx, robo.speedy] = [
                Math.cos(2 * Math.PI * robo.heading / 360),
                Math.sin(2 * Math.PI * robo.heading / 360)
            ].map(x => robo.speed * x);

            this.#inspectors[i].render(trans);
            break;
        default:
            console.error('invalid message from robo worker', e);
        }
    }

    #errorHander(i, e) {
        console.error('error in roboWorker', this, i, e);
    }

    #tickHandler(e) {
        console.debug('tick clicked', this, e);
        this.#robos.forEach(robo => robo.worker.postMessage({ kind: 'tick' }));
    }

    #blinkenHandler(e) {
        console.debug('blinken clicked', this, e);

        this.#running = !this.#running;
        if (this.#running) {
            e.currentTarget.classList.remove('halten');
            e.currentTarget.classList.add('blinken');
            e.currentTarget.querySelector('title').textContent = 'halten';
            this.#timeoutHandler();
            this.#renderFrame(document.timeline.currentTime);
        } else {
            e.currentTarget.classList.remove('blinken');
            e.currentTarget.classList.add('halten');
            e.currentTarget.querySelector('title').textContent = 'blinken';
            this.#arena.invalidateFPS();
        }
    }

    #tickRateHandler(e) {
        console.debug('tick rate changed', this, e, Number(e.target.value));
        this.#arena.tickMS = 1_000 / e.target.value;
    }

    #timeoutHandler() {
        if (!this.#running) {
            return;
        }
        self.setTimeout(this.#timeoutHandler.bind(this), this.#arena.tickMS);
        this.#robos.forEach(robo => robo.worker.postMessage({ kind: 'tick' }));
    }

    #renderFrame(t) {
        if (!this.#running) {
            return;
        }
        self.requestAnimationFrame(this.#renderFrame.bind(this));
        this.#arena.render(this.#robos, t);
    }
}