Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package civilizationclone;
- import civilizationclone.GameMap.MapSize;
- import civilizationclone.Tile.Desert;
- import civilizationclone.Tile.Hills;
- import civilizationclone.Tile.Ocean;
- import civilizationclone.Tile.Plains;
- import civilizationclone.Tile.Tile;
- import civilizationclone.Unit.*;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- import java.util.Set;
- import javax.swing.JFrame;
- import static javax.swing.JFrame.EXIT_ON_CLOSE;
- import javax.swing.JPanel;
- public class GameLoop extends JPanel {
- GameMap map;
- ArrayList<Player> playerList;
- JFrame frame;
- Scanner scan;
- int choice;
- public GameLoop() {
- scan = new Scanner(System.in);
- map = new GameMap(MapSize.MEDIUM, 268);
- Unit.referenceMap(map);
- City.referenceMap(map);
- playerList = new ArrayList<Player>();
- playerList.add(new Player("Joseph Stalin"));
- playerList.get(0).addUnit(new SettlerUnit(playerList.get(0), new Point(30, 65)));
- playerList.get(0).addUnit(new WarriorUnit(playerList.get(0), new Point(30, 64)));
- initializeGraphics();
- repaint();
- choice = 0;
- loop();
- }
- public void loop() {
- for (Player player : playerList) {
- System.out.println("Player " + player.getName() + " starting turn!");
- player.startTurn();
- while (true) {
- System.out.println("What do you want to do?");
- System.out.print("\n1: Manage Units\n2: Manage Cities\n3: Select Research\n4: Next Turn\n---");
- choice = scan.nextInt();
- scan.nextLine();
- if (choice == 1) {
- manageUnit(player);
- } else if (choice == 2) {
- } else if (choice == 3) {
- setResearch(player, player.getResearchableTech());
- } else if (choice == 4) {
- if (player.canEndTurn()) {
- System.out.println("End turn!");
- break;
- } else {
- System.out.println("Cannot end turn! You still need to select things");
- }
- } else {
- System.out.println("Invalid input!");
- }
- repaint();
- }
- }
- loop();
- }
- public void setCity(Player player, ArrayList<City> cityList){
- System.out.println(showCities(0, cityList, ""));
- System.out.print("Which city would you like to manage?: ");
- City c = player.getCityList().get(scan.nextInt()-1);
- System.out.println("The selected city is: " + c.getName());
- }
- public String showCities(int index, ArrayList<City> cityList, String cityNames) {
- if (index == cityList.size()) {
- return cityNames;
- }
- cityNames += (cityList.get(index).getName() + " ");
- return showCities(index + 1, cityList, cityNames);
- }
- public void setResearch(Player player, Set<TechType> researchableTech) {
- TechType[] researchableList = researchableTech.toArray(new TechType[researchableTech.size()]);
- System.out.println(showResearchable(researchableList, 0, ""));
- System.out.print("Which tech would you like to research?: ");
- player.setResearch(researchableList[scan.nextInt() - 1]);
- System.out.println("The current tech being researched is: " + player.getResearch());
- }
- public String showResearchable(TechType[] researchableTech, int index, String researchable) {
- if (index == researchableTech.length) {
- return researchable;
- }
- researchable += (researchableTech[index] + " ");
- return showResearchable(researchableTech, index + 1, researchable);
- }
- //UNIT MANAGEMENT
- //<editor-fold>
- public void manageUnit(Player p) {
- ArrayList<Unit> list = new ArrayList<Unit>();
- for (Unit u : p.getUnitList()) {
- if (u.canMove()) {
- list.add(u);
- }
- }
- if (list.isEmpty()) {
- System.out.println("You have no movable unit at this time!");
- return;
- }
- System.out.println("Please select a unit from the following list: ");
- for (int i = 0; i < list.size(); i++) {
- System.out.println(i + ") " + list.get(i).getClass().getSimpleName());
- }
- choice = scan.nextInt();
- scan.nextLine();
- Unit unit = list.get(choice);
- System.out.println("What do you want to do with " + unit.getClass().getSimpleName());
- System.out.print("\n1: Move Units\n2: Attack\n3: Special Actions\n4: Rest\n---");
- choice = scan.nextInt();
- scan.nextLine();
- switch (choice) {
- case 1:
- moveUnit(unit);
- break;
- case 2:
- if (unit instanceof MilitaryUnit) {
- attackUnit((MilitaryUnit) unit);
- } else {
- System.out.println("This unit cannot attack!");
- return;
- }
- break;
- case 3:
- actUnit(unit);
- break;
- case 4:
- System.out.println(unit.getClass().getSimpleName() + " is resting!");
- unit.setMovement(0);
- break;
- default:
- System.out.println("Invalid input number");
- return;
- }
- }
- public void moveUnit(Unit unit) {
- Point[] movable = unit.getMoves();
- if (movable.length == 0) {
- System.out.println("This unit cannot move anywhere!");
- return;
- }
- System.out.println("Where do you want this unit to move to?");
- for (int i = 0; i < movable.length; i++) {
- System.out.println(i + ") x: " + movable[i].x + " y: " + movable[i].y);
- }
- System.out.print("Input here: ");
- choice = scan.nextInt();
- scan.nextLine();
- if (choice >= 0 && choice < movable.length) {
- unit.move(movable[choice]);
- System.out.println("Unit moved!");
- return;
- }
- System.out.println("Invalid input!");
- }
- public void attackUnit(MilitaryUnit unit) {
- Point[] attackable = unit.getAttackable();
- if (attackable.length == 0) {
- System.out.println("There is nothing for this unit to attack!");
- return;
- }
- System.out.println("Which unit would you like to attack? ");
- for (int i = 0; i < attackable.length; i++) {
- Unit victim = map.getTile(attackable[i].x, attackable[i].y).getUnit();
- System.out.println(i + ") " + victim.getClass().getSimpleName() + " x: " + victim.getX() + " y: " + victim.getY());
- }
- System.out.print("Enter here: ");
- choice = scan.nextInt();
- scan.nextLine();
- if (choice < 0 || choice > attackable.length - 1) {
- System.out.println("Invalid selection!");
- return;
- }
- unit.attack(map.getTile(attackable[choice].x, attackable[choice].y).getUnit());
- }
- public void actUnit(Unit u) {
- if (u instanceof SettlerUnit) {
- System.out.println("What would you like to name this new city?");
- ((SettlerUnit) u).settle(scan.nextLine());
- }
- if (u instanceof BuilderUnit) {
- System.out.println("How would you like to improve the city?");
- ((BuilderUnit) u).improveTile();
- }
- }
- //</editor-fold>
- //TESTING GRAPHICS
- //<editor-fold>
- @Override
- public void paintComponent(Graphics g) {
- //Testing map printing
- super.paintComponent(g);
- setBackground(Color.BLACK);
- for (int i = 0; i < map.getMap().length; i++) {
- for (int k = 0; k < map.getMap()[0].length; k++) {
- Tile t = map.getTile(i, k);
- if (t instanceof Ocean) {
- g.setColor(Color.BLUE);
- } else if (t instanceof Plains) {
- g.setColor(Color.GREEN);
- } else if (t instanceof Hills) {
- g.setColor(Color.GRAY);
- } else if (t instanceof Desert) {
- g.setColor(Color.YELLOW);
- } else {
- g.setColor(Color.BLACK);
- }
- if (t.hasUnit()) {
- g.setColor(Color.RED);
- } else if (t.hasCity()) {
- g.setColor(Color.WHITE);
- }
- if (i % 2 == 0) {
- 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);
- } else {
- 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);
- }
- }
- }
- }
- public void initializeGraphics() {
- frame = new JFrame("TESTING");
- frame.add(this);
- frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- frame.setSize(1060, 960);
- }
- //</editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement