[Add] Maps & Animations.
This commit is contained in:
parent
7a0975288a
commit
79e08a2da5
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
bin/tfg/Map.class
Normal file
BIN
bin/tfg/Map.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
bin/tfg/Tile.class
Normal file
BIN
bin/tfg/Tile.class
Normal file
Binary file not shown.
BIN
res/tiles.png
Normal file
BIN
res/tiles.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 248 KiB |
@ -1,3 +1,3 @@
|
|||||||
package tfg;
|
package tfg;
|
||||||
|
|
||||||
enum Direction { NORTH, SOUTH, EAST, WEST, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST };
|
public enum Direction { NORTH, SOUTH, EAST, WEST };
|
||||||
|
@ -1,71 +1,107 @@
|
|||||||
package tfg;
|
package tfg;
|
||||||
|
|
||||||
import org.jsfml.graphics.Color;
|
|
||||||
import org.jsfml.graphics.RenderWindow;
|
import org.jsfml.graphics.RenderWindow;
|
||||||
import org.jsfml.system.Vector2i;
|
import org.jsfml.system.Vector2i;
|
||||||
import org.jsfml.window.VideoMode;
|
import org.jsfml.window.VideoMode;
|
||||||
import org.jsfml.window.event.Event;
|
import org.jsfml.window.event.Event;
|
||||||
import org.jsfml.window.event.KeyEvent;
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
private RenderWindow window = new RenderWindow();
|
private RenderWindow renderWindow = new RenderWindow();
|
||||||
private final String windowTitle = "TFG Game";
|
private final String renderWindowTitle = "TFG Game";
|
||||||
private final Vector2i screenResolution = new Vector2i(640, 480);
|
private final Vector2i renderWindowDimensions = new Vector2i(640, 480);
|
||||||
|
private Player player;
|
||||||
private Player player = new Player();
|
private ArrayList<Map> maps = new ArrayList<Map>();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Game g = new Game(); /* Create temp object of self. */
|
Game g = new Game(); /* Create temp object of self. */
|
||||||
g.run(); /* Invoke run. */
|
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.
|
* Run the game.
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
initialize();
|
handleInitialization();
|
||||||
|
while(renderWindow.isOpen()) {
|
||||||
|
handleInput();
|
||||||
|
handleLogic();
|
||||||
|
handleDrawing();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Main loop. */
|
public void handleInput() {
|
||||||
while(window.isOpen()) {
|
for(Event event : renderWindow.pollEvents()) {
|
||||||
window.clear(Color.BLACK);
|
switch(event.type) {
|
||||||
window.draw(player.getSprite());
|
case CLOSED:
|
||||||
window.display();
|
renderWindow.close();
|
||||||
|
break;
|
||||||
/* Event handler. */
|
case KEY_PRESSED:
|
||||||
for(Event event : window.pollEvents()) {
|
switch(event.asKeyEvent().key) {
|
||||||
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:
|
case ESCAPE:
|
||||||
window.close();
|
renderWindow.close();
|
||||||
break;
|
break;
|
||||||
case W:
|
case W:
|
||||||
|
case UP:
|
||||||
player.move(Direction.NORTH);
|
player.move(Direction.NORTH);
|
||||||
break;
|
break;
|
||||||
case S:
|
case S:
|
||||||
|
case DOWN:
|
||||||
player.move(Direction.SOUTH);
|
player.move(Direction.SOUTH);
|
||||||
break;
|
break;
|
||||||
case A:
|
case A:
|
||||||
|
case LEFT:
|
||||||
player.move(Direction.WEST);
|
player.move(Direction.WEST);
|
||||||
break;
|
break;
|
||||||
case D:
|
case D:
|
||||||
|
case RIGHT:
|
||||||
player.move(Direction.EAST);
|
player.move(Direction.EAST);
|
||||||
|
break;
|
||||||
|
case N:
|
||||||
|
player.resetLocation();
|
||||||
|
player.changeMap(getRandomMap());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void handleLogic() {
|
||||||
* Configure settings once at startup.
|
|
||||||
*/
|
}
|
||||||
public void initialize() {
|
|
||||||
window.create(new VideoMode(screenResolution.x, screenResolution.y),
|
public void handleDrawing() {
|
||||||
windowTitle);
|
renderWindow.clear();
|
||||||
|
renderWindow.draw(player.getMap());
|
||||||
|
renderWindow.draw(player);
|
||||||
|
renderWindow.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,57 +1,71 @@
|
|||||||
package tfg;
|
package tfg;
|
||||||
import org.jsfml.system.Vector2i;
|
import org.jsfml.system.Vector2i;
|
||||||
|
|
||||||
public class Location {
|
public class Location implements Cloneable {
|
||||||
private Vector2i position = new Vector2i(0,0);
|
private Vector2i locPosition = new Vector2i(0,0);
|
||||||
private Direction facing = Direction.SOUTH;
|
private Direction locDirection;
|
||||||
|
|
||||||
/**
|
|
||||||
* Create new location at x, y.
|
|
||||||
* @param x Starting position.
|
|
||||||
* @param y Starting position.
|
|
||||||
*/
|
|
||||||
public Location(int x, int y) {
|
public Location(int x, int y) {
|
||||||
position = new Vector2i(x,y);
|
this(x, y, Direction.SOUTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public Location(int x, int y, Direction d) {
|
||||||
* Get x Coordinate.
|
locPosition = new Vector2i(x,y);
|
||||||
* @return x
|
locDirection = d;
|
||||||
*/
|
|
||||||
public int getX() {
|
|
||||||
return position.x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public Vector2i getPosition() {
|
||||||
* Get y coordinate.
|
return locPosition;
|
||||||
* @return y
|
|
||||||
*/
|
|
||||||
public int getY() {
|
|
||||||
return position.y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Direction facing.
|
|
||||||
* @return direction
|
|
||||||
*/
|
|
||||||
public Direction getDirection() {
|
public Direction getDirection() {
|
||||||
return facing;
|
return locDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void setPosition(Vector2i newPosition) {
|
||||||
* Update Position.
|
locPosition = newPosition;
|
||||||
* @param x
|
|
||||||
* @param y
|
|
||||||
*/
|
|
||||||
public void setPosition(int x, int y) {
|
|
||||||
position = new Vector2i(x,y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void setDirection(Direction newDirection) {
|
||||||
* Update direction.
|
locDirection = newDirection;
|
||||||
* @param dir
|
}
|
||||||
*/
|
|
||||||
public void setDirection(Direction dir) {
|
public void addPosition(Vector2i position) {
|
||||||
facing = dir;
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
83
src/tfg/Map.java
Normal file
83
src/tfg/Map.java
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,57 +1,102 @@
|
|||||||
package tfg;
|
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.io.IOException;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
import org.jsfml.graphics.IntRect;
|
public class Player implements Drawable {
|
||||||
import org.jsfml.graphics.Sprite;
|
private Location playerLoc;
|
||||||
import org.jsfml.graphics.Texture;
|
private Texture playerSpritesheetTexture = new Texture();
|
||||||
import org.jsfml.system.Vector2f;
|
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 class Player {
|
|
||||||
private Location loc = new Location(0,0);
|
|
||||||
private Sprite sprite;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new player.
|
|
||||||
* Load texture and create sprite.
|
|
||||||
*/
|
|
||||||
public Player() {
|
public Player() {
|
||||||
Texture playerTex = new Texture();
|
playerLoc = new Location(0, 0);
|
||||||
try {
|
try {
|
||||||
playerTex.loadFromFile(Paths.get("res/player.png"));
|
playerSpritesheetTexture.loadFromFile(Paths.get("res/player.png"));
|
||||||
} catch(IOException ex) {
|
} catch(IOException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
sprite = new Sprite(playerTex);
|
playerSprite = new Sprite(playerSpritesheetTexture);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void move(Direction dir) {
|
public void changeMap(Map m) {
|
||||||
loc.setDirection(dir);
|
currentMap = m;
|
||||||
IntRect texRect = new IntRect(0,0,0,0);
|
}
|
||||||
switch(dir) {
|
|
||||||
|
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:
|
case NORTH:
|
||||||
texRect = new IntRect(0, 144, 32, 48);
|
textureCoordsRect =
|
||||||
loc.setPosition(loc.getX(), loc.getY()-1);
|
new IntRect(0+animationAddition, 144, playerSize.x, playerSize.y);
|
||||||
break;
|
break;
|
||||||
case SOUTH:
|
case SOUTH:
|
||||||
texRect = new IntRect(0, 0, 32, 48);
|
textureCoordsRect =
|
||||||
loc.setPosition(loc.getX(), loc.getY()+1);
|
new IntRect(0+animationAddition,0,playerSize.x,playerSize.y);
|
||||||
break;
|
|
||||||
case WEST:
|
|
||||||
texRect = new IntRect(0, 48, 32, 48);
|
|
||||||
loc.setPosition(loc.getX()-1, loc.getY());
|
|
||||||
break;
|
break;
|
||||||
case EAST:
|
case EAST:
|
||||||
texRect = new IntRect(0, 96, 32, 48);
|
textureCoordsRect =
|
||||||
loc.setPosition(loc.getX()+1, loc.getY());
|
new IntRect(0+animationAddition,96,playerSize.x,playerSize.y);
|
||||||
|
break;
|
||||||
|
case WEST:
|
||||||
|
textureCoordsRect =
|
||||||
|
new IntRect(0+animationAddition,48,playerSize.x,playerSize.y);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
sprite.setTextureRect(texRect);
|
return textureCoordsRect;
|
||||||
sprite.setPosition(new Vector2f(loc.getX() * 32, loc.getY()*32));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sprite getSprite() {
|
public void draw(RenderTarget target, RenderStates states) {
|
||||||
return sprite;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
29
src/tfg/Tile.java
Normal file
29
src/tfg/Tile.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user