Compare commits

..

No commits in common. "21917eb82ea56da2542e442ed2a5264479e68274" and "f476bfc80f50a999b2c86b251201891469d03917" have entirely different histories.

19 changed files with 103 additions and 440 deletions

View File

@ -1,12 +1,13 @@
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) {
new GameEngine().run(); GameEngine engine = new GameEngine();
engine.run();
} }
} }

View File

@ -3,21 +3,11 @@ 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;
@ -25,13 +15,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();
map = generateInitialMap(); VillageStage vInitialStage = new VillageStage(100, 0, 2, 30, 0,
0, 1000, 2500, 5000);
map = new Map(new VillageHall(1, vInitialStage), 30);
} }
private void printState() { private void printState() {
@ -81,153 +71,21 @@ 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() {
Map initialMap = generateInitialMap(); return null;
// 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++;
} }
// variate public void getScore(Map map) {
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() {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); Scanner sc = new Scanner(System.in);
Scanner sc = new Scanner(rd);
printState();
System.out.println();
while (running) { 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(); printState();
break; int in = sc.nextInt();
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 CasaDeNarino townHall; private VillageHall townHall;
private int guardTime; private int guardTime;
@ -17,11 +17,11 @@ public class Map {
public List<Inhabitant> inhabitants; public List<Inhabitant> inhabitants;
public Map(CasaDeNarino casaDeNarino, int gTime) { public Map(VillageHall villageHall, int gTime) {
contains = new ArrayList<>(); contains = new ArrayList<>();
inhabitants = new ArrayList<>(); inhabitants = new ArrayList<>();
this.townHall = casaDeNarino; this.townHall = villageHall;
this.contains.add(casaDeNarino); this.contains.add(villageHall);
this.guardTime = gTime; this.guardTime = gTime;
} }
@ -34,20 +34,8 @@ 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;
@ -57,7 +45,7 @@ public class Map {
this.guardTime = gTime; this.guardTime = gTime;
} }
public CasaDeNarino getTownHall(){ public VillageHall getTownHall(){
return townHall; return townHall;
} }

View File

@ -62,9 +62,6 @@ 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

@ -1,74 +0,0 @@
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,14 +1,12 @@
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, Time upgradeTime, int ironCost, int woodCost, public DefenseStage(int dHealth, int goldCost, int requiredVillageLevel, int 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

@ -1,46 +0,0 @@
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,7 +12,5 @@ public class Farm extends ResourceBuilding {
} }
@Override @Override
protected ResourceHarvestHandler getHarvestHandler() { public void harvest(VillageHall hall) {}
return hall -> {};
}
} }

View File

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

View File

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

View File

@ -4,10 +4,6 @@ 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);
@ -24,14 +20,14 @@ public abstract class ResourceBuilding extends Building {
this.harvest_rate += stage.getHarvestRateIncrease(); this.harvest_rate += stage.getHarvestRateIncrease();
} }
public void update(CasaDeNarino hall){ public void update(VillageHall hall){
if (nextHarvestTime.occurred()){ if (nextHarvestTime.occurred()){
getHarvestHandler().harvest(hall); harvest(hall);
nextHarvestTime = Time.getTime().offsetTime(harvestMinTime); nextHarvestTime = Time.getTime().offsetTime(harvestMinTime);
} }
} }
protected abstract ResourceHarvestHandler getHarvestHandler(); protected abstract void harvest(VillageHall hall);
public int getHarvestRate(){ public int getHarvestRate(){
return harvest_rate; return harvest_rate;

View File

@ -1,12 +1,10 @@
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, Time upgradeTime, int ironCost, int woodCost, public ResourceStage(int dHealth, int goldCost, int requiredVillageLevel, int 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

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

View File

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

View File

@ -0,0 +1,63 @@
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

@ -1,30 +0,0 @@
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,7 +1,5 @@
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;
@ -10,7 +8,7 @@ public class VillageStage extends Stage {
protected int woodCapacityIncrease; protected int woodCapacityIncrease;
public VillageStage(int dHealth, int goldCost, int requiredVillageLevel, Time upgradeTime, int ironCost, int woodCost, public VillageStage(int dHealth, int goldCost, int requiredVillageLevel, int 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

@ -1,15 +0,0 @@
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);
}
}