From ba7821dfd9a801421ac5027e8685ff541b9ee31e Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 6 Mar 2023 18:55:03 -0500 Subject: [PATCH] Added basic upgrading functionality --- .../src/ca/cosc3p91/a2/game/GameEngine.java | 19 ++++++- Assignment 2/src/ca/cosc3p91/a2/game/Map.java | 52 +++++++++++++++++++ .../ca/cosc3p91/a2/gameobjects/Archer.java | 6 ++- .../cosc3p91/a2/gameobjects/ArcherTower.java | 5 ++ .../ca/cosc3p91/a2/gameobjects/Building.java | 4 +- .../ca/cosc3p91/a2/gameobjects/Cannon.java | 5 ++ .../cosc3p91/a2/gameobjects/CasaDeNarino.java | 5 ++ .../ca/cosc3p91/a2/gameobjects/Catapult.java | 6 ++- .../ca/cosc3p91/a2/gameobjects/Collector.java | 10 +++- .../a2/gameobjects/DefenseBuilding.java | 2 +- .../src/ca/cosc3p91/a2/gameobjects/Farm.java | 5 ++ .../cosc3p91/a2/gameobjects/Inhabitant.java | 9 ++-- .../ca/cosc3p91/a2/gameobjects/IronMine.java | 5 ++ .../ca/cosc3p91/a2/gameobjects/Knight.java | 6 ++- .../cosc3p91/a2/gameobjects/LumberMine.java | 4 ++ .../cosc3p91/a2/gameobjects/SaulGoodMine.java | 5 ++ .../ca/cosc3p91/a2/gameobjects/Soldier.java | 6 ++- .../ca/cosc3p91/a2/gameobjects/Worker.java | 6 ++- 18 files changed, 137 insertions(+), 23 deletions(-) diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java index 5a195dd..2f27a25 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java @@ -99,7 +99,7 @@ public class GameEngine implements Runnable { if (b instanceof DefenseBuilding) defenseiveCounter++; for (Inhabitant i : map.inhabitants) - if (b instanceof Infantry) + if (i instanceof Infantry) inhabCounter++; pillageFactor = (float) inhabCounter / (float) defenseiveCounter; if (pillageFactor < 0) @@ -250,6 +250,23 @@ public class GameEngine implements Runnable { } break; case '3': + if (args.length < 2) { + System.err.println("Args must include type!"); + } else { + if (args[1].contains("i")) { + Inhabitant unit = map.inhabitants.get(Integer.parseInt(args[1].substring(1))); + if (map.upgradeInhabitant(unit) ){ + System.out.println("successfully trained a(n)"+unit.getClass().getSimpleName()); + } else System.out.println("Missing Resources to train "+unit.getClass().getSimpleName()); + } else if (args[1].contains("b")) { + Building bUnit = map.contains.get(Integer.parseInt(args[1].substring(1))); + if (map.upgradeBuilding(bUnit) ){ + System.out.println("successfully trained a(n)"+bUnit.getClass().getSimpleName()); + } else System.out.println("Missing Resources to train "+bUnit.getClass().getSimpleName()); + } else { + System.err.println("Args are not a valid unit!"); + } + } break; case '4': deleteMyHeart = false; diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java index 63b2347..8b5dcd4 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java @@ -53,6 +53,58 @@ public class Map { return false; } + public boolean upgradeBuilding(Building b) { + int currentLevel = b.getLevel(); + CasaDeNarino hall = getTownHall(); + + if (currentLevel >= 2) return false; + if (b instanceof Farm) return true; + + int goldCost = b.getUpgradeStage().getCost(SaulGoodMine.resource); + int ironCost = b.getUpgradeStage().getCost(IronMine.resource); + int woodCost = b.getUpgradeStage().getCost(LumberMine.resource); + + if (hall.getCurrentGold() >= goldCost && hall.getCurrentIron() >= ironCost && hall.getCurrentWood() >= woodCost) { + + if (b instanceof DefenseBuilding) { + ((DefenseBuilding) b).upgrade( + (b instanceof ArcherTower) ? (DefenseStages.archerTowerStages[currentLevel + 1]) : + (DefenseStages.cannonStages[currentLevel + 1]) + ); + } else if (b instanceof ResourceBuilding) { + ((ResourceBuilding) b).upgrade( + (b instanceof IronMine) ? (ResourceStages.ironStages[currentLevel + 1]) : + (b instanceof SaulGoodMine) ? (ResourceStages.goldStages[currentLevel + 1]) : + (ResourceStages.woodStages[currentLevel + 1]) + ); + } else { + b.upgrade(VillageHallStages.villageStages[currentLevel + 1]); + } + } else return false; + + return true; + } + + public boolean upgradeInhabitant(Inhabitant i) { + int currentLevel = i.getLevel(); + CasaDeNarino hall = getTownHall(); + + if (currentLevel >= 2 || hall.getCurrentGold() < 5) return false; + + i.setLevel(++currentLevel); + hall.addGold(-5); + + if (i instanceof Infantry) { + ((Infantry) i).setHealth(((Infantry) i).getHealth() + 1); + ((Infantry) i).setDamage(((Infantry) i).getDamage() + 1); + ((Infantry) i).setRange(((Infantry) i).getRange() + 1); + } else if (i instanceof Collector) { + ((Collector) i).setCollectionRate(((Collector) i).getCollectionRate() + 1); + } + + return true; + } + public boolean train(Inhabitant i) { CasaDeNarino hall = getTownHall(); int goldCost = i.getCost(); diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Archer.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Archer.java index 24f0fdc..ae55d04 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Archer.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Archer.java @@ -4,6 +4,8 @@ public class Archer extends Infantry { static int cost = 4; + private int lvl = 0; + public Archer() { super(90, 2, 10); } @@ -20,7 +22,7 @@ public class Archer extends Infantry { @Override public int getLevel() { - return super.getLevel(); + return lvl; } @Override @@ -30,7 +32,7 @@ public class Archer extends Infantry { @Override public void setLevel(int level) { - + lvl = level; } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ArcherTower.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ArcherTower.java index 9407188..2961a6c 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ArcherTower.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ArcherTower.java @@ -6,4 +6,9 @@ public class ArcherTower extends DefenseBuilding { setLevel(1); upgrade(DefenseStages.archerTowerStages[0]); } + + @Override + public Stage getUpgradeStage() { + return DefenseStages.archerTowerStages[getLevel()+1]; + } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Building.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Building.java index f9d54af..bd028f3 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Building.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Building.java @@ -38,9 +38,7 @@ public abstract class Building { return stage; } - public int getUpgradeCost() { - return 0; - } + public abstract Stage getUpgradeStage(); public void setLevel(int level) { this.level = level; diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Cannon.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Cannon.java index 3a54d1a..d6ae7b2 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Cannon.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Cannon.java @@ -6,4 +6,9 @@ public class Cannon extends DefenseBuilding { setLevel(1); upgrade(DefenseStages.cannonStages[0]); } + + @Override + public Stage getUpgradeStage() { + return DefenseStages.cannonStages[getLevel()+1]; + } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/CasaDeNarino.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/CasaDeNarino.java index ae0403e..ebc4b6a 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/CasaDeNarino.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/CasaDeNarino.java @@ -15,6 +15,11 @@ public class CasaDeNarino extends Building { upgrade(baseStage); } + @Override + public Stage getUpgradeStage() { + return VillageHallStages.villageStages[getLevel()+1]; + } + public void upgrade(VillageStage stage) { super.upgrade(stage); this.goldCapacity += stage.getGoldCapacityIncrease(); diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Catapult.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Catapult.java index c74f842..97065b0 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Catapult.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Catapult.java @@ -4,6 +4,8 @@ public class Catapult extends Infantry { static int cost = 6; + private int lvl = 0; + public Catapult() { super(80, 12, 12); } @@ -20,7 +22,7 @@ public class Catapult extends Infantry { @Override public int getLevel() { - return super.getLevel(); + return lvl; } @Override @@ -30,6 +32,6 @@ public class Catapult extends Infantry { @Override public void setLevel(int level) { - + lvl = level; } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java index 0830c8f..b7e6627 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java @@ -6,10 +6,16 @@ public class Collector implements Inhabitant { static int cost = 2; + private int lvl = 0; + public int getCollectionRate() { return averageCollectionRate; } + public void setCollectionRate(int rate) { + averageCollectionRate = rate; + } + @Override public void move(Tile t) { @@ -22,7 +28,7 @@ public class Collector implements Inhabitant { @Override public int getLevel() { - return Inhabitant.super.getLevel(); + return lvl; } @Override @@ -32,6 +38,6 @@ public class Collector implements Inhabitant { @Override public void setLevel(int level) { - + lvl = level; } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java index fa82a41..c6a8d84 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java @@ -1,6 +1,6 @@ package ca.cosc3p91.a2.gameobjects; -public class DefenseBuilding extends Building { +public abstract class DefenseBuilding extends Building { private int damage = 0; private int range = 0; diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java index 3f1892b..4eacb1c 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java @@ -15,4 +15,9 @@ public class Farm extends ResourceBuilding { protected ResourceHarvestHandler getHarvestHandler() { return hall -> {}; } + + @Override + public Stage getUpgradeStage() { + return ResourceStages.goldStages[getLevel()+1]; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java index 19d9627..997c5a5 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java @@ -6,17 +6,14 @@ public interface Inhabitant { Map map = null; Building building = null; - int lvl = 1; + int lvl = 0; void move(Tile t); void getPosition(); - default int getLevel() { - return lvl; - } + int getLevel(); + void setLevel(int level); int getCost(); - void setLevel(int level); - } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java index 9ee9473..b949894 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java @@ -12,4 +12,9 @@ public class IronMine extends ResourceBuilding { protected ResourceHarvestHandler getHarvestHandler() { return hall -> hall.addIron(getHarvestRate()); } + + @Override + public Stage getUpgradeStage() { + return ResourceStages.ironStages[getLevel()+1]; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Knight.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Knight.java index 2b4eb0a..e01248e 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Knight.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Knight.java @@ -4,6 +4,8 @@ public class Knight extends Infantry { static int cost = 6; + private int lvl = 0; + public Knight() { super(150, 6, 6); } @@ -20,7 +22,7 @@ public class Knight extends Infantry { @Override public int getLevel() { - return super.getLevel(); + return lvl; } @Override @@ -30,6 +32,6 @@ public class Knight extends Infantry { @Override public void setLevel(int level) { - + lvl = level; } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java index 113c52e..514621b 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java @@ -13,4 +13,8 @@ public class LumberMine extends ResourceBuilding { return hall -> hall.addWood(getHarvestRate()); } + @Override + public Stage getUpgradeStage() { + return ResourceStages.woodStages[getLevel()+1]; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java index eab7c70..2c3cbff 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java @@ -12,4 +12,9 @@ public class SaulGoodMine extends ResourceBuilding { protected ResourceHarvestHandler getHarvestHandler() { return hall -> hall.addGold(getHarvestRate()); } + + @Override + public Stage getUpgradeStage() { + return ResourceStages.goldStages[getLevel()+1]; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Soldier.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Soldier.java index 2fcb157..3a11d8c 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Soldier.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Soldier.java @@ -4,6 +4,8 @@ public class Soldier extends Infantry { static int cost = 4; + int lvl = 0; + public Soldier() { super(100, 4, 4); } @@ -20,7 +22,7 @@ public class Soldier extends Infantry { @Override public int getLevel() { - return super.getLevel(); + return lvl; } @Override @@ -30,6 +32,6 @@ public class Soldier extends Infantry { @Override public void setLevel(int level) { - + lvl = level; } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java index b3d37fd..357b0b2 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java @@ -6,6 +6,8 @@ public class Worker implements Inhabitant { static int cost = 2; + private int lvl = 0; + public void set_IsBuilding(boolean state) { currentlyBuilding = state; } @@ -26,7 +28,7 @@ public class Worker implements Inhabitant { @Override public int getLevel() { - return Inhabitant.super.getLevel(); + return lvl; } @Override @@ -36,6 +38,6 @@ public class Worker implements Inhabitant { @Override public void setLevel(int level) { - + lvl = level; } }