Compare commits

..

2 Commits

Author SHA1 Message Date
Brett f476bfc80f format 2023-03-02 14:09:09 -05:00
Brett b9a58c61d1 Pretty print game engine info 2023-03-02 14:06:48 -05:00
10 changed files with 117 additions and 35 deletions

View File

@ -7,7 +7,6 @@ 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,10 +2,14 @@ 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;
@ -16,22 +20,54 @@ 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, 12, 12, 12); 0, 1000, 2500, 5000);
map = new Map(new Village_Hall(1, vInitialStage), 30); map = new Map(new VillageHall(1, vInitialStage), 30);
} }
public void printState() { private void printState() {
// Print toPrint = new Print("~ Current Village Buildings ~",2); Print resourcesPrinter = new Print("Current Village Resources", 2);
System.out.println("In Map:\n"); resourcesPrinter.addColumn(new Print.Column("Resource Type"));
System.out.println("\t~ Current Village Buildings ~\n"); resourcesPrinter.addColumn(new Print.Column("Max"));
for (Building b : map.contains) { resourcesPrinter.addColumn(new Print.Column("Amount"));
System.out.println("\t|> " + b.getClass().getSimpleName() + " lvl: " + b.getLevel() + " health: " + b.getHealth());
} resourcesPrinter.addRow(new Print.Row(
System.out.println("\n\t~ Current Village Inhabitants ~\n\n"); "Wood",
for (Inhabitant i : map.inhabitants) { Integer.toString(map.getTownHall().getWoodCapacity()),
System.out.println("\t|> " + i.getClass().getSimpleName() + " lvl: " + i.getLevel()); Integer.toString(map.getTownHall().getCurrentWood())));
}
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) {
@ -46,6 +82,10 @@ 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 Village_Hall townHall; private VillageHall townHall;
private int guardTime; private int guardTime;
@ -17,7 +17,7 @@ public class Map {
public List<Inhabitant> inhabitants; public List<Inhabitant> inhabitants;
public Map(Village_Hall villageHall, int gTime) { public Map(VillageHall villageHall, int gTime) {
contains = new ArrayList<>(); contains = new ArrayList<>();
inhabitants = new ArrayList<>(); inhabitants = new ArrayList<>();
this.townHall = villageHall; this.townHall = villageHall;
@ -45,4 +45,8 @@ 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(Village_Hall hall) {} public void harvest(VillageHall hall) {}
} }

View File

@ -10,7 +10,7 @@ public class IronMine extends ResourceBuilding {
} }
@Override @Override
public void harvest(Village_Hall hall) { public void harvest(VillageHall 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(Village_Hall hall) { public void harvest(VillageHall 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(Village_Hall hall){ public void update(VillageHall 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(Village_Hall hall); protected abstract void harvest(VillageHall hall);
public int getHarvestRate(){ public int getHarvestRate(){
return harvest_rate; return harvest_rate;

View File

@ -1,7 +1,5 @@
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";
@ -12,7 +10,7 @@ public class SaulGoodMine extends ResourceBuilding {
} }
@Override @Override
public void harvest(Village_Hall hall) { public void harvest(VillageHall 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 Village_Hall extends Building { public class VillageHall extends Building {
private int goldCapacity = 0; private int goldCapacity = 0;
private int ironCapacity = 0; private int ironCapacity = 0;
@ -12,7 +12,7 @@ public class Village_Hall extends Building {
private int currentIron; private int currentIron;
private int currentWood; private int currentWood;
public Village_Hall(int lvl, VillageStage baseStage) { public VillageHall(int lvl, VillageStage baseStage) {
setLevel(lvl); setLevel(lvl);
upgrade(baseStage); upgrade(baseStage);
} }

View File

@ -1,6 +1,7 @@
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++)
@ -19,13 +20,18 @@ 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 final String columnName; private String columnName;
private long maxColumnLength = 0; private long maxColumnLength = 0;
public Column(String columnName) { public Column(String columnName) {
@ -38,15 +44,21 @@ public class Print {
private final String tableName; private final String tableName;
private final int columnPadding; private final int columnPadding;
private int maxColumnWidth; private int width;
private final int targetWidth;
public Print(String tableName, int columnPadding) { public Print(String tableName, int columnPadding, int targetWidth) {
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("", 2); this("");
} }
private String createPadding(int amount) { private String createPadding(int amount) {
@ -114,7 +126,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('+');
@ -130,7 +142,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() - 1); column.maxColumnLength = Math.max(column.maxColumnLength, row.values.get(i).length());
} }
} }
} }
@ -159,12 +171,37 @@ 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);
@ -200,6 +237,10 @@ 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);