Skip to content

Commit 3307101

Browse files
author
Walle Cyril
committed
set up collision system
1 parent e52ed59 commit 3307101

5 files changed

Lines changed: 55 additions & 16 deletions

File tree

general/wasm/js/main.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
import {d} from "../node_modules/dom99/source/dom99.js";
2-
import {generateRandomWorld, drawWorld, updateWorld} from "./world.js";
2+
import {generateRandomWorld, generateSimpleWorld, drawWorld, updateWorld} from "./world.js";
33
import {reduceDraw, initializeCanvas} from "./canvas.js";
44

5+
let drawEverything;
56

6-
const drawEverything = reduceDraw(d.elements.canvas, [
7-
drawWorld
8-
]);
9-
10-
const world = generateRandomWorld();
7+
const world = generateSimpleWorld(800, 400) || generateRandomWorld(800, 400);
118

9+
let animationFrameId = 0;
1210
const loop = function () {
13-
requestAnimationFrame(loop);
11+
animationFrameId = requestAnimationFrame(loop);
1412
drawEverything(world);
1513
updateWorld(world);
1614
};
1715

1816
d.start(
1917
{
20-
18+
playPause: function () {
19+
if (animationFrameId === 0) {
20+
animationFrameId = requestAnimationFrame(loop);
21+
d.feed(`playPause`, `Pause`);
22+
} else {
23+
cancelAnimationFrame(animationFrameId);
24+
animationFrameId = 0;
25+
d.feed(`playPause`, `Play`);
26+
}
27+
28+
}
2129
}, // functions
2230
{
2331

@@ -29,5 +37,8 @@ d.start(
2937

3038
d.elements.loadingHint.remove();
3139
initializeCanvas(d.elements.canvas);
32-
requestAnimationFrame(loop);
40+
drawEverything = reduceDraw(d.elements.canvas, [
41+
drawWorld
42+
]);
43+
animationFrameId = requestAnimationFrame(loop);
3344
});

general/wasm/js/world.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export {generateRandomWorld, drawWorld, updateWorld};
1+
2+
/*
3+
A world is a rectangle
4+
Inside a world there are living creatures with
5+
speed, direction, etc
6+
*/
7+
export {generateRandomWorld, drawWorld, updateWorld, generateSimpleWorld};
28

39
import {
410
randomPositiveInt,
@@ -8,6 +14,7 @@ import {
814
import {
915
fillArrayWithFunctionResult
1016
} from "../node_modules/utilsac/utility.js";
17+
import {Collisions, Circle, Polygon, Point} from "../node_modules/collisions/src/Collisions.mjs";
1118

1219

1320
/*
@@ -19,11 +26,24 @@ const drawWorld = function (context, world) {
1926
});
2027
};
2128

22-
/*
23-
A world is a rectangle
24-
Inside a world there are living creatures with
25-
speed, direction, etc
26-
*/
29+
30+
31+
const generateSimpleWorld = function (width = 300, height = 300) {
32+
const world = {
33+
width,
34+
height,
35+
creatures : [{
36+
width: 30,
37+
height: 20,
38+
speedX: 0.5,
39+
speedY: 0.1,
40+
x: 15,
41+
y: 15
42+
}]
43+
};
44+
return world;
45+
};
46+
2747
const generateRandomWorld = function (width = 300, height = 300) {
2848
const populationSize = 25;
2949
const world = {
@@ -39,6 +59,8 @@ const generateRandomWorld = function (width = 300, height = 300) {
3959
return world;
4060
};
4161

62+
63+
4264
const generateRandomCreature = function (maxX, maxY) {
4365
const maxSize = 5;
4466
const maxSpeed2d = 1

general/wasm/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

general/wasm/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"license": "ISC",
1010
"devDependencies": {},
1111
"dependencies": {
12+
"collisions": "^2.0.13",
1213
"dom99": "^14.4.0",
1314
"utilsac": "^7.0.0"
1415
}

general/wasm/wasm-disabled.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<h1>wasm-disabled</h1>
1515

1616
<p data-element="loadingHint">Loading ...</p>
17-
17+
<button data-function="playPause" data-variable="playPause">Pause</button>
1818
<canvas data-element="canvas"></canvas>
1919

2020

0 commit comments

Comments
 (0)