Added building and inhabitant factories

main
mike 2023-03-19 15:53:49 -04:00
parent 1b3a48e4d1
commit a58273d787
6 changed files with 66 additions and 50 deletions

View File

@ -1,6 +1,8 @@
package ca.cosc3p91.a2.game; package ca.cosc3p91.a2.game;
import ca.cosc3p91.a2.gameobjects.*; import ca.cosc3p91.a2.gameobjects.*;
import ca.cosc3p91.a2.gameobjects.factory.BuildingFactory;
import ca.cosc3p91.a2.gameobjects.factory.InhabitantFactory;
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.Time;
@ -227,7 +229,8 @@ public class GameEngine<T> implements Runnable {
if (args.length < 2) { if (args.length < 2) {
System.err.println("Args must include type!"); System.err.println("Args must include type!");
} else { } else {
Building type = determineBuildingType(args[1]); BuildingFactory bfactory = new BuildingFactory();
Building type = bfactory.getBuilding(args[1]);
if (type == null) if (type == null)
System.err.println("Args are not a valid building!"); System.err.println("Args are not a valid building!");
else if (this.map.build(new Tile(), type) ) { else if (this.map.build(new Tile(), type) ) {
@ -240,7 +243,8 @@ public class GameEngine<T> implements Runnable {
if (args.length < 2) { if (args.length < 2) {
System.err.println("Args must include type!"); System.err.println("Args must include type!");
} else { } else {
Inhabitant type = determineInhabitantType(args[1]); InhabitantFactory ifactory = new InhabitantFactory();
Inhabitant type = ifactory.getInhabitant(args[1]);
if (type == null) if (type == null)
System.err.println("Args are not a valid inhabitant!"); System.err.println("Args are not a valid inhabitant!");
else if (this.map.train(type) ) { else if (this.map.train(type) ) {
@ -302,51 +306,4 @@ public class GameEngine<T> implements Runnable {
} }
} }
private static char determineChar(String str){
char c = ' ';
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 (argument.contains("gold") || argument.contains("good") || c == 'g') {
return new SaulGoodMine(ResourceStages.goldStages[0]);
} else if (argument.contains("iron") || c == 'i') {
return new IronMine(ResourceStages.ironStages[0]);
} else if (argument.contains("wood") || argument.contains("lumber") || c == 'w' || c == 'l') {
return new LumberMine(ResourceStages.woodStages[0]);
} else if (argument.contains("archer") || c == 'a') {
return new ArcherTower();
} else if (argument.contains("can") || c == 'c') {
return new Cannon();
}
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();
} else if (argument.contains("work") || c == 'w') {
return new Worker();
} else if (argument.contains("collect") || c == 'c') {
return new Collector();
} else if (argument.contains("cat")) {
return new Catapult();
} else if (argument.contains("arch") || c == 'a') {
return new Archer();
}
return null;
}
} }

View File

@ -0,0 +1,25 @@
package ca.cosc3p91.a2.gameobjects.factory;
import ca.cosc3p91.a2.gameobjects.*;
import ca.cosc3p91.a2.util.Util;
public class BuildingFactory {
public Building getBuilding(String buildingName) {
buildingName = buildingName.toLowerCase();
char c = Util.getFirstChar(buildingName);
if (buildingName.contains("gold") || buildingName.contains("good") || c == 'g') {
return new SaulGoodMine(ResourceStages.goldStages[0]);
} else if (buildingName.contains("iron") || c == 'i') {
return new IronMine(ResourceStages.ironStages[0]);
} else if (buildingName.contains("wood") || buildingName.contains("lumber") || c == 'w' || c == 'l') {
return new LumberMine(ResourceStages.woodStages[0]);
} else if (buildingName.contains("archer") || c == 'a') {
return new ArcherTower();
} else if (buildingName.contains("can") || c == 'c') {
return new Cannon();
}
return null;
}
}

View File

@ -0,0 +1,27 @@
package ca.cosc3p91.a2.gameobjects.factory;
import ca.cosc3p91.a2.gameobjects.*;
import ca.cosc3p91.a2.util.Util;
public class InhabitantFactory {
public Inhabitant getInhabitant(String inhabitantName) {
inhabitantName = inhabitantName.toLowerCase();
char c = Util.getFirstChar(inhabitantName);
if (inhabitantName.contains("soldier") || c == 's') {
return new Soldier();
} else if (inhabitantName.contains("knight") || c == 'k') {
return new Knight();
} else if (inhabitantName.contains("work") || c == 'w') {
return new Worker();
} else if (inhabitantName.contains("collect") || c == 'c') {
return new Collector();
} else if (inhabitantName.contains("cat")) {
return new Catapult();
} else if (inhabitantName.contains("arch") || c == 'a') {
return new Archer();
}
return null;
}
}

View File

@ -12,4 +12,11 @@ public class Util {
return Math.min(Math.max(i, min), max); return Math.min(Math.max(i, min), max);
} }
public static char getFirstChar(String str){
char c = ' ';
if (str.trim().length() == 1)
c = str.charAt(0);
return c;
}
} }

View File

@ -3,7 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Assignment 2/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/Assignment 3/src" isTestSource="false" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />