Package com.aqpproject.worldmodel.game.state

Source Code of com.aqpproject.worldmodel.game.state.CarSelectionGameState

/*
* 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.tools.Vector2D;
import com.aqpproject.worldmodel.Control;
import com.aqpproject.worldmodel.GameState;
import com.aqpproject.worldmodel.game.WorldModelGame;
import com.aqpproject.worldmodel.game.entity.WEAlphaTextButton;
import com.aqpproject.worldmodel.game.entity.WEMenu;
import com.aqpproject.worldmodel.game.entity.WorldEntity;
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.util.ArrayList;
import java.util.HashMap;
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 CarSelectionGameState implements GameState {

    @Override
    public void initialize() {
        if (!m_initialized) {
            m_worldEntities = new HashMap();
            m_miniaturesList = new ArrayList();
            m_spritesList = new HashMap();
            m_carNameList = new HashMap();
            m_displayNameList = new HashMap();
            m_velocityList = new HashMap();
            m_maniabilityList = new HashMap();
            m_carCount = 0;
            m_carWidth = 0;
            m_carHeight = 0;
            try {
                m_resolution = Singleton.getVisualisation().getResolution();
                loadCarList("config/carselection.xml");
                Singleton.getVisualisation().loadSprites("config/mainmenu.xml");
            } catch (Exception ex) {
                Logger.getLogger(WorldModelGame.class.getName()).log(Level.SEVERE, null, ex);
            }
            m_decX = 1420;
            m_decY = 1750;
            m_isUpdating = false;

            m_waitTime = 200;

            m_lastInput = 0;
            m_position = 0;
            m_previousSelectedCar = -1;
            m_currentSelectedCar = m_miniaturesList.indexOf(Singleton.getOptionsController().getCar());
            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;
            createEntities();
            m_initialized = true;
        }
    }

    private void createEntities() {

        Singleton.getVisualisation().createMap(Singleton.getWorldModel().getMaps().get("MapMenu").getPath());
        Singleton.getVisualisation().setCameraPosition(m_decX, m_decY);

        //Bandeau
        m_worldEntities.put("bandeauY", new WEMenu("bandeauY", "bandeauY", new Vector2D(975, 1000), 0, 1));

        //Stars
        for (int i = 0; i < 5; i++) {
            m_worldEntities.put("vel" + i, new WEMenu("vel" + i, "bug", new Vector2D(m_minX + m_menuX + m_menuWidth - 32 * 5 - 10 + 32 * i, m_resolution.y + m_minY - 275), 0, 2));
            m_worldEntities.put("man" + i, new WEMenu("man" + i, "bug", new Vector2D(m_minX + m_menuX + m_menuWidth - 32 * 5 - 10 + 32 * i, m_resolution.y + m_minY - 325), 0, 2));
        }

        //Title
        float textWidth = calcWidth("Car selection", "ar30whi");
        m_title = new WEAlphaTextButton("Title", m_menuX + m_menuWidth / 2 - textWidth / 2, m_resolution.y - 50, "Car selection", "ar30whi");
        String selected_car = m_miniaturesList.get(m_currentSelectedCar);
        String display_name = m_displayNameList.get(selected_car);

        //Car, continue and quit texts
        m_car = new WEAlphaTextButton("Car", m_menuX + 50, m_resolution.y - 150, display_name, "ar30whi");
        m_car.setSelect(true);
        m_car.m_setMInitialize(true);

        m_quit = new WEAlphaTextButton("Quit", m_menuX + 50, m_resolution.y - 450, "Cancel", "ar30whi");
        m_quit.m_setMInitialize(true);

        String lib_select;
        if (Singleton.getOptionsController().getMode().equals("MONO")) {
            lib_select = "Start !";
        } else {
            lib_select = "Select";
        }

        m_continue = new WEAlphaTextButton("Continue", m_menuX + 50, m_resolution.y - 400, lib_select, "ar30whi");
        m_continue.m_setMInitialize(true);

        //Characteristics texts
        m_velocity = new WEAlphaTextButton("Velocity", m_menuX + 50, m_resolution.y - 250, "Velocity", "ar30whi");
        m_velocity.m_setMInitialize(true);
        m_maniability = new WEAlphaTextButton("Maniability", m_menuX + 50, m_resolution.y - 300, "Maniability", "ar30whi");
        m_maniability.m_setMInitialize(true);

        //Miniatures
        m_miniature = new WEMenu("Miniature", m_spritesList.get(selected_car), new Vector2D(m_minX + m_menuX + m_menuWidth - 170, m_resolution.y + m_minY - 150 - m_carHeight), 0, 1);

        //Arrows
        m_worldEntities.put("arrow_sel_car", new WEMenu("arrow_sel_car", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 178), 0, 1));
        m_worldEntities.put("arrow_continue", new WEMenu("arrow_continue", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 428), 0, 2));
        m_worldEntities.put("arrow_quit", new WEMenu("arrow_quit", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 478), 0, 2));
    }

    @Override
    public void destroy() {
        //Entities
        for (WorldEntity e : m_worldEntities.values()) {
            e.destroy();
        }
        //Title
        m_title.destroy();
        m_car.destroy();
        m_quit.destroy();
        m_continue.destroy();
        m_velocity.destroy();
        m_miniature.destroy();
        m_maniability.destroy();
    }

    @Override
    public void update() {

        if (m_initialized) {
            m_isUpdating = true;
            long time = Singleton.getWorldModel().getTime();
            m_car.setSelect(m_position == 0 ? true : false);
            m_continue.setSelect(m_position == 1 ? true : false);
            m_quit.setSelect(m_position == 2 ? true : false);
            Singleton.getVisualisation().updateActorFrame("arrow_sel_car", m_position == 0 ? 1 : 2);
            Singleton.getVisualisation().updateActorFrame("arrow_continue", m_position == 1 ? 1 : 2);
            Singleton.getVisualisation().updateActorFrame("arrow_quit", m_position == 2 ? 1 : 2);

            m_car.update(time);
            m_quit.update(time);
            m_continue.update(time);
            m_velocity.update(time);
            m_maniability.update(time);

            if (m_currentSelectedCar != m_previousSelectedCar) {
                String selected_car = m_miniaturesList.get(m_currentSelectedCar);
                String display_name = m_displayNameList.get(selected_car);
                m_car.updateText(display_name);
                m_miniature.destroy();
                // Singleton.getVisualisation().deleteActor("Miniature", true);
                m_miniature = new WEMenu("Miniature", m_spritesList.get(selected_car), new Vector2D(m_minX + m_menuX + m_menuWidth - 170, m_resolution.y + m_minY - 150 - m_carHeight), 0, 1);;
                for (int i = 0; i < 5; i++) {
                    if (m_velocityList.get(m_miniaturesList.get(m_currentSelectedCar)) > i) {
                        Singleton.getVisualisation().updateActorFrame("vel" + i, 1);
                    } else {
                        Singleton.getVisualisation().updateActorFrame("vel" + i, 2);
                    }
                    if (m_maniabilityList.get(m_miniaturesList.get(m_currentSelectedCar)) > i) {
                        Singleton.getVisualisation().updateActorFrame("man" + i, 1);
                    } else {
                        Singleton.getVisualisation().updateActorFrame("man" + i, 2);
                    }
                }
                m_previousSelectedCar = m_currentSelectedCar;

            }
            m_isUpdating = false;


        }

    }

    @Override
    public void control(Control control) {
        if (control == Control.RIGHT && Singleton.getWorldModel().getTime() - m_lastInput > m_waitTime && m_position == 0) {
            m_previousSelectedCar = m_currentSelectedCar;
            m_currentSelectedCar = m_currentSelectedCar + 1;
            if (m_currentSelectedCar >= m_carCount) {
                m_currentSelectedCar = 0;
            }
            m_lastInput = Singleton.getWorldModel().getTime();
        }
        if (control == Control.LEFT && Singleton.getWorldModel().getTime() - m_lastInput > m_waitTime && m_position == 0) {
            m_previousSelectedCar = m_currentSelectedCar;
            m_currentSelectedCar = m_currentSelectedCar - 1;
            if (m_currentSelectedCar < 0) {
                m_currentSelectedCar = m_carCount - 1;
            }
            m_lastInput = Singleton.getWorldModel().getTime();
        }
        if (control == Control.DOWN && Singleton.getWorldModel().getTime() - m_lastInput > m_waitTime) {
            m_position++;
            if (m_position > 2) {
                m_position = 0;
            }
            System.out.println(m_position);
            try {
                Singleton.getAudioController().playSound("ding", false);
            } catch (ParserConfigurationException | SAXException | IOException ex) {
                Logger.getLogger(CarSelectionGameState.class.getName()).log(Level.SEVERE, null, ex);
            }
            m_lastInput = Singleton.getWorldModel().getTime();
        }
        if (control == Control.UP && Singleton.getWorldModel().getTime() - m_lastInput > m_waitTime) {
            m_position--;
            if (m_position < 0) {
                m_position = 2;
            }
            try {
                Singleton.getAudioController().playSound("ding", false);
            } catch (ParserConfigurationException | SAXException | IOException ex) {
                Logger.getLogger(CarSelectionGameState.class.getName()).log(Level.SEVERE, null, ex);
            }
            m_lastInput = Singleton.getWorldModel().getTime();
        }

        if (control == Control.ENTER && m_position == 1) {
            String miniature_name = m_miniaturesList.get(m_currentSelectedCar);
            String carSprite = m_spritesList.get(miniature_name);
            Singleton.getOptionsController().setCar(miniature_name);
            if (Singleton.getOptionsController().getMode().equals("MONO")) {
                Singleton.getWorldModel().setNextState(new RaceGameState("server", "player1", Singleton.getOptionsController().getMap(), miniature_name, 1));
            } else {
                if (Singleton.getOptionsController().getRole().equals("SERVER")) {
                    Singleton.getWorldModel().setNextState(new HostMenuGameState());
                } else {
                    Singleton.getNetwork().setSendCar(true);
                    Singleton.getWorldModel().setNextState(new JoinMenuGameState());
                }
            }
        }

        if (control == Control.ESCAPE || (control == Control.ENTER && m_position == 2)) {
            if (Singleton.getOptionsController().getMode().equals("MONO")) {
                Singleton.getWorldModel().setNextState(new MapSelectionGameState());
            } else {
                if (Singleton.getOptionsController().getRole().equals("SERVER")) {
                    Singleton.getWorldModel().setNextState(new HostMenuGameState());
                } else {
                    Singleton.getWorldModel().setNextState(new JoinMenuGameState());
                }
            }
        }
    }

    public Vector2D getCameraPosition() {
        return new Vector2D();
    }

    @Override
    public void updateActorTransform() {
        //Vector2D camPos = getCameraPosition();
        //Singleton.getVisualisation().setCameraPosition(camPos.x, camPos.y);
        //Entities
        if (!m_isUpdating) {
            m_miniature.updateActorTransform();
            for (WorldEntity e : m_worldEntities.values()) {
                e.updateActorTransform();
            }
        }
    }

    @Override
    public void saveEntitiesState() {
        /*
         * for (WorldEntity e : m_worldEntities.values()) { e.saveTransform(); }
         *
         */
    }

    @Override
    public boolean isInitialized() {
        return m_initialized;
    }

    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;
                    m_carCount++;
                    if (i == 1) {
                        m_carWidth = Integer.parseInt(e.getAttribute("width"));
                        m_carHeight = Integer.parseInt(e.getAttribute("height"));
                    }
                    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_miniaturesList.add(name);
                    m_spritesList.put(name, e.getAttribute("spriteName"));
                    m_carNameList.put(name, e.getAttribute("correspondingCar"));
                    m_displayNameList.put(name, e.getAttribute("displayName"));
                    m_velocityList.put(name, Integer.parseInt(e.getAttribute("velocity")));
                    m_maniabilityList.put(name, Integer.parseInt(e.getAttribute("maniability")));
                    Singleton.getVisualisation().createSprite(spriteName, spritePath, new Rectangle(x, y, width, height), frame);
                }
            }
        }
    }

    @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 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;
    }

    @Override
    public HashMap<String, WorldEntity> getWorldEntities() {
        return m_worldEntities;
    }

    @Override
    public void control(Control control, String entity) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    protected HashMap<String, WorldEntity> m_worldEntities;
    protected boolean m_initialized;
    protected int m_carCount;
    protected HashMap<String, String> m_spritesList;
    protected HashMap<String, String> m_carNameList;
    protected HashMap<String, String> m_displayNameList;
    protected HashMap<String, Integer> m_velocityList;
    protected HashMap<String, Integer> m_maniabilityList;
    protected ArrayList<String> m_miniaturesList;
    protected int m_carWidth, m_carHeight;
    protected Vector2D m_resolution;
    protected int m_currentSelectedCar;
    protected int m_previousSelectedCar;
    protected int m_decY;
    protected int m_decX;
    protected long m_lastInput;
    protected long m_waitTime;
    protected WEMenu m_miniature;
    protected WEAlphaTextButton m_title;
    protected WEAlphaTextButton m_car;
    protected WEAlphaTextButton m_velocity;
    protected WEAlphaTextButton m_maniability;
    protected WEAlphaTextButton m_quit;
    protected WEAlphaTextButton m_continue;
    protected float m_minX;
    protected float m_minY;
    protected float m_menuX;
    protected float m_menuWidth;
    protected boolean m_isUpdating;
    protected int m_position;
}
TOP

Related Classes of com.aqpproject.worldmodel.game.state.CarSelectionGameState

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.