Package com.aqpproject.worldmodel.game.state

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

/*
* 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.WorldModelGame;
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.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
*
* @author Alexandre
*/
public class MainMenuGameState implements GameState {

    /**
     * Constructor
     */
    public MainMenuGameState() {
        m_initialized = false;
        HashMap keyboardState = Singleton.getInput().getKeyboardState();
        if ((boolean) keyboardState.get(Control.ESCAPE)) {
            m_escapeReady = false;
        } else {
            m_escapeReady = true;
        }
        if ((boolean) keyboardState.get(Control.ENTER)) {
            m_enterReady = false;
        } else {
            m_enterReady = true;
        }
    }

    /**
     * @see WorldModel#initialize()
     */
    @Override
    public void initialize() {

        if (!m_initialized) {   //If not initialized

            m_worldEntities = new HashMap<>();
            m_worldTextEntities = new HashMap<>();
            try {
                sc = Singleton.getAudioController();
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(MainMenuGameState.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SAXException ex) {
                Logger.getLogger(MainMenuGameState.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MainMenuGameState.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
                Singleton.getVisualisation().loadSprites("config/mainmenu.xml");
            } catch (Exception ex) {
                Logger.getLogger(WorldModelGame.class.getName()).log(Level.SEVERE, null, ex);
            }
            m_position = 0;
            m_lastInput = 0;
            m_waitTime = 200;
            m_resolution = Singleton.getVisualisation().getResolution();
            m_decX = 1420;
            m_decY = 1750;
            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();

            //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("bandeauY", new WEMenu("bandeauY", "bandeauY", new Vector2D(975, 1000), 0, 1));

        float textWidth = calcWidth("AQP Racing", "ar30whi");
        m_worldTextEntities.put("Title", new WEAlphaTextButton("Title", m_menuX + m_menuWidth / 2 - textWidth / 2, m_resolution.y - 50, "AQP Racing", "ar30whi"));
        m_worldTextEntities.put("Single", new WEAlphaTextButton("Single", m_menuX + 50, m_resolution.y - 150, "Singleplayer", "ar30whi"));
        m_worldTextEntities.put("Multi", new WEAlphaTextButton("Multi", m_menuX + 50, m_resolution.y - 200, "Multiplayer", "ar30whi"));
        m_worldTextEntities.put("HighScores", new WEAlphaTextButton("HighScores", m_menuX + 50, m_resolution.y - 250, "High Scores", "ar30whi"));
        m_worldTextEntities.put("Options", new WEAlphaTextButton("Options", m_menuX + 50, m_resolution.y - 300, "Options", "ar30whi"));
        m_worldTextEntities.put("Quit", new WEAlphaTextButton("Quit", m_menuX + 50, m_resolution.y - 350, "Quit", "ar30whi"));

        ((WEAlphaTextButton) m_worldTextEntities.get("Single")).m_setMInitialize(true);
        ((WEAlphaTextButton) m_worldTextEntities.get("Multi")).m_setMInitialize(true);
        ((WEAlphaTextButton) m_worldTextEntities.get("HighScores")).m_setMInitialize(true);
        ((WEAlphaTextButton) m_worldTextEntities.get("Options")).m_setMInitialize(true);
        ((WEAlphaTextButton) m_worldTextEntities.get("Quit")).m_setMInitialize(true);

        ((WEAlphaTextButton) m_worldTextEntities.get("Single")).setSelect(true);

        //Arrows
        m_worldEntities.put("arrow_single", new WEMenu("arrow_single", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 178), 0, 1));
        m_worldEntities.put("arrow_continue", new WEMenu("arrow_multi", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 228), 0, 2));
        m_worldEntities.put("arrow_highscores", new WEMenu("arrow_highscores", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 278), 0, 2));
        m_worldEntities.put("arrow_options", new WEMenu("arrow_options", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 328), 0, 2));
        m_worldEntities.put("arrow_quit", new WEMenu("arrow_quit", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 378), 0, 2));


    }

    /**
     * @see WorldModel#update()
     */
    @Override
    public void update() {
        if (m_initialized) {
            //Keyboard state
            HashMap keyboardState = Singleton.getInput().getKeyboardState();
            if (!(boolean) keyboardState.get(Control.ESCAPE)) {
                m_escapeReady = true;
            }
            if (!(boolean) keyboardState.get(Control.ENTER)) {
                m_enterReady = true;
            }


            ((WEAlphaTextButton) m_worldTextEntities.get("Single")).setSelect(m_position == 0 ? true : false);
            ((WEAlphaTextButton) m_worldTextEntities.get("Multi")).setSelect(m_position == 1 ? true : false);
            ((WEAlphaTextButton) m_worldTextEntities.get("HighScores")).setSelect(m_position == 2 ? true : false);
            ((WEAlphaTextButton) m_worldTextEntities.get("Options")).setSelect(m_position == 3 ? true : false);
            ((WEAlphaTextButton) m_worldTextEntities.get("Quit")).setSelect(m_position == 4 ? true : false);

            Singleton.getVisualisation().updateActorFrame("arrow_single", m_position == 0 ? 1 : 2);
            Singleton.getVisualisation().updateActorFrame("arrow_multi", m_position == 1 ? 1 : 2);
            Singleton.getVisualisation().updateActorFrame("arrow_highscores", m_position == 2 ? 1 : 2);
            Singleton.getVisualisation().updateActorFrame("arrow_options", m_position == 3 ? 1 : 2);
            Singleton.getVisualisation().updateActorFrame("arrow_quit", m_position == 4 ? 1 : 2);


            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 (control == Control.UP && m_lastInput + m_waitTime < Singleton.getWorldModel().getTime()) {
            m_position--;
            sc.playSound("ding", false);
            if (m_position < 0) {
                m_position = 4;
            }
            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 > 4) {
                m_position = 0;
            }
            m_lastInput = Singleton.getWorldModel().getTime();

        }
        if (control == Control.ESCAPE && m_escapeReady) {
            Singleton.getWorldModel().quit();
        }
        if (control == Control.ENTER && m_enterReady) {
            switch (m_position) {
                case 0:
                    Singleton.getOptionsController().setMode("MONO");
                    Singleton.getOptionsController().setRole("SERVER");
                    Singleton.getWorldModel().setNextState(new MapSelectionGameState());
                    break;
                case 1:
                    Singleton.getOptionsController().setMode("MULTI");
                    Singleton.getWorldModel().setNextState(new MultiplayersMenuGameState());
                    break;
                case 2:
                    Singleton.getWorldModel().setNextState(new HighscoresGameState());
                    break;
                case 3:
                    Singleton.getWorldModel().setNextState(new OptionsGameState());
                    break;
                case 4:
                    Singleton.getWorldModel().quit();
                    break;
            }
        }
    }

    /**
     * @see WorldModel#updateActorTransform()
     */
    @Override
    public void updateActorTransform() {

        //Entities
        for (WorldEntity e : m_worldEntities.values()) {
            e.updateActorTransform();
        }
    }

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

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

    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.");
    }
    /////////////////////////////////////////
    // Attributes
    /////////////////////////////////////////
    protected HashMap<String, WorldEntity> m_worldEntities;
    protected HashMap<String, WorldTextEntity> m_worldTextEntities;
    protected boolean m_initialized;
    protected int m_position;
    protected long m_lastInput;
    protected long m_waitTime;
    protected SoundController sc;
    protected float m_minX;
    protected float m_minY;
    protected float m_menuX;
    protected float m_menuWidth;
    protected int m_decX;
    protected int m_decY;
    protected boolean m_isUpdating;
    protected Vector2D m_resolution;
    protected boolean m_escapeReady;
    protected boolean m_enterReady;
}
TOP

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

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.