Package com.aqpproject.worldmodel.game.state

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

/*
* 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.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, Fabrice
*/
public class MultiplayersMenuGameState implements GameState {

    /**
     * Constructor
     */
    public MultiplayersMenuGameState() {
        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) {
            m_worldEntities = new HashMap<>();
            m_worldTextEntities = new HashMap<>();

            try {
                sc = Singleton.getAudioController();
                Singleton.getVisualisation().loadSprites("config/mainmenu.xml");
            } catch (ParserConfigurationException | SAXException | IOException ex) {
                Logger.getLogger(MultiplayersMenuGameState.class.getName()).log(Level.SEVERE, null, ex);
            }
            m_position = 0;
            m_lastInput = 0;
            m_waitTime = 125;
            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_dec_x = 1420;
            m_dec_y = 1750;
            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));

        //Texts creation
        float title_width = calcWidth("Multiplayer", "ar30whi");
        m_worldTextEntities.put("Title", new WEAlphaTextButton("Title", m_menuX + m_menuWidth / 2 - title_width / 2, m_resolution.y - 50, "Multiplayer", "ar30whi"));
        m_worldTextEntities.put("Create", new WEAlphaTextButton("Create", m_menuX + 50, m_resolution.y - 150, "Create", "ar30whi"));
        m_worldTextEntities.put("Join", new WEAlphaTextButton("Join", m_menuX + 50, m_resolution.y - 200, "Join", "ar30whi"));
        m_worldTextEntities.put("Cancel", new WEAlphaTextButton("Cancel", m_menuX + 50, m_resolution.y - 300, "Cancel", "ar30whi"));

        //Texts initialization
        ((WEAlphaTextButton) m_worldTextEntities.get("Create")).m_setMInitialize(true);
        ((WEAlphaTextButton) m_worldTextEntities.get("Create")).setSelect(true);
        ((WEAlphaTextButton) m_worldTextEntities.get("Join")).m_setMInitialize(true);
        ((WEAlphaTextButton) m_worldTextEntities.get("Cancel")).m_setMInitialize(true);

        //Arrows
        m_worldEntities.put("arrow_create", new WEMenu("arrow_create", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 178), 0, 1));
        m_worldEntities.put("arrow_join", new WEMenu("arrow_join", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 228), 0, 2));
        m_worldEntities.put("arrow_cancel", new WEMenu("arrow_cancel", "arrow", new Vector2D(m_minX + m_menuX + 10, m_resolution.y + m_minY - 328), 0, 2));
    }

    /**
     * @see WorldModel#update()
     */
    @Override
    public void update() {
        if (m_initialized) {
            long time = Singleton.getWorldModel().getTime();

            //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("Create")).setSelect(m_position == 0 ? true : false);
            ((WEAlphaTextButton) m_worldTextEntities.get("Join")).setSelect(m_position == 1 ? true : false);
            ((WEAlphaTextButton) m_worldTextEntities.get("Cancel")).setSelect(m_position == 2 ? true : false);

            Singleton.getVisualisation().updateActorFrame("arrow_create", m_position == 0 ? 1 : 2);
            Singleton.getVisualisation().updateActorFrame("arrow_join", m_position == 1 ? 1 : 2);
            Singleton.getVisualisation().updateActorFrame("arrow_cancel", m_position == 2 ? 1 : 2);

            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 = 2;
            }
            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 > 2) {
                m_position = 0;
            }
            m_lastInput = Singleton.getWorldModel().getTime();

        }
        if (control == Control.ESCAPE && m_escapeReady) {
            //Appel menu principal
            Singleton.getWorldModel().setNextState(new MainMenuGameState());
        }
        if (control == Control.ENTER && m_enterReady) {
            switch (m_position) {
                case 0:
                    Singleton.getOptionsController().setRole("SERVER");
                    Singleton.getWorldModel().setNextState(new HostMenuGameState());
                    break;
                case 1:
                    Singleton.getOptionsController().setRole("CLIENT");
                    Singleton.getWorldModel().setNextState(new JoinMenuGameState());
                    break;
                case 2:
                    Singleton.getWorldModel().setNextState(new MainMenuGameState());
                    break;
            }
        }
    }

    @Override
    public void control(Control control, String entity) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

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

    /**
     * @see WorldModel#updateActorTransform()
     */
    @Override
    public void updateActorTransform() {
        //Camera
        //Singleton.getVisualisation().setCameraPosition(0, 0);

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

    @Override
    public HashMap<String, WorldEntity> getWorldEntities() {
        return m_worldEntities;
    }
    /////////////////////////////////////////
    // 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 Vector2D m_resolution;
    protected float m_minX;
    protected float m_minY;
    protected float m_menuX;
    protected float m_menuWidth;
    protected int m_dec_y;
    protected int m_dec_x;
    protected boolean m_escapeReady;
    protected boolean m_enterReady;
}
TOP

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

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.