diff --git a/bin/tfg/Game.class b/bin/tfg/Game.class index c4a3087..65bf4a6 100644 Binary files a/bin/tfg/Game.class and b/bin/tfg/Game.class differ diff --git a/bin/tfg/Player.class b/bin/tfg/Player.class index cfb5828..d2cf8cf 100644 Binary files a/bin/tfg/Player.class and b/bin/tfg/Player.class differ diff --git a/res/kpixel.ttf b/res/kpixel.ttf new file mode 100644 index 0000000..e3dc57e Binary files /dev/null and b/res/kpixel.ttf differ diff --git a/res/tiles.png b/res/terrain.png similarity index 100% rename from res/tiles.png rename to res/terrain.png diff --git a/src/tfg/Game.java b/src/tfg/Game.java index ab9a6dd..277f8c2 100644 --- a/src/tfg/Game.java +++ b/src/tfg/Game.java @@ -1,12 +1,18 @@ package tfg; +import org.jsfml.graphics.Color; +import org.jsfml.graphics.Font; import org.jsfml.graphics.RenderWindow; +import org.jsfml.graphics.Text; +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; +import java.io.IOException; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; @@ -20,6 +26,10 @@ public class Game { private final Vector2i renderWindowDimensions = new Vector2i(640, 480); private Player player; private ArrayList<Map> maps = new ArrayList<Map>(); + private boolean windowFocused = true; + private Font pixel = new Font(); + private int fps; + private static boolean limitFPS = false; public static void main(String[] args) { Game g = new Game(); /* Create temp object of self. */ @@ -32,10 +42,17 @@ public class Game { } public void handleInitialization() { + try { + pixel.loadFromFile(Paths.get("res/kpixel.ttf")); + } catch(IOException ex) { + ex.printStackTrace(); + } renderWindow.create(new VideoMode(renderWindowDimensions.x, renderWindowDimensions.y), renderWindowTitle); - renderWindow.setFramerateLimit(60); + if(limitFPS) { + renderWindow.setFramerateLimit(60); + } player = new Player(); @@ -51,10 +68,31 @@ public class 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(); - handleLogic(); + 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); + framesDrawn = 0; + frameClock.restart(); + } } } @@ -69,32 +107,39 @@ public class Game { } } - 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(); - } - - if(Keyboard.isKeyPressed(Key.N)) { - player.resetLocation(); - player.changeMap(getRandomMap()); + if(windowFocused) { + 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(); + } + + if(Keyboard.isKeyPressed(Key.N)) { + player.resetLocation(); + player.changeMap(getRandomMap()); + } } } public void handleLogic() { - + player.update(); } public void handleDrawing() { + Text fpsCount = new Text("FPS: " + fps, pixel, 24); + fpsCount.setColor(Color.YELLOW); + fpsCount.setStyle(Text.BOLD); + fpsCount.setPosition(0,0); renderWindow.clear(); renderWindow.draw(player.getMap()); renderWindow.draw(player); + renderWindow.draw(fpsCount); renderWindow.display(); } diff --git a/src/tfg/Player.java b/src/tfg/Player.java index f31bc24..5bf3907 100644 --- a/src/tfg/Player.java +++ b/src/tfg/Player.java @@ -32,6 +32,7 @@ public class Player implements Drawable { private int animationFrame = 0; private SoundBuffer cannotMoveBuffer = new SoundBuffer(); private Sound cannotMove = new Sound(); + private Vector2f tempPosition = new Vector2f(0,0); /* Horrible Hack. */ public Player() { playerLoc = new Location(0, 0); @@ -60,7 +61,7 @@ public class Player implements Drawable { if(currentMap.isValidLocation(newLoc)) { currentAction = PlayerAction.MOVING; newPlayerLoc = newLoc; - } else if(cannotMove.getStatus() != SoundSource.Status.PLAYING) { + } else if(cannotMove.getStatus() == SoundSource.Status.STOPPED) { cannotMove.play(); } } @@ -94,8 +95,9 @@ public class Player implements Drawable { return textureCoordsRect; } - public void draw(RenderTarget target, RenderStates states) { - Vector2f spritePosition = new Vector2f(0,0); + public void update() { + /* Dirty dirty hack and should be fixed asap. */ + tempPosition = new Vector2f(0,0); Vector2i currentPlayerPosition = playerLoc.getPosition(); if(currentAction == PlayerAction.MOVING) { if(frameCounter >= animationSpeed) { @@ -104,7 +106,7 @@ public class Player implements Drawable { playerLoc = newPlayerLoc; newPlayerLoc = null; Vector2i newPlayerPosition = playerLoc.getPosition(); - spritePosition = new Vector2f(newPlayerPosition.x, newPlayerPosition.y); + tempPosition = new Vector2f(newPlayerPosition.x, newPlayerPosition.y); animationFrame = 0; } else { float additionX = 0.0f; @@ -132,17 +134,20 @@ public class Player implements Drawable { } else if(change >= .75f && change <= 1.0f) { animationFrame = 3; } - spritePosition = new Vector2f(currentPlayerPosition.x + additionX, + tempPosition = new Vector2f(currentPlayerPosition.x + additionX, currentPlayerPosition.y + additionY); } frameCounter++; } else { - spritePosition = new Vector2f(currentPlayerPosition.x, currentPlayerPosition.y); + tempPosition = new Vector2f(currentPlayerPosition.x, currentPlayerPosition.y); } + } + + public void draw(RenderTarget target, RenderStates states) { playerSprite.setPosition( - new Vector2f(spritePosition.x*playerSize.x, - (spritePosition.y*playerSize.x) - (playerSize.y - playerSize.x))); - + new Vector2f(tempPosition.x*playerSize.x, + (tempPosition.y*playerSize.x)-(playerSize.y-playerSize.x))); + playerSprite.setTextureRect(getTextureCoords()); RenderStates newStates = new RenderStates(playerSpritesheetTexture); playerSprite.draw(target, newStates);