Add in throwable items (dagger) from pixel dungeon

This commit is contained in:
Peter Stockings
2026-01-06 20:58:53 +11:00
parent 3b29180a00
commit 9b1fc78409
18 changed files with 659 additions and 155 deletions

View File

@@ -0,0 +1,53 @@
import { type World, type Vec2, type EntityId } from "../../core/types";
import { isBlocked } from "../world/world-logic";
import { raycast } from "../../core/math";
import { EntityManager } from "../EntityManager";
export interface ProjectileResult {
path: Vec2[];
blockedPos: Vec2;
hitActorId?: EntityId;
}
/**
* Calculates the path and impact of a projectile.
*/
export function traceProjectile(
world: World,
start: Vec2,
target: Vec2,
entityManager: EntityManager,
shooterId?: EntityId
): ProjectileResult {
const points = raycast(start.x, start.y, target.x, target.y);
let blockedPos = target;
let hitActorId: EntityId | undefined;
// Iterate points (skip start)
for (let i = 1; i < points.length; i++) {
const p = points[i];
// Check for blocking
if (isBlocked(world, p.x, p.y, entityManager)) {
// Check if we hit a combatant
const actors = entityManager.getActorsAt(p.x, p.y);
const enemy = actors.find(a => a.category === "combatant" && a.id !== shooterId);
if (enemy) {
hitActorId = enemy.id;
blockedPos = p;
} else {
// Hit wall or other obstacle
blockedPos = p;
}
break;
}
blockedPos = p;
}
return {
path: points,
blockedPos,
hitActorId
};
}