/**
* TimeHero - http://timehero.sf.net
*
* @license See LICENSE
*/
package it.timehero.slick.states;
import it.timehero.entities.Entity;
import it.timehero.object.OggettoGenerico;
import it.timehero.slick.ui.UserInterface;
import it.timehero.util.UserInputController;
import it.timehero.world.WorldEngine;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.Log;
/**
* State of game
*
* @author AM
*/
public class GameState extends BasicGameState {
/** The ID given to this state */
public static final int ID = 3;
/** The height of the display in tiles */
private int heightInTiles;
/** The offset from the centre of the screen to the left edge in tiles */
private int leftOffsetInTiles;
/** The offset from the centre of the screen to the top edge in tiles */
private int topOffsetInTiles;
/** User Interface **/
private UserInterface ui;
/** User Controller Handler **/
private UserInputController userInputController;
/** The width of the display in tiles */
private int widthInTiles;
/** World of Game **/
private WorldEngine world;
/**
* Ritorna ID dello stato
*/
public int getID() {
return ID;
}
/**
* Game Init and loading resources
*/
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
Log.info("Init Slick");
try {
// creo l'interfaccia utente
ui = new UserInterface(container);
// inizializzo modello mondo
world = new WorldEngine(ui);
// inizializzazione gestione input utente
userInputController = new UserInputController(world);
// Init Window
// Calculate some layout values for rendering the tilemap. How many
// tiles do we need to render to fill the screen in each dimension and how far
// is it from the centre of the screen
widthInTiles = container.getWidth() / world.getTileSize();
heightInTiles = container.getHeight() / world.getTileSize();
topOffsetInTiles = heightInTiles / 2;
leftOffsetInTiles = widthInTiles / 2;
Log.info("Window Dimensions in Tiles: " + widthInTiles + "x" + heightInTiles);
} catch (Exception e){
e.printStackTrace();
}
}
/**
* Render everything on screen
*/
public void render(GameContainer game, StateBasedGame state, Graphics g)
throws SlickException {
Entity gigi = world.getPlayerActor().getEntity();
// draw the appropriate section of the tilemap based on the centre
// (hence the -(TANK_SIZE/2)) of the player
int playerTileX = (int) gigi.getX();
int playerTileY = (int) gigi.getY();
// caculate the offset of the player from the edge of the tile. As the
// player moves around this varies and this tells us how far to offset the tile based rendering
// to give the smooth motion of scrolling
int playerTileOffsetX = (int) ((playerTileX - gigi.getX()) * world
.getTileSize());
int playerTileOffsetY = (int) ((playerTileY - gigi.getY()) * world
.getTileSize());
// render the section of the map that should be visible. Notice the -1
// and +3 which renders a little extra map around the edge of the screen to cope with tiles
// scrolling on and off the screen
world.render(playerTileOffsetX - (gigi.ENTITY_SPRITE_SIZE / 2),
playerTileOffsetY - (gigi.ENTITY_SPRITE_SIZE / 2), playerTileX
- leftOffsetInTiles - 1, playerTileY - topOffsetInTiles
- 1, widthInTiles + 3, heightInTiles + 3);
// draw entities relative to the player that must appear in the centre
// of the screen
g.translate(400 - (int) (gigi.getX() * 32), 300 - (int) (gigi.getY() * 32));
// draw objects
for (int i = 0; i < world.getOggettiSize(); i++) {
OggettoGenerico og = ((OggettoGenerico)world.getOggetto(i));
og.draw(g);
}
// remove actors with hit points < 0
world.removeDeadActors();
// draw entities
for (int i = 0; i < world.getActorSize(); i++) {
Entity ent = world.getActor(i).getEntity();
ent.draw(g);
}
g.resetTransform();
// draw user interface
ui.render(g, world.getPlayerActor().getScheda().getPf().getValore());
}
/**
* Update game logic
*/
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
// handling player input
userInputController.getInput(container, game, delta);
}
}