Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.78 KB | None | 0 0
  1. package civilizationclone;
  2.  
  3. import civilizationclone.GameMap.MapSize;
  4. import civilizationclone.Tile.Desert;
  5. import civilizationclone.Tile.Hills;
  6. import civilizationclone.Tile.Ocean;
  7. import civilizationclone.Tile.Plains;
  8. import civilizationclone.Tile.Tile;
  9. import civilizationclone.Unit.*;
  10. import java.awt.Color;
  11. import java.awt.Graphics;
  12. import java.awt.Point;
  13. import java.util.ArrayList;
  14. import java.util.InputMismatchException;
  15. import java.util.Scanner;
  16. import java.util.Set;
  17. import javax.swing.JFrame;
  18. import static javax.swing.JFrame.EXIT_ON_CLOSE;
  19. import javax.swing.JPanel;
  20.  
  21. public class GameLoop extends JPanel {
  22.  
  23. GameMap map;
  24. ArrayList<Player> playerList;
  25.  
  26. JFrame frame;
  27. Scanner scan;
  28. int choice;
  29.  
  30. public GameLoop() {
  31.  
  32. scan = new Scanner(System.in);
  33. map = new GameMap(MapSize.MEDIUM, 268);
  34. Unit.referenceMap(map);
  35. City.referenceMap(map);
  36.  
  37. playerList = new ArrayList<Player>();
  38. playerList.add(new Player("Joseph Stalin"));
  39. playerList.get(0).addUnit(new SettlerUnit(playerList.get(0), new Point(30, 65)));
  40. playerList.get(0).addUnit(new WarriorUnit(playerList.get(0), new Point(30, 64)));
  41.  
  42. initializeGraphics();
  43. repaint();
  44.  
  45. choice = 0;
  46. loop();
  47.  
  48. }
  49.  
  50. public void loop() {
  51.  
  52. for (Player player : playerList) {
  53.  
  54. System.out.println("Player " + player.getName() + " starting turn!");
  55. player.startTurn();
  56.  
  57. while (true) {
  58. System.out.println("What do you want to do?");
  59. System.out.print("\n1: Manage Units\n2: Manage Cities\n3: Select Research\n4: Next Turn\n---");
  60.  
  61. choice = scan.nextInt();
  62. scan.nextLine();
  63.  
  64. if (choice == 1) {
  65. manageUnit(player);
  66. } else if (choice == 2) {
  67.  
  68. } else if (choice == 3) {
  69. setResearch(player, player.getResearchableTech());
  70. } else if (choice == 4) {
  71. if (player.canEndTurn()) {
  72. System.out.println("End turn!");
  73. break;
  74. } else {
  75. System.out.println("Cannot end turn! You still need to select things");
  76. }
  77. } else {
  78. System.out.println("Invalid input!");
  79. }
  80.  
  81. repaint();
  82. }
  83. }
  84.  
  85. loop();
  86.  
  87. }
  88.  
  89. public void setCity(Player player, ArrayList<City> cityList){
  90. System.out.println(showCities(0, cityList, ""));
  91.  
  92. System.out.print("Which city would you like to manage?: ");
  93.  
  94. City c = player.getCityList().get(scan.nextInt()-1);
  95. System.out.println("The selected city is: " + c.getName());
  96. }
  97.  
  98. public String showCities(int index, ArrayList<City> cityList, String cityNames) {
  99. if (index == cityList.size()) {
  100. return cityNames;
  101. }
  102.  
  103. cityNames += (cityList.get(index).getName() + " ");
  104.  
  105. return showCities(index + 1, cityList, cityNames);
  106. }
  107.  
  108. public void setResearch(Player player, Set<TechType> researchableTech) {
  109. TechType[] researchableList = researchableTech.toArray(new TechType[researchableTech.size()]);
  110. System.out.println(showResearchable(researchableList, 0, ""));
  111.  
  112. System.out.print("Which tech would you like to research?: ");
  113. player.setResearch(researchableList[scan.nextInt() - 1]);
  114. System.out.println("The current tech being researched is: " + player.getResearch());
  115.  
  116. }
  117.  
  118. public String showResearchable(TechType[] researchableTech, int index, String researchable) {
  119. if (index == researchableTech.length) {
  120. return researchable;
  121. }
  122.  
  123. researchable += (researchableTech[index] + " ");
  124.  
  125. return showResearchable(researchableTech, index + 1, researchable);
  126. }
  127.  
  128. //UNIT MANAGEMENT
  129. //<editor-fold>
  130. public void manageUnit(Player p) {
  131.  
  132. ArrayList<Unit> list = new ArrayList<Unit>();
  133. for (Unit u : p.getUnitList()) {
  134. if (u.canMove()) {
  135. list.add(u);
  136. }
  137. }
  138.  
  139. if (list.isEmpty()) {
  140. System.out.println("You have no movable unit at this time!");
  141. return;
  142. }
  143.  
  144. System.out.println("Please select a unit from the following list: ");
  145.  
  146. for (int i = 0; i < list.size(); i++) {
  147. System.out.println(i + ") " + list.get(i).getClass().getSimpleName());
  148. }
  149.  
  150. choice = scan.nextInt();
  151. scan.nextLine();
  152.  
  153. Unit unit = list.get(choice);
  154.  
  155. System.out.println("What do you want to do with " + unit.getClass().getSimpleName());
  156. System.out.print("\n1: Move Units\n2: Attack\n3: Special Actions\n4: Rest\n---");
  157.  
  158. choice = scan.nextInt();
  159. scan.nextLine();
  160.  
  161. switch (choice) {
  162. case 1:
  163. moveUnit(unit);
  164. break;
  165. case 2:
  166. if (unit instanceof MilitaryUnit) {
  167. attackUnit((MilitaryUnit) unit);
  168. } else {
  169. System.out.println("This unit cannot attack!");
  170. return;
  171. }
  172. break;
  173. case 3:
  174. actUnit(unit);
  175. break;
  176. case 4:
  177. System.out.println(unit.getClass().getSimpleName() + " is resting!");
  178. unit.setMovement(0);
  179. break;
  180. default:
  181. System.out.println("Invalid input number");
  182. return;
  183.  
  184. }
  185. }
  186.  
  187. public void moveUnit(Unit unit) {
  188.  
  189. Point[] movable = unit.getMoves();
  190.  
  191. if (movable.length == 0) {
  192. System.out.println("This unit cannot move anywhere!");
  193. return;
  194. }
  195.  
  196. System.out.println("Where do you want this unit to move to?");
  197.  
  198. for (int i = 0; i < movable.length; i++) {
  199. System.out.println(i + ") x: " + movable[i].x + " y: " + movable[i].y);
  200. }
  201.  
  202. System.out.print("Input here: ");
  203. choice = scan.nextInt();
  204. scan.nextLine();
  205.  
  206. if (choice >= 0 && choice < movable.length) {
  207. unit.move(movable[choice]);
  208. System.out.println("Unit moved!");
  209. return;
  210. }
  211.  
  212. System.out.println("Invalid input!");
  213.  
  214. }
  215.  
  216. public void attackUnit(MilitaryUnit unit) {
  217.  
  218. Point[] attackable = unit.getAttackable();
  219.  
  220. if (attackable.length == 0) {
  221. System.out.println("There is nothing for this unit to attack!");
  222. return;
  223. }
  224.  
  225. System.out.println("Which unit would you like to attack? ");
  226.  
  227. for (int i = 0; i < attackable.length; i++) {
  228. Unit victim = map.getTile(attackable[i].x, attackable[i].y).getUnit();
  229. System.out.println(i + ") " + victim.getClass().getSimpleName() + " x: " + victim.getX() + " y: " + victim.getY());
  230. }
  231.  
  232. System.out.print("Enter here: ");
  233. choice = scan.nextInt();
  234. scan.nextLine();
  235.  
  236. if (choice < 0 || choice > attackable.length - 1) {
  237. System.out.println("Invalid selection!");
  238. return;
  239. }
  240.  
  241. unit.attack(map.getTile(attackable[choice].x, attackable[choice].y).getUnit());
  242. }
  243.  
  244. public void actUnit(Unit u) {
  245. if (u instanceof SettlerUnit) {
  246. System.out.println("What would you like to name this new city?");
  247. ((SettlerUnit) u).settle(scan.nextLine());
  248. }
  249.  
  250. if (u instanceof BuilderUnit) {
  251. System.out.println("How would you like to improve the city?");
  252. ((BuilderUnit) u).improveTile();
  253. }
  254.  
  255. }
  256.  
  257. //</editor-fold>
  258. //TESTING GRAPHICS
  259. //<editor-fold>
  260. @Override
  261. public void paintComponent(Graphics g) {
  262.  
  263. //Testing map printing
  264. super.paintComponent(g);
  265.  
  266. setBackground(Color.BLACK);
  267. for (int i = 0; i < map.getMap().length; i++) {
  268. for (int k = 0; k < map.getMap()[0].length; k++) {
  269.  
  270. Tile t = map.getTile(i, k);
  271.  
  272. if (t instanceof Ocean) {
  273. g.setColor(Color.BLUE);
  274. } else if (t instanceof Plains) {
  275. g.setColor(Color.GREEN);
  276. } else if (t instanceof Hills) {
  277. g.setColor(Color.GRAY);
  278. } else if (t instanceof Desert) {
  279. g.setColor(Color.YELLOW);
  280. } else {
  281. g.setColor(Color.BLACK);
  282. }
  283.  
  284. if (t.hasUnit()) {
  285. g.setColor(Color.RED);
  286. } else if (t.hasCity()) {
  287. g.setColor(Color.WHITE);
  288. }
  289.  
  290. if (i % 2 == 0) {
  291. g.fillPolygon(new int[]{k * 12 + 5 + k, k * 12 + 12 + k, k * 12 + 12 + k, k * 12 + 5 + k, k * 12 + k, k * 12 + k}, new int[]{i * 12 - i, i * 12 + 3 - i, i * 12 + 9 - i, i * 12 + 12 - i, i * 12 + 9 - i, i * 12 + 3 - i}, 6);
  292. } else {
  293. g.fillPolygon(new int[]{k * 12 + 5 + 5 + k + 2, k * 12 + 12 + 5 + k + 2, k * 12 + 12 + 5 + k + 2, k * 12 + 5 + 5 + k + 2, k * 12 + 5 + k + 2, k * 12 + 5 + k + 2}, new int[]{i * 12 - i, i * 12 + 3 - i, i * 12 + 9 - i, i * 12 + 12 - i, i * 12 + 9 - i, i * 12 + 3 - i}, 6);
  294. }
  295. }
  296. }
  297. }
  298.  
  299. public void initializeGraphics() {
  300. frame = new JFrame("TESTING");
  301. frame.add(this);
  302. frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  303. frame.setLocationRelativeTo(null);
  304. frame.setVisible(true);
  305. frame.setSize(1060, 960);
  306. }
  307. //</editor-fold>
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement