Compare commits

..

No commits in common. "f476bfc80f50a999b2c86b251201891469d03917" and "0495018430c8418f19ac5016dcd9f30ef755a793" have entirely different histories.

10 changed files with 35 additions and 117 deletions

View File

@ -7,6 +7,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
GameEngine engine = new GameEngine(); GameEngine engine = new GameEngine();
engine.printState();
engine.run(); engine.run();
} }

View File

@ -2,14 +2,10 @@ 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 java.util.Scanner;
public class GameEngine implements Runnable { public class GameEngine implements Runnable {
private Player player; private Player player;
boolean running = true;
private int pillageFactor; private int pillageFactor;
@ -20,54 +16,22 @@ public class GameEngine implements Runnable {
public GameEngine() { public GameEngine() {
player = new Player(); player = new Player();
VillageStage vInitialStage = new VillageStage(100, 0, 2, 30, 0, VillageStage vInitialStage = new VillageStage(100, 0, 2, 30, 0,
0, 1000, 2500, 5000); 0, 12, 12, 12);
map = new Map(new VillageHall(1, vInitialStage), 30); map = new Map(new Village_Hall(1, vInitialStage), 30);
} }
private void printState() { public void printState() {
Print resourcesPrinter = new Print("Current Village Resources", 2); // Print toPrint = new Print("~ Current Village Buildings ~",2);
resourcesPrinter.addColumn(new Print.Column("Resource Type")); System.out.println("In Map:\n");
resourcesPrinter.addColumn(new Print.Column("Max")); System.out.println("\t~ Current Village Buildings ~\n");
resourcesPrinter.addColumn(new Print.Column("Amount")); for (Building b : map.contains) {
System.out.println("\t|> " + b.getClass().getSimpleName() + " lvl: " + b.getLevel() + " health: " + b.getHealth());
resourcesPrinter.addRow(new Print.Row( }
"Wood", System.out.println("\n\t~ Current Village Inhabitants ~\n\n");
Integer.toString(map.getTownHall().getWoodCapacity()), for (Inhabitant i : map.inhabitants) {
Integer.toString(map.getTownHall().getCurrentWood()))); System.out.println("\t|> " + i.getClass().getSimpleName() + " lvl: " + i.getLevel());
}
resourcesPrinter.addRow(new Print.Row(
"Iron",
Integer.toString(map.getTownHall().getIronCapacity()),
Integer.toString(map.getTownHall().getCurrentIron())));
resourcesPrinter.addRow(new Print.Row(
"Gold",
Integer.toString(map.getTownHall().getGoldCapacity()),
Integer.toString(map.getTownHall().getCurrentGold())));
Print.print(resourcesPrinter.createTable(true, false, true));
Print buildingPrinter = new Print("Village Buildings", 2, resourcesPrinter.getWidth());
buildingPrinter.addColumn(new Print.Column("Name"));
buildingPrinter.addColumn(new Print.Column("Level"));
buildingPrinter.addColumn(new Print.Column("Health"));
for (Building b : map.contains)
buildingPrinter.addRow(new Print.Row(b.getClass().getSimpleName(),
Integer.toString(b.getLevel()),
Integer.toString(b.getHealth())));
Print.print(buildingPrinter.createTable(true, false, true));
Print inhabs = new Print("Village Inhabitants", 2, buildingPrinter.getWidth());
inhabs.addColumn(new Print.Column("Name"));
inhabs.addColumn(new Print.Column("Level"));
for (Inhabitant i : map.inhabitants)
inhabs.addRow(new Print.Row(i.getClass().getSimpleName(), Integer.toString(i.getLevel())));
Print.print(inhabs.createTable(true, true, true));
} }
public void attackVillage(Map map) { public void attackVillage(Map map) {
@ -82,10 +46,6 @@ public class GameEngine implements Runnable {
@Override @Override
public void run() { public void run() {
Scanner sc = new Scanner(System.in);
while (running) {
printState();
int in = sc.nextInt();
}
} }
} }

View File

@ -9,7 +9,7 @@ public class Map {
static int MAXSIZE = 400; static int MAXSIZE = 400;
private VillageHall townHall; private Village_Hall townHall;
private int guardTime; private int guardTime;
@ -17,7 +17,7 @@ public class Map {
public List<Inhabitant> inhabitants; public List<Inhabitant> inhabitants;
public Map(VillageHall villageHall, int gTime) { public Map(Village_Hall villageHall, int gTime) {
contains = new ArrayList<>(); contains = new ArrayList<>();
inhabitants = new ArrayList<>(); inhabitants = new ArrayList<>();
this.townHall = villageHall; this.townHall = villageHall;
@ -45,8 +45,4 @@ public class Map {
this.guardTime = gTime; this.guardTime = gTime;
} }
public VillageHall getTownHall(){
return townHall;
}
} }

View File

@ -12,5 +12,5 @@ public class Farm extends ResourceBuilding {
} }
@Override @Override
public void harvest(VillageHall hall) {} public void harvest(Village_Hall hall) {}
} }

View File

@ -10,7 +10,7 @@ public class IronMine extends ResourceBuilding {
} }
@Override @Override
public void harvest(VillageHall hall) { public void harvest(Village_Hall hall) {
hall.addCurrentIron(getHarvestRate()); hall.addCurrentIron(getHarvestRate());
} }
} }

View File

@ -9,7 +9,7 @@ public class LumberMine extends ResourceBuilding {
upgrade(baseStage); upgrade(baseStage);
} }
public void harvest(VillageHall hall) { public void harvest(Village_Hall hall) {
hall.addCurrentWood(getHarvestRate()); hall.addCurrentWood(getHarvestRate());
} }

View File

@ -20,14 +20,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(Village_Hall hall){
if (nextHarvestTime.occurred()){ if (nextHarvestTime.occurred()){
harvest(hall); harvest(hall);
nextHarvestTime = Time.getTime().offsetTime(harvestMinTime); nextHarvestTime = Time.getTime().offsetTime(harvestMinTime);
} }
} }
protected abstract void harvest(VillageHall hall); protected abstract void harvest(Village_Hall hall);
public int getHarvestRate(){ public int getHarvestRate(){
return harvest_rate; return harvest_rate;

View File

@ -1,5 +1,7 @@
package ca.cosc3p91.a2.gameobjects; package ca.cosc3p91.a2.gameobjects;
import ca.cosc3p91.a2.util.Time;
public class SaulGoodMine extends ResourceBuilding { public class SaulGoodMine extends ResourceBuilding {
public static String resource = "gold"; public static String resource = "gold";
@ -10,7 +12,7 @@ public class SaulGoodMine extends ResourceBuilding {
} }
@Override @Override
public void harvest(VillageHall hall) { public void harvest(Village_Hall hall) {
hall.addCurrentGold(getHarvestRate()); hall.addCurrentGold(getHarvestRate());
} }
} }

View File

@ -2,7 +2,7 @@ package ca.cosc3p91.a2.gameobjects;
import java.util.ArrayList; import java.util.ArrayList;
public class VillageHall extends Building { public class Village_Hall extends Building {
private int goldCapacity = 0; private int goldCapacity = 0;
private int ironCapacity = 0; private int ironCapacity = 0;
@ -12,7 +12,7 @@ public class VillageHall extends Building {
private int currentIron; private int currentIron;
private int currentWood; private int currentWood;
public VillageHall(int lvl, VillageStage baseStage) { public Village_Hall(int lvl, VillageStage baseStage) {
setLevel(lvl); setLevel(lvl);
upgrade(baseStage); upgrade(baseStage);
} }

View File

@ -1,7 +1,6 @@
package ca.cosc3p91.a2.util; package ca.cosc3p91.a2.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
/** /**
* Ported from blt::TableFormatter (C++) * Ported from blt::TableFormatter (C++)
@ -20,18 +19,13 @@ public class Print {
this.values = new ArrayList<>(); this.values = new ArrayList<>();
} }
public Row(String... values){
this();
this.values.addAll(Arrays.asList(values));
}
public void add(String value) { public void add(String value) {
values.add(value); values.add(value);
} }
} }
public static class Column { public static class Column {
private String columnName; private final String columnName;
private long maxColumnLength = 0; private long maxColumnLength = 0;
public Column(String columnName) { public Column(String columnName) {
@ -44,21 +38,15 @@ public class Print {
private final String tableName; private final String tableName;
private final int columnPadding; private final int columnPadding;
private int width; private int maxColumnWidth;
private final int targetWidth;
public Print(String tableName, int columnPadding, int targetWidth) { public Print(String tableName, int columnPadding) {
this.tableName = tableName; this.tableName = tableName;
this.columnPadding = columnPadding; this.columnPadding = columnPadding;
this.targetWidth = targetWidth;
} }
public Print(String tableName, int columnPadding){this(tableName, columnPadding, -1);}
public Print(String tableName){this(tableName, 2);}
public Print() { public Print() {
this(""); this("", 2);
} }
private String createPadding(int amount) { private String createPadding(int amount) {
@ -126,7 +114,7 @@ public class Print {
StringBuilder wholeWidthSeparator = new StringBuilder(); StringBuilder wholeWidthSeparator = new StringBuilder();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (i == nextIndex) { if (i == nextIndex) {
//System.out.println(currentColumnIndex + " " + nextIndex + " " + size); System.out.println(currentColumnIndex + " " + nextIndex + " " + size);
int currentColumnSize = (int) (columns.get(currentColumnIndex++).maxColumnLength + columnPadding * 2); int currentColumnSize = (int) (columns.get(currentColumnIndex++).maxColumnLength + columnPadding * 2);
nextIndex += currentColumnSize + 1; nextIndex += currentColumnSize + 1;
wholeWidthSeparator.append('+'); wholeWidthSeparator.append('+');
@ -142,7 +130,7 @@ public class Print {
Column column = columns.get(i); Column column = columns.get(i);
column.maxColumnLength = column.columnName.length(); column.maxColumnLength = column.columnName.length();
for (Row row : rows) { for (Row row : rows) {
column.maxColumnLength = Math.max(column.maxColumnLength, row.values.get(i).length()); column.maxColumnLength = Math.max(column.maxColumnLength, row.values.get(i).length() - 1);
} }
} }
} }
@ -171,37 +159,12 @@ public class Print {
} }
public ArrayList<String> createTable(boolean top, boolean bottom, boolean separators) { public ArrayList<String> createTable(boolean top, boolean bottom, boolean separators) {
ArrayList<String> lines = new ArrayList<>();
String header = generateColumnHeader(); String header = generateColumnHeader();
String topSeparator = generateTopSelector(header.length()); String topSeparator = generateTopSelector(header.length());
String lineSeparator = generateSeparator(header.length() - 1); String lineSeparator = generateSeparator(header.length() - 1);
if (targetWidth > 0) {
int diff = targetWidth - header.length();
if (diff > 0) {
int left = (int) Math.floor(diff / 2.0);
int right = (int) Math.ceil(diff / 2.0);
int leftleft = (int) Math.floor(left / 2.0);
int leftright = (int) Math.ceil(left / 2.0);
int rightleft = (int) Math.floor(right / 2.0);
int rightright = (int) Math.ceil(right / 2.0);
columns.get(0).columnName = createPadding(leftleft) + columns.get(0).columnName + createPadding(leftright);
columns.get(columns.size() - 1).columnName = createPadding(rightleft) + columns.get(columns.size() - 1).columnName + createPadding(rightright);
header = generateColumnHeader();
topSeparator = generateTopSelector(header.length());
lineSeparator = generateSeparator(header.length() - 1);
}
}
width = header.length();
ArrayList<String> lines = new ArrayList<>();
if (top) if (top)
lines.add(topSeparator); lines.add(topSeparator);
@ -237,10 +200,6 @@ public class Print {
return createTable(true, true, true); return createTable(true, true, true);
} }
public int getWidth(){
return width;
}
public static void print(ArrayList<String> lines) { public static void print(ArrayList<String> lines) {
for (String line : lines) for (String line : lines)
System.out.println(line); System.out.println(line);