Skip to content

Commit b7e2d5a

Browse files
Refactored examples. Added collision detection.
1 parent 8e25730 commit b7e2d5a

File tree

16 files changed

+985
-228
lines changed

16 files changed

+985
-228
lines changed

src/Collider.ts

Lines changed: 564 additions & 0 deletions
Large diffs are not rendered by default.

src/Renderer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ export class Renderer {
2929

3030
if (pixelMap.has(key)) {
3131
const existingPixel = pixelMap.get(key)!;
32-
33-
if (pixel.color.a >= 0.999) {
32+
const threshold = 0.999;
33+
if (pixel.color.a >= threshold) {
3434
pixelMap.set(key, pixel);
3535
}
36-
else if (existingPixel.color.a < 0.999) {
36+
else if (existingPixel.color.a < threshold) {
3737
const blended = Renderer.blendPixel(existingPixel, pixel);
3838
pixelMap.set(key, blended);
3939
}

src/examples.ts

Lines changed: 0 additions & 220 deletions
This file was deleted.

src/examples/BasicShapes.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { CanvasDrawer } from "../Utils.js";
2+
import { Renderer } from "../Renderer.js";
3+
import { Collection } from "../shapes/Collection.js";
4+
import { Circle } from "../shapes/Circle.js";
5+
import { Rectangle } from "../shapes/Rectangle.js"
6+
import { Polygon } from "../shapes/Polygon.js";
7+
import { RegularPolygon } from "../shapes/RegularPolygon.js";
8+
import { LineSegment } from "../shapes/LineSegment.js";
9+
10+
export function runBasicShapesExample() {
11+
const width = 64;
12+
const height = 64;
13+
const drawer = new CanvasDrawer('myCanvas', width, height, 0);
14+
drawer.clear('#ffffff');
15+
16+
const renderer = new Renderer(width, height, { r: 255, g: 255, b: 255, a: 1 });
17+
18+
const scene = new Collection({ x: 0, y: 0, color: { r: 0, g: 0, b: 0, a: 1 } });
19+
20+
// Add basic shapes
21+
const circle = new Circle({
22+
x: 16, y: 16,
23+
color: { r: 255, g: 0, b: 0, a: 1 },
24+
radius: 8,
25+
fill: true
26+
});
27+
scene.addShape(circle);
28+
29+
const rectangle = new Rectangle({
30+
x: 40, y: 10,
31+
color: { r: 0, g: 255, b: 0, a: 1 },
32+
width: 12, height: 16,
33+
fill: true
34+
});
35+
scene.addShape(rectangle);
36+
37+
const polygon = new Polygon({
38+
x: 20, y: 48,
39+
color: { r: 0, g: 0, b: 255, a: 1 },
40+
vertices: [
41+
{ x: 0, y: -8, relative: true },
42+
{ x: 8, y: 8, relative: true },
43+
{ x: -8, y: 8, relative: true },
44+
],
45+
fill: true
46+
});
47+
scene.addShape(polygon);
48+
49+
const polygon2 = new Polygon({
50+
x: 35, y: 28,
51+
color: { r: 0, g: 255, b: 255, a: 1 },
52+
vertices: [
53+
{ x: 0, y: -8, relative: true },
54+
{ x: 8, y: 8, relative: true },
55+
{ x: -8, y: 8, relative: true },
56+
{ x: -16, y: 0, relative: true },
57+
],
58+
fill: true
59+
});
60+
scene.addShape(polygon2);
61+
62+
const regular_polygon = new RegularPolygon({
63+
x: 48,
64+
y: 48,
65+
sides: 6,
66+
sideLength: 8,
67+
fill: true,
68+
color: { r: 255, g: 165, b: 0, a: 1 }
69+
});
70+
scene.addShape(regular_polygon);
71+
72+
const line = new LineSegment({
73+
x: 0, y: 20,
74+
x2: 33, y2: 43,
75+
color: { r: 125, g: 0, b: 120, a: 1 },
76+
});
77+
scene.addShape(line);
78+
const start = performance.now();
79+
const pixels = renderer.render([scene], {
80+
screen_width: width,
81+
screen_height: height,
82+
antialias: true
83+
});
84+
const end = performance.now();
85+
console.log(`Avg: ${(end - start).toFixed(2)} ms`);
86+
87+
for (const px of pixels) {
88+
drawer.setPixel(px.x, px.y, px.color);
89+
}
90+
}

src/examples/Collision.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { CanvasDrawer } from '../Utils.js';
2+
import { Renderer } from '../Renderer.js';
3+
import { Collection } from '../shapes/Collection.js';
4+
import { Rectangle } from '../shapes/Rectangle.js';
5+
import { Circle } from '../shapes/Circle.js';
6+
7+
export function runCollisionExample() {
8+
const width = 64;
9+
const height = 64;
10+
const drawer = new CanvasDrawer('myCanvas', width, height, 1);
11+
drawer.clear('#ffffff');
12+
13+
const renderer = new Renderer(width, height, { r: 255, g: 255, b: 255, a: 1 });
14+
15+
const scene = new Collection({ x: 0, y: 0, color: { r: 0, g: 0, b: 0, a: 1 } });
16+
17+
const ground = new Rectangle({
18+
x: 0, y: 56,
19+
color: { r: 100, g: 100, b: 100, a: 1 },
20+
width: 64, height: 8,
21+
fill: true
22+
});
23+
scene.addShape(ground);
24+
ground.addCollider();
25+
26+
const fallingBox = new Rectangle({
27+
x: 28, y: 0,
28+
color: { r: 200, g: 0, b: 0, a: 1 },
29+
width: 8, height: 8,
30+
fill: true
31+
});
32+
scene.addShape(fallingBox);
33+
fallingBox.addCollider();
34+
35+
const fallingSphere = new Circle({
36+
x: 25, y: 20,
37+
color: { r: 0, g: 0, b: 200, a: 1 },
38+
radius: 6,
39+
fill: true
40+
});
41+
scene.addShape(fallingSphere);
42+
fallingSphere.addCollider();
43+
44+
let count = 0;
45+
let sum = 0;
46+
setInterval(() => {
47+
fallingBox.translate(0, 1);
48+
fallingSphere.translate(0, 1);
49+
if (fallingBox.intersects(ground) || fallingBox.intersects(fallingSphere)) {
50+
fallingBox.translate(0, -1); // Move back up
51+
}
52+
if (fallingSphere.intersects(ground)) {
53+
fallingSphere.translate(0, -1); // Move back up
54+
}
55+
const start = performance.now();
56+
const pixels = renderer.render([scene], { screen_height: height, screen_width: width, antialias: true });
57+
const end = performance.now();
58+
sum += end - start;
59+
count++;
60+
if (count % 60 == 0) {
61+
console.log(`Avg: ${(sum / count).toFixed(2)} ms`);
62+
count = 0;
63+
sum = 0;
64+
}
65+
66+
drawer.clear('#ffffff');
67+
for (const pixel of pixels) {
68+
drawer.setPixel(pixel.x, pixel.y, pixel.color);
69+
}
70+
71+
}, 1000 / 10);
72+
}

0 commit comments

Comments
 (0)