/*
* AQP Project
* http://http://code.google.com/p/aqp-project/
* Alexandre Gomez - Clément Troesch - Fabrice Latterner
*/
package com.aqpproject.worldmodel.game.state;
import com.aqpproject.game.Singleton;
import com.aqpproject.sound.SoundController;
import com.aqpproject.tools.Vector2D;
import com.aqpproject.worldmodel.Control;
import com.aqpproject.worldmodel.GameState;
import com.aqpproject.worldmodel.game.entity.WEAlphaTextButton;
import com.aqpproject.worldmodel.game.entity.WEMenu;
import com.aqpproject.worldmodel.game.entity.WorldEntity;
import com.aqpproject.worldmodel.game.entity.WorldTextEntity;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
* @author admlatterner
*/
public class JoinMenuGameState implements GameState {
/**
* Constructor
*/
public JoinMenuGameState() {
m_initialized = false;
m_menuOk = false;
}
/**
* @see WorldModel#initialize()
*/
@Override
public void initialize() {
if (!m_initialized) { //If not initialized
m_worldEntities = new HashMap<>();
m_worldTextEntities = new HashMap<>();
m_spritesNameList = new HashMap<>();
m_mapDisplayList = new HashMap<>();
try {
sc = Singleton.getAudioController();
Singleton.getVisualisation().loadSprites("config/mainmenu.xml");
loadCarList("config/carselection.xml");
loadMapList("config/mapselection.xml");
} catch (ParserConfigurationException | SAXException | IOException ex) {
Logger.getLogger(HostMenuGameState.class.getName()).log(Level.SEVERE, null, ex);
}
m_position = 0;
m_lastInput = 0;
m_waitTime = 200;
m_keyboardState = new HashMap();
for (Control c : Singleton.getInput().getKeyboardState().keySet()) {
m_keyboardState.put(c, false);
}
m_resolution = Singleton.getVisualisation().getResolution();
m_minX = 1420 - m_resolution.x / 2;
m_minY = 1750 - m_resolution.y / 2;
m_menuX = 975 - m_minX;
m_menuWidth = Singleton.getVisualisation().getSpriteSize("bandeauY").x;
m_continuer = true;
m_pseudo = Singleton.getOptionsController().getPseudo();
m_port = Singleton.getOptionsController().getPort();
m_IP = Singleton.getOptionsController().getIP();
m_currentIPPart = 0;
m_IPAddress = getAddrIP();
m_isConnected = false;
createEntities();
//Initialisation
m_initialized = true;
}
}
/**
* @see WorldModel#destroy()
*/
@Override
public void destroy() {
//Entities
for (WorldEntity e : m_worldEntities.values()) {
e.destroy();
}
for (WorldTextEntity e : m_worldTextEntities.values()) {
e.destroy();
}
}
/**
* Create the entities
*/
private void createEntities() {
Singleton.getVisualisation().createMap(Singleton.getWorldModel().getMaps().get("MapMenu").getPath());
Singleton.getVisualisation().setCameraPosition(1420, 1750);
m_worldEntities.put("bandeauY1", new WEMenu("bandeauY1", "bandeauY", new Vector2D(975, 1000), 0, 1));
float textWidth = calcWidth("Join game", "ar30whi");
//Texts
m_worldTextEntities.put("Title", new WEAlphaTextButton("Title", m_menuX + m_menuWidth / 2 - textWidth / 2, m_resolution.y - 50, "Join game", "ar30whi"));
m_worldTextEntities.put("Port", new WEAlphaTextButton("Port", m_menuX + 50, m_resolution.y - 150, "Port", "ar30whi"));
m_worldTextEntities.put("PortNum", new WEAlphaTextButton("PortNum", m_menuX + m_menuWidth - 300, m_resolution.y - 150, m_port, "ar30whi"));
m_worldTextEntities.put("IP", new WEAlphaTextButton("IP", m_menuX + 50, m_resolution.y - 200, "IP", "ar30whi"));
m_worldTextEntities.put("IPAddress", new WEAlphaTextButton("IPAddress", m_menuX + m_menuWidth - 300, m_resolution.y - 200, m_IPAddress, "ar30whi"));
m_worldTextEntities.put("Name", new WEAlphaTextButton("Name", m_menuX + 50, m_resolution.y - 300, "Name", "ar30whi"));
m_worldTextEntities.put("Pseudo", new WEAlphaTextButton("Pseudo", m_menuX + m_menuWidth - 300, m_resolution.y - 300, m_pseudo, "ar30whi"));
m_worldTextEntities.put("Car", new WEAlphaTextButton("Car", m_menuX + 50, m_resolution.y - 350, "Car", "ar30whi"));
m_worldTextEntities.put("Map", new WEAlphaTextButton("Map", m_menuX + 50, m_resolution.y - 400, "Map", "ar30whi"));
m_worldTextEntities.put("MapName", new WEAlphaTextButton("MapName", m_menuX + m_menuWidth - 300, m_resolution.y - 400, "", "ar30whi"));
m_worldTextEntities.put("Connect", new WEAlphaTextButton("Connect", m_menuX + 50, m_resolution.y - 500, "Connect", "ar30whi"));
m_worldTextEntities.put("Cancel", new WEAlphaTextButton("Cancel", m_menuX + 50, m_resolution.y - 600, "Cancel", "ar30whi"));
//Initialisation
((WEAlphaTextButton) m_worldTextEntities.get("Port")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("Port")).setSelect(true);
((WEAlphaTextButton) m_worldTextEntities.get("PortNum")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("PortNum")).setSelect(true);
((WEAlphaTextButton) m_worldTextEntities.get("IP")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("IPAddress")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("Name")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("Pseudo")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("Map")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("MapName")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("Car")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("Connect")).m_setMInitialize(true);
((WEAlphaTextButton) m_worldTextEntities.get("Cancel")).m_setMInitialize(true);
//Car miniature
m_worldEntities.put("car_mini", new WEMenu("car_mini", m_spritesNameList.get(Singleton.getOptionsController().getCar()), new Vector2D(m_minX + m_menuX + m_menuWidth - 300, m_resolution.y + m_minY - 378), 0, 1));
//Arrows
m_worldEntities.put("arrow_port", new WEMenu("arrow_port", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 178), 0, 1));
m_worldEntities.put("arrow_IP", new WEMenu("arrow_IP", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 228), 0, 2));
m_worldEntities.put("arrow_name", new WEMenu("arrow_name", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 328), 0, 2));
m_worldEntities.put("arrow_car", new WEMenu("arrow_car", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 378), 0, 2));
m_worldEntities.put("arrow_map", new WEMenu("arrow_map", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 428), 0, 2));
m_worldEntities.put("arrow_connect", new WEMenu("arrow_connect", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 528), 0, 2));
m_worldEntities.put("arrow_cancel", new WEMenu("arrow_cancel", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 628), 0, 2));
}
/**
* @see WorldModel#update()
*/
@Override
public void update() {
if (m_initialized) {
((WEAlphaTextButton) m_worldTextEntities.get("Port")).setSelect(m_position == 0 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("PortNum")).setSelect(m_position == 0 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("IP")).setSelect(m_position == 1 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("IPAddress")).setSelect(m_position == 1 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("Name")).setSelect(m_position == 2 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("Pseudo")).setSelect(m_position == 2 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("Car")).setSelect(m_position == 3 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("Map")).setSelect(m_position == 4 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("MapName")).setSelect(m_position == 4 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("Connect")).setSelect(m_position == 5 ? true : false);
((WEAlphaTextButton) m_worldTextEntities.get("Cancel")).setSelect(m_position == 6 ? true : false);
Singleton.getVisualisation().updateActorFrame("arrow_port", m_position == 0 ? 1 : 2);
Singleton.getVisualisation().updateActorFrame("arrow_IP", m_position == 1 ? 1 : 2);
Singleton.getVisualisation().updateActorFrame("arrow_name", m_position == 2 ? 1 : 2);
Singleton.getVisualisation().updateActorFrame("arrow_car", m_position == 3 ? 1 : 2);
Singleton.getVisualisation().updateActorFrame("arrow_map", m_position == 4 ? 1 : 2);
Singleton.getVisualisation().updateActorFrame("arrow_connect", m_position == 5 ? 1 : 2);
Singleton.getVisualisation().updateActorFrame("arrow_cancel", m_position == 6 ? 1 : 2);
//Update of keyboard state
HashMap<Control, Boolean> kbState = Singleton.getInput().getKeyboardState();
for (Entry e : kbState.entrySet()) {
if (!(boolean) e.getValue()) {
m_keyboardState.put((Control) e.getKey(), false);
}
}
if (Singleton.getOptionsController().getconnexionStatus().equals("CONNECTED")) {
m_isConnected = true;
String selected_map = Singleton.getOptionsController().getMap();
((WEAlphaTextButton) m_worldTextEntities.get("MapName")).updateText(m_mapDisplayList.get(selected_map));
((WEAlphaTextButton) m_worldTextEntities.get("Connect")).updateText("Disconnect");
} else {
m_isConnected = false;
((WEAlphaTextButton) m_worldTextEntities.get("Connect")).updateText("Connect");
}
((WEAlphaTextButton) m_worldTextEntities.get("Pseudo")).updateText(m_pseudo);
((WEAlphaTextButton) m_worldTextEntities.get("PortNum")).updateText(m_port);
((WEAlphaTextButton) m_worldTextEntities.get("IPAddress")).updateText(m_IPAddress);
long time = Singleton.getWorldModel().getTime();
for (WorldTextEntity e : m_worldTextEntities.values()) {
e.update(time);
}
}
}
/**
* @see WorldModel#controlEntity(java.lang.String,
* com.aqpproject.worldmodel.Control)
*/
@Override
public void control(Control control) {
if (m_position == 0) {
if (control.compareTo(Control.NUM_0) >= 0 && control.compareTo(Control.NUM_9) <= 0) {
if ((!m_keyboardState.get(control) || m_lastInput + m_waitTime / 2 < Singleton.getWorldModel().getTime()) && m_port.length() < 5) {
m_port += control.toString().substring(4);
m_keyboardState.put(control, true);
m_lastInput = Singleton.getWorldModel().getTime();
}
}
if (control == Control.RETURN) {
if ((!m_keyboardState.get(Control.RETURN) || m_lastInput + m_waitTime / 2 < Singleton.getWorldModel().getTime()) && m_port.length() > 0) {
m_port = m_port.substring(0, m_port.length() - 1);
m_keyboardState.put(control, true);
m_lastInput = Singleton.getWorldModel().getTime();
}
}
}
if (m_position == 1) {
if (control.compareTo(Control.NUM_0) >= 0 && control.compareTo(Control.NUM_9) <= 0) {
if ((!m_keyboardState.get(control) || m_lastInput + m_waitTime < Singleton.getWorldModel().getTime())) {
if (m_IP.get(m_currentIPPart).length() < 3) {
m_IP.set(m_currentIPPart, m_IP.get(m_currentIPPart).concat(control.toString().substring(4)));
m_keyboardState.put(control, true);
m_lastInput = Singleton.getWorldModel().getTime();
m_IPAddress = getAddrIP();
} else {
if (m_currentIPPart < 3) {
m_currentIPPart++;
control(control);
}
}
}
}
if (control == Control.RIGHT && m_currentIPPart < 3 && m_lastInput + m_waitTime < Singleton.getWorldModel().getTime()) {
m_currentIPPart++;
m_lastInput = Singleton.getWorldModel().getTime();
}
if (control == Control.LEFT && m_currentIPPart > 0 && m_lastInput + m_waitTime < Singleton.getWorldModel().getTime()) {
m_currentIPPart--;
m_lastInput = Singleton.getWorldModel().getTime();
}
if (control == Control.TAB && m_lastInput + m_waitTime < Singleton.getWorldModel().getTime()) {
m_currentIPPart++;
if (m_currentIPPart > 3) {
m_currentIPPart = 0;
}
m_lastInput = Singleton.getWorldModel().getTime();
}
if ((!m_keyboardState.get(control) || m_lastInput + m_waitTime < Singleton.getWorldModel().getTime()) && control == Control.RETURN) {
if (m_IP.get(m_currentIPPart).length() > 0) {
m_IP.set(m_currentIPPart, m_IP.get(m_currentIPPart).substring(0, m_IP.get(m_currentIPPart).length() - 1));
m_keyboardState.put(control, true);
m_IPAddress = getAddrIP();
m_lastInput = Singleton.getWorldModel().getTime();
} else if (m_currentIPPart > 0) {
m_currentIPPart--;
control(control.RETURN);
}
m_keyboardState.put(control, true);
}
}
if (m_position == 2) {
if (control.compareTo(Control.A) >= 0 && control.compareTo(Control.Z) <= 0) {
if ((!m_keyboardState.get(control) || m_lastInput + m_waitTime / 2 < Singleton.getWorldModel().getTime()) && m_pseudo.length() < 9) {
m_pseudo += control.toString();
m_keyboardState.put(control, true);
m_lastInput = Singleton.getWorldModel().getTime();
}
}
if (control == Control.RETURN) {
if ((!m_keyboardState.get(Control.RETURN) || m_lastInput + m_waitTime / 2 < Singleton.getWorldModel().getTime()) && m_pseudo.length() > 0) {
m_pseudo = m_pseudo.substring(0, m_pseudo.length() - 1);
m_keyboardState.put(control, true);
m_lastInput = Singleton.getWorldModel().getTime();
}
}
}
if (control == Control.UP && m_lastInput + m_waitTime < Singleton.getWorldModel().getTime()) {
m_position--;
sc.playSound("ding", false);
if (m_position < 0) {
m_position = 6;
}
m_lastInput = Singleton.getWorldModel().getTime();
}
if (control == Control.DOWN && m_lastInput + m_waitTime < Singleton.getWorldModel().getTime()) {
m_position++;
sc.playSound("ding", false);
if (m_position > 6) {
m_position = 0;
}
m_lastInput = Singleton.getWorldModel().getTime();
}
if (control == Control.ENTER && m_lastInput + m_waitTime < Singleton.getWorldModel().getTime()) {
switch (m_position) {
case 3:
//Car selection
Singleton.getOptionsController().setPort(m_port);
Singleton.getOptionsController().setIP(m_IP);
Singleton.getOptionsController().setPseudo(m_pseudo);
Singleton.getWorldModel().setNextState(new CarSelectionGameState());
break;
case 5:
//Connect
if (!m_isConnected) {
String address = m_IP.get(0) + "." + m_IP.get(1) + "." + m_IP.get(2) + "." + m_IP.get(3);
InetAddress IP_address;
try {
IP_address = InetAddress.getByName(address);
Singleton.getNetwork().connectToServer(IP_address, Integer.parseInt(m_port));
Singleton.getOptionsController().setPseudo(m_pseudo);
Singleton.getNetwork().setSendName(true);
Singleton.getNetwork().setSendCar(true);
if (Singleton.getNetwork().equals("CONNECTED")) {
m_isConnected = true;
}
} catch (Exception ex) {
Singleton.getOptionsController().setConnexionStatus("DISCONNECTING");
Singleton.getOptionsController().setConnexionStatus("UNKNOWN HOST");
Logger.getLogger(JoinMenuGameState.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
//Deconnexion
try {
Singleton.getNetwork().setSendClientDisconnexion(true);
Singleton.getNetwork().closeClient();
} catch (Exception e) {
Logger.getLogger(JoinMenuGameState.class.getName()).log(Level.SEVERE, null, e);
}
}
break;
case 6:
//Cancel
Singleton.getOptionsController().setCarToDefault();
Singleton.getWorldModel().setNextState(new MultiplayersMenuGameState());
break;
}
m_lastInput = Singleton.getWorldModel().getTime();
}
if (control == Control.ESCAPE) {
Singleton.getWorldModel().setNextState(new MultiplayersMenuGameState());
}
}
/**
* @see WorldModel#updateActorTransform()
*/
@Override
public void updateActorTransform() {
}
/**
* @see WorldModel#saveEntitiesState()
*/
@Override
public void saveEntitiesState() {
}
@Override
public boolean isInitialized() {
return m_initialized;
}
@Override
public void startContact(String firstEntity, String secondEntity, Vector2D normal, Vector2D point) {
}
@Override
public void stopContact(String firstEntity, String secondEntity, Vector2D normal, Vector2D point) {
}
public void addTextEntity(String name, WorldTextEntity entity) {
m_worldTextEntities.put(name, entity);
}
private void loadCarList(String path) throws ParserConfigurationException, SAXException, IOException {
if (path != null) {
File file = new File(path);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node n = children.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) n;
String name = e.getAttribute("name");
String spritePath = e.getAttribute("path");
String spriteName = e.getAttribute("spriteName");
int x = Integer.parseInt(e.getAttribute("x"));
int y = Integer.parseInt(e.getAttribute("y"));
int width = Integer.parseInt(e.getAttribute("width"));
int height = Integer.parseInt(e.getAttribute("height"));
int frame = Integer.parseInt(e.getAttribute("frame"));
m_spritesNameList.put(name, spriteName);
Singleton.getVisualisation().createSprite(spriteName, spritePath, new Rectangle(x, y, width, height), frame);
}
}
}
}
private void loadMapList(String path) throws ParserConfigurationException, SAXException, IOException {
if (path != null) {
File file = new File(path);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node n = children.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) n;
m_mapDisplayList.put(e.getAttribute("correspondingMap"), e.getAttribute("displayName"));
}
}
}
}
public float calcWidth(String s, String font) {
BitmapFont myFont = new BitmapFont(Gdx.files.internal("data/font/" + font + ".fnt"), Gdx.files.internal("data/font/" + font + ".png"), false); //TODO: change the font/size
float res = myFont.getBounds(s).width;
return res;
}
private String getAddrIP() {
ArrayList<String> parts = new ArrayList<>();
for (int i = 0; i < 4; i++) {
String part = "";
for (int x = 0; x < 3 - m_IP.get(i).length(); x++) {
part += " ";
}
part += m_IP.get(i);
parts.add(part);
}
return parts.get(0) + "." + parts.get(1) + "." + parts.get(2) + "." + parts.get(3);
}
@Override
public HashMap<String, WorldEntity> getWorldEntities() {
return m_worldEntities;
}
@Override
public void control(Control control, String entity) {
throw new UnsupportedOperationException("Not supported yet.");
}
/////////////////////////////////////////
// Attributes
/////////////////////////////////////////
protected HashMap<String, WorldEntity> m_worldEntities;
protected HashMap<String, WorldTextEntity> m_worldTextEntities;
protected HashMap<String, String> m_spritesNameList;
protected boolean m_initialized;
protected int m_position;
protected long m_lastInput;
protected long m_lastTextInput;
protected long m_waitTime;
protected long m_getTimeCreationState;
protected boolean m_menuOk;
protected boolean m_continuer;
protected SoundController sc;
protected float m_minX;
protected float m_minY;
protected float m_menuX;
protected float m_menuWidth;
protected long m_waitTextTime;
protected ArrayList<String> m_IP;
protected int m_currentIPPart;
protected Vector2D m_resolution;
protected String m_IPAddress;
protected String m_pseudo;
protected String m_port;
protected HashMap<Control, Boolean> m_keyboardState;
protected boolean m_isConnected;
protected HashMap<String, String> m_mapDisplayList;
}