Package com.aqpproject.visualisation.gdx

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

/*
* 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.tools.Vector2D;
import com.aqpproject.visualisation.Visualisation.MAP_LAYER;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.tiled.*;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import java.io.File;
import java.util.ArrayList;

/**
* MapActorGDX class
*
* @author Alexandre, Clement, Fabrice
*/
public class MapActorGDX extends Actor {

    /**
     * Constructor
     *
     * @param path path to the map
     * @param stage stage
     */
    public MapActorGDX(String path, Stage stage) {
        super();

        m_stage = stage;
        m_path = path;

        FileHandle mapHandle = Gdx.files.internal(m_path);
        FileHandle baseDir = Gdx.files.internal(new File(m_path).getAbsolutePath());

        m_map = TiledLoader.createMap(mapHandle);
        m_atlas = new TileAtlas(m_map, mapHandle.parent());

        m_tileMapRenderer = new TileMapRenderer(m_map, m_atlas, 64, 64, 64, 64);

        m_layers = new int[]{0, 1, 2};

    }

    /**
     * @see Actor#draw(com.badlogic.gdx.graphics.g2d.SpriteBatch, float)
     */
    @Override
    public void draw(SpriteBatch sb, float f) {
        m_tileMapRenderer.render((OrthographicCamera) m_stage.getCamera(), m_layers);
    }

    /**
     * Draw the foreground of the map
     */
    public void drawForeGround(SpriteBatch sb) {
        m_tileMapRenderer.render((OrthographicCamera) m_stage.getCamera(), new int[]{4});
    }

    /**
     * @see Actor#touchDown(float, float, int)
     */
    @Override
    public boolean touchDown(float f, float f1, int i) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @see Actor#touchUp(float, float, int)
     */
    @Override
    public void touchUp(float f, float f1, int i) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @see Actor#touchDragged(float, float, int)
     */
    @Override
    public void touchDragged(float f, float f1, int i) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @see Actor#hit(float, float)
     */
    @Override
    public Actor hit(float f, float f1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getTile(MAP_LAYER layer, Vector2D position) {
        int posX = (int) (position.x) / 64;
        int posY = (int) (position.y) / 64;
        if (m_map != null && posX >= 0 && posY >= 0 && posX < m_map.width && posY < m_map.height) {
            TiledLayer l = m_map.layers.get(layer.getCode());
            return l.tiles[m_map.height - posY - 1][posX];
        }
        return 0;
    }

    public ArrayList<Vector2D> getTilePositions(MAP_LAYER layer, int code) {
        ArrayList<Vector2D> res = new ArrayList<>();
        for (int x = 0; x < m_map.width; x++) {
            for (int y = 0; y < m_map.height; y++) {
                TiledLayer l = m_map.layers.get(layer.getCode());
                int c = l.tiles[m_map.height - y - 1][x];
                if (c == code) {
                    res.add(new Vector2D(x * 64f, y * 64f));
                }
            }
        }
        return res;
    }

    public int getMapWidth() {
        return (m_map != null) ? m_map.width : 0;
    }

    public int getMapHeight() {
        return (m_map != null) ? m_map.height : 0;
    }
    /////////////////////////////////////////
    // Attributes
    /////////////////////////////////////////
    private String m_path;
    private TileMapRenderer m_tileMapRenderer;
    TiledMap m_map;
    TileAtlas m_atlas;
    Stage m_stage;
    int[] m_layers;
}
TOP

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

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.