Java tutorial
/* * Inmisericordia * Copyright (C) 2014 Ruben Rosado <rrosadoalba@gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package eu.rubenrosado.inmisericordia.screens; import java.util.ArrayList; import java.util.Iterator; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture.TextureWrap; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.input.GestureDetector; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import eu.rubenrosado.inmisericordia.MainGame; import eu.rubenrosado.inmisericordia.LSGame; import eu.rubenrosado.inmisericordia.MonstersManager; import eu.rubenrosado.inmisericordia.StatusBar; import eu.rubenrosado.inmisericordia.actors.*; import eu.rubenrosado.inmisericordia.listeners.PlayListener; /** * This class is the screen on the user plays with the hero * * @author Ruben Rosado * */ public class PlayGame extends BaseScreen { private Texture bgTexture; public static TextureAtlas gameAtlas; public static Hero hero; private StatusBar statusBar; public static ArrayList<Monster> monsters; public static ArrayList<Hit> hits; public static Vector2 speed; public static Vector3 touch; public static float delta; private BitmapFont font; private final int tileCount = 100; private ShapeRenderer renderer; private TextureRegion dead; public static TextureRegion backbutton; private MonstersManager mg; private LSGame lsg; public PlayGame(MainGame maingame) { super(maingame); Gdx.input.setInputProcessor(new GestureDetector(new PlayListener())); touch = new Vector3(); speed = new Vector2(); font = new BitmapFont(); renderer = new ShapeRenderer(); statusBar = new StatusBar(); bgTexture = maingame.manager.get("images/grassfloor.png"); bgTexture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat); gameAtlas = maingame.manager.get("images/game.atlas"); dead = gameAtlas.findRegion("dead"); backbutton = gameAtlas.findRegion("back"); monsters = new ArrayList<Monster>(); mg = new MonstersManager(); lsg = new LSGame(); hits = new ArrayList<Hit>(); if (lsg.existsSavedGame()) { lsg.loadGame(); } else { hero = new Hero(MainMenu.playerName); mg.manage(); } } @Override public void render(float delta) { if (Gdx.input.isKeyPressed(Keys.BACK)) { if (hero.isAlive()) { lsg.saveGame(); } back(); } PlayGame.delta = delta; speed.x -= touch.x * (500 / width) * delta / 2; speed.y -= touch.y * (500 / height) * delta / 2; if (Math.abs(Math.round(speed.x)) >= bgTexture.getWidth()) { speed.x = 0; } if (Math.abs(Math.round(speed.y)) >= bgTexture.getWidth()) { speed.y = 0; } monsters = Monster.orderMonstersByY(monsters); maingame.batch.begin(); maingame.batch.draw(bgTexture, (-width * 2) + speed.x, (-height * 2) + speed.y, bgTexture.getWidth() * tileCount, bgTexture.getHeight() * tileCount, 0, tileCount, tileCount, 0); int i; for (i = 0; i < monsters.size(); i++) { if (monsters.get(i).getY() >= hero.getY()) { monsters.get(i).draw(maingame.batch, 0); } else { break; } } hero.draw(maingame.batch, 0); for (int j = i; j < monsters.size(); j++) { monsters.get(j).draw(maingame.batch, 0); } Iterator<Hit> it = hits.iterator(); while (it.hasNext()) { Hit h = (Hit) it.next(); if (h.getTime() > 1) { it.remove(); } else { h.draw(maingame.batch, 0); } } if (!hero.isAlive()) { percentW = width / MainGame.BASEW; percentH = height / MainGame.BASEH; maingame.batch.draw(dead, -83 * percentW, (height / 2) - 110 * percentH, 166 * percentW, 90 * percentH); maingame.batch.draw(backbutton, (-width / 2) + (5 * percentW), (-height / 2) + (5 * percentH), 54 * percentW, 54 * percentH); } font.draw(maingame.batch, String.valueOf(Gdx.graphics.getFramesPerSecond()), width / 2 - 17, height / 2); maingame.batch.end(); statusBar.draw(renderer); mg.manage(); lsg.autosave(); } /** * Method called when the user touch the back button */ public static void back() { maingame.setScreen(new MainMenu(maingame)); } }