WIP
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -283,9 +283,9 @@ export function moveAndSlide(
|
||||
continue; // TODO
|
||||
}
|
||||
|
||||
const ts = Math.min(0, time - 0.001);
|
||||
velocity.x += velocity.x * ts;
|
||||
velocity.y += velocity.y * ts;
|
||||
// const ts = Math.min(0, time - 0.001);
|
||||
// velocity.x += velocity.x * ts;
|
||||
// velocity.y += velocity.y * ts;
|
||||
|
||||
const dot = getDotProduct(velocity, collision.normal);
|
||||
const rt = 1 - time + 0.001;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
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 $ from "#/engine/Scene.ts";
|
||||
@@ -21,7 +22,7 @@ import {
|
||||
} from "#/engine/Vector.ts";
|
||||
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 process = defineEntity("hero", {
|
||||
@@ -32,6 +33,12 @@ const process = defineEntity("hero", {
|
||||
collider: createCollider(Shape.Rect, createRect(createVector(), 16, 16)),
|
||||
});
|
||||
|
||||
createEffect(() => {
|
||||
console.log(speed());
|
||||
});
|
||||
|
||||
let onceGuard = true;
|
||||
|
||||
process((id, delta) => {
|
||||
const position = $<Vector>("position").get(id)!.value;
|
||||
const velocity = $<Vector>("velocity").get(id)!.value;
|
||||
@@ -39,8 +46,15 @@ process((id, delta) => {
|
||||
if (pointer.isDown) {
|
||||
position.x = pointer.position.x;
|
||||
position.y = pointer.position.y;
|
||||
|
||||
if (onceGuard) {
|
||||
onceGuard = false;
|
||||
setSpeed(0.1);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
onceGuard = true;
|
||||
|
||||
velocity.x -= keyboardInput.arrowLeft ? 1 : 0;
|
||||
velocity.x += keyboardInput.arrowRight ? 1 : 0;
|
||||
@@ -48,7 +62,7 @@ process((id, delta) => {
|
||||
velocity.y += keyboardInput.arrowDown ? 1 : 0;
|
||||
|
||||
normalize(velocity);
|
||||
scale(velocity, speed);
|
||||
scale(velocity, speed());
|
||||
moveAndSlide(id, velocity, delta);
|
||||
|
||||
position.x += velocity.x * delta;
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
"init": {
|
||||
"position": {
|
||||
"x": 24,
|
||||
"y": 40
|
||||
"y": 44
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user