/**
* TimeHero - http://timehero.sf.net
*
* @license See LICENSE
*/
package it.timehero.slick.states;
import it.timehero.entities.Entity;
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;
/**
* Stato di gioco
*
* @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;
private UserInterface ui;
private UserInputController userInputController;
/** The width of the display in tiles */
private int widthInTiles;
private WorldEngine world;
/**
* Ritorna ID dello stato
*/
public int getID() {
return ID;
}
/**
* Init del gioco, caricamento
*/
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
Log.info("Init Slick");
// inizializzo modello mondo
world = new WorldEngine();
// crep l'interfaccia utente
ui = new UserInterface();
// inizializzazione gestione input utente
userInputController = new UserInputController(world);
// -----------------------------------------------------------------------------------
// Init Finestra
// -----------------------------------------------------------------------------------
// 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);
}
/**
* Disegna interfaccia utente, attori e mappa relativamente alla posizione del pg
*/
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));
// elimino gli attori con zero o meno pf
world.removeDeadActors();
// disegno entit�
for (int i = 0; i < world.getActorSize(); i++) {
Entity ent = world.getActor(i).getEntity();
ent.draw(g);
}
g.resetTransform();
// disegno interfaccia utente
ui.render(g, world.getPlayerActor().getScheda().getPf().getValore());
}
/**
* Aggiorna la logica di gioco
*/
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
// vado a gestire l'input del giocatore
userInputController.getInput(container, game, delta);
}
}