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