Add bounds checking on resources

main
Brett 2023-03-03 14:39:42 -05:00
parent fb0425ec1d
commit f2793b1a97
5 changed files with 31 additions and 14 deletions

View File

@ -1,16 +1,14 @@
package ca.cosc3p91.a2.gameobjects;
import java.util.ArrayList;
public class CasaDeNarino extends Building {
private int goldCapacity = 0;
private int ironCapacity = 0;
private int woodCapacity = 0;
private int currentGold;
private int currentIron;
private int currentWood;
private int currentGold = 0;
private int currentIron = 0;
private int currentWood = 50;
public CasaDeNarino(int lvl, VillageStage baseStage) {
setLevel(lvl);
@ -41,23 +39,38 @@ public class CasaDeNarino extends Building {
return currentGold;
}
public void addCurrentGold(int currentGold) {
this.currentGold += currentGold;
public boolean addGold(int amount) {
int newGold = this.currentGold + amount;
if (newGold <= goldCapacity && this.currentGold + amount >= 0){
this.currentGold += amount;
return true;
}
return false;
}
public int getCurrentIron() {
return currentIron;
}
public void addCurrentIron(int currentIron) {
this.currentIron += currentIron;
public boolean addIron(int amount) {
int newIron = this.currentIron + amount;
if (newIron <= ironCapacity && newIron >= 0) {
this.currentIron += amount;
return true;
}
return false;
}
public int getCurrentWood() {
return currentWood;
}
public void addCurrentWood(int currentWood) {
this.currentWood += currentWood;
public boolean addWood(int amount) {
int newWood = this.currentWood + amount;
if (newWood <= woodCapacity && newWood >= 0) {
this.currentWood += amount;
return true;
}
return false;
}
}

View File

@ -11,6 +11,6 @@ public class IronMine extends ResourceBuilding {
@Override
protected ResourceHarvestHandler getHarvestHandler() {
return hall -> hall.addCurrentIron(getHarvestRate());
return hall -> hall.addIron(getHarvestRate());
}
}

View File

@ -11,7 +11,7 @@ public class LumberMine extends ResourceBuilding {
@Override
protected ResourceHarvestHandler getHarvestHandler() {
return hall -> hall.addCurrentWood(getHarvestRate());
return hall -> hall.addWood(getHarvestRate());
}
}

View File

@ -11,6 +11,6 @@ public class SaulGoodMine extends ResourceBuilding {
@Override
protected ResourceHarvestHandler getHarvestHandler() {
return hall -> hall.addCurrentGold(getHarvestRate());
return hall -> hall.addGold(getHarvestRate());
}
}

View File

@ -0,0 +1,4 @@
package ca.cosc3p91.a2.gameobjects;
public class VillageHallStages {
}