@dryanovski/gamefoo 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/animate.d.ts +129 -0
- package/dist/core/animate.d.ts.map +1 -0
- package/dist/core/asset.d.ts +59 -0
- package/dist/core/asset.d.ts.map +1 -0
- package/dist/core/behaviour.d.ts +118 -0
- package/dist/core/behaviour.d.ts.map +1 -1
- package/dist/core/behaviours/collidable.d.ts +203 -4
- package/dist/core/behaviours/collidable.d.ts.map +1 -1
- package/dist/core/behaviours/control.d.ts +51 -3
- package/dist/core/behaviours/control.d.ts.map +1 -1
- package/dist/core/behaviours/healtkit.d.ts +120 -3
- package/dist/core/behaviours/healtkit.d.ts.map +1 -1
- package/dist/core/behaviours/sprite_render.d.ts +141 -0
- package/dist/core/behaviours/sprite_render.d.ts.map +1 -0
- package/dist/core/camera.d.ts +101 -0
- package/dist/core/camera.d.ts.map +1 -1
- package/dist/core/engine.d.ts +377 -15
- package/dist/core/engine.d.ts.map +1 -1
- package/dist/core/fonts/font_bitmap.d.ts +156 -0
- package/dist/core/fonts/font_bitmap.d.ts.map +1 -0
- package/dist/core/fonts/font_bitmap_prebuild.d.ts +102 -0
- package/dist/core/fonts/font_bitmap_prebuild.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_3x5.d.ts +76 -0
- package/dist/core/fonts/internal/font_3x5.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_4x6.d.ts +76 -0
- package/dist/core/fonts/internal/font_4x6.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_5x5.d.ts +79 -0
- package/dist/core/fonts/internal/font_5x5.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_6x8.d.ts +76 -0
- package/dist/core/fonts/internal/font_6x8.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_8x13.d.ts +76 -0
- package/dist/core/fonts/internal/font_8x13.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_8x8.d.ts +76 -0
- package/dist/core/fonts/internal/font_8x8.d.ts.map +1 -0
- package/dist/core/game_object_register.d.ts +101 -1
- package/dist/core/game_object_register.d.ts.map +1 -1
- package/dist/core/input.d.ts +131 -0
- package/dist/core/input.d.ts.map +1 -1
- package/dist/core/sprite.d.ts +232 -0
- package/dist/core/sprite.d.ts.map +1 -0
- package/dist/core/utils/perlin_noise.d.ts +136 -0
- package/dist/core/utils/perlin_noise.d.ts.map +1 -0
- package/dist/core/world.d.ts +147 -0
- package/dist/core/world.d.ts.map +1 -1
- package/dist/debug/monitor.d.ts +12 -0
- package/dist/debug/monitor.d.ts.map +1 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/log.d.ts +33 -0
- package/dist/decorators/log.d.ts.map +1 -0
- package/dist/entities/dynamic_entity.d.ts +82 -0
- package/dist/entities/dynamic_entity.d.ts.map +1 -1
- package/dist/entities/entity.d.ts +216 -11
- package/dist/entities/entity.d.ts.map +1 -1
- package/dist/entities/player.d.ts +76 -0
- package/dist/entities/player.d.ts.map +1 -1
- package/dist/entities/text.d.ts +52 -0
- package/dist/entities/text.d.ts.map +1 -0
- package/dist/index.d.ts +29 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -604
- package/dist/index.js.map +3 -15
- package/dist/subsystems/camera_system.d.ts +25 -0
- package/dist/subsystems/camera_system.d.ts.map +1 -0
- package/dist/subsystems/collision_system.d.ts +16 -0
- package/dist/subsystems/collision_system.d.ts.map +1 -0
- package/dist/subsystems/monitor_system.d.ts +17 -0
- package/dist/subsystems/monitor_system.d.ts.map +1 -0
- package/dist/subsystems/object_system.d.ts +18 -0
- package/dist/subsystems/object_system.d.ts.map +1 -0
- package/dist/subsystems/types.d.ts +40 -0
- package/dist/subsystems/types.d.ts.map +1 -0
- package/dist/types.d.ts +140 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +25 -11
package/dist/index.js
CHANGED
|
@@ -1,610 +1,29 @@
|
|
|
1
|
-
// core/behaviour.ts
|
|
2
|
-
class Behaviour {
|
|
3
|
-
owner;
|
|
4
|
-
priority = 1;
|
|
5
|
-
enabled = true;
|
|
6
|
-
get key() {
|
|
7
|
-
return this.type.toLowerCase();
|
|
8
|
-
}
|
|
9
|
-
constructor(owner) {
|
|
10
|
-
this.owner = owner;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
// core/behaviours/collidable.ts
|
|
14
|
-
class Collidable extends Behaviour {
|
|
15
|
-
type = "collidable";
|
|
16
|
-
shape;
|
|
17
|
-
layer = 0;
|
|
18
|
-
tags = new Set;
|
|
19
|
-
collidesWith = new Set;
|
|
20
|
-
solid = false;
|
|
21
|
-
fixed = false;
|
|
22
|
-
onCollision;
|
|
23
|
-
world;
|
|
24
|
-
constructor(owner, world, options) {
|
|
25
|
-
super(owner);
|
|
26
|
-
this.world = world;
|
|
27
|
-
const size = owner.getSize();
|
|
28
|
-
this.shape = options.shape;
|
|
29
|
-
this.shape = options.shape ?? {
|
|
30
|
-
type: "aabb",
|
|
31
|
-
width: size.width,
|
|
32
|
-
height: size.height
|
|
33
|
-
};
|
|
34
|
-
this.layer = options.layer ?? 0;
|
|
35
|
-
this.tags = options.tags ?? new Set;
|
|
36
|
-
this.solid = options.solid ?? false;
|
|
37
|
-
this.fixed = options.fixed ?? false;
|
|
38
|
-
this.collidesWith = options.collidesWith ?? new Set;
|
|
39
|
-
this.onCollision = options.onCollision || (() => {});
|
|
40
|
-
}
|
|
41
|
-
update(_deltaTime) {}
|
|
42
|
-
onAttach() {
|
|
43
|
-
this.world.register(this);
|
|
44
|
-
}
|
|
45
|
-
onDetach() {
|
|
46
|
-
this.world.unregister(this);
|
|
47
|
-
}
|
|
48
|
-
getOwner() {
|
|
49
|
-
return this.owner;
|
|
50
|
-
}
|
|
51
|
-
getWorldBounds() {
|
|
52
|
-
const pos = this.owner.getPosition();
|
|
53
|
-
const offset = "offset" in this.shape && this.shape.offset ? this.shape.offset : { x: 0, y: 0 };
|
|
54
|
-
if (this.shape.type === "aabb") {
|
|
55
|
-
return {
|
|
56
|
-
x: pos.x + offset.x,
|
|
57
|
-
y: pos.y + offset.y,
|
|
58
|
-
width: this.shape.width,
|
|
59
|
-
height: this.shape.height
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
const r = this.shape.radius;
|
|
63
|
-
return {
|
|
64
|
-
x: pos.x + offset.x - r,
|
|
65
|
-
y: pos.y + offset.y - r,
|
|
66
|
-
width: r * 2,
|
|
67
|
-
height: r * 2
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
// core/behaviours/control.ts
|
|
72
|
-
class Control extends Behaviour {
|
|
73
|
-
type = "control";
|
|
74
|
-
input;
|
|
75
|
-
speed = 500;
|
|
76
|
-
constructor(owner, input) {
|
|
77
|
-
super(owner);
|
|
78
|
-
this.input = input;
|
|
79
|
-
}
|
|
80
|
-
update(deltaTime) {
|
|
81
|
-
let dx = 0;
|
|
82
|
-
let dy = 0;
|
|
83
|
-
if (this.input.isKeyDown("a") || this.input.isKeyDown("arrowleft"))
|
|
84
|
-
dx -= 1;
|
|
85
|
-
if (this.input.isKeyDown("d") || this.input.isKeyDown("arrowright"))
|
|
86
|
-
dx += 1;
|
|
87
|
-
if (this.input.isKeyDown("w") || this.input.isKeyDown("arrowup"))
|
|
88
|
-
dy -= 1;
|
|
89
|
-
if (this.input.isKeyDown("s") || this.input.isKeyDown("arrowdown"))
|
|
90
|
-
dy += 1;
|
|
91
|
-
const len = Math.sqrt(dx * dx + dy * dy);
|
|
92
|
-
if (len > 0) {
|
|
93
|
-
this.owner.x += dx / len * this.speed * deltaTime;
|
|
94
|
-
this.owner.y += dy / len * this.speed * deltaTime;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
// core/behaviours/healtkit.ts
|
|
99
|
-
class HealthKit extends Behaviour {
|
|
100
|
-
type = "healthkit";
|
|
101
|
-
health;
|
|
102
|
-
maxHP;
|
|
103
|
-
constructor(owner, health, maxHP) {
|
|
104
|
-
super(owner);
|
|
105
|
-
this.health = health;
|
|
106
|
-
this.maxHP = maxHP || health;
|
|
107
|
-
}
|
|
108
|
-
update(_deltaTime) {}
|
|
109
|
-
takeDamage(amount) {
|
|
110
|
-
this.health = Math.max(0, this.health - amount);
|
|
111
|
-
}
|
|
112
|
-
heal(amount) {
|
|
113
|
-
this.health = Math.min(this.maxHP, this.health + amount);
|
|
114
|
-
}
|
|
115
|
-
getHealth() {
|
|
116
|
-
return this.health;
|
|
117
|
-
}
|
|
118
|
-
getMaxHealth() {
|
|
119
|
-
return this.maxHP;
|
|
120
|
-
}
|
|
121
|
-
setMaxHealth(value) {
|
|
122
|
-
this.maxHP = value;
|
|
123
|
-
if (this.health > this.maxHP) {
|
|
124
|
-
this.health = this.maxHP;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
isDead() {
|
|
128
|
-
return this.health <= 0;
|
|
129
|
-
}
|
|
130
|
-
getHealthPercent() {
|
|
131
|
-
return this.maxHP > 0 ? this.health / this.maxHP : 0;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
// core/camera.ts
|
|
135
|
-
class Camera {
|
|
136
|
-
x = 0;
|
|
137
|
-
y = 0;
|
|
138
|
-
width;
|
|
139
|
-
height;
|
|
140
|
-
constructor(width, height) {
|
|
141
|
-
this.width = width;
|
|
142
|
-
this.height = height;
|
|
143
|
-
}
|
|
144
|
-
follow(target) {
|
|
145
|
-
this.x = target.x;
|
|
146
|
-
this.y = target.y;
|
|
147
|
-
}
|
|
148
|
-
moveTo(target) {
|
|
149
|
-
this.x = target.x;
|
|
150
|
-
this.y = target.y;
|
|
151
|
-
}
|
|
152
|
-
getPosition() {
|
|
153
|
-
return { x: this.x, y: this.y };
|
|
154
|
-
}
|
|
155
|
-
getViewRect() {
|
|
156
|
-
return {
|
|
157
|
-
x: this.x - this.width / 2,
|
|
158
|
-
y: this.y - this.height / 2,
|
|
159
|
-
width: this.width,
|
|
160
|
-
height: this.height
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
resize(width, height) {
|
|
164
|
-
this.width = width;
|
|
165
|
-
this.height = height;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
// core/game_object_register.ts
|
|
169
|
-
class GameObjectRegister {
|
|
170
|
-
objects = new Map;
|
|
171
|
-
register(object) {
|
|
172
|
-
this.objects.set(object.id, object);
|
|
173
|
-
}
|
|
174
|
-
get(id) {
|
|
175
|
-
return this.objects.get(id);
|
|
176
|
-
}
|
|
177
|
-
has(id) {
|
|
178
|
-
return this.objects.has(id);
|
|
179
|
-
}
|
|
180
|
-
getAll(_filter) {
|
|
181
|
-
return Array.from(this.objects.values()).filter(_filter);
|
|
182
|
-
}
|
|
183
|
-
updateAll(deltaTime) {
|
|
184
|
-
this.getAll(() => true).forEach((obj) => {
|
|
185
|
-
obj.update(deltaTime);
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
renderAll(ctx) {
|
|
189
|
-
this.getAll(() => true).forEach((obj) => {
|
|
190
|
-
obj.render(ctx);
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// core/world.ts
|
|
196
|
-
class World {
|
|
197
|
-
colliders = new Set;
|
|
198
|
-
register(collider) {
|
|
199
|
-
this.colliders.add(collider);
|
|
200
|
-
}
|
|
201
|
-
unregister(collider) {
|
|
202
|
-
this.colliders.delete(collider);
|
|
203
|
-
}
|
|
204
|
-
detect() {
|
|
205
|
-
const list = Array.from(this.colliders);
|
|
206
|
-
const len = list.length;
|
|
207
|
-
for (let i = 0;i < len; i++) {
|
|
208
|
-
const obj = list[i];
|
|
209
|
-
if (!obj?.enabled)
|
|
210
|
-
continue;
|
|
211
|
-
for (let j = i + 1;j < len; j++) {
|
|
212
|
-
const other = list[j];
|
|
213
|
-
if (!other?.enabled)
|
|
214
|
-
continue;
|
|
215
|
-
if (obj.layer !== other.layer)
|
|
216
|
-
continue;
|
|
217
|
-
const objWantOther = this.tagsOverlap(obj.collidesWith, other.tags);
|
|
218
|
-
const otherWantObj = this.tagsOverlap(other.collidesWith, obj.tags);
|
|
219
|
-
const boundsObj = obj.getWorldBounds();
|
|
220
|
-
const boundsOther = other.getWorldBounds();
|
|
221
|
-
if (!this.intersects(obj, boundsObj, other, boundsOther))
|
|
222
|
-
continue;
|
|
223
|
-
if (obj.solid && other.solid) {
|
|
224
|
-
this.resolveOverlap(obj, boundsObj, other, boundsOther);
|
|
225
|
-
}
|
|
226
|
-
if (objWantOther && obj.onCollision) {
|
|
227
|
-
obj.onCollision({
|
|
228
|
-
self: obj.getOwner(),
|
|
229
|
-
other: other.getOwner(),
|
|
230
|
-
selfTags: obj.tags,
|
|
231
|
-
otherTags: other.tags
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
if (otherWantObj && other.onCollision) {
|
|
235
|
-
other.onCollision({
|
|
236
|
-
self: other.getOwner(),
|
|
237
|
-
other: obj.getOwner(),
|
|
238
|
-
selfTags: other.tags,
|
|
239
|
-
otherTags: obj.tags
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
tagsOverlap(wants, has) {
|
|
246
|
-
for (const tag of wants) {
|
|
247
|
-
if (has.has(tag))
|
|
248
|
-
return true;
|
|
249
|
-
}
|
|
250
|
-
return false;
|
|
251
|
-
}
|
|
252
|
-
intersects(a, boundsA, b, boundsB) {
|
|
253
|
-
const shapeA = a.shape;
|
|
254
|
-
const shapeB = b.shape;
|
|
255
|
-
if (shapeA.type === "aabb" && shapeB.type === "aabb") {
|
|
256
|
-
return this.aabbVSAabb(boundsA, boundsB);
|
|
257
|
-
}
|
|
258
|
-
if (shapeA.type === "circle" && shapeB.type === "circle") {
|
|
259
|
-
return this.circleVSCircle(a, boundsA, b, boundsB);
|
|
260
|
-
}
|
|
261
|
-
const [circle, circleBounds, rect] = shapeA.type === "circle" ? [a, boundsA, boundsB] : [b, boundsB, boundsA];
|
|
262
|
-
return this.circleVSAAabb(circle, circleBounds, rect);
|
|
263
|
-
}
|
|
264
|
-
aabbVSAabb(a, b) {
|
|
265
|
-
return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
|
|
266
|
-
}
|
|
267
|
-
circleVSCircle(a, boundsA, b, boundsB) {
|
|
268
|
-
if (a.shape.type !== "circle" || b.shape.type !== "circle")
|
|
269
|
-
return false;
|
|
270
|
-
const cx1 = boundsA.x + a.shape.radius;
|
|
271
|
-
const cy1 = boundsA.y + a.shape.radius;
|
|
272
|
-
const cx2 = boundsB.x + b.shape.radius;
|
|
273
|
-
const cy2 = boundsB.y + b.shape.radius;
|
|
274
|
-
const dx = cx2 - cx1;
|
|
275
|
-
const dy = cy2 - cy1;
|
|
276
|
-
const distSq = dx * dx + dy * dy;
|
|
277
|
-
const radSum = a.shape.radius + b.shape.radius;
|
|
278
|
-
return distSq <= radSum * radSum;
|
|
279
|
-
}
|
|
280
|
-
circleVSAAabb(circle, circleBounds, rect) {
|
|
281
|
-
if (circle.shape.type !== "circle")
|
|
282
|
-
return false;
|
|
283
|
-
const cx = circleBounds.x + circle.shape.radius;
|
|
284
|
-
const cy = circleBounds.y + circle.shape.radius;
|
|
285
|
-
const closestX = Math.max(rect.x, Math.min(cx, rect.x + rect.width));
|
|
286
|
-
const closestY = Math.max(rect.y, Math.min(cy, rect.y + rect.height));
|
|
287
|
-
const dx = cx - closestX;
|
|
288
|
-
const dy = cy - closestY;
|
|
289
|
-
return dx * dx + dy * dy <= circle.shape.radius * circle.shape.radius;
|
|
290
|
-
}
|
|
291
|
-
resolveOverlap(a, boundsA, b, boundsB) {
|
|
292
|
-
const overlapX = Math.min(boundsA.x + boundsA.width - boundsB.x, boundsB.x + boundsB.width - boundsA.x);
|
|
293
|
-
const overlapY = Math.min(boundsA.y + boundsA.height - boundsB.y, boundsB.y + boundsB.height - boundsA.y);
|
|
294
|
-
let pushX = 0;
|
|
295
|
-
let pushY = 0;
|
|
296
|
-
if (overlapX < overlapY) {
|
|
297
|
-
pushX = boundsA.x < boundsB.x ? -overlapX : overlapX;
|
|
298
|
-
} else {
|
|
299
|
-
pushY = boundsA.y < boundsB.y ? -overlapY : overlapY;
|
|
300
|
-
}
|
|
301
|
-
const ownerA = a.getOwner();
|
|
302
|
-
const ownerB = b.getOwner();
|
|
303
|
-
if (a.fixed && b.fixed) {
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
if (a.fixed) {
|
|
307
|
-
ownerB.x -= pushX;
|
|
308
|
-
ownerB.y -= pushY;
|
|
309
|
-
} else if (b.fixed) {
|
|
310
|
-
ownerA.x += pushX;
|
|
311
|
-
ownerA.y += pushY;
|
|
312
|
-
} else {
|
|
313
|
-
ownerA.x += pushX / 2;
|
|
314
|
-
ownerA.y += pushY / 2;
|
|
315
|
-
ownerB.x -= pushX / 2;
|
|
316
|
-
ownerB.y -= pushY / 2;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// core/engine.ts
|
|
322
|
-
class Engine {
|
|
323
|
-
canvas;
|
|
324
|
-
ctx;
|
|
325
|
-
lastTime = 0;
|
|
326
|
-
width;
|
|
327
|
-
height;
|
|
328
|
-
_initialized = false;
|
|
329
|
-
running = false;
|
|
330
|
-
cnf = {
|
|
331
|
-
backgroundColor: "#000000"
|
|
332
|
-
};
|
|
333
|
-
_player;
|
|
334
|
-
engine;
|
|
335
|
-
constructor(canvasId, width, height, config) {
|
|
336
|
-
this.canvas = document.getElementById(canvasId);
|
|
337
|
-
this.height = height;
|
|
338
|
-
this.width = width;
|
|
339
|
-
this.canvas.width = width;
|
|
340
|
-
this.canvas.height = height;
|
|
341
|
-
const context = this.canvas.getContext("2d");
|
|
342
|
-
if (!context) {
|
|
343
|
-
throw new Error("Failed to get 2D context");
|
|
344
|
-
}
|
|
345
|
-
this.ctx = context;
|
|
346
|
-
this.cnf = { ...this.cnf, ...config };
|
|
347
|
-
this.engine = {
|
|
348
|
-
camera: new Camera(this.width, this.height),
|
|
349
|
-
objects: new GameObjectRegister,
|
|
350
|
-
collisions: new World
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
set player(player) {
|
|
354
|
-
this._player = player;
|
|
355
|
-
}
|
|
356
|
-
get player() {
|
|
357
|
-
return this._player;
|
|
358
|
-
}
|
|
359
|
-
get collisions() {
|
|
360
|
-
return this.engine.collisions;
|
|
361
|
-
}
|
|
362
|
-
attachObjects(objects) {
|
|
363
|
-
this.engine.objects.register(objects);
|
|
364
|
-
}
|
|
365
|
-
handleResize() {
|
|
366
|
-
if (!this.canvas)
|
|
367
|
-
return;
|
|
368
|
-
const container = this.canvas.parentElement;
|
|
369
|
-
if (!container)
|
|
370
|
-
return;
|
|
371
|
-
const containerWidth = container.clientWidth;
|
|
372
|
-
const containerHeight = container.clientHeight;
|
|
373
|
-
const scaleX = containerWidth / this.width;
|
|
374
|
-
const scaleY = containerHeight / this.height;
|
|
375
|
-
const scale = Math.min(scaleX, scaleY);
|
|
376
|
-
const offsetX = (containerWidth - this.width * scale) / 2;
|
|
377
|
-
const offsetY = (containerHeight - this.height * scale) / 2;
|
|
378
|
-
this.canvas.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(${scale})`;
|
|
379
|
-
}
|
|
380
|
-
resize(width, height) {
|
|
381
|
-
this.canvas.width = width;
|
|
382
|
-
this.canvas.height = height;
|
|
383
|
-
}
|
|
384
|
-
loop(timestamp) {
|
|
385
|
-
if (!this.running) {
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
if (this.lastTime === 0) {
|
|
389
|
-
this.lastTime = timestamp;
|
|
390
|
-
}
|
|
391
|
-
const deltaTime = (timestamp - this.lastTime) / 1000;
|
|
392
|
-
this.lastTime = timestamp;
|
|
393
|
-
this.update(deltaTime);
|
|
394
|
-
this.render();
|
|
395
|
-
requestAnimationFrame((timestamp2) => this.loop(timestamp2));
|
|
396
|
-
}
|
|
397
|
-
async setup(setupFn) {
|
|
398
|
-
if (this._initialized) {
|
|
399
|
-
console.warn("Engine is already initialized.");
|
|
400
|
-
return;
|
|
401
|
-
}
|
|
402
|
-
if (typeof setupFn !== "function") {
|
|
403
|
-
throw new Error("Setup function must be provided and must be a function.");
|
|
404
|
-
}
|
|
405
|
-
this.lastTime = 0;
|
|
406
|
-
setupFn();
|
|
407
|
-
this._initialized = true;
|
|
408
|
-
this.running = true;
|
|
409
|
-
requestAnimationFrame((timestamp) => this.loop(timestamp));
|
|
410
|
-
}
|
|
411
|
-
update(deltaTime) {
|
|
412
|
-
if (this.player) {
|
|
413
|
-
this.player.update(deltaTime);
|
|
414
|
-
}
|
|
415
|
-
if (this.engine.objects) {
|
|
416
|
-
this.engine.objects.updateAll(deltaTime);
|
|
417
|
-
}
|
|
418
|
-
this.engine.collisions.detect();
|
|
419
|
-
if (this.engine.camera && this.player) {
|
|
420
|
-
this.engine.camera.follow(this.player.getPosition());
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
render() {
|
|
424
|
-
this.ctx.clearRect(0, 0, this.width, this.height);
|
|
425
|
-
this.ctx.fillStyle = this.cnf.backgroundColor || "#000000";
|
|
426
|
-
this.ctx.fillRect(0, 0, this.width, this.height);
|
|
427
|
-
if (this.player) {
|
|
428
|
-
this.player.render(this.ctx);
|
|
429
|
-
}
|
|
430
|
-
if (this.engine.objects) {
|
|
431
|
-
this.engine.objects.renderAll(this.ctx);
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
pause() {
|
|
435
|
-
this.running = false;
|
|
436
|
-
}
|
|
437
|
-
clear() {
|
|
438
|
-
this.running = false;
|
|
439
|
-
this.ctx.clearRect(0, 0, this.width, this.height);
|
|
440
|
-
}
|
|
441
|
-
destroy() {}
|
|
442
|
-
}
|
|
443
|
-
// core/input.ts
|
|
444
|
-
class Input {
|
|
445
|
-
keys = new Set;
|
|
446
|
-
mouseButtons = new Set;
|
|
447
|
-
mousePosition = { x: 0, y: 0 };
|
|
448
|
-
constructor() {
|
|
449
|
-
window.addEventListener("keydown", (e) => {
|
|
450
|
-
this.keys.add(e.key.toLowerCase());
|
|
451
|
-
});
|
|
452
|
-
window.addEventListener("keyup", (e) => {
|
|
453
|
-
this.keys.delete(e.key.toLowerCase());
|
|
454
|
-
});
|
|
455
|
-
window.addEventListener("mousedown", (e) => {
|
|
456
|
-
this.mouseButtons.add(e.button);
|
|
457
|
-
});
|
|
458
|
-
window.addEventListener("mouseup", (e) => {
|
|
459
|
-
this.mouseButtons.delete(e.button);
|
|
460
|
-
});
|
|
461
|
-
window.addEventListener("mousemove", (e) => {
|
|
462
|
-
this.mousePosition = { x: e.clientX, y: e.clientY };
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
isKeyDown(key) {
|
|
466
|
-
return this.keys.has(key.toLowerCase());
|
|
467
|
-
}
|
|
468
|
-
getPressedKeys() {
|
|
469
|
-
return new Set(this.keys);
|
|
470
|
-
}
|
|
471
|
-
isMouseButtonDown(button) {
|
|
472
|
-
return this.mouseButtons.has(button);
|
|
473
|
-
}
|
|
474
|
-
getMousePosition() {
|
|
475
|
-
return { ...this.mousePosition };
|
|
476
|
-
}
|
|
477
|
-
reset() {
|
|
478
|
-
this.keys.clear();
|
|
479
|
-
this.mouseButtons.clear();
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
// entities/entity.ts
|
|
483
|
-
class Entity {
|
|
484
|
-
id = "";
|
|
485
|
-
position = { x: 0, y: 0 };
|
|
486
|
-
size = { width: 0, height: 0 };
|
|
487
|
-
behaviorMap = new Map;
|
|
488
|
-
_sortedBehaviors = null;
|
|
489
|
-
get x() {
|
|
490
|
-
return this.position.x;
|
|
491
|
-
}
|
|
492
|
-
set x(value) {
|
|
493
|
-
this.position.x = value;
|
|
494
|
-
}
|
|
495
|
-
get y() {
|
|
496
|
-
return this.position.y;
|
|
497
|
-
}
|
|
498
|
-
set y(value) {
|
|
499
|
-
this.position.y = value;
|
|
500
|
-
}
|
|
501
|
-
constructor(id, x, y, width, height) {
|
|
502
|
-
this.id = id;
|
|
503
|
-
this.position = { x, y };
|
|
504
|
-
this.size = { width, height };
|
|
505
|
-
}
|
|
506
|
-
getPosition() {
|
|
507
|
-
return { ...this.position };
|
|
508
|
-
}
|
|
509
|
-
getSize() {
|
|
510
|
-
return { ...this.size };
|
|
511
|
-
}
|
|
512
|
-
getBehaviour(key) {
|
|
513
|
-
return this.behaviorMap.get(key.toLowerCase());
|
|
514
|
-
}
|
|
515
|
-
getBehavioursByType(type) {
|
|
516
|
-
return this.behaviors.filter((b) => b instanceof type);
|
|
517
|
-
}
|
|
518
|
-
hasBehaviour(key) {
|
|
519
|
-
return this.behaviorMap.has(key.toLowerCase());
|
|
520
|
-
}
|
|
521
|
-
attachBehaviour(behavior) {
|
|
522
|
-
this.behaviorMap.set(behavior.key, behavior);
|
|
523
|
-
this._sortedBehaviors = null;
|
|
524
|
-
if (behavior.onAttach) {
|
|
525
|
-
behavior.onAttach();
|
|
526
|
-
}
|
|
527
|
-
return behavior;
|
|
528
|
-
}
|
|
529
|
-
detachBehaviour(key) {
|
|
530
|
-
const behavior = this.behaviorMap.get(key.toLowerCase());
|
|
531
|
-
if (!behavior)
|
|
532
|
-
return;
|
|
533
|
-
if (behavior.onDetach) {
|
|
534
|
-
behavior.onDetach();
|
|
535
|
-
}
|
|
536
|
-
this.behaviorMap.delete(key.toLowerCase());
|
|
537
|
-
this._sortedBehaviors = null;
|
|
538
|
-
}
|
|
539
|
-
get behaviors() {
|
|
540
|
-
if (!this._sortedBehaviors) {
|
|
541
|
-
this._sortedBehaviors = Array.from(this.behaviorMap.values()).sort((a, b) => a.priority - b.priority);
|
|
542
|
-
}
|
|
543
|
-
return this._sortedBehaviors;
|
|
544
|
-
}
|
|
545
|
-
updateBehaviours(deltaTime) {
|
|
546
|
-
for (const behavior of this.behaviors) {
|
|
547
|
-
if (behavior.enabled) {
|
|
548
|
-
behavior.update(deltaTime);
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
renderBehaviours(ctx) {
|
|
553
|
-
for (const behavior of this.behaviors) {
|
|
554
|
-
if (behavior.enabled && behavior.render) {
|
|
555
|
-
behavior.render(ctx);
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
// entities/dynamic_entity.ts
|
|
562
|
-
class DynamicEntity extends Entity {
|
|
563
|
-
velocity = { x: 0, y: 0 };
|
|
564
|
-
speed = 0;
|
|
565
|
-
setVelocity(velocity) {
|
|
566
|
-
this.velocity = velocity;
|
|
567
|
-
}
|
|
568
|
-
getVelocity() {
|
|
569
|
-
return { ...this.velocity };
|
|
570
|
-
}
|
|
571
|
-
setSpeed(speed) {
|
|
572
|
-
this.speed = speed;
|
|
573
|
-
}
|
|
574
|
-
getSpeed() {
|
|
575
|
-
return this.speed;
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
// entities/player.ts
|
|
579
|
-
class Player extends DynamicEntity {
|
|
580
|
-
get control() {
|
|
581
|
-
return this.getBehaviour("control");
|
|
582
|
-
}
|
|
583
|
-
get healthkit() {
|
|
584
|
-
return this.getBehaviour("healthkit");
|
|
585
|
-
}
|
|
586
|
-
update(deltaTime) {
|
|
587
|
-
this.updateBehaviours(deltaTime);
|
|
588
|
-
}
|
|
589
|
-
render(ctx) {
|
|
590
|
-
ctx.fillStyle = "blue";
|
|
591
|
-
ctx.fillRect(this.x, this.y, this.size.width, this.size.height);
|
|
592
|
-
this.renderBehaviours(ctx);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
1
|
export {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
2
|
+
log,
|
|
3
|
+
default11 as World,
|
|
4
|
+
default16 as Text,
|
|
5
|
+
default3 as SpriteRender,
|
|
6
|
+
default10 as Sprite,
|
|
7
|
+
default15 as Player,
|
|
8
|
+
PerlinNoise,
|
|
9
|
+
ObjectSystem,
|
|
10
|
+
MonitorSystem,
|
|
11
|
+
default12 as Monitor,
|
|
12
|
+
default9 as Input,
|
|
599
13
|
HealthKit,
|
|
600
|
-
GameObjectRegister,
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
14
|
+
default8 as GameObjectRegister,
|
|
15
|
+
default7 as FontBitmapPrebuild,
|
|
16
|
+
default6 as FontBitmap,
|
|
17
|
+
default14 as Entity,
|
|
18
|
+
default5 as Engine,
|
|
19
|
+
default13 as DynamicEntity,
|
|
604
20
|
Control,
|
|
21
|
+
CollisionSystem,
|
|
605
22
|
Collidable,
|
|
606
|
-
|
|
607
|
-
|
|
23
|
+
CameraSystem,
|
|
24
|
+
default4 as Camera,
|
|
25
|
+
Behaviour,
|
|
26
|
+
default2 as Asset
|
|
608
27
|
};
|
|
609
28
|
|
|
610
|
-
//# debugId=
|
|
29
|
+
//# debugId=B36FB65262DD014964756E2164756E21
|