Compare commits

...

8 Commits

Author SHA1 Message Date
mike 21917eb82e I put the new Forgis on the Jeep 2023-03-03 19:30:42 -05:00
Brett 5b59d9f930 Building 2023-03-03 16:08:23 -05:00
Brett b087a47015 Stage Auto leveling 2023-03-03 15:08:01 -05:00
Brett 295049702f pushy 2023-03-03 15:04:33 -05:00
Brett 989c76ced9 village stages 2023-03-03 14:50:28 -05:00
Brett f2793b1a97 Add bounds checking on resources 2023-03-03 14:39:42 -05:00
Brett fb0425ec1d Stages Array 2023-03-03 14:27:10 -05:00
Brett debb59bc39 Updating + stages 2023-03-03 14:24:33 -05:00
19 changed files with 440 additions and 103 deletions

View File

@ -1,13 +1,12 @@
package ca.cosc3p91.a2; package ca.cosc3p91.a2;
import ca.cosc3p91.a2.game.GameEngine; import ca.cosc3p91.a2.game.GameEngine;
import ca.cosc3p91.a2.gameobjects.Stage;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
GameEngine engine = new GameEngine(); new GameEngine().run();
engine.run();
} }
} }

View File

@ -3,11 +3,21 @@ package ca.cosc3p91.a2.game;
import ca.cosc3p91.a2.gameobjects.*; import ca.cosc3p91.a2.gameobjects.*;
import ca.cosc3p91.a2.player.*; import ca.cosc3p91.a2.player.*;
import ca.cosc3p91.a2.util.Print; import ca.cosc3p91.a2.util.Print;
import ca.cosc3p91.a2.util.Time;
import ca.cosc3p91.a2.util.Util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
public class GameEngine implements Runnable { public class GameEngine implements Runnable {
public static final double GOLD_FACTOR = 5;
public static final double IRON_FACTOR = 1;
public static final double WOOD_FACTOR = 0.1;
private Player player; private Player player;
boolean running = true; boolean running = true;
@ -15,13 +25,13 @@ public class GameEngine implements Runnable {
private int currentTime; private int currentTime;
private final Random random = new Random(System.nanoTime());
public Map map; public Map map;
public GameEngine() { public GameEngine() {
player = new Player(); player = new Player();
VillageStage vInitialStage = new VillageStage(100, 0, 2, 30, 0, map = generateInitialMap();
0, 1000, 2500, 5000);
map = new Map(new VillageHall(1, vInitialStage), 30);
} }
private void printState() { private void printState() {
@ -71,21 +81,153 @@ public class GameEngine implements Runnable {
} }
public void attackVillage(Map map) { public void attackVillage(Map map) {
}
private Map generateInitialMap(){
return new Map(new CasaDeNarino(1, VillageHallStages.villageStages[0]), 30);
} }
public Map generateMap() { public Map generateMap() {
return null; Map initialMap = generateInitialMap();
// 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]);
int buildingCount = this.map.contains.size();
int saulGoodMines = 0;
int ironMines = 0;
int woodMines = 0;
int archerTowers = 0;
int cannons = 0;
// count buildings in our map
for (Building b : this.map.contains){
if (b instanceof SaulGoodMine)
saulGoodMines++;
else if (b instanceof IronMine)
ironMines++;
else if (b instanceof LumberMine)
woodMines++;
else if (b instanceof ArcherTower)
archerTowers++;
else if (b instanceof Cannon)
cannons++;
} }
public void getScore(Map map) { // variate
saulGoodMines += random.nextInt();;
ironMines += random.nextInt();
woodMines += random.nextInt();
archerTowers += random.nextInt();
cannons += random.nextInt();
// generate a map with a similar number of buildings
for (int i = 0; i < Math.max(buildingCount + random.nextInt(5) - 2, 1); i++) {
int selection = random.nextInt(5);
// select a random building. Doing it this way because if we build based on buildings we have
// then the maps will be VERY boring as they would be all the same
switch (selection) {
case 0:
initialMap.contains.add(new LumberMine(ResourceStages.woodStages[random.nextInt(ResourceStages.woodStages.length)]));
break;
case 1:
if (ironMines > 0)
initialMap.contains.add(new IronMine(ResourceStages.ironStages[random.nextInt(ResourceStages.ironStages.length)]));
break;
case 2:
if (saulGoodMines > 0)
initialMap.contains.add(new SaulGoodMine(ResourceStages.goldStages[random.nextInt(ResourceStages.goldStages.length)]));
break;
case 3:
initialMap.contains.add(new ArcherTower());
break;
case 4:
initialMap.contains.add(new Cannon());
break;
default:
break;
}
}
return initialMap;
}
public int getScore(Map map) {
CasaDeNarino hall = map.getTownHall();
int score = (int)(hall.getCurrentGold() * GOLD_FACTOR + hall.getCurrentIron() * IRON_FACTOR + hall.getCurrentWood() * WOOD_FACTOR);
score += map.contains.size();
score += map.inhabitants.size();
return score;
} }
@Override @Override
public void run() { public void run() {
Scanner sc = new Scanner(System.in); BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
while (running) { Scanner sc = new Scanner(rd);
printState(); printState();
int in = sc.nextInt(); System.out.println();
while (running) {
for (Building b : map.contains){
if ((b instanceof ResourceBuilding)) {
((ResourceBuilding) b).update(map.getTownHall());
}
}
//System.out.println("Updating");
try {
if (rd.ready()) {
String in = sc.nextLine();
String[] args = in.split(" ");
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':
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);
}
break;
default:
break;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }
} }
private static Building determineType(String building){
building = building.toLowerCase();
char c = ' ';
if (building.trim().length() == 1)
c = building.charAt(0);
if (building.contains("gold") || building.contains("good") || c == 'g') {
return new SaulGoodMine(ResourceStages.goldStages[0]);
} else if (building.contains("iron") || c == 'i') {
return new IronMine(ResourceStages.ironStages[0]);
} else if (building.contains("wood") || building.contains("lumber") || c == 'w' || c == 'l') {
return new LumberMine(ResourceStages.woodStages[0]);
} else if (building.contains("archer") || c == 'a') {
return new ArcherTower();
} else if (building.contains("can") || c == 'c'){
return new Cannon();
}
return null;
}
}

View File

@ -9,7 +9,7 @@ public class Map {
static int MAXSIZE = 400; static int MAXSIZE = 400;
private VillageHall townHall; private CasaDeNarino townHall;
private int guardTime; private int guardTime;
@ -17,11 +17,11 @@ public class Map {
public List<Inhabitant> inhabitants; public List<Inhabitant> inhabitants;
public Map(VillageHall villageHall, int gTime) { public Map(CasaDeNarino casaDeNarino, int gTime) {
contains = new ArrayList<>(); contains = new ArrayList<>();
inhabitants = new ArrayList<>(); inhabitants = new ArrayList<>();
this.townHall = villageHall; this.townHall = casaDeNarino;
this.contains.add(villageHall); this.contains.add(casaDeNarino);
this.guardTime = gTime; this.guardTime = gTime;
} }
@ -34,8 +34,20 @@ public class Map {
} }
public void build(Tile t, Building b) { public void 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);
CasaDeNarino hall = getTownHall();
if (hall.getCurrentGold() >= goldCost && hall.getCurrentIron() >= ironCost && hall.getCurrentWood() >= woodCost) {
if(!hall.addGold(-goldCost))
throw new RuntimeException("Unable to subtract gold despite valid check!");
if(!hall.addIron(-ironCost))
throw new RuntimeException("Unable to subtract iron despite valid check!");
if(!hall.addWood(-woodCost))
throw new RuntimeException("Unable to subtract wood despite valid check!");
contains.add(b); contains.add(b);
} }
}
public int getGuardTime() { public int getGuardTime() {
return guardTime; return guardTime;
@ -45,7 +57,7 @@ public class Map {
this.guardTime = gTime; this.guardTime = gTime;
} }
public VillageHall getTownHall(){ public CasaDeNarino getTownHall(){
return townHall; return townHall;
} }

View File

@ -62,6 +62,9 @@ public abstract class Building {
public void upgrade(Stage stage) { public void upgrade(Stage stage) {
this.stage = stage; this.stage = stage;
this.health += stage.dHealth; this.health += stage.dHealth;
// evil hack
String name = stage.getClass().getSimpleName();
this.level = Integer.parseInt(name.charAt(name.length()-1) + "") - 1;
// interact with the timer regarding Upgrade time // interact with the timer regarding Upgrade time
} }

View File

@ -0,0 +1,74 @@
package ca.cosc3p91.a2.gameobjects;
public class CasaDeNarino extends Building {
private int goldCapacity = 0;
private int ironCapacity = 0;
private int woodCapacity = 0;
private int currentGold = 0;
private int currentIron = 0;
private int currentWood = 50;
public CasaDeNarino(int lvl, VillageStage baseStage) {
setLevel(lvl);
upgrade(baseStage);
}
public void upgrade(VillageStage stage) {
super.upgrade(stage);
this.goldCapacity += stage.getGoldCapacityIncrease();
this.ironCapacity += stage.getIronCapacityIncrease();
this.woodCapacity += stage.getWoodCapacityIncrease();
}
public int getGoldCapacity() {
return goldCapacity;
}
public int getIronCapacity() {
return ironCapacity;
}
public int getWoodCapacity() {
return woodCapacity;
}
public int getCurrentGold() {
return currentGold;
}
public int getCurrentIron() {
return currentIron;
}
public int getCurrentWood() {return currentWood;}
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 boolean addIron(int amount) {
int newIron = this.currentIron + amount;
if (newIron <= ironCapacity && newIron >= 0) {
this.currentIron += amount;
return true;
}
return false;
}
public boolean addWood(int amount) {
int newWood = this.currentWood + amount;
if (newWood <= woodCapacity && newWood >= 0) {
this.currentWood += amount;
return true;
}
return false;
}
}

View File

@ -1,12 +1,14 @@
package ca.cosc3p91.a2.gameobjects; package ca.cosc3p91.a2.gameobjects;
import ca.cosc3p91.a2.util.Time;
class DefenseStage extends Stage { class DefenseStage extends Stage {
protected int dDamage; protected int dDamage;
protected int dRange; protected int dRange;
public DefenseStage(int dHealth, int goldCost, int requiredVillageLevel, int upgradeTime, int ironCost, int woodCost, public DefenseStage(int dHealth, int goldCost, int requiredVillageLevel, Time upgradeTime, int ironCost, int woodCost,
int damageIncrease, int rangeIncrease) { int damageIncrease, int rangeIncrease) {
super(dHealth, goldCost, requiredVillageLevel, upgradeTime, ironCost, woodCost); super(dHealth, goldCost, requiredVillageLevel, upgradeTime, ironCost, woodCost);
this.dDamage = damageIncrease; this.dDamage = damageIncrease;

View File

@ -0,0 +1,46 @@
package ca.cosc3p91.a2.gameobjects;
import ca.cosc3p91.a2.util.Time;
public class DefenseStages {
public static class ArcherTowerStage1 extends DefenseStage {
public ArcherTowerStage1() {
super(100, 0, 0, new Time().offsetMinutes(1), 25, 250, 4,6);
}
}
public static class ArcherTowerStage2 extends DefenseStage {
public ArcherTowerStage2() {
super(150, 0, 1, new Time().offsetMinutes(15), 25, 250, 8,8);
}
}
public static class ArcherTowerStage3 extends DefenseStage {
public ArcherTowerStage3() {
super(200, 0, 2, new Time().offsetHours(1), 25, 250, 12,12);
}
}
public static class CannonStage1 extends DefenseStage {
public CannonStage1() {
super(125, 0, 0, new Time().offsetMinutes(2), 25, 250, 8,4);
}
}
public static class CannonStage2 extends DefenseStage {
public CannonStage2() {
super(175, 0, 1, new Time().offsetMinutes(20), 25, 250, 12,6);
}
}
public static class CannonStage3 extends DefenseStage {
public CannonStage3() {
super(225, 0, 2, new Time().offsetHours(1), 25, 250, 14,8);
}
}
public static DefenseStage[] archerTowerStages = {new DefenseStages.ArcherTowerStage1(), new DefenseStages.ArcherTowerStage2(), new DefenseStages.ArcherTowerStage3()};
public static DefenseStage[] cannonStages = {new DefenseStages.CannonStage1(), new DefenseStages.CannonStage2(), new DefenseStages.CannonStage3()};
}

View File

@ -12,5 +12,7 @@ public class Farm extends ResourceBuilding {
} }
@Override @Override
public void harvest(VillageHall hall) {} protected ResourceHarvestHandler getHarvestHandler() {
return hall -> {};
}
} }

View File

@ -4,13 +4,12 @@ public class IronMine extends ResourceBuilding {
public static String resource = "iron"; public static String resource = "iron";
public IronMine(int lvl, ResourceStage baseStage) { public IronMine(ResourceStage baseStage) {
setLevel(lvl);
upgrade(baseStage); upgrade(baseStage);
} }
@Override @Override
public void harvest(VillageHall hall) { protected ResourceHarvestHandler getHarvestHandler() {
hall.addCurrentIron(getHarvestRate()); return hall -> hall.addIron(getHarvestRate());
} }
} }

View File

@ -4,13 +4,13 @@ public class LumberMine extends ResourceBuilding {
public static String resource = "wood"; public static String resource = "wood";
public LumberMine(int lvl, ResourceStage baseStage) { public LumberMine(ResourceStage baseStage) {
setLevel(lvl);
upgrade(baseStage); upgrade(baseStage);
} }
public void harvest(VillageHall hall) { @Override
hall.addCurrentWood(getHarvestRate()); protected ResourceHarvestHandler getHarvestHandler() {
return hall -> hall.addWood(getHarvestRate());
} }
} }

View File

@ -4,6 +4,10 @@ import ca.cosc3p91.a2.util.Time;
public abstract class ResourceBuilding extends Building { public abstract class ResourceBuilding extends Building {
protected interface ResourceHarvestHandler {
void harvest(CasaDeNarino hall);
}
public static String resource; public static String resource;
private final Time harvestMinTime = new Time().offsetSeconds(10); private final Time harvestMinTime = new Time().offsetSeconds(10);
@ -20,14 +24,14 @@ public abstract class ResourceBuilding extends Building {
this.harvest_rate += stage.getHarvestRateIncrease(); this.harvest_rate += stage.getHarvestRateIncrease();
} }
public void update(VillageHall hall){ public void update(CasaDeNarino hall){
if (nextHarvestTime.occurred()){ if (nextHarvestTime.occurred()){
harvest(hall); getHarvestHandler().harvest(hall);
nextHarvestTime = Time.getTime().offsetTime(harvestMinTime); nextHarvestTime = Time.getTime().offsetTime(harvestMinTime);
} }
} }
protected abstract void harvest(VillageHall hall); protected abstract ResourceHarvestHandler getHarvestHandler();
public int getHarvestRate(){ public int getHarvestRate(){
return harvest_rate; return harvest_rate;

View File

@ -1,10 +1,12 @@
package ca.cosc3p91.a2.gameobjects; package ca.cosc3p91.a2.gameobjects;
import ca.cosc3p91.a2.util.Time;
public class ResourceStage extends Stage { public class ResourceStage extends Stage {
protected int harvestRateIncrease; protected int harvestRateIncrease;
public ResourceStage(int dHealth, int goldCost, int requiredVillageLevel, int upgradeTime, int ironCost, int woodCost, public ResourceStage(int dHealth, int goldCost, int requiredVillageLevel, Time upgradeTime, int ironCost, int woodCost,
int harvestRateIncr) { int harvestRateIncr) {
super(dHealth, goldCost, requiredVillageLevel, upgradeTime, ironCost, woodCost); super(dHealth, goldCost, requiredVillageLevel, upgradeTime, ironCost, woodCost);
this.harvestRateIncrease = harvestRateIncr; this.harvestRateIncrease = harvestRateIncr;

View File

@ -0,0 +1,65 @@
package ca.cosc3p91.a2.gameobjects;
import ca.cosc3p91.a2.util.Time;
public class ResourceStages {
public static class GoldStage1 extends ResourceStage {
public GoldStage1() {
super(100, 0, 0, new Time().offsetMinutes(1), 25, 250, 25);
}
}
public static class GoldStage2 extends ResourceStage {
public GoldStage2() {
super(100, 100, 1, new Time().offsetMinutes(15), 25, 275, 35);
}
}
public static class GoldStage3 extends ResourceStage {
public GoldStage3() {
super(100, 150, 2, new Time().offsetHours(1), 50, 325, 50);
}
}
public static class IronStage1 extends ResourceStage {
public IronStage1() {
super(100, 0, 0, new Time().offsetMinutes(1), 0, 125, 25);
}
}
public static class IronStage2 extends ResourceStage {
public IronStage2() {
super(100, 15, 1, new Time().offsetMinutes(15), 25, 155, 35);
}
}
public static class IronStage3 extends ResourceStage {
public IronStage3() {
super(100, 50, 2, new Time().offsetHours(1), 50, 250, 50);
}
}
public static class WoodStage1 extends ResourceStage {
public WoodStage1() {
super(100, 0, 0, new Time().offsetMinutes(1), 0, 50, 25);
}
}
public static class WoodStage2 extends ResourceStage {
public WoodStage2() {
super(100, 5, 1, new Time().offsetMinutes(15), 5, 75, 35);
}
}
public static class WoodStage3 extends ResourceStage {
public WoodStage3() {
super(100, 10, 2, new Time().offsetHours(1), 25, 100, 50);
}
}
public static ResourceStage[] goldStages = {new GoldStage1(), new GoldStage2(), new GoldStage3()};
public static ResourceStage[] ironStages = {new IronStage1(), new IronStage2(), new IronStage3()};
public static ResourceStage[] woodStages = {new WoodStage1(), new WoodStage2(), new WoodStage3()};
}

View File

@ -4,13 +4,12 @@ public class SaulGoodMine extends ResourceBuilding {
public static String resource = "gold"; public static String resource = "gold";
public SaulGoodMine(int lvl, ResourceStage baseStage) { public SaulGoodMine(ResourceStage baseStage) {
setLevel(lvl);
upgrade(baseStage); upgrade(baseStage);
} }
@Override @Override
public void harvest(VillageHall hall) { protected ResourceHarvestHandler getHarvestHandler() {
hall.addCurrentGold(getHarvestRate()); return hall -> hall.addGold(getHarvestRate());
} }
} }

View File

@ -1,6 +1,10 @@
package ca.cosc3p91.a2.gameobjects; package ca.cosc3p91.a2.gameobjects;
abstract class Stage { import ca.cosc3p91.a2.util.Time;
import java.util.HashMap;
public abstract class Stage {
protected int dHealth; protected int dHealth;
@ -8,13 +12,13 @@ abstract class Stage {
protected int requiredVillageLevel; protected int requiredVillageLevel;
protected int upgradeTime; protected Time upgradeTime;
protected int ironCost; protected int ironCost;
protected int woodCost; protected int woodCost;
public Stage(int dHealth, int goldCost, int requiredVillageLevel, int upgradeTime, int ironCost, int woodCost) { public Stage(int dHealth, int goldCost, int requiredVillageLevel, Time upgradeTime, int ironCost, int woodCost) {
this.dHealth = dHealth; this.dHealth = dHealth;
this.goldCost = goldCost; this.goldCost = goldCost;
this.requiredVillageLevel = requiredVillageLevel; this.requiredVillageLevel = requiredVillageLevel;
@ -36,7 +40,7 @@ abstract class Stage {
return requiredVillageLevel; return requiredVillageLevel;
} }
public int getUpgradeTime() { public Time getUpgradeTime() {
return upgradeTime; return upgradeTime;
} }

View File

@ -1,63 +0,0 @@
package ca.cosc3p91.a2.gameobjects;
import java.util.ArrayList;
public class VillageHall extends Building {
private int goldCapacity = 0;
private int ironCapacity = 0;
private int woodCapacity = 0;
private int currentGold;
private int currentIron;
private int currentWood;
public VillageHall(int lvl, VillageStage baseStage) {
setLevel(lvl);
upgrade(baseStage);
}
public void upgrade(VillageStage stage) {
super.upgrade(stage);
this.goldCapacity += stage.getGoldCapacityIncrease();
this.ironCapacity += stage.getIronCapacityIncrease();
this.woodCapacity += stage.getWoodCapacityIncrease();
}
public int getGoldCapacity() {
return goldCapacity;
}
public int getIronCapacity() {
return ironCapacity;
}
public int getWoodCapacity() {
return woodCapacity;
}
public int getCurrentGold() {
return currentGold;
}
public void addCurrentGold(int currentGold) {
this.currentGold += currentGold;
}
public int getCurrentIron() {
return currentIron;
}
public void addCurrentIron(int currentIron) {
this.currentIron += currentIron;
}
public int getCurrentWood() {
return currentWood;
}
public void addCurrentWood(int currentWood) {
this.currentWood += currentWood;
}
}

View File

@ -0,0 +1,30 @@
package ca.cosc3p91.a2.gameobjects;
import ca.cosc3p91.a2.util.Time;
public class VillageHallStages {
public static class VillageStage1 extends VillageStage {
public VillageStage1() {
super(100, 0, 0, new Time(), 0,
0, 1000, 2500, 5000);
}
}
public static class VillageStage2 extends VillageStage {
public VillageStage2() {
super(550, 1000, 0, new Time().offsetHours(2), 2500,
5000, 2500, 5000, 10000);
}
}
public static class VillageStage3 extends VillageStage {
public VillageStage3() {
super(550, 2500, 0, new Time().offsetHours(2), 5000,
10000, 5000, 7500, 15000);
}
}
public static VillageStage[] villageStages = {new VillageStage1(), new VillageStage2(), new VillageStage3()};
}

View File

@ -1,5 +1,7 @@
package ca.cosc3p91.a2.gameobjects; package ca.cosc3p91.a2.gameobjects;
import ca.cosc3p91.a2.util.Time;
public class VillageStage extends Stage { public class VillageStage extends Stage {
protected int goldCapacityIncrease; protected int goldCapacityIncrease;
@ -8,7 +10,7 @@ public class VillageStage extends Stage {
protected int woodCapacityIncrease; protected int woodCapacityIncrease;
public VillageStage(int dHealth, int goldCost, int requiredVillageLevel, int upgradeTime, int ironCost, int woodCost, public VillageStage(int dHealth, int goldCost, int requiredVillageLevel, Time upgradeTime, int ironCost, int woodCost,
int goldCapIncrease, int ironCapIncrease, int woodCapIncrease) { int goldCapIncrease, int ironCapIncrease, int woodCapIncrease) {
super(dHealth, goldCost, requiredVillageLevel, upgradeTime, ironCost, woodCost); super(dHealth, goldCost, requiredVillageLevel, upgradeTime, ironCost, woodCost);
this.goldCapacityIncrease = goldCapIncrease; this.goldCapacityIncrease = goldCapIncrease;

View File

@ -0,0 +1,15 @@
package ca.cosc3p91.a2.util;
import java.util.Random;
public class Util {
public static int randomInt(int min, int max, Random random) {
return random.nextInt(max - min) + min;
}
public static int clamp(int min, int max, int i){
return Math.min(Math.max(i, min), max);
}
}