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.game; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.VertexAttributes; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g3d.Material; import com.badlogic.gdx.graphics.g3d.Model; import com.badlogic.gdx.graphics.g3d.ModelInstance; import com.badlogic.gdx.graphics.g3d.RenderableProvider; import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute; import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; import com.badlogic.gdx.graphics.g3d.decals.Decal; import com.badlogic.gdx.graphics.g3d.utils.CameraInputController; import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder; import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.utils.Array; import com.github.fauu.helix.core.*; public class Game implements ApplicationListener { private Camera camera; private MapRegion mapRegion; private AssetManager assets; private Renderer renderer; private CameraInputController cameraInputController; private SpriteBatch spriteBatch; private Player player; private Model waterModel; private ModelInstance waterModelInstance; private Texture waterTexture; private Model entityModel; public Game() { } @Override public void create() { camera = new Camera(new Vector3(32, 32, 0)); cameraInputController = new CameraInputController(camera); // Gdx.input.setInputProcessor(cameraInputController); assets = new AssetManager(); assets.setLoader(GeometrySet.class, new GeometrySetLoader(new InternalFileHandleResolver())); assets.setLoader(MapRegion.class, new MapRegionLoader(new InternalFileHandleResolver())); assets.load("assets/mapregions/0.hmr", MapRegion.class); assets.finishLoading(); mapRegion = assets.get("assets/mapregions/0.hmr", MapRegion.class); spriteBatch = new SpriteBatch(); renderer = new Renderer(); player = new Player(new Vector2(8, 17), new Texture(Gdx.files.internal("assets/sprites/player.png"))); camera.move(player.getRealPosition()); final ModelBuilder modelBuilder = new ModelBuilder(); waterTexture = new Texture("assets/textures/water.png"); final MeshPartBuilder.VertexInfo corner00 = new MeshPartBuilder.VertexInfo(); corner00.hasPosition = true; corner00.hasNormal = true; corner00.hasUV = true; corner00.setPos(0, 0, 0); corner00.setNor(0, 1, 0); corner00.setUV(0, 0); final MeshPartBuilder.VertexInfo corner10 = new MeshPartBuilder.VertexInfo(); corner10.hasPosition = true; corner10.hasNormal = true; corner10.hasUV = true; corner10.setPos(4, 0, 0); corner10.setNor(0, 1, 0); corner10.setUV(0, 1); final MeshPartBuilder.VertexInfo corner11 = new MeshPartBuilder.VertexInfo(); corner11.hasPosition = true; corner11.hasNormal = true; corner11.hasUV = true; corner11.setPos(4, 0, 14); corner11.setNor(0, 1, 0); corner11.setUV(1, 1); final MeshPartBuilder.VertexInfo corner01 = new MeshPartBuilder.VertexInfo(); corner01.hasPosition = true; corner01.hasNormal = true; corner01.hasUV = true; corner01.setPos(0, 0, 14); corner01.setNor(0, 1, 0); corner01.setUV(1, 0); modelBuilder.begin(); modelBuilder.node(); MeshPartBuilder meshPartBuilder = modelBuilder.part("water1", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.TextureCoordinates, new Material(TextureAttribute.createDiffuse(waterTexture), ColorAttribute.createDiffuse(Color.WHITE), new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA))); meshPartBuilder.rect(corner00, corner10, corner11, corner01); waterModel = modelBuilder.end(); waterModelInstance = new ModelInstance(waterModel); WaterData waterData = new WaterData(); waterData.waveAmplitude = 0.02f; waterData.waveAngle = 0; waterData.waveFrequency = 0.5f; waterModelInstance.userData = waterData; // waterModelInstance.userData = new float[2]; // ((float[]) waterModelInstance.userData)[0] = 0; // waveAngle // ((float[]) waterModelInstance.userData)[1] = 0.1f; // waveAmplitude waterModelInstance.transform.translate(8, -0.1f, 19).rotate(0, 1, 0, -90).scale(1, 1, -1); // ((float[]) waterModelInstance.userData)[2] = 8; // startX // ((float[]) waterModelInstance.userData)[3] = 7; // sizeX // ((float[]) waterModelInstance.userData)[4] = 19; // startZ // ((float[]) waterModelInstance.userData)[5] = 5; // sizeZ } @Override public void render() { // cameraInputController.update(); final float delta = Gdx.graphics.getDeltaTime(); handleInput(delta); updateLogic(delta); final WaterData waterData = (WaterData) waterModelInstance.userData; waterData.waveAngle += waterData.waveFrequency * 2 * Gdx.graphics.getDeltaTime(); while (waterData.waveAngle > 2 * Math.PI) { waterData.waveAngle -= 2 * Math.PI; } if (mapRegion.getMesh().isReady()) { final Array<RenderableProvider> renderableProviders = new Array<RenderableProvider>(); renderableProviders.add(mapRegion); renderableProviders.addAll(mapRegion.getObjectsModelInstances()); renderableProviders.add(waterModelInstance); Array<Decal> decals = new Array<Decal>(); decals.add(player.getDecal()); renderer.render(camera, renderableProviders, decals); } // final Vector3 playerScreenPosition = camera.project(player.getRealPosition().cpy()); // spriteBatch.begin(); // player.getSheet().bind(); // spriteBatch.draw(player.getCurrentFrame(), playerScreenPosition.x, playerScreenPosition.y, 92, 92); // spriteBatch.end(); } public void handleInput(float delta) { Direction movementKeyPressedDirection = null; if (Gdx.input.isKeyPressed(Input.Keys.W)) { movementKeyPressedDirection = Direction.NORTH; } else if (Gdx.input.isKeyPressed(Input.Keys.S)) { movementKeyPressedDirection = Direction.SOUTH; } else if (Gdx.input.isKeyPressed(Input.Keys.A)) { movementKeyPressedDirection = Direction.WEST; } else if (Gdx.input.isKeyPressed(Input.Keys.D)) { movementKeyPressedDirection = Direction.EAST; } if (movementKeyPressedDirection != null) { final Vector2 playerPosition = player.getPosition().cpy(); Tile targetTile = null; switch (movementKeyPressedDirection) { case NORTH: targetTile = mapRegion.getTile(playerPosition.add(0, -1)); break; case SOUTH: targetTile = mapRegion.getTile(playerPosition.add(0, 1)); break; case WEST: targetTile = mapRegion.getTile(playerPosition.add(-1, 0)); break; case EAST: targetTile = mapRegion.getTile(playerPosition.add(1, 0)); break; } if (targetTile != null && targetTile.getType() != Tile.Type.DEFAULT_NONWALKABLE) { player.handleMovementInput(delta, movementKeyPressedDirection); } else if (!player.isMoving()) { player.setFacing(movementKeyPressedDirection); } } } public void updateLogic(float delta) { final Vector3 playerTranslation = player.processMovement(delta); camera.move(playerTranslation); } @Override public void dispose() { // FIXME: There are things to dispose here! } @Override public void resize(int width, int height) { spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, width, height); } @Override public void pause() { } @Override public void resume() { } public AssetManager getAssets() { return assets; } public MapRegion getMapRegion() { return mapRegion; } }