package main;
import framework.component.ComponentSystem;
import framework.component.ParentComponent;
import framework.spacial.PositionComponent;
import generators.BackgroundGenerator;
import generators.CursorGenerator;
import generators.DotsGenerator;
import generators.GameControllerGenerator;
import generators.GridLineGenerator;
import generators.ScoreCounterGenerator;
import generators.SquareGenerator;
import generators.TurnCounterGenerator;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.geom.Vector2f;
import squaresgame.ComputerPlayer;
import squaresgame.GameGrid;
import squaresgame.Grid;
import squaresgame.HumanPlayer;
import squaresgame.Player;
public class GameLoader {
private static GameContainer gc = null;
public static void loadGameWithOptionsMenu(int gridWidth, int gridHeight, GameContainer gameContainer){
if(gameContainer != null){
gc = gameContainer;
}
Object[] options = { "Human vs Human", "Human vs Computer", "Quit"};
String message = "Select a mode!";
int returnValue = JOptionPane.showOptionDialog(null, message, "Options", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
switch (returnValue) {
case 0:
loadGame(gridWidth, gridHeight, 2, 0);
break;
case 1:
loadGame(gridWidth, gridHeight, 1, 1);
break;
case 2:
gc.exit();
break;
default:
loadGame(gridWidth, gridHeight, 2, 0);
break;
}
}
public static void loadGame(int gridWidth, int gridHeight, int numHumanPlayers, int numAIPlayers){
clearOldGame();
//Make the mouse cursor work with in-game objects
CursorGenerator.setUpCursor();
//Create the logical representation of the game's grid.
Grid grid = new GameGrid(gridWidth, gridHeight);
//Set up the game controller to manage the turn system, and run in a seperate thread.
List<Player> players = generatePlayers(numHumanPlayers, numAIPlayers);
GameControllerGenerator.setUpGameController(grid, players);
//Load the graphics
loadGridGraphics(grid);
BackgroundGenerator.generateBackground();
//Set up the score and turn counters:
ScoreCounterGenerator.setUpScoreCounters(players);
TurnCounterGenerator.setUpTurnCounter();
}
private static void clearOldGame() {
ComponentSystem.clearComponentSystem();
}
private static void loadGridGraphics(Grid grid){
//The order of the code here is important. Do not change, or dots and lines will not be positioned correctly.
//TODO Make the order unimportant here.
ParentComponent gridParent = new ParentComponent();
ComponentSystem.getInstance().getRoot().addComponent(gridParent);
//Create the graphics for the dots, lines, and squares.
int topLeftX = 196;
int topLeftY = 105;
int spacing = 75;
GridLineGenerator.generateLineGrid(grid, gridParent, spacing);
SquareGenerator.generateSquares(grid, gridParent, spacing);
PositionComponent pos = new PositionComponent(0, 0);
gridParent.addComponent(pos);
pos.addVector(new Vector2f(topLeftX, topLeftY));
DotsGenerator.createDots(topLeftX, topLeftY , grid.getDotsWidth() , grid.getDotsHeight() , spacing, gridParent);
}
public static ArrayList<Player> generatePlayers(int numHumanPlayers, int numAIPlayers){
ArrayList<Player> players = new ArrayList<Player>();
for(int i =0; i < numHumanPlayers; i++){
players.add(new HumanPlayer(i, String.valueOf(i+1)));
}
for(int i=0;i < numAIPlayers; i++){
players.add(new ComputerPlayer(i+numHumanPlayers, String.valueOf(i+numHumanPlayers+1)));
}
return players;
}
}