diff --git a/bin/tfg/Direction.class b/bin/tfg/Direction.class index 419a0dc..246995b 100644 Binary files a/bin/tfg/Direction.class and b/bin/tfg/Direction.class differ diff --git a/bin/tfg/Game.class b/bin/tfg/Game.class index 3ba635a..4380cdf 100644 Binary files a/bin/tfg/Game.class and b/bin/tfg/Game.class differ diff --git a/bin/tfg/Location.class b/bin/tfg/Location.class index 762ca0f..438f446 100644 Binary files a/bin/tfg/Location.class and b/bin/tfg/Location.class differ diff --git a/bin/tfg/Map.class b/bin/tfg/Map.class new file mode 100644 index 0000000..dce8fe2 Binary files /dev/null and b/bin/tfg/Map.class differ diff --git a/bin/tfg/Player.class b/bin/tfg/Player.class index ba1fbdf..2209abd 100644 Binary files a/bin/tfg/Player.class and b/bin/tfg/Player.class differ diff --git a/bin/tfg/Tile.class b/bin/tfg/Tile.class new file mode 100644 index 0000000..4cd68f7 Binary files /dev/null and b/bin/tfg/Tile.class differ diff --git a/res/tiles.png b/res/tiles.png new file mode 100644 index 0000000..30266cf Binary files /dev/null and b/res/tiles.png differ diff --git a/src/tfg/Direction.java b/src/tfg/Direction.java index 6c46e0d..53a114d 100644 --- a/src/tfg/Direction.java +++ b/src/tfg/Direction.java @@ -1,3 +1,3 @@ package tfg; -enum Direction { NORTH, SOUTH, EAST, WEST, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST }; +public enum Direction { NORTH, SOUTH, EAST, WEST }; diff --git a/src/tfg/Game.java b/src/tfg/Game.java index 47771a3..09a0c23 100644 --- a/src/tfg/Game.java +++ b/src/tfg/Game.java @@ -1,71 +1,107 @@ package tfg; -import org.jsfml.graphics.Color; import org.jsfml.graphics.RenderWindow; import org.jsfml.system.Vector2i; import org.jsfml.window.VideoMode; import org.jsfml.window.event.Event; -import org.jsfml.window.event.KeyEvent; +import java.util.ArrayList; +import java.util.Collections; public class Game { - private RenderWindow window = new RenderWindow(); - private final String windowTitle = "TFG Game"; - private final Vector2i screenResolution = new Vector2i(640, 480); - - private Player player = new Player(); + private RenderWindow renderWindow = new RenderWindow(); + private final String renderWindowTitle = "TFG Game"; + private final Vector2i renderWindowDimensions = new Vector2i(640, 480); + private Player player; + private ArrayList maps = new ArrayList(); 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() { + renderWindow.create(new VideoMode(renderWindowDimensions.x, + renderWindowDimensions.y), renderWindowTitle); + + 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()); + } + /** * Run the game. */ public void run() { - initialize(); - - /* Main loop. */ - while(window.isOpen()) { - window.clear(Color.BLACK); - window.draw(player.getSprite()); - window.display(); - - /* Event handler. */ - for(Event event : window.pollEvents()) { - if(event.type == Event.Type.CLOSED) { - /* User clicked window close. */ - window.close(); - } else if(event.type == Event.Type.KEY_PRESSED) { - KeyEvent keyEv = event.asKeyEvent(); - switch(keyEv.key) { - case ESCAPE: - window.close(); - break; - case W: - player.move(Direction.NORTH); - break; - case S: - player.move(Direction.SOUTH); - break; - case A: - player.move(Direction.WEST); - break; - case D: - player.move(Direction.EAST); - } - } - } + handleInitialization(); + while(renderWindow.isOpen()) { + handleInput(); + handleLogic(); + handleDrawing(); } } - /** - * Configure settings once at startup. - */ - public void initialize() { - window.create(new VideoMode(screenResolution.x, screenResolution.y), - windowTitle); + public void handleInput() { + for(Event event : renderWindow.pollEvents()) { + switch(event.type) { + case CLOSED: + renderWindow.close(); + break; + case KEY_PRESSED: + switch(event.asKeyEvent().key) { + case ESCAPE: + renderWindow.close(); + break; + case W: + case UP: + player.move(Direction.NORTH); + break; + case S: + case DOWN: + player.move(Direction.SOUTH); + break; + case A: + case LEFT: + player.move(Direction.WEST); + break; + case D: + case RIGHT: + player.move(Direction.EAST); + break; + case N: + player.resetLocation(); + player.changeMap(getRandomMap()); + break; + default: + break; + + } + break; + default: + break; + } + + } + } + + public void handleLogic() { + + } + + public void handleDrawing() { + renderWindow.clear(); + renderWindow.draw(player.getMap()); + renderWindow.draw(player); + renderWindow.display(); } } \ No newline at end of file diff --git a/src/tfg/Location.java b/src/tfg/Location.java index c97d712..d8b5b14 100644 --- a/src/tfg/Location.java +++ b/src/tfg/Location.java @@ -1,57 +1,71 @@ package tfg; import org.jsfml.system.Vector2i; -public class Location { - private Vector2i position = new Vector2i(0,0); - private Direction facing = Direction.SOUTH; +public class Location implements Cloneable { + private Vector2i locPosition = new Vector2i(0,0); + private Direction locDirection; - /** - * Create new location at x, y. - * @param x Starting position. - * @param y Starting position. - */ public Location(int x, int y) { - position = new Vector2i(x,y); + this(x, y, Direction.SOUTH); } - /** - * Get x Coordinate. - * @return x - */ - public int getX() { - return position.x; + public Location(int x, int y, Direction d) { + locPosition = new Vector2i(x,y); + locDirection = d; } - /** - * Get y coordinate. - * @return y - */ - public int getY() { - return position.y; + public Vector2i getPosition() { + return locPosition; } - /** - * Get Direction facing. - * @return direction - */ public Direction getDirection() { - return facing; + return locDirection; } - /** - * Update Position. - * @param x - * @param y - */ - public void setPosition(int x, int y) { - position = new Vector2i(x,y); + public void setPosition(Vector2i newPosition) { + locPosition = newPosition; } - /** - * Update direction. - * @param dir - */ - public void setDirection(Direction dir) { - facing = dir; + public void setDirection(Direction newDirection) { + locDirection = newDirection; + } + + public void addPosition(Vector2i position) { + locPosition = Vector2i.add(locPosition, position); + } + + public void subtractPosition(Vector2i position) { + locPosition = Vector2i.sub(locPosition, position); + } + + Location getRelativeLocation(Direction d) { + Location thisLocation = this; + Location newLocation = new Location(0,0); + + try { + newLocation = thisLocation.clone(); + } catch(CloneNotSupportedException ex) { + ex.printStackTrace(); + } + + switch(d) { + case NORTH: + newLocation.subtractPosition(new Vector2i(0,1)); + break; + case SOUTH: + newLocation.addPosition(new Vector2i(0,1)); + break; + case EAST: + newLocation.addPosition(new Vector2i(1,0)); + break; + case WEST: + newLocation.subtractPosition(new Vector2i(1,0)); + break; + } + return newLocation; + } + + protected Location clone() throws CloneNotSupportedException { + return (Location) super.clone(); } } diff --git a/src/tfg/Map.java b/src/tfg/Map.java new file mode 100644 index 0000000..7c71e0a --- /dev/null +++ b/src/tfg/Map.java @@ -0,0 +1,83 @@ +package tfg; + +import org.jsfml.graphics.Drawable; +import org.jsfml.graphics.PrimitiveType; +import org.jsfml.graphics.RenderStates; +import org.jsfml.graphics.RenderTarget; +import org.jsfml.graphics.Texture; +import org.jsfml.graphics.Vertex; +import org.jsfml.graphics.VertexArray; +import org.jsfml.system.Vector2f; +import org.jsfml.system.Vector2i; + +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Arrays; + +public class Map implements Drawable { + private Vector2i mapDimensions = new Vector2i(0, 0); + private Tile[][] tileArray; + private Texture mapTilesheetTexture = new Texture(); + private VertexArray mapVertexArray = new VertexArray(); + + public Map(int l, int w, Tile t) { + mapDimensions = new Vector2i(l,w); + tileArray = new Tile[mapDimensions.x][mapDimensions.y]; + try { + mapTilesheetTexture.loadFromFile(Paths.get("res/terrain.png")); + } catch(IOException ex) { + ex.printStackTrace(); + } + mapVertexArray.setPrimitiveType(PrimitiveType.QUADS); + initializeMap(t); + } + + public Map(int l, int w) { + this(l,w,Tile.GRASS); + } + + public void initializeMap(Tile initialTile) { + for(Tile[] row : tileArray) { + Arrays.fill(row,initialTile); + } + } + + public void draw(RenderTarget target, RenderStates states) { + final int tileSize = Tile.getSize(); + for(int i = 0; i < mapDimensions.x; i++) { + for(int j = 0; j < mapDimensions.y; j++) { + Tile tileType = tileArray[i][j]; + Vector2f textureCoords = Tile.getTextureCoords(tileType); + + /* Top left. */ + mapVertexArray.add(new Vertex( + new Vector2f(i*tileSize, j* tileSize), textureCoords)); + + /* Bottom left. */ + mapVertexArray.add(new Vertex( + new Vector2f(i*tileSize,(j*tileSize)+tileSize), + Vector2f.add(textureCoords, new Vector2f(tileSize,tileSize)))); + + /* Bottom right. */ + mapVertexArray.add(new Vertex( + new Vector2f((i*tileSize)+tileSize, (j*tileSize)+tileSize), + Vector2f.add(textureCoords, new Vector2f(tileSize,tileSize)))); + + /* Top right. */ + mapVertexArray.add(new Vertex( + new Vector2f((i*tileSize)+tileSize, j*tileSize), + Vector2f.add(textureCoords, new Vector2f(tileSize,0)))); + } + } + RenderStates newStates = new RenderStates(mapTilesheetTexture); + mapVertexArray.draw(target, newStates); + } + + public boolean isValidLocation(Location l) { + Vector2i coordinates = l.getPosition(); + return ((coordinates.x >= 0) && (coordinates.y >=0) && + (coordinates.x < mapDimensions.x) && + (coordinates.y < mapDimensions.y)); + } + +} diff --git a/src/tfg/Player.java b/src/tfg/Player.java index a19bd25..cb05f6f 100644 --- a/src/tfg/Player.java +++ b/src/tfg/Player.java @@ -1,57 +1,102 @@ package tfg; +import org.jsfml.graphics.Drawable; +import org.jsfml.graphics.IntRect; +import org.jsfml.graphics.RenderStates; +import org.jsfml.graphics.RenderTarget; +import org.jsfml.graphics.Sprite; +import org.jsfml.graphics.Texture; +import org.jsfml.system.Vector2f; +import org.jsfml.system.Vector2i; + import java.io.IOException; import java.nio.file.Paths; -import org.jsfml.graphics.IntRect; -import org.jsfml.graphics.Sprite; -import org.jsfml.graphics.Texture; -import org.jsfml.system.Vector2f; - -public class Player { - private Location loc = new Location(0,0); - private Sprite sprite; - - /** - * Create a new player. - * Load texture and create sprite. - */ +public class Player implements Drawable { + private Location playerLoc; + private Texture playerSpritesheetTexture = new Texture(); + private Sprite playerSprite = new Sprite(); + private Map currentMap; + private final Vector2i playerSize = new Vector2i(32, 48); + private int animationCounter = 0; + private int animationFrame = 0; + private boolean animated = false; + public Player() { - Texture playerTex = new Texture(); + playerLoc = new Location(0, 0); try { - playerTex.loadFromFile(Paths.get("res/player.png")); + playerSpritesheetTexture.loadFromFile(Paths.get("res/player.png")); } catch(IOException ex) { ex.printStackTrace(); } - sprite = new Sprite(playerTex); + playerSprite = new Sprite(playerSpritesheetTexture); } - public void move(Direction dir) { - loc.setDirection(dir); - IntRect texRect = new IntRect(0,0,0,0); - switch(dir) { + public void changeMap(Map m) { + currentMap = m; + } + + public Map getMap() { + return currentMap; + } + + public void move(Direction d) { + Location newLoc = playerLoc.getRelativeLocation(d); + if(currentMap.isValidLocation(newLoc)) { + animated = true; + playerLoc = newLoc; + playerLoc.setDirection(d); + } + } + + public void resetLocation() { + playerLoc = new Location(0, 0); + } + + public IntRect getTextureCoords() { + IntRect textureCoordsRect = new IntRect(0,0,0,0); + int animationAddition = animationFrame*32; + + switch(playerLoc.getDirection()) { case NORTH: - texRect = new IntRect(0, 144, 32, 48); - loc.setPosition(loc.getX(), loc.getY()-1); + textureCoordsRect = + new IntRect(0+animationAddition, 144, playerSize.x, playerSize.y); break; case SOUTH: - texRect = new IntRect(0, 0, 32, 48); - loc.setPosition(loc.getX(), loc.getY()+1); - break; - case WEST: - texRect = new IntRect(0, 48, 32, 48); - loc.setPosition(loc.getX()-1, loc.getY()); + textureCoordsRect = + new IntRect(0+animationAddition,0,playerSize.x,playerSize.y); break; case EAST: - texRect = new IntRect(0, 96, 32, 48); - loc.setPosition(loc.getX()+1, loc.getY()); + textureCoordsRect = + new IntRect(0+animationAddition,96,playerSize.x,playerSize.y); + break; + case WEST: + textureCoordsRect = + new IntRect(0+animationAddition,48,playerSize.x,playerSize.y); break; } - sprite.setTextureRect(texRect); - sprite.setPosition(new Vector2f(loc.getX() * 32, loc.getY()*32)); + return textureCoordsRect; } - public Sprite getSprite() { - return sprite; + public void draw(RenderTarget target, RenderStates states) { + if(animated) { + animationCounter++; + if(animationCounter >= 10) { + animationCounter = 0; + animationFrame++; + if(animationFrame >= 4) { + animationFrame = 0; + animated = false; + } + } + } + Vector2i playerPosition = playerLoc.getPosition(); + Direction playerDirection = playerLoc.getDirection(); + playerSprite.setPosition( + new Vector2f((playerPosition.x*playerSize.x), + (playerPosition.y*playerSize.x) - (playerSize.y-playerSize.x))); + playerSprite.setTextureRect(getTextureCoords()); + RenderStates newStates = new RenderStates(playerSpritesheetTexture); + playerSprite.draw(target, newStates); } } diff --git a/src/tfg/Tile.java b/src/tfg/Tile.java new file mode 100644 index 0000000..dd9fc21 --- /dev/null +++ b/src/tfg/Tile.java @@ -0,0 +1,29 @@ +package tfg; + +import org.jsfml.system.Vector2f; + +public enum Tile { + WATER, SAND, GRASS; + + private final static int tileSize = 32; + + public static Vector2f getTextureCoords(Tile t) { + Vector2f textureCoordinates = new Vector2f(0, 0); + switch(t) { + case WATER: + textureCoordinates = new Vector2f(480, 544); + break; + case SAND: + textureCoordinates = new Vector2f(576, 352); + break; + case GRASS: + textureCoordinates = new Vector2f(448, 252); + break; + } + return textureCoordinates; + } + + public static int getSize() { + return tileSize; + } +}