Compare commits

...

7 Commits

Author SHA1 Message Date
mike e6a9b410c9 Instant upgrade working 2023-03-06 20:48:04 -05:00
mike ba7821dfd9 Added basic upgrading functionality 2023-03-06 18:55:03 -05:00
Brett 1ebfdca6c9 Merge remote-tracking branch 'refs/remotes/origin/main' 2023-03-06 18:01:58 -05:00
Brett aba8b47045 bit of sexual calculations based on michaels cum 2023-03-06 18:01:38 -05:00
mike 31a9f97618 minor menu changes 2023-03-06 17:17:28 -05:00
Brett c8e301f9cc Break my heart 2023-03-06 17:12:30 -05:00
Brett 33feedc3b1 fix weird generic function 2023-03-06 16:45:35 -05:00
18 changed files with 253 additions and 67 deletions

View File

@ -21,7 +21,7 @@ public class GameEngine<T> implements Runnable {
private Player player;
boolean running = true;
private int pillageFactor;
private float pillageFactor = 0.5f;
private int currentTime;
@ -65,7 +65,7 @@ public class GameEngine<T> implements Runnable {
for (Building b : map.contains)
buildingPrinter.addRow(new Print.Row(b.getClass().getSimpleName(),
Integer.toString(b.getLevel()),
Integer.toString(b.getLevel() + 1),
Integer.toString(b.getHealth())));
Print.print(buildingPrinter.createTable(true, false, true));
@ -75,23 +75,40 @@ public class GameEngine<T> implements Runnable {
inhabs.addColumn(new Print.Column("Level"));
for (Inhabitant i : map.inhabitants)
inhabs.addRow(new Print.Row(i.getClass().getSimpleName(), Integer.toString(i.getLevel())));
inhabs.addRow(new Print.Row(i.getClass().getSimpleName(), Integer.toString(i.getLevel() + 1)));
Print.print(inhabs.createTable(true, true, true));
}
private void printMenuOptions() {
System.out.println("~ Player Options:\n" +
System.out.println("\n~ Player Options:\n" +
"1. Build {command: '1 <building name>'}\n" +
"2. Train inhabitants {command: '2 <unit name>'}\n"+
"3. Upgrade Building\n"+
"3. Upgrade Building {command: '3 i<index>'} / {command: '3 b<index>'}\n"+
"4. Explore\n"+
"5. Check Village Stats\n"+
"6. Quit");
"5. Attack\n"+
"5. Print Village Stats\n"+
"6. Quit\n" +
"7. Attack last explored\n");
}
public void attackVillage(Map map) {
int defenseiveCounter = 1;
int inhabCounter = 0;
for (Building b : map.contains)
if (b instanceof DefenseBuilding)
defenseiveCounter++;
for (Inhabitant i : map.inhabitants)
if (i instanceof Infantry)
inhabCounter++;
pillageFactor = (float) inhabCounter / (float) defenseiveCounter;
if (pillageFactor < 0)
pillageFactor = 0;
if (pillageFactor > 1)
pillageFactor = 1;
this.map.getTownHall().addWood((int) (map.getTownHall().getCurrentWood() * pillageFactor));
this.map.getTownHall().addIron((int) (map.getTownHall().getCurrentIron() * pillageFactor));
this.map.getTownHall().addGold((int) (map.getTownHall().getCurrentGold() * pillageFactor));
}
private Map generateInitialMap(){
@ -100,12 +117,19 @@ public class GameEngine<T> implements Runnable {
public Map generateMap() {
Map initialMap = generateInitialMap();
CasaDeNarino hall = initialMap.getTownHall();
// generate a similar town hall
int levelChange = random.nextInt(2) - 1;
int nextLevel = this.map.getTownHall().getLevel() + levelChange;
// only need to change if the new village level is higher than initial
if (nextLevel > 0)
initialMap.getTownHall().upgrade(VillageHallStages.villageStages[nextLevel]);
hall.upgrade(VillageHallStages.villageStages[nextLevel]);
hall.addWood(this.map.getTownHall().getCurrentWood() + random.nextInt(500) - 150);
hall.addIron(this.map.getTownHall().getCurrentIron() + random.nextInt(500) - 150);
hall.addGold(this.map.getTownHall().getCurrentGold() + random.nextInt(500) - 150);
int buildingCount = this.map.contains.size();
@ -182,6 +206,8 @@ public class GameEngine<T> implements Runnable {
printState(this.map,"Current Village State");
printMenuOptions();
System.out.println();
Map exploringMap = null;
boolean deleteMyHeart = true;
while (running) {
for (Building b : this.map.contains){
if ((b instanceof ResourceBuilding)) {
@ -193,25 +219,29 @@ public class GameEngine<T> implements Runnable {
String in = sc.nextLine();
String[] args = in.split(" ");
System.out.println("Your Input: ");
System.out.println("\t->" + in);
System.out.println("\t->" + in + '\n');
// reset the map if they aren't exploring
if (in.charAt(0) != '4')
deleteMyHeart = true;
switch (in.charAt(0)) {
case '1':
if (args.length < 2) {
System.err.println("Args must include type!");
} else {
Building type = (Building)determineType(args[1],new Cannon());
Building type = determineBuildingType(args[1]);
if (type == null)
System.err.println("Args are not a valid building!");
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());
} 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 = (Inhabitant)determineType(args[1],new Worker());
Inhabitant type = determineInhabitantType(args[1]);
if (type == null)
System.err.println("Args are not a valid inhabitant!");
else if (this.map.train(type) ) {
@ -219,8 +249,43 @@ public class GameEngine<T> implements Runnable {
} else System.out.println("Missing gold to train "+type.getClass().getSimpleName());
}
break;
case '3':
if (args.length < 2) {
System.err.println("Args must include type!");
} else {
int unitIndex = Integer.parseInt(args[1].substring(1));
if (unitIndex < 0) {
System.err.println("Invalid Index");
break;
}
if (args[1].contains("i") && (unitIndex < map.inhabitants.size()) ) {
if ( map.upgradeInhabitant(unitIndex) ) {
System.out.println("successfully upgraded a(n) "+map.inhabitants.get(unitIndex).getClass().getSimpleName());
} else System.out.println("Missing Resources to upgrade "+map.inhabitants.get(unitIndex).getClass().getSimpleName());
} else if (args[1].contains("b") && (unitIndex < map.contains.size()) ) {
if ( map.upgradeBuilding(unitIndex) ) {
System.out.println("successfully upgraded a(n) "+map.contains.get(unitIndex).getClass().getSimpleName());
} else System.out.println("Missing Resources to upgrade "+map.contains.get(unitIndex).getClass().getSimpleName());
} else {
System.err.println("Args are not a valid unit!");
}
}
break;
case '4':
deleteMyHeart = false;
exploringMap = generateMap();
printState(exploringMap, "Other Village");
break;
case '7':
if (exploringMap != null)
attackVillage(exploringMap);
else
System.out.println("Error: Explored map is null. Have you explored last command?");
break;
case '5':
printState(this.map,"Current Village State");
printState(this.map,"Home Village");
break;
case '6':
System.exit(0);
@ -230,19 +295,25 @@ public class GameEngine<T> implements Runnable {
}
printMenuOptions();
}
} catch (IOException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
if (deleteMyHeart)
exploringMap = null;
}
}
private static <T> Object determineType(String argument, T type){
argument = argument.toLowerCase();
private static char determineChar(String str){
char c = ' ';
if (argument.trim().length() == 1)
c = argument.charAt(0);
if (str.trim().length() == 1)
c = str.charAt(0);
return c;
}
private static Building determineBuildingType(String argument){
argument = argument.toLowerCase();
char c = determineChar(argument);
if (type instanceof Building) {
if (argument.contains("gold") || argument.contains("good") || c == 'g') {
return new SaulGoodMine(ResourceStages.goldStages[0]);
} else if (argument.contains("iron") || c == 'i') {
@ -254,8 +325,15 @@ public class GameEngine<T> implements Runnable {
} else if (argument.contains("can") || c == 'c') {
return new Cannon();
}
} else if (type instanceof Inhabitant) {
if (argument.contains("soldier") || argument.contains("sold") || c == 's') {
return null;
}
private static Inhabitant determineInhabitantType(String argument) {
argument = argument.toLowerCase();
char c = determineChar(argument);
if (argument.contains("soldier") || c == 's') {
return new Soldier();
} else if (argument.contains("knight") || c == 'k') {
return new Knight();
@ -268,7 +346,7 @@ public class GameEngine<T> implements Runnable {
} else if (argument.contains("arch") || c == 'a') {
return new Archer();
}
}
return null;
}

View File

@ -49,7 +49,70 @@ public class Map {
throw new RuntimeException("Unable to subtract wood despite valid check!");
contains.add(b);
return true;
} else
return false;
}
public boolean upgradeBuilding(int buildingIndex) {
if (buildingIndex >= contains.size()) return false;
Building b = contains.get(buildingIndex);
int currentLevel = b.getLevel();
CasaDeNarino hall = getTownHall();
if (currentLevel >= 2) return false;
else 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(int inhabitantIndex) {
if (inhabitantIndex >= inhabitants.size()) return false;
Inhabitant i = inhabitants.get(inhabitantIndex);
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) {

View File

@ -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;
}
}

View File

@ -6,4 +6,9 @@ public class ArcherTower extends DefenseBuilding {
setLevel(1);
upgrade(DefenseStages.archerTowerStages[0]);
}
@Override
public Stage getUpgradeStage() {
return DefenseStages.archerTowerStages[getLevel()+1];
}
}

View File

@ -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;

View File

@ -6,4 +6,9 @@ public class Cannon extends DefenseBuilding {
setLevel(1);
upgrade(DefenseStages.cannonStages[0]);
}
@Override
public Stage getUpgradeStage() {
return DefenseStages.cannonStages[getLevel()+1];
}
}

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -15,4 +15,9 @@ public class Farm extends ResourceBuilding {
protected ResourceHarvestHandler getHarvestHandler() {
return hall -> {};
}
@Override
public Stage getUpgradeStage() {
return ResourceStages.goldStages[getLevel()+1];
}
}

View File

@ -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);
}

View File

@ -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];
}
}

View File

@ -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;
}
}

View File

@ -13,4 +13,8 @@ public class LumberMine extends ResourceBuilding {
return hall -> hall.addWood(getHarvestRate());
}
@Override
public Stage getUpgradeStage() {
return ResourceStages.woodStages[getLevel()+1];
}
}

View File

@ -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];
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}