com.redthirddivision.astilade.world.World.java Source code

Java tutorial

Introduction

Here is the source code for com.redthirddivision.astilade.world.World.java

Source

/*   Copyright 2014 Matthew Rogers "BossLetsPlays"
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package com.redthirddivision.astilade.world;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.redthirddivision.astilade.Astilade;
import com.redthirddivision.astilade.ai.Mover;
import com.redthirddivision.astilade.ai.TileBasedMap;
import com.redthirddivision.astilade.entities.Entity;
import com.redthirddivision.astilade.entities.mobs.Player;
import com.redthirddivision.astilade.utils.LogHelper;

/**
 * <strong>Project:</strong> Kingdom of Astilade-core <br>
 * <strong>File:</strong> World.java
 *
 * @author <a href = "http://redthirddivision.com/team/BossLetsPlays"> Matthew Rogers</a>
 */
public class World implements TileBasedMap {

    private ArrayList<Entity> entities;
    private ArrayList<Rectangle> bounds;
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private TiledMap map;
    private OrthogonalTiledMapRenderer mapRenderer;
    private int width, height, size;
    private boolean[][] visited;
    private boolean[][] solids;

    public World(int mapSize, int tileSize) {
        this.width = height = mapSize;
        this.size = width * height;
        this.entities = new ArrayList<Entity>();
        this.bounds = new ArrayList<Rectangle>();
        this.batch = new SpriteBatch();
        this.camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        map = new TmxMapLoader().load("textures/temp.tmx");
        mapRenderer = new OrthogonalTiledMapRenderer(map);
        visited = new boolean[width][height];
        solids = new boolean[width][height];

        for (int i = 0; i < mapSize; i++) {
            for (int j = 0; j < mapSize; j++) {
                TiledMapTileLayer cur = (TiledMapTileLayer) map.getLayers().get(1);
                Cell cell = new Cell();
                if (cur.getCell(i, j) != null) {
                    cell = cur.getCell(i, j);
                    solids[i][j] = true;
                    bounds.add(new Rectangle(i * tileSize, j * tileSize, tileSize, tileSize));
                } else
                    solids[i][j] = false;
            }
        }
    }

    public int getWidthInTiles() {
        return width;
    }

    public int getHeightInTiles() {
        return height;
    }

    public int getAreaInTiles() {
        return size;
    }

    public Player loadPlayer() {
        LogHelper.info("Loading player data");
        Preferences prefs = Gdx.app.getPreferences(Astilade.TITLE);
        Player player = new Player(this, new Vector2(prefs.getFloat("playerX"), prefs.getFloat("playerY")));

        LogHelper.info("Player data loaded");
        return player;
    }

    public void update() {
        for (Rectangle r : bounds) {
            if (getPlayer().hasCollisionWith(r))
                getPlayer().readjust();
        }
        for (Entity e : getEntities())
            e.update();
        camera.position.x = getPlayer().getPosition().x + (getPlayer().getCurrentFrame().getRegionWidth() / 2);
        camera.position.y = getPlayer().getPosition().y + (getPlayer().getCurrentFrame().getRegionHeight() / 2);
        camera.update();
    }

    public void render() {

        mapRenderer.setView(camera);
        mapRenderer.render();

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        for (Entity e : getEntities())
            e.render(batch);
        batch.end();

        renderDebug();
    }

    public void renderDebug() {
        for (Entity e : getEntities())
            e.renderDebug();
    }

    public synchronized ArrayList<Entity> getEntities() {
        return entities;
    }

    public void addEntity(Entity e) {
        entities.add(e);
    }

    public Player getPlayer() {
        for (Entity e : getEntities()) {
            if (e instanceof Player)
                return (Player) e;
        }
        return null;
    }

    public void dispose() {
        for (Entity e : getEntities())
            e.dispose();
        batch.dispose();
    }

    public OrthographicCamera getCamera() {
        return camera;
    }

    /**
     * Clear the array marking which tiles have been visited by the path 
     * finder.
     */
    public void clearVisited() {
        for (int x = 0; x < getWidthInTiles(); x++) {
            for (int y = 0; y < getHeightInTiles(); y++) {
                visited[x][y] = false;
            }
        }
    }

    public boolean hasTileBeenVisited(int x, int y) {
        return visited[x][y];
    }

    @Override
    public void pathFinderVisited(int x, int y) {
        visited[x][y] = true;
    }

    @Override
    public boolean isBlocked(Mover mover, int x, int y) {
        return solids[x][y];
    }

    @Override
    public float getCost(Mover mover, int sx, int sy, int tx, int ty) {
        return 1;
    }
}