Java tutorial
/* * Copyright (C) 2014 Helix Engine Developers (http://github.com/fauu/HelixEngine) * * This software is licensed under the GNU General Public License * (version 3 or later). See the COPYING file in this distribution. * * You should have received a copy of the GNU Library General Public License * along with this software. If not, see <http://www.gnu.org/licenses/>. * * Authored by: Piotr Grabowski <fau999@gmail.com> */ package com.github.fauu.helix.core; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g3d.Material; import com.badlogic.gdx.graphics.g3d.ModelInstance; import com.badlogic.gdx.graphics.g3d.Renderable; import com.badlogic.gdx.graphics.g3d.RenderableProvider; import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Pool; public class MapRegion implements RenderableProvider { private Vector2 size; private Tile[] tiles; private MapRegionMesh mesh; private TextureAtlas textureAtlas; private GeometrySet geometrySet; private Array<Object> objects; private Renderable renderable; private boolean objectsHidden; public MapRegion(Vector2 size, Tile[] tiles, Array<Object> objects, TextureAtlas textureAtlas, GeometrySet geometrySet) { create(size, tiles, objects, textureAtlas, geometrySet); } public void create(Vector2 size, Tile[] tiles, Array<Object> objects, TextureAtlas textureAtlas, GeometrySet geometrySet) { this.size = size; this.textureAtlas = textureAtlas; this.geometrySet = geometrySet; this.objects = objects; if (tiles != null) { this.tiles = tiles; } else { final int tilesLength = (int) (size.x * size.y); tiles = new Tile[tilesLength]; for (int i = 0; i < tilesLength; i++) { final Tile.Builder tileBuilder = new Tile.Builder(); final int tileX = i % (int) size.x; final int tileY = (int) Math.floor(i / (tilesLength / size.y)); tiles[i] = tileBuilder.setNo(i).setPosition(new Vector2(tileX, tileY)).setElevation(0) .setTextureId(0).setGeometryId(0).setFacing(Direction.SOUTH).build(); } } this.tiles = tiles; mesh = new MapRegionMesh(tiles, geometrySet, textureAtlas); renderable = new Renderable(); renderable.mesh = mesh; renderable.material = new Material(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE), new TextureAttribute(TextureAttribute.Diffuse, textureAtlas.getTextures().first())); renderable.meshPartOffset = 0; renderable.meshPartSize = mesh.getNumVertices(); renderable.primitiveType = GL20.GL_TRIANGLES; renderable.worldTransform.idt(); } private void setObjectsVisibility(boolean visible) { for (Object object : objects) { object.setVisible(visible); } } public void showObjects() { setObjectsVisibility(true); objectsHidden = false; } public void hideObjects() { setObjectsVisibility(false); objectsHidden = true; } public Vector2 getSize() { return size; } public Array<Object> getObjects() { return objects; } public Tile[] getTiles() { return tiles; } public Tile getTile(Vector2 position) { if (position.x >= 0 && position.x < size.x && position.y >= 0 && position.y < size.y) { final int tileIndex = (int) (position.x + position.y * size.x); return tiles[tileIndex]; } else { return null; } } public MapRegionMesh getMesh() { return mesh; } public TextureAtlas getTextureAtlas() { return textureAtlas; } public GeometrySet getGeometrySet() { return geometrySet; } public Vector2 getCenterCoordinates() { final float centerX = (size.x / 2); final float centerY = (size.y / 2); return new Vector2(centerX, centerY); } public Array<ModelInstance> getObjectsModelInstances() { final Array<ModelInstance> modelInstances = new Array<ModelInstance>(); for (Object object : objects) { if (object.isVisible()) { modelInstances.add(object.getModelInstance()); } } return modelInstances; } public boolean areObjectsHidden() { return objectsHidden; } public void deleteObject(Object object) { objects.removeValue(object, true); } public void setObjectColor(Object object, Color color) { object.getModelInstance().materials.first().set(new ColorAttribute(ColorAttribute.createDiffuse(color))); } @Override public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool) { renderables.add(renderable); } }