Session 10: Buy Max bulk-buy feature for buildings
This commit is contained in:
+58
-1
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user