summaryrefslogtreecommitdiffstats
path: root/static/games/rms/gates.js
blob: 4ec1955b1c8d937d2de1dd7c4b1952f4e054b232 (plain) (blame)
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 * as constants from '/static/games/rms/constants.js';

export class Gates {
	constructor() {
		this.x = 200;
		this.y = 40;
		this.v_x = 0;
		this.v_y = 0;
		this.a_x = 0;
		this.a_y = 0;
		this.target_x = 200;
		this.target_y = 40;
		this.health = 1;
	}

	radius() {
		return 30;
	}

	choose_position() {
		const [x, y] = random_position();
		this.target_x = x;
		this.target_y = y;
	}

	act(count) {
		const max_accel = (1 - this.health) * 0.75 + 0.25;
		const max_velocity = (1 - this.health) * 5 + 5;
		let dx = this.target_x - this.x;
		let dy = this.target_y - this.y;
		/* If we've arrived (close and stopped) */
		if (Math.sqrt(dx * dx + dy * dy) < max_velocity / max_accel && Math.sqrt(this.v_x * this.v_x + this.v_y * this.v_y) < 0.01) {
			/* Then pick a new target */
			this.choose_position();

		}

		dx = this.target_x - this.x;
		dy = this.target_y - this.y;
		/* if we're close */
		if (Math.sqrt(dx * dx + dy * dy) < max_velocity / max_accel) {
			/* Then slow down */
			const [x, y] = shorten(-this.v_x, -this.v_y, max_accel);
			this.a_x = x;
			this.a_y = y;
		} else {
			/* Otherwise, speed up */
			const [x, y] = shorten(dx, dy, max_accel);
			this.a_x = x;
			this.a_y = y;
		}

		/* If we're going too fast */
		if (Math.sqrt(this.v_x * this.v_x + this.v_y * this.v_y) > max_velocity) {
			/* Then don't go faster */
			if (Math.sign(this.v_x) == Math.sign(this.a_x)) {
				this.a_x = 0;
			}
			if (Math.sign(this.v_y) == Math.sign(this.a_y)) {
				this.a_y = 0;
			}
		}

		this.v_x += this.a_x;
		this.x += this.v_x;
		this.v_y += this.a_y;
		this.y += this.v_y;
		if (count % 20 === 0) {
			let num_bullets;
			if (this.health >= 0.8) {
				num_bullets = 5;
			} else if (this.health >= 0.7) {
				num_bullets = 6;
			} else if (this.health >= 0.5) {
				num_bullets = 7;
			} else if (this.health >= 0.4) {
				num_bullets = 8;
			} else if (this.health >= 0.3) {
				num_bullets = 9;
			} else if (this.health >= 0.2) {
				num_bullets = 10;
			} else {
				num_bullets = 11;
			}
			const result = [];
			for (let i = 0; i < num_bullets; i++) {
				result.push(random_bullet(this));
			}
			return result;
		} else {
			return [];
		}
	}

	injure(damage) {
		this.health -= damage;
	}

	center() {
		return [16, 23];
	}
}

export class GatesBullet {
	constructor(x, d_x, y, d_y) {
		this.x = x;
		this.y = y;
		this.d_x = d_x;
		this.d_y = d_y;
	}

	radius() {
		return 10;
	}

	center() {
		return [20, 20];
	}
}

function random_position() {
	return [
		Math.random() * (constants.x_dim - 200) + 100,
		Math.random() * 100,
	];
}

function random_bullet(enemy) {
	const multiplier = 2 + 3 * (1 - enemy.health);
	const theta = Math.random() * 2 * Math.PI / 3 + Math.PI / 6;
	const dx = Math.cos(theta) * multiplier;
	const dy = Math.sin(theta) * multiplier;
	return new GatesBullet(enemy.x, dx, enemy.y, dy);
}

function shorten(x, y, maxlen) {
	const len = Math.sqrt(x * x + y * y);
	if (len > maxlen) {
		return [x / len / 2, y / len / 2];
	} else {
		return [x, y];
	}
}