Package com.aqpproject.worldmodel.game

Source Code of com.aqpproject.worldmodel.game.WorldModelGame

/*
* AQP Project
* http://http://code.google.com/p/aqp-project/
* Alexandre Gomez - Clément Troesch - Fabrice Latterner
*/
package com.aqpproject.worldmodel.game;

import com.aqpproject.game.Singleton;
import com.aqpproject.tools.Vector2D;
import com.aqpproject.worldmodel.Control;
import com.aqpproject.worldmodel.GameState;
import com.aqpproject.worldmodel.WorldModel;
import com.aqpproject.worldmodel.data.Map;
import com.aqpproject.worldmodel.game.entity.WECar;
import com.aqpproject.worldmodel.game.entity.WorldEntity;
import com.aqpproject.worldmodel.game.state.RaceGameState;
import com.aqpproject.worldmodel.game.state.TitleGameState;
import java.io.File;
import java.io.IOException;
import java.util.Date;
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 Alexandre, Clement, Fabrice
*/
public class WorldModelGame implements WorldModel {

    /**
     * Constructor
     */
    public WorldModelGame() {
        m_initialized = false;
        m_running = true;
    }

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

        if (!m_initialized) {   //If not initialized

            //Map
            m_maps = new HashMap<>();
            try {
                loadMaps("config/maps.xml");
                Singleton.getVisualisation().loadSprites("config/sprites.xml");
                Singleton.getVisualisation().loadParticleEffects("config/particles.xml");
            } catch (ParserConfigurationException | IOException | SAXException ex) {
                Logger.getLogger(WorldModelGame.class.getName()).log(Level.SEVERE, null, ex);
            }

            //State
            m_state = new TitleGameState();
            m_state.initialize();

            //Initialisation
            m_initialized = true;

            //New State
            m_nextState = null;

            m_startTime = new Date().getTime();

        }

    }

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

    /**
     * @see WorldModel#getTime()
     */
    @Override
    public long getTime() {
        if (m_initialized) {
            return new Date().getTime() - m_startTime;
        } else {
            return 0;
        }
    }

    /**
     * @see WorldModel#getLastUpdate()
     */
    @Override
    public long getLastUpdate() {
        return m_lastUpdate;
    }

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

        long time = getTime();
        m_deltaTime = time - m_lastUpdate;

        //Update
        m_state.update();

        m_lastUpdate = time;
    }

    /**
     * @see WorldModel#isRunning()
     */
    @Override
    public boolean isRunning() {
        return m_running;
    }

    /**
     * @see WorldModel#control(com.aqpproject.worldmodel.Control)
     */
    @Override
    public void control(Control control) {

        m_state.control(control);
    }

    /**
     * @see WorldModel#quit()
     */
    @Override
    public void quit() {
        m_running = false;
    }

    /**
     * @see WorldModel#getDeltaTime()
     */
    @Override
    public float getDeltaTime() {
        return m_deltaTime * 0.001f;
    }

    /**
     * @see WorldModel#getInterpolation()
     */
    @Override
    public float getInterpolation() {
        float i = (getTime() - m_lastUpdate) / (float) UPDATE_RATE;
        return i > 1 ? 1 : i;
    }

    /**
     * @see WorldModel#updateActorTransform()
     */
    @Override
    public void updateActorTransform() {
        m_state.updateActorTransform();
    }

    /**
     * @see WorldModel#saveEntitiesState()
     */
    @Override
    public void saveEntitiesState() {
        m_state.saveEntitiesState();
    }

    @Override
    public void setNextState(GameState newState) {
        m_nextState = newState;
    }

    @Override
    public boolean hasNextState() {
        return m_nextState != null;
    }

    @Override
    public void applyNewState() {
        m_state.destroy();
        m_state = m_nextState;
        m_state.initialize();
        m_nextState = null;
    }

    @Override
    public boolean canBeRendered() {
        return m_initialized && (m_nextState == null) && m_state.isInitialized();
    }

    @Override
    public void startContact(String firstEntity, String secondEntity, Vector2D normal, Vector2D point) {
        m_state.startContact(firstEntity, secondEntity, normal, point);
    }

    @Override
    public void stopContact(String firstEntity, String secondEntity, Vector2D normal, Vector2D point) {
        m_state.stopContact(firstEntity, secondEntity, normal, point);
    }

//    @Override
//    public void addEntity(String name, WorldEntity entity){
//        m_state.addEntity(name,entity);     
//    }
    @Override
    public GameState getState() {
        return m_state;
    }

    private void loadMaps(String path) throws ParserConfigurationException, IOException, SAXException {
        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 mapPath = e.getAttribute("path");
                String miniMap = e.getAttribute("mini");
                int nbLaps = Integer.parseInt(e.getAttribute("laps"));
                m_maps.put(name, new Map(name, mapPath, miniMap, nbLaps));
            }

        }
    }

    @Override
    public HashMap<String, Map> getMaps() {
        return m_maps;
    }

    @Override
    public Vector2D getEntityPosition(String entityName) {
        return new Vector2D(m_state.getWorldEntities().get(entityName).getPosition());
    }

    @Override
    public float getEntityRotation(String entityName) {
        return m_state.getWorldEntities().get(entityName).getRotation();
    }

    @Override
    public Vector2D getEntityVelocity(String entityName) {
        return Singleton.getPhysics().getLinearVelocity(entityName);
    }

    @Override
    public boolean getEntityHasItem(String entityName) {
        WorldEntity we = m_state.getWorldEntities().get(entityName);
        if (we instanceof WECar) {
            WECar car = (WECar) we;
            return car.getInventory().hasItem();
        }
        return false;
    }

    @Override
    public void resetEntity(String entityName, Vector2D position, float rotation) {
        WorldEntity we = m_state.getWorldEntities().get(entityName);
        if (we instanceof WECar) {
            Singleton.getPhysics().resetBody(entityName, position, new Vector2D(), rotation);

        }
    }

    @Override
    public boolean isRaceStarted() {
        if (!(m_state instanceof RaceGameState)) {
            return false;
        } else {
            RaceGameState rs = (RaceGameState) m_state;
            return rs.isRaceStarted();
        }
    }

    @Override
    public void control(Control control, String entity) {
        m_state.control(control, entity);
    }
    /////////////////////////////////////////
    // Attributes
    /////////////////////////////////////////
    protected long m_startTime;
    protected boolean m_running;
    protected boolean m_initialized;
    protected long m_lastUpdate;
    protected long m_oldTime;
    protected float m_deltaTime;
    protected GameState m_state;
    protected GameState m_nextState;
    protected HashMap m_maps;
}
TOP

Related Classes of com.aqpproject.worldmodel.game.WorldModelGame

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.