From 781807ea70d33232089255ce4f6b1526ae0e2595 Mon Sep 17 00:00:00 2001 From: mike Date: Sun, 5 Mar 2023 17:11:06 -0500 Subject: [PATCH] Prototypes for building & training assembled --- .../src/ca/cosc3p91/a2/game/GameEngine.java | 74 +++++++++++++++---- Assignment 2/src/ca/cosc3p91/a2/game/Map.java | 25 +++++-- .../ca/cosc3p91/a2/gameobjects/Archer.java | 12 ++- .../cosc3p91/a2/gameobjects/ArcherTower.java | 5 ++ .../ca/cosc3p91/a2/gameobjects/Cannon.java | 5 ++ .../ca/cosc3p91/a2/gameobjects/Catapult.java | 11 ++- .../ca/cosc3p91/a2/gameobjects/Collector.java | 28 ++++++- .../a2/gameobjects/DefenseBuilding.java | 4 +- .../a2/gameobjects/DefenseStages.java | 3 + .../cosc3p91/a2/gameobjects/Inhabitant.java | 5 +- .../ca/cosc3p91/a2/gameobjects/Knight.java | 11 ++- .../ca/cosc3p91/a2/gameobjects/Soldier.java | 11 ++- .../ca/cosc3p91/a2/gameobjects/Worker.java | 11 ++- 13 files changed, 170 insertions(+), 35 deletions(-) diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java index f3ca9de..6f6c63c 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java @@ -34,8 +34,8 @@ public class GameEngine implements Runnable { map = generateInitialMap(); } - private void printState() { - Print resourcesPrinter = new Print("Current Village Resources", 2); + private void printState(Map map, String displayName) { + Print resourcesPrinter = new Print(displayName, 2); resourcesPrinter.addColumn(new Print.Column("Resource Type")); resourcesPrinter.addColumn(new Print.Column("Max")); @@ -80,6 +80,16 @@ public class GameEngine implements Runnable { Print.print(inhabs.createTable(true, true, true)); } + private void printMenuOptions() { + System.out.println("~ Player Options:\n" + + "1. Build {command: '1 '}\n" + + "2. Train inhabitants {command: '2 '}\n"+ + "3. Upgrade Building\n"+ + "4. Explore\n"+ + "5. Check Village Stats\n"+ + "6. Quit"); + } + public void attackVillage(Map map) { } @@ -169,12 +179,13 @@ public class GameEngine implements Runnable { public void run() { BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); Scanner sc = new Scanner(rd); - printState(); + printState(this.map,"Current Village State"); + printMenuOptions(); System.out.println(); while (running) { - for (Building b : map.contains){ + for (Building b : this.map.contains){ if ((b instanceof ResourceBuilding)) { - ((ResourceBuilding) b).update(map.getTownHall()); + ((ResourceBuilding) b).update(this.map.getTownHall()); } } //System.out.println("Updating"); @@ -185,26 +196,40 @@ public class GameEngine implements Runnable { System.out.println("Your Input: "); System.out.println("\t->" + in); switch (in.charAt(0)) { - case 'p': - printState(); - break; - case 'q': - System.exit(0); - break; - case 'b': + case '1': if (args.length < 2) { System.err.println("Args must include type!"); } else { Building type = determineType(args[1]); if (type == null) System.err.println("Args are not a valid building!"); - else - map.build(new Tile(), type); + else if (this.map.build(new Tile(), type) ) { + System.out.println(type.getClass().getSimpleName()+" successfully built\n"); + } else System.out.println("Missing resources to build "+type.getClass().getSimpleName()); } break; + case '2': + if (args.length < 2) { + System.err.println("Args must include type!"); + } else { + Inhabitant type = determineInhabitantType(args[1]); + if (type == null) + System.err.println("Args are not a valid inhabitant!"); + else if (this.map.train(type) ) { + System.out.println("successfully trained a(n) "+type.getClass().getSimpleName()); + } else System.out.println("Missing gold to train "+type.getClass().getSimpleName()); + } + break; + case '5': + printState(this.map,"Current Village State"); + break; + case '6': + System.exit(0); + break; default: break; } + printMenuOptions(); } } catch (IOException e) { throw new RuntimeException(e); @@ -230,4 +255,25 @@ public class GameEngine implements Runnable { } return null; } + + private static Inhabitant determineInhabitantType(String inhabitant){ + inhabitant = inhabitant.toLowerCase(); + char c = ' '; + if (inhabitant.trim().length() == 1) + c = inhabitant.charAt(0); + if (inhabitant.contains("soldier") || inhabitant.contains("sold") || c == 's') { + return new Soldier(); + } else if (inhabitant.contains("knight") || c == 'k') { + return new Knight(); + } else if (inhabitant.contains("work")|| c == 'w') { + return new Worker(); + } else if (inhabitant.contains("collect") || c == 'c') { + return new Collector(); + } else if (inhabitant.contains("cat")){ + return new Catapult(); + } else if (inhabitant.contains("archer") || c == 'a'){ + return new Archer(); + } + return null; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java index 97d9d9f..c76ee5a 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java @@ -7,7 +7,7 @@ import java.util.List; public class Map { - static int MAXSIZE = 400; + static int MAXSIZE = 50; private CasaDeNarino townHall; @@ -22,6 +22,8 @@ public class Map { inhabitants = new ArrayList<>(); this.townHall = casaDeNarino; this.contains.add(casaDeNarino); + this.inhabitants.add(new Worker()); this.inhabitants.add(new Worker()); + this.inhabitants.add(new Collector()); this.guardTime = gTime; } @@ -33,10 +35,10 @@ public class Map { } - public void build(Tile t, Building b) { + public boolean build(Tile t, Building b) { int goldCost = b.getStage().getCost(SaulGoodMine.resource); - int ironCost = b.getStage().getCost(SaulGoodMine.resource); - int woodCost = b.getStage().getCost(SaulGoodMine.resource); + int ironCost = b.getStage().getCost(IronMine.resource); + int woodCost = b.getStage().getCost(LumberMine.resource); CasaDeNarino hall = getTownHall(); if (hall.getCurrentGold() >= goldCost && hall.getCurrentIron() >= ironCost && hall.getCurrentWood() >= woodCost) { if(!hall.addGold(-goldCost)) @@ -46,7 +48,20 @@ public class Map { if(!hall.addWood(-woodCost)) throw new RuntimeException("Unable to subtract wood despite valid check!"); contains.add(b); - } + return true; + } else return false; + } + + public boolean train(Inhabitant i) { + CasaDeNarino hall = getTownHall(); + int goldCost = i.getCost(); + + if (hall.getCurrentGold() >= goldCost) { + if(!hall.addGold(-goldCost)) + throw new RuntimeException("Unable to subtract gold despite valid check!"); + inhabitants.add(i); + return true; + } else return false; } public int getGuardTime() { diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Archer.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Archer.java index e11f54d..24f0fdc 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Archer.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Archer.java @@ -2,6 +2,8 @@ package ca.cosc3p91.a2.gameobjects; public class Archer extends Infantry { + static int cost = 4; + public Archer() { super(90, 2, 10); } @@ -22,7 +24,13 @@ public class Archer extends Infantry { } @Override - public int setLevel(int level) { - return super.setLevel(level); + public int getCost() { + return cost; } + + @Override + public void setLevel(int 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 bfafb2a..9407188 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ArcherTower.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ArcherTower.java @@ -1,4 +1,9 @@ package ca.cosc3p91.a2.gameobjects; public class ArcherTower extends DefenseBuilding { + + public ArcherTower() { + setLevel(1); + upgrade(DefenseStages.archerTowerStages[0]); + } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Cannon.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Cannon.java index 4a7ae5a..3a54d1a 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Cannon.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Cannon.java @@ -1,4 +1,9 @@ package ca.cosc3p91.a2.gameobjects; public class Cannon extends DefenseBuilding { + + public Cannon() { + setLevel(1); + upgrade(DefenseStages.cannonStages[0]); + } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Catapult.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Catapult.java index 4bc871e..c74f842 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Catapult.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Catapult.java @@ -2,6 +2,8 @@ package ca.cosc3p91.a2.gameobjects; public class Catapult extends Infantry { + static int cost = 6; + public Catapult() { super(80, 12, 12); } @@ -22,7 +24,12 @@ public class Catapult extends Infantry { } @Override - public int setLevel(int level) { - return super.setLevel(level); + public int getCost() { + return cost; + } + + @Override + public void setLevel(int 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 dcf9a4f..0830c8f 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java @@ -1,11 +1,37 @@ package ca.cosc3p91.a2.gameobjects; -public class Collector { +public class Collector implements Inhabitant { private int averageCollectionRate; + static int cost = 2; + public int getCollectionRate() { return averageCollectionRate; } + @Override + public void move(Tile t) { + + } + + @Override + public void getPosition() { + + } + + @Override + public int getLevel() { + return Inhabitant.super.getLevel(); + } + + @Override + public int getCost() { + return cost; + } + + @Override + public void setLevel(int level) { + + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java index 89ae5c2..fa82a41 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java @@ -2,8 +2,8 @@ package ca.cosc3p91.a2.gameobjects; public class DefenseBuilding extends Building { - private int damage; - private int range; + private int damage = 0; + private int range = 0; public void upgrade(DefenseStage stage) { super.upgrade(stage); diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java index 6526b49..c1aba1e 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java @@ -4,6 +4,9 @@ import ca.cosc3p91.a2.util.Time; public class DefenseStages { + // !! need to adjust these values | | + // v v + public static class ArcherTowerStage1 extends DefenseStage { public ArcherTowerStage1() { super(100, 0, 0, new Time().offsetMinutes(1), 25, 250, 4,6); diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java index 9818dd2..19d9627 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java @@ -15,9 +15,8 @@ public interface Inhabitant { default int getLevel() { return lvl; } + int getCost(); - default int setLevel(int level) { - return lvl; - } + void setLevel(int level); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Knight.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Knight.java index e8b91e1..2b4eb0a 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Knight.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Knight.java @@ -2,6 +2,8 @@ package ca.cosc3p91.a2.gameobjects; public class Knight extends Infantry { + static int cost = 6; + public Knight() { super(150, 6, 6); } @@ -22,7 +24,12 @@ public class Knight extends Infantry { } @Override - public int setLevel(int level) { - return super.setLevel(level); + public int getCost() { + return cost; + } + + @Override + public void setLevel(int level) { + } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Soldier.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Soldier.java index 4656c78..2fcb157 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Soldier.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Soldier.java @@ -2,6 +2,8 @@ package ca.cosc3p91.a2.gameobjects; public class Soldier extends Infantry { + static int cost = 4; + public Soldier() { super(100, 4, 4); } @@ -22,7 +24,12 @@ public class Soldier extends Infantry { } @Override - public int setLevel(int level) { - return super.setLevel(level); + public int getCost() { + return cost; + } + + @Override + public void setLevel(int 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 7ef950a..b3d37fd 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java @@ -4,6 +4,8 @@ public class Worker implements Inhabitant { private boolean currentlyBuilding; + static int cost = 2; + public void set_IsBuilding(boolean state) { currentlyBuilding = state; } @@ -28,7 +30,12 @@ public class Worker implements Inhabitant { } @Override - public int setLevel(int level) { - return Inhabitant.super.setLevel(level); + public int getCost() { + return cost; + } + + @Override + public void setLevel(int level) { + } }