import {Vec2} from "../../lib/math.js";
import {
vec2_add,
vec2_length,
vec2_normalize,
vec2_rotate,
vec2_scale,
vec2_transform_direction,
} from "../../lib/vec2.js";
import {Entity} from "../../lib/world.js";
import {Game} from "../game.js";
import {Has} from "../world.js";
function update(game: Game, entity: Entity, delta: number) {
let local = game.World.LocalTransform2D[entity];
let move = game.World.Move2D[entity];
if (move.Direction[0] || move.Direction[1]) {
direction[0] = move.Direction[0];
direction[1] = move.Direction[1];
let amount = Math.min(1, vec2_length(direction));
if (game.World.Signature[entity] & Has.SpatialNode2D) {
let node = game.World.SpatialNode2D[entity];
if (node.Parent !== undefined) {
let parent = game.World.SpatialNode2D[node.Parent];
vec2_transform_direction(direction, direction, parent.Self);
} else {
vec2_transform_direction(direction, direction, node.World);
}
} else {
vec2_rotate(direction, direction, local.Rotation);
}
vec2_normalize(direction, direction);
vec2_scale(direction, direction, amount * move.MoveSpeed * delta);
vec2_add(local.Translation, local.Translation, direction);
move.Direction[0] = 0;
move.Direction[1] = 0;
}
if (move.Rotation) {
local.Rotation += move.Rotation * move.RotationSpeed * delta;
move.Rotation = 0;
}
}