[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;
|
||||
|
||||
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;
|
||||
|
||||
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<Map> maps = new ArrayList<Map>();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
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