This commit is contained in:
Schluffe
2026-03-17 21:09:17 +01:00
parent a2af7b9940
commit 3242e8cbbb
6 changed files with 57 additions and 34 deletions
+37 -9
View File
@@ -1,4 +1,7 @@
import type { Circle } from "#/engine/Circle.ts";
import type MapProxy from "#/engine/lib/MapProxy.ts";
import type { Polygon } from "#/engine/Polygon.ts";
import type { Rect } from "#/engine/Rect.ts";
import $ from "#/engine/Scene.ts";
import type { Vector } from "#/engine/Vector.ts";
@@ -8,26 +11,51 @@ const POLYGON = 2;
type Shape = 0 | 1 | 2;
export type Collider<T> = {
export type Collider = {
shape: Shape;
body: T;
};
export type Collision = {};
export type Collision = {
id: string;
};
export function moveAndCollide<T>(
collider: Collider<T>,
const collisions = new Map<string, Collision>();
function check(
id: string,
collider: Collider,
position: Vector,
colliders: MapProxy<string, Collider>,
): string | undefined {
for (const [oid, other] of colliders.entries()) {
if (oid !== id && other) {
return oid;
}
}
}
export function moveAndCollide(
id: string,
velocity: Vector,
delta: number,
): Collision {
return {};
): Collision | undefined {
return collisions.get(id);
}
export function processPhysics(
ctx: CanvasRenderingContext2D,
delta: number,
): void {
for (const [id, collider] of $<Collider<unknown>>("collider")!.entries()) {
const position = $<Vector>("position")!.get(id);
const colliders = $<Collider>("collider")!;
const positions = $<Vector>("position")!;
for (const [id, collider] of colliders.entries()) {
const position = positions.get(id)!;
const target = check(id, collider.value, position.value, colliders);
if (target !== undefined) {
collisions.set(id, { id: target });
} else {
collisions.delete(id);
}
}
}