Add ammo counter for ranged items in quickslot
This commit is contained in:
@@ -174,6 +174,56 @@ export class GameScene extends Phaser.Scene {
|
||||
if (itemIdx === -1) return;
|
||||
const item = player.inventory.items[itemIdx];
|
||||
|
||||
// Ranged Weapon Logic
|
||||
if (item.type === "Weapon" && item.weaponType === "ranged") {
|
||||
// Check Ammo
|
||||
if (item.stats.currentAmmo <= 0) {
|
||||
// Try Reload
|
||||
const ammoId = `ammo_${item.stats.ammoType}`;
|
||||
const ammoItem = player.inventory.items.find(it => it.id === ammoId); // Simple check
|
||||
|
||||
if (ammoItem && ammoItem.quantity && ammoItem.quantity > 0) {
|
||||
const needed = item.stats.magazineSize - item.stats.currentAmmo;
|
||||
const toTake = Math.min(needed, ammoItem.quantity);
|
||||
|
||||
item.stats.currentAmmo += toTake;
|
||||
ammoItem.quantity -= toTake;
|
||||
|
||||
if (ammoItem.quantity <= 0) {
|
||||
player.inventory.items = player.inventory.items.filter(it => it !== ammoItem);
|
||||
}
|
||||
|
||||
this.dungeonRenderer.showFloatingText(player.pos.x, player.pos.y, "Reloaded!", "#00ff00");
|
||||
console.log("Reloaded. Ammo:", item.stats.currentAmmo);
|
||||
this.commitPlayerAction({ type: "wait" });
|
||||
this.emitUIUpdate();
|
||||
} else {
|
||||
this.dungeonRenderer.showFloatingText(player.pos.x, player.pos.y, "No Ammo!", "#ff0000");
|
||||
console.log("No ammo found for", item.name);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Has ammo, start targeting
|
||||
if (this.targetingSystem.isActive && this.targetingSystem.itemId === item.id) {
|
||||
// Already targeting - execute shoot
|
||||
if (this.targetingSystem.cursorPos) {
|
||||
this.executeThrow(this.targetingSystem.cursorPos.x, this.targetingSystem.cursorPos.y);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.targetingSystem.startTargeting(
|
||||
item.id,
|
||||
player.pos,
|
||||
this.world,
|
||||
this.dungeonRenderer.seenArray,
|
||||
this.world.width
|
||||
);
|
||||
this.emitUIUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
const result = this.itemManager.handleUse(data.itemId, player);
|
||||
|
||||
if (result.success && result.consumed) {
|
||||
@@ -565,10 +615,12 @@ export class GameScene extends Phaser.Scene {
|
||||
this.playerId,
|
||||
this.entityManager,
|
||||
(blockedPos, hitActorId, item) => {
|
||||
// Damage Logic
|
||||
if (hitActorId !== undefined) {
|
||||
const victim = this.world.actors.get(hitActorId) as CombatantActor;
|
||||
if (victim) {
|
||||
const dmg = item.stats?.attack ?? 1;
|
||||
const stats = 'stats' in item ? item.stats : undefined;
|
||||
const dmg = (stats && 'attack' in stats) ? (stats.attack ?? 1) : 1;
|
||||
victim.stats.hp -= dmg;
|
||||
this.dungeonRenderer.showDamage(victim.pos.x, victim.pos.y, dmg);
|
||||
this.dungeonRenderer.shakeCamera();
|
||||
@@ -576,13 +628,30 @@ export class GameScene extends Phaser.Scene {
|
||||
}
|
||||
|
||||
const player = this.world.actors.get(this.playerId) as CombatantActor;
|
||||
|
||||
// Projectile Visuals
|
||||
let projectileId = item.id;
|
||||
if (item.type === "Weapon" && item.weaponType === "ranged") {
|
||||
projectileId = `ammo_${item.stats.ammoType}`; // Show ammo sprite
|
||||
|
||||
// Consume Ammo
|
||||
if (item.stats.currentAmmo > 0) {
|
||||
item.stats.currentAmmo--;
|
||||
}
|
||||
}
|
||||
|
||||
this.dungeonRenderer.showProjectile(
|
||||
player.pos,
|
||||
blockedPos,
|
||||
item.id,
|
||||
projectileId,
|
||||
() => {
|
||||
// Drop the actual item at the landing spot
|
||||
this.itemManager.spawnItem(item, blockedPos);
|
||||
// Only drop item if it acts as a thrown item (Consumable/Misc), NOT Weapon
|
||||
const shouldDrop = item.type !== "Weapon";
|
||||
|
||||
if (shouldDrop) {
|
||||
// Drop the actual item at the landing spot
|
||||
this.itemManager.spawnItem(item, blockedPos);
|
||||
}
|
||||
|
||||
// Trigger destruction/interaction
|
||||
if (tryDestructTile(this.world, blockedPos.x, blockedPos.y)) {
|
||||
@@ -590,7 +659,7 @@ export class GameScene extends Phaser.Scene {
|
||||
}
|
||||
|
||||
this.targetingSystem.cancel();
|
||||
this.commitPlayerAction({ type: "throw" });
|
||||
this.commitPlayerAction({ type: "throw" }); // Or 'attack' if shooting? 'throw' is fine for now
|
||||
this.emitUIUpdate();
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user