Session 10: Buy Max bulk-buy feature for buildings

This commit is contained in:
2026-08-22 16:04:56 +00:00
parent 2cee2c8b99
commit 478b9741a7
4 changed files with 127 additions and 2 deletions
+58 -1
View File
@@ -233,7 +233,7 @@ const Game = {
},
/**
* Buy a building
* Buy a single building
*/
buyBuilding(buildingId) {
const cost = this.getBuildingCost(buildingId);
@@ -261,6 +261,63 @@ const Game = {
return false;
},
/**
* Calculate how many of a building the player can currently afford.
* Does not modify game state.
*/
getMaxAffordable(buildingId) {
const building = BUILDINGS[buildingId];
if (!building) return 0;
let stardust = this.state.stardust;
let count = 0;
let owned = this.state.buildings[buildingId] || 0;
while (true) {
const cost = Math.floor(building.baseCost * Math.pow(this.config.costScaling, owned + count));
if (stardust < cost) break;
stardust -= cost;
count++;
}
return count;
},
/**
* Buy as many of a building as the player can afford at once.
* Returns the number purchased.
*/
buyMaxBuilding(buildingId) {
const building = BUILDINGS[buildingId];
if (!building) return 0;
let count = 0;
while (this.state.stardust >= this.getBuildingCost(buildingId)) {
const cost = this.getBuildingCost(buildingId);
this.state.stardust -= cost;
this.state.buildings[buildingId] = (this.state.buildings[buildingId] || 0) + 1;
count++;
}
if (count > 0) {
UI.showNotification(`Bought ${count}x ${building.name}!`, 'success');
// Audio feedback
Audio.playPurchase();
// Particle effect on building purchase
const card = document.getElementById(`building-${buildingId}`);
if (card) {
const rect = card.getBoundingClientRect();
Particles.createBuildingEffect(rect.left + rect.width / 2, rect.top + rect.height / 2);
}
UI.update();
}
return count;
},
/**
* Buy an upgrade
*/
+16
View File
@@ -69,6 +69,7 @@ const UI = {
<div class="building-cost">${Game.formatNumber(cost)} ✨</div>
<div class="building-count">${count}</div>
<button class="buy-btn" ${!canAfford || locked ? 'disabled' : ''}>Buy</button>
<button class="buy-max-btn" data-building="${id}" ${locked || count === 0 ? 'title="Buy as many as you can afford"' : 'disabled'}>${Game.formatNumber(Game.getMaxAffordable(id))}x Max</button>
</div>
`;
@@ -76,6 +77,12 @@ const UI = {
card.querySelector('.buy-btn').addEventListener('click', () => {
Game.buyBuilding(id);
});
const maxBtn = card.querySelector('.buy-max-btn');
if (maxBtn) {
maxBtn.addEventListener('click', () => {
Game.buyMaxBuilding(id);
});
}
}
container.appendChild(card);
@@ -103,6 +110,15 @@ const UI = {
btn.disabled = !canAfford || locked;
}
// Buy Max button - reflect how many can be bought at current stardust
const maxBtn = card.querySelector('.buy-max-btn');
if (maxBtn) {
const affordable = Game.getMaxAffordable(id);
maxBtn.textContent = `${Game.formatNumber(affordable)}x Max`;
maxBtn.disabled = locked || affordable === 0;
maxBtn.title = affordable > 0 ? `Buy up to ${affordable} ${building.name}(s)` : 'Not enough stardust';
}
card.classList.toggle('locked', locked);
}
},