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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
import * as state from '/static/games/stallman-shooter/state.js';
import * as constants from '/static/games/stallman-shooter/constants.js';
let events = [];
window.addEventListener('keydown', (e) => events.push(['keydown', e.key]));
window.addEventListener('keyup', (e) => events.push(['keyup', e.key]));
const duration = 1000 / 60;
function frame(ctx, st, events, textures) {
const new_bullets = [];
const count = st.count;
for (const enemy of st.enemies) {
new_bullets.push(...enemy.act(count));
}
for (const bullet of st.bullets) {
state.straight_bullet_act(bullet);
}
for (const bullet of st.player_bullets) {
state.straight_bullet_act(bullet);
}
st.player.act(events);
if (count % st.player.fire_rate === 0) {
st.player_bullets.push(new state.PBullet(st.player.x, 0, st.player.y, -3.5));
}
for (const bullet of st.bullets) {
if (state.overlaps(st.player, bullet)) {
return 'loss';
}
}
for (const bullet of new_bullets) {
if (state.overlaps(st.player, bullet)) {
return 'loss';
}
}
for (const enemy of st.enemies) {
for (const bullet of st.player_bullets) {
if (state.overlaps(enemy, bullet)) {
enemy.injure(bullet.damage());
state.straight_bullet_delete(bullet);
}
}
}
st.enemies = st.enemies.filter((enemy) => enemy.health > 0);
if (st.enemies.length === 0) {
return 'win';
}
st.bullets = st.bullets.filter((bullet) => !state.outside(bullet));
st.bullets.push(...new_bullets);
st.player_bullets = st.player_bullets.filter((bullet) => !state.outside(bullet));
for (const bullet of st.player_bullets) {
state.draw(bullet, ctx, textures.pbullet);
}
for (const enemy of st.enemies) {
state.draw(enemy, ctx, textures.enemy);
}
state.draw(st.player, ctx, textures.player);
for (const bullet of st.bullets) {
state.draw(bullet, ctx, textures.ebullet);
}
ctx.strokeStyle = "red";
ctx.strokeRect(0, 0, Math.round(st.enemies[0].health * constants.x_dim), 20);
st.count++;
return 'nothing';
}
async function get_bitmap(id) {
const image = document.getElementById(id);
await image.decode();
return await window.createImageBitmap(image);
}
async function get_player_bitmap() {
return get_bitmap('stallman');
}
async function get_enemy_bitmap() {
return get_bitmap('gates');
}
async function get_ebullet_bitmap() {
return get_bitmap('binary-blob');
}
async function get_pbullet_surface() {
return get_bitmap('player-pepp');
}
async function generic_message(ctx, image, okay) {
const bitmap = await get_bitmap(image);
while (true) {
await new Promise(window.requestAnimationFrame);
const ev = events;
events = [];
for (const event of ev) {
const [type, keycode] = event;
if (type === 'keydown' && keycode === okay) {
return;
}
}
ctx.drawImage(bitmap, 0, 0);
}
}
async function introduction(ctx) {
await generic_message(ctx, 'introduction', 'Enter');
}
async function loss(ctx) {
await generic_message(ctx, 'loss', 'r');
}
async function victory(ctx) {
await generic_message(ctx, 'victory', 'r');
}
async function main() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
await introduction(ctx);
while (true) {
const st = new state.State();
const [
player_bitmap,
enemy_bitmap,
ebullet_bitmap,
pbullet_bitmap,
] = await Promise.all([
get_bitmap('stallman'),
get_bitmap('gates'),
get_bitmap('binary-blob'),
get_bitmap('player-pepp'),
]);
const textures = {
player: player_bitmap,
enemy: enemy_bitmap,
ebullet: ebullet_bitmap,
pbullet: pbullet_bitmap,
};
let last_frame_time = null;
let accumulator = 0;
game: while (true) {
const now = await new Promise(requestAnimationFrame);
if (last_frame_time !== null) {
accumulator += now - last_frame_time;
}
last_frame_time = now;
while (accumulator >= duration) {
accumulator -= duration;
ctx.fillStyle = "white";
ctx.fillRect(0, 0, constants.x_dim, constants.y_dim);
const result = frame(ctx, st, events, textures);
events = [];
if (result === 'loss') {
await loss(ctx);
break game;
}
if (result === 'win') {
await victory(ctx);
break game;
}
}
}
}
}
main()
|