[Add] UI.
This commit is contained in:
parent
675bb53d31
commit
566ac8e48f
Binary file not shown.
BIN
bin/tfg/InterfacePosition.class
Normal file
BIN
bin/tfg/InterfacePosition.class
Normal file
Binary file not shown.
BIN
bin/tfg/UITextElement.class
Normal file
BIN
bin/tfg/UITextElement.class
Normal file
Binary file not shown.
@ -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<Map> maps = new ArrayList<Map>();
|
||||
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();
|
||||
}
|
||||
|
||||
|
15
src/tfg/InterfacePosition.java
Normal file
15
src/tfg/InterfacePosition.java
Normal file
@ -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
|
||||
}
|
55
src/tfg/UITextElement.java
Normal file
55
src/tfg/UITextElement.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user