Skip to content

Commit 93b1b96

Browse files
committed
small bugs
1 parent ee3b060 commit 93b1b96

File tree

6 files changed

+167
-2
lines changed

6 files changed

+167
-2
lines changed

Parts/Children/BoxCollider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class BoxCollider extends Collider {
2828

2929
for (let i = 0; i < 4; i++) {
3030
this.rotatedCorners.push(new Vector(0, 0));
31+
this.vertices.push(new Vector(0, 0));
3132
}
3233
}
3334

@@ -57,7 +58,7 @@ export class BoxCollider extends Collider {
5758
const y = c.x * sin + c.y * cos + transform.worldPosition.y;
5859

5960
this.rotatedCorners[i].set(x, y);
60-
61+
this.vertices[i].set(x - transform.worldPosition.x, y - transform.worldPosition.y);
6162
if (x < minX) minX = x;
6263
if (x > maxX) maxX = x;
6364
if (y < minY) minY = y;

Parts/Children/Collider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export abstract class Collider extends Part {
1111
radius: number;
1212
realWorldStart: Vector;
1313
realWorldEnd: Vector;
14+
vertices: Vector[];
1415

1516
constructor({ tag }: { tag?: string }) {
1617
super({ name: "Collider" });
@@ -20,6 +21,7 @@ export abstract class Collider extends Part {
2021
this.radius = 0;
2122
this.realWorldStart = new Vector(0, 0);
2223
this.realWorldEnd = new Vector(0, 0);
24+
this.vertices = [];
2325
}
2426

2527
setTag(tag: string) {

Parts/Children/PolygonCollider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class PolygonCollider extends Collider {
1313
super({ tag: tag });
1414
this.name = "PolygonCollider";
1515
this.localVertices = vertices;
16-
16+
this.vertices = vertices;
1717
let maxDist = 0;
1818
for (let i = 0; i < this.localVertices.length; i++) {
1919
for (let j = i + 1; j < this.localVertices.length; j++) {

engine/bundle.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11998,6 +11998,7 @@ class Collider extends Part {
1199811998
radius;
1199911999
realWorldStart;
1200012000
realWorldEnd;
12001+
vertices;
1200112002
constructor({ tag }) {
1200212003
super({ name: "Collider" });
1200312004
this.type = "Collider";
@@ -12006,6 +12007,7 @@ class Collider extends Part {
1200612007
this.radius = 0;
1200712008
this.realWorldStart = new Vector(0, 0);
1200812009
this.realWorldEnd = new Vector(0, 0);
12010+
this.vertices = [];
1200912011
}
1201012012
setTag(tag) {
1201112013
this.tag = tag;
@@ -12135,6 +12137,7 @@ class PolygonCollider extends Collider {
1213512137
super({ tag });
1213612138
this.name = "PolygonCollider";
1213712139
this.localVertices = vertices;
12140+
this.vertices = vertices;
1213812141
let maxDist = 0;
1213912142
for (let i = 0;i < this.localVertices.length; i++) {
1214012143
for (let j = i + 1;j < this.localVertices.length; j++) {
@@ -12259,6 +12262,7 @@ class BoxCollider extends Collider {
1225912262
this.type = "BoxCollider";
1226012263
for (let i = 0;i < 4; i++) {
1226112264
this.rotatedCorners.push(new Vector(0, 0));
12265+
this.vertices.push(new Vector(0, 0));
1226212266
}
1226312267
}
1226412268
get worldVertices() {
@@ -12281,6 +12285,7 @@ class BoxCollider extends Collider {
1228112285
const x = c.x * cos - c.y * sin + transform.worldPosition.x;
1228212286
const y = c.x * sin + c.y * cos + transform.worldPosition.y;
1228312287
this.rotatedCorners[i].set(x, y);
12288+
this.vertices[i].set(x - transform.worldPosition.x, y - transform.worldPosition.y);
1228412289
if (x < minX)
1228512290
minX = x;
1228612291
if (x > maxX)

engine/editor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24978,6 +24978,7 @@ Defaulting to 2020, but this will stop working in the future.`);
2497824978
radius;
2497924979
realWorldStart;
2498024980
realWorldEnd;
24981+
vertices;
2498124982
constructor({ tag }) {
2498224983
super({ name: "Collider" });
2498324984
this.type = "Collider";
@@ -24986,6 +24987,7 @@ Defaulting to 2020, but this will stop working in the future.`);
2498624987
this.radius = 0;
2498724988
this.realWorldStart = new Vector(0, 0);
2498824989
this.realWorldEnd = new Vector(0, 0);
24990+
this.vertices = [];
2498924991
}
2499024992
setTag(tag) {
2499124993
this.tag = tag;
@@ -25113,6 +25115,7 @@ Defaulting to 2020, but this will stop working in the future.`);
2511325115
super({ tag });
2511425116
this.name = "PolygonCollider";
2511525117
this.localVertices = vertices;
25118+
this.vertices = vertices;
2511625119
let maxDist = 0;
2511725120
for (let i = 0;i < this.localVertices.length; i++) {
2511825121
for (let j = i + 1;j < this.localVertices.length; j++) {
@@ -25235,6 +25238,7 @@ Defaulting to 2020, but this will stop working in the future.`);
2523525238
this.type = "BoxCollider";
2523625239
for (let i = 0;i < 4; i++) {
2523725240
this.rotatedCorners.push(new Vector(0, 0));
25241+
this.vertices.push(new Vector(0, 0));
2523825242
}
2523925243
}
2524025244
get worldVertices() {
@@ -25257,6 +25261,7 @@ Defaulting to 2020, but this will stop working in the future.`);
2525725261
const x = c.x * cos - c.y * sin + transform.worldPosition.x;
2525825262
const y = c.x * sin + c.y * cos + transform.worldPosition.y;
2525925263
this.rotatedCorners[i].set(x, y);
25264+
this.vertices[i].set(x - transform.worldPosition.x, y - transform.worldPosition.y);
2526025265
if (x < minX)
2526125266
minX = x;
2526225267
if (x > maxX)

engine/test.html

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<html lang="en">
2+
<head>
3+
<meta charset="UTF-8">
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5+
<title>Document</title>
6+
</head>
7+
<body>
8+
<canvas id="gameCanvas"></canvas>
9+
<script type = "module">
10+
import * as Forge from '/engine/bundle.js';
11+
12+
13+
const { Vector, PolygonCollider, Part, GameObject, Game, Scene, Layer, PhysicsBody, PhysicsEngine, ColorRender, Transform, BoxCollider, Camera} = Forge;
14+
15+
16+
const game = new Game({
17+
name: "my game",
18+
canvas: document.getElementById('gameCanvas'),
19+
width: 800,
20+
height: 600,
21+
showFrameStats: "BASIC"
22+
});
23+
24+
const scene = new Scene({
25+
name: "scene 1"
26+
});
27+
28+
const pe = new PhysicsEngine({
29+
gravity: {
30+
x: 0,
31+
y: 1,
32+
scale: 0.001
33+
}
34+
});
35+
const camera = new Camera({
36+
name: "cam1"
37+
});
38+
39+
const l1 = new Layer({
40+
name: "layer 1",
41+
});
42+
43+
class PolySpawner extends Part {
44+
constructor({ name, bottomRange, topRange, polyParent }) {
45+
super({ name });
46+
this.bottomRange = bottomRange;
47+
this.topRange = topRange;
48+
this.spawned = false;
49+
this.polyParent = polyParent;
50+
}
51+
52+
static singular = true;
53+
static properties = {
54+
name: { type: "text", default: "Part" },
55+
bottomRange: { type: "number", default: 0 },
56+
topRange: { type: "number", default: 100 },
57+
polyParent: { type: "Part", default: null, subType: "Layer" },
58+
};
59+
randomPolygonPoints(sides, radius) {
60+
const points = [];
61+
const angleStep = (Math.PI * 2) / sides;
62+
for (let i = 0; i < sides; i++) {
63+
const angle = i * angleStep;
64+
points.push(new Vector(
65+
Math.cos(angle) * radius,
66+
Math.sin(angle) * radius
67+
));
68+
}
69+
return points;
70+
}
71+
act(d) {
72+
super.act(d);
73+
if (!this.spawned) {
74+
this.spawned = true;
75+
// Spawn the polygons
76+
const numPolygons = Math.floor(Math.random() * (this.topRange - this.bottomRange + 1)) + this.bottomRange;
77+
for (let i = 0; i < numPolygons; i++) {
78+
const sides = Math.floor(Math.random() * 5) + 3; // 3 to 7 sides
79+
const radius = Math.random() * 40 + 20; // 20 to 60 px
80+
const x = Math.random() * (this.top.width - 100) + 50;
81+
const y = Math.random() * 200 + 50;
82+
const color = `hsl(${Math.random() * 360}, 80%, 60%)`;
83+
84+
const nGon = new GameObject({
85+
name: `nGon${i}`
86+
});
87+
const points = this.randomPolygonPoints(sides, radius);
88+
nGon.addChildren(
89+
new Transform({
90+
position: new Vector(x, y),
91+
rotation: Math.random() * Math.PI * 2
92+
}),
93+
new PolygonCollider({
94+
vertices: points,
95+
}),
96+
new ColorRender({
97+
vertices: points,
98+
color: color
99+
}),
100+
new PhysicsBody({
101+
isStatic: false,
102+
restitution: 0.5, // Bounciness
103+
})
104+
);
105+
// Add components to the polygon
106+
this.polyParent.addChild(nGon);
107+
}
108+
}
109+
}
110+
}
111+
112+
const polySpawner = new PolySpawner({
113+
name: "polySpawner",
114+
bottomRange: 1,
115+
topRange: 5,
116+
polyParent: l1
117+
});
118+
119+
const go = new GameObject({
120+
name: "block"
121+
});
122+
123+
const transform = new Transform({
124+
position: new Vector(0, 300),
125+
rotation: 0,
126+
scale: Vector.From(1)
127+
});
128+
const cr = new ColorRender({
129+
width: 300,
130+
height: 50,
131+
color: "red"
132+
})
133+
const bc = new BoxCollider({
134+
width: 300,
135+
height: 50
136+
});
137+
const pb = new PhysicsBody({
138+
isStatic: true,
139+
restitution: 0.8
140+
});
141+
142+
go.addChildren(transform, cr, bc, pb);
143+
l1.addChild(go);
144+
145+
scene.addChildren(pe, camera, l1, polySpawner);
146+
147+
game.addChild(scene);
148+
149+
game.start();
150+
</script>
151+
</body>
152+
</html>

0 commit comments

Comments
 (0)