From 9b75d41bea6ff789a87eb13ebe3f225a0a3eaecc Mon Sep 17 00:00:00 2001 From: Schluffe Date: Tue, 14 Apr 2026 21:39:59 +0200 Subject: [PATCH] WIP --- engine/system/physics.ts | 10 +++++----- game/entities/portal/entity.ts | 6 +++--- game/entities/testEntity/entity.ts | 26 +++++++++++++++++++------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/engine/system/physics.ts b/engine/system/physics.ts index 4dc41eb..1042d68 100644 --- a/engine/system/physics.ts +++ b/engine/system/physics.ts @@ -6,6 +6,7 @@ import { createVector, getDotProduct, invScale, + scale, type Vector, } from "#/engine/Vector.ts"; @@ -527,9 +528,9 @@ export function moveAndSlide( break; } - const scale = Math.max(0, time - 0.001); - position.x += velocity.x * delta * timeLeft * scale; - position.y += velocity.y * delta * timeLeft * scale; + const scalar = Math.max(0, time - 0.001) * delta * timeLeft; + position.x += velocity.x * scalar; + position.y += velocity.y * scalar; const dot = getDotProduct(velocity, closest.normal); velocity.x -= closest.normal.x * dot; @@ -538,6 +539,5 @@ export function moveAndSlide( timeLeft *= 1 - time; } - velocity.x *= timeLeft; - velocity.y *= timeLeft; + scale(velocity, timeLeft); } diff --git a/game/entities/portal/entity.ts b/game/entities/portal/entity.ts index f526f6b..9126d74 100644 --- a/game/entities/portal/entity.ts +++ b/game/entities/portal/entity.ts @@ -10,11 +10,11 @@ import { createVector } from "#/engine/Vector.ts"; import portal from "#/game/assets/portal.png"; const portalSrc = await defineSpriteSheet(portal, 3, 1); -const position = createVector(); defineEntity("portal", { - position, + position: createVector(), + velocity: createVector(), + collider: createCollider(Shape.Circle, createCircle(8)), sprite: createSprite(portalSrc, createVector(-8, -8)), animation: createSpriteAnimation(400, 0), - collider: createCollider(Shape.Circle, createCircle(8)), }); diff --git a/game/entities/testEntity/entity.ts b/game/entities/testEntity/entity.ts index b701ce5..737cf47 100644 --- a/game/entities/testEntity/entity.ts +++ b/game/entities/testEntity/entity.ts @@ -1,4 +1,5 @@ import { defineEntity } from "#/engine/Entity.ts"; +import { createRect } from "#/engine/Rect.ts"; import $ from "#/engine/Scene.ts"; import { createSpriteAnimation, @@ -6,6 +7,11 @@ import { type Sprite, type SpriteAnimation, } from "#/engine/Sprite.ts"; +import { + type Collider, + createCollider, + Shape, +} from "#/engine/system/physics.ts"; import { createVector, type Vector } from "#/engine/Vector.ts"; import { viewport } from "#/engine/Viewport.ts"; import charIdle from "#/game/assets/char-idle.png"; @@ -40,10 +46,10 @@ const animations = { walk: createSpriteAnimation(250, 2), }; -const position = createVector(); - const process = defineEntity<{ position: Vector; + velocity: Vector; + collider: Collider; animation: SpriteAnimation; animations: { idle: SpriteAnimation; @@ -56,7 +62,9 @@ const process = defineEntity<{ }; stuff: number; }>("testEntity", { - position, + position: createVector(), + velocity: createVector(), + collider: createCollider(Shape.Rect, createRect(16, 16)), animation: animations.idle, animations: animations, sprite: sprites.idle, @@ -65,17 +73,21 @@ const process = defineEntity<{ }); process((id, _delta) => { - const position = $("position").get(id)!; + const position = $("position").get(id)!.value; + const velocity = $("velocity").get(id)!.value; + const collider = $("collider").get(id)!.value; const stuff = $("stuff").get(id)!; const sprite = $("sprite").get(id)!; const animation = $("animation").get(id)!; - position.value.x += stuff.value * 0.5; - if (position.value.x > viewport.canvas.width - 16) { + velocity.x = stuff.value * 0.5; + position.x += velocity.x; + + if (position.x + collider.offset.x > viewport.canvas.width - 16) { stuff.value = -1; animation.value = animations.walk; sprite.value = sprites.walk; - } else if (position.value.x < 0) { + } else if (position.x + collider.offset.x < 0) { stuff.value = 1; animation.value = animations.idle; sprite.value = sprites.idle;