130 lines
3.2 KiB
Java
130 lines
3.2 KiB
Java
package tfg;
|
|
|
|
import org.jsfml.graphics.Color;
|
|
import org.jsfml.graphics.Font;
|
|
import org.jsfml.graphics.RenderWindow;
|
|
import org.jsfml.graphics.TextStyle;
|
|
import org.jsfml.system.Clock;
|
|
import org.jsfml.system.Vector2i;
|
|
import org.jsfml.window.Keyboard;
|
|
import org.jsfml.window.Keyboard.Key;
|
|
import org.jsfml.window.VideoMode;
|
|
import org.jsfml.window.event.Event;
|
|
|
|
/**
|
|
* TFG Game's main class.
|
|
* @author Ritchie Cunningham
|
|
*/
|
|
public class Game {
|
|
private RenderWindow renderWindow = new RenderWindow();
|
|
private final String renderWindowTitle = "TFG Game";
|
|
private final Vector2i renderWindowDimensions = new Vector2i(640, 480);
|
|
private Player player;
|
|
private Camera camera;
|
|
private boolean renderWindowFocused = true;
|
|
private Font pixel = new Font();
|
|
private int fps;
|
|
private static boolean limitFPS = false;
|
|
private UITextElement fpsCounter;
|
|
|
|
public static void main(String[] args) {
|
|
Game g = new Game(); /* Create temp object of self. */
|
|
g.run(); /* Invoke run. */
|
|
}
|
|
|
|
public void handleInitialization() {
|
|
renderWindow.create(new VideoMode(renderWindowDimensions.x,
|
|
renderWindowDimensions.y), renderWindowTitle);
|
|
|
|
fpsCounter = new UITextElement(InterfacePosition.TOP_LEFT,
|
|
Color.YELLOW,24,TextStyle.BOLD);
|
|
|
|
if(limitFPS) {
|
|
renderWindow.setFramerateLimit(60);
|
|
}
|
|
|
|
player = new Player();
|
|
|
|
player.changeMap(new Map(10, 10, Tile.SAND));
|
|
camera = new Camera(renderWindow);
|
|
}
|
|
|
|
/**
|
|
* Run the game.
|
|
*/
|
|
public void run() {
|
|
handleInitialization();
|
|
int framesDrawn = 0;
|
|
float updateRate = 20.0f; /* 20hz */
|
|
long maxUpdates = 1;
|
|
Clock updateClock = new Clock();
|
|
Clock frameClock = new Clock();
|
|
updateClock.restart();
|
|
long nextUpdate = updateClock.getElapsedTime().asMilliseconds();
|
|
|
|
while(renderWindow.isOpen()) {
|
|
long updates = 0;
|
|
handleInput();
|
|
long updateTime = updateClock.getElapsedTime().asMicroseconds();
|
|
while ((updateTime-nextUpdate) >= updateRate && updates++ < maxUpdates) {
|
|
handleLogic();
|
|
nextUpdate += updateRate;
|
|
}
|
|
handleDrawing();
|
|
framesDrawn++;
|
|
|
|
float elapsedTime = frameClock.getElapsedTime().asSeconds();
|
|
if(elapsedTime >= 1.0f) {
|
|
fps = (int)(framesDrawn/elapsedTime);
|
|
fpsCounter.updateString("FPS: " + fps);
|
|
framesDrawn = 0;
|
|
frameClock.restart();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void handleInput() {
|
|
for(Event event : renderWindow.pollEvents()) {
|
|
switch(event.type) {
|
|
case CLOSED:
|
|
renderWindow.close();
|
|
break;
|
|
case GAINED_FOCUS:
|
|
renderWindowFocused = true;
|
|
break;
|
|
case LOST_FOCUS:
|
|
renderWindowFocused = false;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(renderWindowFocused) {
|
|
if(Keyboard.isKeyPressed(Key.W)) {
|
|
player.move(Direction.NORTH);
|
|
} else if(Keyboard.isKeyPressed(Key.S)) {
|
|
player.move(Direction.SOUTH);
|
|
} else if(Keyboard.isKeyPressed(Key.A)) {
|
|
player.move(Direction.WEST);
|
|
} else if(Keyboard.isKeyPressed(Key.D)) {
|
|
player.move(Direction.EAST);
|
|
} else if(Keyboard.isKeyPressed(Key.ESCAPE)) {
|
|
renderWindow.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void handleLogic() {
|
|
player.update();
|
|
Camera.MoveTo(player.getPosition(), 0.5f);
|
|
}
|
|
|
|
public void handleDrawing() {
|
|
renderWindow.clear();
|
|
renderWindow.draw(player.getMap());
|
|
renderWindow.draw(player);
|
|
renderWindow.draw(fpsCounter);
|
|
renderWindow.display();
|
|
}
|
|
|
|
} |