diff --git a/engine/Vector.ts b/engine/Vector.ts index c902940..840e509 100644 --- a/engine/Vector.ts +++ b/engine/Vector.ts @@ -8,17 +8,11 @@ export function createVector(x: number = 0, y: number = 0): Vector { } export function copyVector(vector: Vector): Vector { - return { - x: vector.x, - y: vector.y, - }; + return { x: vector.x, y: vector.y }; } -export function fromPolar(angle: number, magnitude: number): Vector { - return { - x: Math.cos(angle) * magnitude, - y: Math.sin(angle) * magnitude, - }; +export function fromPolar(angle: number, mag: number): Vector { + return { x: Math.cos(angle) * mag, y: Math.sin(angle) * mag }; } export function getAngle(vector: Vector): number { @@ -36,33 +30,23 @@ export function getDistance(a: Vector, b: Vector): number { return Math.sqrt(dx * dx + dy * dy); } -export function getDistanceSquared(a: Vector, b: Vector): number { +export function getDistanceSqrt(a: Vector, b: Vector): number { const dx = a.x - b.x; const dy = a.y - b.y; return dx * dx + dy * dy; } -export function getSquared(vector: Vector): number { +export function getSqrt(vector: Vector): number { return vector.x * vector.x + vector.y * vector.y; } export function setUnit(unit: Vector, a: Vector, b: Vector): void { - const dx = a.x - b.x; - const dy = a.y - b.y; - const inv = 1.0 / Math.sqrt(dx * dx + dy * dy); - - unit.x = dx * inv; - unit.y = dy * inv; + invScale(unit, a.x - b.x, a.y - b.y); } export function setUnitNormal(unit: Vector, a: Vector, b: Vector): void { - const nx = a.y - b.y; - const ny = b.x - a.x; - const inv = 1.0 / Math.sqrt(nx * nx + ny * ny); - - unit.x = nx * inv; - unit.y = ny * inv; + invScale(unit, a.y - b.y, b.x - a.x); } export function getDotProduct(a: Vector, b: Vector): number { @@ -79,17 +63,25 @@ export function scale(vector: Vector, scalar: number): void { } export function normalize(vector: Vector): void { - const inv = 1.0 / Math.sqrt(vector.x * vector.x + vector.y * vector.y); - vector.x *= inv; - vector.y *= inv; + invScale(vector, vector.x, vector.y); +} + +export function invScale(vector: Vector, x: number, y: number): void { + if (x === 0 && y === 0) { + return; + } + + const inv = 1.0 / Math.sqrt(x * x + y * y); + vector.x = x * inv; + vector.y = y * inv; } export function isZero(vector: Vector): boolean { return vector.x === 0 && vector.y === 0; } -export function isBelowThreshold(vector: Vector, threshold: number): boolean { - return Math.abs(vector.x) < threshold && Math.abs(vector.y) < threshold; +export function isBelowAbs(vector: Vector, abs: number): boolean { + return Math.abs(vector.x) < abs && Math.abs(vector.y) < abs; } export function equals(a: Vector, b: Vector): boolean { diff --git a/game/entities/hero/entity.ts b/game/entities/hero/entity.ts index 8b4fd8e..e63ddd1 100644 --- a/game/entities/hero/entity.ts +++ b/game/entities/hero/entity.ts @@ -13,7 +13,12 @@ import { moveAndCollide, Shape, } from "#/engine/system/physics.ts"; -import { createVector, type Vector } from "#/engine/Vector.ts"; +import { + createVector, + normalize, + scale, + type Vector, +} from "#/engine/Vector.ts"; import charIdle from "#/game/assets/char-idle.png"; const speed = 0.05; @@ -36,10 +41,12 @@ process((id, delta) => { position.y = pointer.position.y; } - velocity.x -= keyboardInput.arrowLeft ? speed : 0; - velocity.x += keyboardInput.arrowRight ? speed : 0; - velocity.y -= keyboardInput.arrowUp ? speed : 0; - velocity.y += keyboardInput.arrowDown ? speed : 0; + velocity.x -= keyboardInput.arrowLeft ? 1 : 0; + velocity.x += keyboardInput.arrowRight ? 1 : 0; + velocity.y -= keyboardInput.arrowUp ? 1 : 0; + velocity.y += keyboardInput.arrowDown ? 1 : 0; + normalize(velocity); + scale(velocity, speed); position.x = Math.round(velocity.x * delta + position.x); position.y = Math.round(velocity.y * delta + position.y);