Add in throwable items (dagger) from pixel dungeon
This commit is contained in:
53
src/engine/gameplay/CombatLogic.ts
Normal file
53
src/engine/gameplay/CombatLogic.ts
Normal 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
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user