summaryrefslogtreecommitdiff
path: root/2026/games_submissions/crm148/star.js
blob: 78173a8a0a944b809d6af149b31fa3ad2a21d9d1 (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

import { getCosmos } from './cosmos.js';
import { globalView } from './view.js';
import Vec2 from './vec2.js';
import Color from './color.js';
import Pulse from './pulse.js';

var cosmos;

export default class Star {
    pos = Vec2.zero();
    radius;

    difficulty_mult = 0.2;
    growth_base = 3;

    pulses = [];
    state = 'state_glowing';

    energy = 5;
    growth_rate;

    constructor(radius, difficulty) {
        cosmos = getCosmos();
        this.radius = radius;
        const diff_adjust = 1.0 + difficulty * this.difficulty_mult;
        this.growth_rate = this.growth_base + diff_adjust;
    }

    update(dt) {
        this.energy += dt * this. growth_rate;
        if (this[this.state]) {
            this[this.state](dt);
        }
    }

    state_glowing(dt) {
        if (this.energy > cosmos.thresh_crit) {
            this.state = 'state_critical';
        }
    }

    // Multiply the energy by a random value
    // If the result is over the threshold, dump that energy into a pulse
    state_critical(dt) {
        var rand = 0.1 + 0.8 * Math.random();
        var strength = Math.min(rand * this.energy, cosmos.pulse_max_str);
        if (strength > cosmos.pulse_min_str) {
            this.energy -= strength;
            cosmos.pulses.push(Pulse.StrengthPulse(this.radius, strength));
        }
    }

    getColor() {
        var color = Color.StarColor;
        switch (this.state) {
        case 'state_glowing':
            color = Color.interpolate(Color.StarDim,
                                      Color.StarBright,
                                      this.energy / cosmos.thresh_crit);
            break;
        case 'state_critical':
            color = Color.StarBright;
            break;
        }
        return color.to_string();
    }

    render(ctx) {
        const view = globalView();
        var s_pos = view.project(this.pos);
        var surface_vec = view.project(new Vec2(this.radius, this.pos.y));
        var s_rad = surface_vec.x - s_pos.x;
        ctx.fillStyle = this.getColor();
        ctx.beginPath();
        ctx.arc(s_pos.x, s_pos.y, s_rad, 0, 2 * Math.PI);
        ctx.fill();
    }
}