diff --git a/engine/lib/Signal.ts b/engine/lib/Signal.ts index 44ce2ee..b871e6d 100644 --- a/engine/lib/Signal.ts +++ b/engine/lib/Signal.ts @@ -21,7 +21,7 @@ export function createSignal(value: T): [() => T, (value: T) => void] { return [getter, setter]; } -export function createEffect(callback: () => void) { +export function createEffect(callback: () => void): void { current = callback; callback(); current = null; diff --git a/engine/system/physics.ts b/engine/system/physics.ts index 66bcf3b..1234659 100644 --- a/engine/system/physics.ts +++ b/engine/system/physics.ts @@ -224,13 +224,7 @@ export function moveAndCollide( delta: number, ): Array { const colliders = $("collider")!; - const positions = $("position")!; - const self = colliders.get(id)!.value; - const pos = positions.get(id)!.value; - - self.aabb.position.x = pos.x; - self.aabb.position.y = pos.y; const slice = self.buffer.slice; slice.length = 0; @@ -283,6 +277,7 @@ export function moveAndSlide( continue; // TODO } + // FIXME // const ts = Math.min(0, time - 0.001); // velocity.x += velocity.x * ts; // velocity.y += velocity.y * ts; diff --git a/game/entities/hero/entity.ts b/game/entities/hero/entity.ts index ebe847f..347d279 100644 --- a/game/entities/hero/entity.ts +++ b/game/entities/hero/entity.ts @@ -2,7 +2,7 @@ import { defineEntity } from "#/engine/Entity.ts"; import { keyboardInput } from "#/engine/Keyboard.ts"; import { createEffect, createSignal } from "#/engine/lib/Signal.ts"; import { pointer } from "#/engine/Pointer.ts"; -import { createRect } from "#/engine/Rect.ts"; +import { createRect, fillRect, strokeRect } from "#/engine/Rect.ts"; import $ from "#/engine/Scene.ts"; import { createSprite, @@ -10,6 +10,7 @@ import { defineSpriteSheet, } from "#/engine/Sprite.ts"; import { + type Collider, createCollider, moveAndSlide, Shape, @@ -20,6 +21,7 @@ import { scale, type Vector, } from "#/engine/Vector.ts"; +import { viewport } from "#/engine/Viewport.ts"; import charIdle from "#/game/assets/char-idle.png"; const [speed, setSpeed] = createSignal(0.1); @@ -42,6 +44,7 @@ let onceGuard = true; process((id, delta) => { const position = $("position").get(id)!.value; const velocity = $("velocity").get(id)!.value; + const collider = $("collider").get(id)!.value; if (pointer.isDown) { position.x = pointer.position.x; @@ -70,4 +73,7 @@ process((id, delta) => { velocity.x = 0; velocity.y = 0; + + fillRect(viewport.ctx, collider.aabb, "orange"); + strokeRect(viewport.ctx, collider.aabb, "red"); }); diff --git a/game/entities/portal/entity.ts b/game/entities/portal/entity.ts index e4f7914..d788b00 100644 --- a/game/entities/portal/entity.ts +++ b/game/entities/portal/entity.ts @@ -25,12 +25,7 @@ const process = defineEntity("portal", { }); process((id, delta) => { - const position = $("position").get(id)!.value; const collider = $("collider").get(id)!.value; - const cPos = collider.aabb.position; - - cPos.x = position.x; - cPos.y = position.y; fillRect(viewport.ctx, collider.aabb, "orange"); strokeRect(viewport.ctx, collider.aabb, "red"); diff --git a/main.ts b/main.ts index 964e0b9..8d52764 100644 --- a/main.ts +++ b/main.ts @@ -1,6 +1,6 @@ import { listenKeyboard } from "#/engine/Keyboard.ts"; import { listenPointer } from "#/engine/Pointer.ts"; -import { currentScene, setScene } from "#/engine/Scene.ts"; +import $, { currentScene, setScene } from "#/engine/Scene.ts"; import { animateSprites } from "#/engine/system/animateSprites.ts"; import { drawSprites } from "#/engine/system/drawSprites.ts"; import { @@ -9,6 +9,20 @@ import { resizeCanvas, viewport, } from "#/engine/Viewport.ts"; +import type { Collider } from "./engine/system/physics"; +import type { Vector } from "./engine/Vector"; + +function updateAABBs() { + const colliders = $("collider"); + const positions = $("position"); + + for (const [id, collider] of colliders.entries()) { + const position = positions.get(id)!.value; + const aabb = collider.value.aabb; + aabb.position.x = position.x; + aabb.position.y = position.y; + } +} let then = self.performance.now(); let delta = 0; @@ -17,6 +31,9 @@ function animate(timestamp: number): void { then = timestamp; resetCtx(); + + updateAABBs(); + currentScene.process(viewport.ctx, delta); animateSprites(delta);