diff --git a/bin/tfg/Game.class b/bin/tfg/Game.class index 65bf4a6..ab17ed3 100644 Binary files a/bin/tfg/Game.class and b/bin/tfg/Game.class differ diff --git a/bin/tfg/InterfacePosition.class b/bin/tfg/InterfacePosition.class new file mode 100644 index 0000000..f9b3efe Binary files /dev/null and b/bin/tfg/InterfacePosition.class differ diff --git a/bin/tfg/UITextElement.class b/bin/tfg/UITextElement.class new file mode 100644 index 0000000..ce9f62e Binary files /dev/null and b/bin/tfg/UITextElement.class differ diff --git a/src/tfg/Game.java b/src/tfg/Game.java index 277f8c2..aba84fa 100644 --- a/src/tfg/Game.java +++ b/src/tfg/Game.java @@ -3,7 +3,7 @@ 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.graphics.TextStyle; import org.jsfml.system.Clock; import org.jsfml.system.Vector2i; import org.jsfml.window.Keyboard; @@ -11,9 +11,6 @@ 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; /** @@ -25,30 +22,23 @@ public class Game { private final String renderWindowTitle = "TFG Game"; private final Vector2i renderWindowDimensions = new Vector2i(640, 480); private Player player; - private ArrayList maps = new ArrayList(); - private boolean windowFocused = true; + 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 Map getRandomMap() { - Collections.shuffle(maps); - return maps.get(0); - } - - public void handleInitialization() { - try { - pixel.loadFromFile(Paths.get("res/kpixel.ttf")); - } catch(IOException ex) { - ex.printStackTrace(); - } + 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); @@ -56,11 +46,7 @@ public class Game { player = new Player(); - maps.add(new Map(10, 10, Tile.SAND)); - maps.add(new Map( 5, 4, Tile.WATER)); - maps.add(new Map(15, 20, Tile.GRASS)); - - player.changeMap(getRandomMap()); + player.changeMap(new Map(10, 10, Tile.SAND)); } /** @@ -90,6 +76,7 @@ public class Game { float elapsedTime = frameClock.getElapsedTime().asSeconds(); if(elapsedTime >= 1.0f) { fps = (int)(framesDrawn/elapsedTime); + fpsCounter.updateString("FPS: " + fps); framesDrawn = 0; frameClock.restart(); } @@ -102,12 +89,17 @@ public class Game { case CLOSED: renderWindow.close(); break; + case GAINED_FOCUS: + renderWindowFocused = true; + break; + case LOST_FOCUS: + renderWindowFocused = false; default: break; } } - if(windowFocused) { + if(renderWindowFocused) { if(Keyboard.isKeyPressed(Key.W)) { player.move(Direction.NORTH); } else if(Keyboard.isKeyPressed(Key.S)) { @@ -119,11 +111,6 @@ public class Game { } else if(Keyboard.isKeyPressed(Key.ESCAPE)) { renderWindow.close(); } - - if(Keyboard.isKeyPressed(Key.N)) { - player.resetLocation(); - player.changeMap(getRandomMap()); - } } } @@ -132,14 +119,10 @@ public class Game { } 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.draw(fpsCounter); renderWindow.display(); } diff --git a/src/tfg/InterfacePosition.java b/src/tfg/InterfacePosition.java new file mode 100644 index 0000000..a8c65e7 --- /dev/null +++ b/src/tfg/InterfacePosition.java @@ -0,0 +1,15 @@ +package tfg; + +/** + * Positions for UI elements. + * @author Ritchie Cunningham + */ +public enum InterfacePosition { + TOP_LEFT, + TOP_RIGHT, + BOTTOM_LEFT, + BOTTOM_RIGHT, + TOP_CENTER, + BOTTOM_CENTER, + EXACT_CENTER +} diff --git a/src/tfg/UITextElement.java b/src/tfg/UITextElement.java new file mode 100644 index 0000000..509ee00 --- /dev/null +++ b/src/tfg/UITextElement.java @@ -0,0 +1,55 @@ +package tfg; + +import org.jsfml.graphics.Color; +import org.jsfml.graphics.Drawable; +import org.jsfml.graphics.Font; +import org.jsfml.graphics.RenderStates; +import org.jsfml.graphics.RenderTarget; +import org.jsfml.graphics.Text; +import org.jsfml.graphics.TextStyle; + +import java.io.IOException; +import java.nio.file.Paths; + +/** + * GUI Elements + * @author Ritchie Cunningham + */ +public class UITextElement implements Drawable { + private Font font = new Font(); + private Text text = new Text(); + private String value; + + public UITextElement(InterfacePosition p, Color c, int size) { + this(p, c, size, TextStyle.REGULAR); + } + + public UITextElement(InterfacePosition p, Color c, int size, int style) { + try { + font.loadFromFile(Paths.get("res/kpixel.ttf")); + } catch (IOException ex) { + ex.printStackTrace(); + } + + text = new Text("", font, size); + text.setColor(c); + text.setStyle(style); + setPosition(p); + } + + public void updateString(String s) { + text.setString(s); + } + + public void setPosition(InterfacePosition p) { + switch(p) { + case TOP_LEFT: + text.setPosition(0,0); + break; + } + } + + public void draw(RenderTarget target, RenderStates states) { + text.draw(target, states); + } +}