This commit is contained in:
Schluffe
2026-04-01 10:48:11 +02:00
parent 512072688a
commit ea0acab180
4 changed files with 48 additions and 6 deletions
+28
View File
@@ -0,0 +1,28 @@
type Current = null | (() => void);
let current: Current = null;
export function createSignal<T>(value: T): [() => T, (value: T) => void] {
let innerValue = value;
const observers = new Set<() => void>();
const getter = () => {
current !== null && observers.add(current);
return innerValue;
};
const setter = (value: T) => {
innerValue = value;
for (const observer of observers) {
observer();
}
};
return [getter, setter];
}
export function createEffect(callback: () => void) {
current = callback;
callback();
current = null;
}
+3 -3
View File
@@ -283,9 +283,9 @@ export function moveAndSlide(
continue; // TODO continue; // TODO
} }
const ts = Math.min(0, time - 0.001); // const ts = Math.min(0, time - 0.001);
velocity.x += velocity.x * ts; // velocity.x += velocity.x * ts;
velocity.y += velocity.y * ts; // velocity.y += velocity.y * ts;
const dot = getDotProduct(velocity, collision.normal); const dot = getDotProduct(velocity, collision.normal);
const rt = 1 - time + 0.001; const rt = 1 - time + 0.001;
+16 -2
View File
@@ -1,5 +1,6 @@
import { defineEntity } from "#/engine/Entity.ts"; import { defineEntity } from "#/engine/Entity.ts";
import { keyboardInput } from "#/engine/Keyboard.ts"; import { keyboardInput } from "#/engine/Keyboard.ts";
import { createEffect, createSignal } from "#/engine/lib/Signal.ts";
import { pointer } from "#/engine/Pointer.ts"; import { pointer } from "#/engine/Pointer.ts";
import { createRect } from "#/engine/Rect.ts"; import { createRect } from "#/engine/Rect.ts";
import $ from "#/engine/Scene.ts"; import $ from "#/engine/Scene.ts";
@@ -21,7 +22,7 @@ import {
} from "#/engine/Vector.ts"; } from "#/engine/Vector.ts";
import charIdle from "#/game/assets/char-idle.png"; import charIdle from "#/game/assets/char-idle.png";
const speed = 0.1; const [speed, setSpeed] = createSignal(0.1);
const idleSrc = await defineSpriteSheet(charIdle, 2, 4); const idleSrc = await defineSpriteSheet(charIdle, 2, 4);
const process = defineEntity("hero", { const process = defineEntity("hero", {
@@ -32,6 +33,12 @@ const process = defineEntity("hero", {
collider: createCollider(Shape.Rect, createRect(createVector(), 16, 16)), collider: createCollider(Shape.Rect, createRect(createVector(), 16, 16)),
}); });
createEffect(() => {
console.log(speed());
});
let onceGuard = true;
process((id, delta) => { process((id, delta) => {
const position = $<Vector>("position").get(id)!.value; const position = $<Vector>("position").get(id)!.value;
const velocity = $<Vector>("velocity").get(id)!.value; const velocity = $<Vector>("velocity").get(id)!.value;
@@ -39,8 +46,15 @@ process((id, delta) => {
if (pointer.isDown) { if (pointer.isDown) {
position.x = pointer.position.x; position.x = pointer.position.x;
position.y = pointer.position.y; position.y = pointer.position.y;
if (onceGuard) {
onceGuard = false;
setSpeed(0.1);
}
return; return;
} }
onceGuard = true;
velocity.x -= keyboardInput.arrowLeft ? 1 : 0; velocity.x -= keyboardInput.arrowLeft ? 1 : 0;
velocity.x += keyboardInput.arrowRight ? 1 : 0; velocity.x += keyboardInput.arrowRight ? 1 : 0;
@@ -48,7 +62,7 @@ process((id, delta) => {
velocity.y += keyboardInput.arrowDown ? 1 : 0; velocity.y += keyboardInput.arrowDown ? 1 : 0;
normalize(velocity); normalize(velocity);
scale(velocity, speed); scale(velocity, speed());
moveAndSlide(id, velocity, delta); moveAndSlide(id, velocity, delta);
position.x += velocity.x * delta; position.x += velocity.x * delta;
+1 -1
View File
@@ -17,7 +17,7 @@
"init": { "init": {
"position": { "position": {
"x": 24, "x": 24,
"y": 40 "y": 44
} }
} }
}, },