Package com.aqpproject.visualisation.gdx

Source Code of com.aqpproject.visualisation.gdx.GameGDX

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

import com.aqpproject.game.Singleton;
import com.aqpproject.tools.Vector2D;
import com.aqpproject.visualisation.Visualisation.MAP_LAYER;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;

/**
* GameGDX class
*
* @author Alexandre, Clement, Fabrice
*/
public class GameGDX implements ApplicationListener {

    /**
     * Constructor
     *
     * @param fullscreen true if fullscreen mode
     */
    public GameGDX(boolean fullscreen) {
        m_fullscreen = fullscreen;
    }

    /**
     * @see ApplicationListener#create()
     */
    @Override
    public void create() {
        m_spriteBatch = new SpriteBatch();

        m_stage = new Stage(0, 0, true);

        m_map = null;

        m_font = new BitmapFont();

        m_entityGroup = new Group("EntityGroup");
        m_menuGroup = new Group("MenuGroup");

        m_stage.addActor(m_entityGroup);
        //m_stage.addActor(m_menuGroup);

        //Keyboard
        try {
            Keyboard.create();
        } catch (LWJGLException ex) {
            Logger.getLogger(VisualisationGDX.class.getName()).log(Level.SEVERE, null, ex);
        }

        //Fullscreen
        if (m_fullscreen) {
            Graphics.DisplayMode[] modes = Gdx.graphics.getDisplayModes();
            Gdx.graphics.setDisplayMode(modes[modes.length - 1]);
        }

        //Notify
        Singleton.getVisualisation().notifyInitializationListener();


    }

    /**
     * @see ApplicationListener#resize(int, int)
     */
    @Override
    public void resize(int width, int height) {
        m_stage.setViewport(width, height, true);
    }

    /**
     * @see ApplicationListener#render()
     */
    @Override
    public void render() {
        if (Singleton.getWorldModel().canBeRendered()) {
            Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


            //Stage
            Singleton.getWorldModel().updateActorTransform();
            m_stage.act(Gdx.graphics.getDeltaTime());
            m_stage.draw();

            //MAP
            if (m_map != null) {
                //Foreground
                m_map.drawForeGround(m_spriteBatch);
            }

            m_spriteBatch.begin();
            m_menuGroup.draw(m_spriteBatch, 1.f);
            m_spriteBatch.end();


            //DEBUG PHYSIC
//            ShapeRenderer shapeRenderer = new ShapeRenderer();
//            shapeRenderer.setProjectionMatrix(m_stage.getCamera().combined);
//
//            if (Singleton.getPhysics().getDebugSegments() != null) {
//                for (Segment2D seg : Singleton.getPhysics().getDebugSegments()) {
//                    shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
//                    shapeRenderer.setColor(1, 0, 0, 1);
//                    shapeRenderer.line(seg.getFirst().x, seg.getFirst().y, seg.getSecond().x, seg.getSecond().y);
//                    shapeRenderer.end();
//                }
//            }







            //Fps
            m_spriteBatch.begin();
            int fps = (int) (1f / Gdx.graphics.getDeltaTime());
            m_font.draw(m_spriteBatch, "fps: " + fps, 2, 29);
            int ups = (int) (1f / Singleton.getWorldModel().getDeltaTime());
            m_font.draw(m_spriteBatch, "ups: " + ups, 2, 17);
            m_spriteBatch.end();






        }
        if (Singleton.getWorldModel().hasNextState()) {
            Singleton.getWorldModel().applyNewState();
        }

    }

    /**
     * @see ApplicationListener#pause()
     */
    @Override
    public void pause() {
    }

    /**
     * @see ApplicationListener#resume()
     */
    @Override
    public void resume() {
    }

    /**
     * @see ApplicationListener#dispose()
     */
    @Override
    public void dispose() {
        Keyboard.destroy();
        m_stage.dispose();
    }

    /**
     * Add an actor to the scene
     *
     * @param actor
     */
    public void addActor(Actor actor, boolean menu) {
        if (!menu) {
            m_entityGroup.addActor(actor);
        } else {
            m_menuGroup.addActor(actor);
        }
    }

    /**
     * Remove an actor from the scene
     *
     * @param actor
     */
    public void removeActor(Actor actor, boolean menu) {
        if (!menu) {
            m_entityGroup.removeActor(actor);
        } else {
            m_menuGroup.removeActor(actor);
        }
    }

    /**
     * Set the map
     *
     * @param actor
     */
    public void setMap(MapActorGDX actor) {
        removeMap();
        m_map = actor;
        m_stage.getRoot().addActorAt(0, m_map);
    }

    /**
     * Remove the map
     */
    public void removeMap() {
        if (m_map != null) {
            m_stage.getRoot().removeActor(m_map);
            m_map = null;
        }
    }

    /**
     * Set the position of the camera
     *
     * @param posX X coordinate
     * @param posY Y coordinate
     */
    public void setCameraPosition(float posX, float posY) {
        m_stage.getCamera().position.x = posX;
        m_stage.getCamera().position.y = posY;
    }

    public void setZoom(float zoom) {
        OrthographicCamera o = (OrthographicCamera) m_stage.getCamera();
        o.zoom = zoom;
    }

    /**
     * @return the stage of the game
     */
    public Stage getStage() {
        return m_stage;
    }

    public int getTile(MAP_LAYER layer, Vector2D position) {
        return m_map.getTile(layer, position);
    }

    public ArrayList<Vector2D> getTilePositions(MAP_LAYER layer, int code) {
        return m_map.getTilePositions(layer, code);
    }

    public int getMapWidth() {
        return m_map.getMapWidth();
    }

    public int getMapHeight() {
        return m_map.getMapHeight();
    }
    /////////////////////////////////////////
    // Attributes
    /////////////////////////////////////////
    private SpriteBatch m_spriteBatch;
    private Stage m_stage;
    private BitmapFont m_font;
    private Group m_entityGroup;
    private Group m_menuGroup;
    private boolean m_fullscreen;
    private MapActorGDX m_map;
}
TOP

Related Classes of com.aqpproject.visualisation.gdx.GameGDX

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.