diff --git a/bin/tfg/Game.class b/bin/tfg/Game.class index 4380cdf..7e40da1 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 438f446..8832977 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 index dce8fe2..76fe33c 100644 Binary files a/bin/tfg/Map.class and b/bin/tfg/Map.class differ diff --git a/bin/tfg/Player.class b/bin/tfg/Player.class index 2209abd..ce74f69 100644 Binary files a/bin/tfg/Player.class and b/bin/tfg/Player.class differ diff --git a/bin/tfg/PlayerAction.class b/bin/tfg/PlayerAction.class new file mode 100644 index 0000000..33ae852 Binary files /dev/null and b/bin/tfg/PlayerAction.class differ diff --git a/bin/tfg/Tile.class b/bin/tfg/Tile.class index 4cd68f7..002062d 100644 Binary files a/bin/tfg/Tile.class and b/bin/tfg/Tile.class differ diff --git a/res/stuck.wav b/res/stuck.wav new file mode 100644 index 0000000..310156c Binary files /dev/null and b/res/stuck.wav differ diff --git a/src/tfg/Game.java b/src/tfg/Game.java index 09a0c23..d9b1aa4 100644 --- a/src/tfg/Game.java +++ b/src/tfg/Game.java @@ -8,6 +8,10 @@ import org.jsfml.window.event.Event; import java.util.ArrayList; import java.util.Collections; +/** + * TFG Game's main class. + * @author Ritchie Cunningham + */ public class Game { private RenderWindow renderWindow = new RenderWindow(); private final String renderWindowTitle = "TFG Game"; diff --git a/src/tfg/Location.java b/src/tfg/Location.java index d8b5b14..0249f42 100644 --- a/src/tfg/Location.java +++ b/src/tfg/Location.java @@ -1,6 +1,10 @@ package tfg; import org.jsfml.system.Vector2i; +/** + * Contains a position and direction. + * @author Ritchie Cunningham + */ public class Location implements Cloneable { private Vector2i locPosition = new Vector2i(0,0); private Direction locDirection; @@ -62,6 +66,7 @@ public class Location implements Cloneable { newLocation.subtractPosition(new Vector2i(1,0)); break; } + newLocation.setDirection(d); return newLocation; } diff --git a/src/tfg/Map.java b/src/tfg/Map.java index 7c71e0a..72b1b42 100644 --- a/src/tfg/Map.java +++ b/src/tfg/Map.java @@ -14,6 +14,11 @@ import java.io.IOException; import java.nio.file.Paths; import java.util.Arrays; +/** + * A map or level for the player to interact in. Can be created + * manually or procedurally. + * @author Ritchie Cunningham + */ public class Map implements Drawable { private Vector2i mapDimensions = new Vector2i(0, 0); private Tile[][] tileArray; diff --git a/src/tfg/Player.java b/src/tfg/Player.java index cb05f6f..cf091a9 100644 --- a/src/tfg/Player.java +++ b/src/tfg/Player.java @@ -1,5 +1,8 @@ package tfg; +import org.jsfml.audio.Sound; +import org.jsfml.audio.SoundBuffer; +import org.jsfml.audio.SoundSource; import org.jsfml.graphics.Drawable; import org.jsfml.graphics.IntRect; import org.jsfml.graphics.RenderStates; @@ -12,24 +15,34 @@ import org.jsfml.system.Vector2i; import java.io.IOException; import java.nio.file.Paths; +/** + * Player Class + * @author Ritchie Cunningham + */ 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 PlayerAction currentAction = PlayerAction.NONE; + private Location newPlayerLoc; + private int frameCounter = 0; + private final float animationSpeed = 20.f; private int animationFrame = 0; - private boolean animated = false; + private SoundBuffer cannotMoveBuffer = new SoundBuffer(); + private Sound cannotMove = new Sound(); public Player() { playerLoc = new Location(0, 0); try { playerSpritesheetTexture.loadFromFile(Paths.get("res/player.png")); + cannotMoveBuffer.loadFromFile(Paths.get("res/stuck.wav")); } catch(IOException ex) { ex.printStackTrace(); } playerSprite = new Sprite(playerSpritesheetTexture); + cannotMove.setBuffer(cannotMoveBuffer); } public void changeMap(Map m) { @@ -41,11 +54,15 @@ public class Player implements Drawable { } public void move(Direction d) { - Location newLoc = playerLoc.getRelativeLocation(d); - if(currentMap.isValidLocation(newLoc)) { - animated = true; - playerLoc = newLoc; + if(currentAction == PlayerAction.NONE) { + Location newLoc = playerLoc.getRelativeLocation(d); playerLoc.setDirection(d); + if(currentMap.isValidLocation(newLoc)) { + currentAction = PlayerAction.MOVING; + newPlayerLoc = newLoc; + } else if(cannotMove.getStatus() != SoundSource.Status.PLAYING) { + cannotMove.play(); + } } } @@ -55,46 +72,77 @@ public class Player implements Drawable { public IntRect getTextureCoords() { IntRect textureCoordsRect = new IntRect(0,0,0,0); - int animationAddition = animationFrame*32; switch(playerLoc.getDirection()) { case NORTH: textureCoordsRect = - new IntRect(0+animationAddition, 144, playerSize.x, playerSize.y); + new IntRect(0+(animationFrame*32), 144, playerSize.x, playerSize.y); break; case SOUTH: textureCoordsRect = - new IntRect(0+animationAddition,0,playerSize.x,playerSize.y); + new IntRect(0+(animationFrame*32),0,playerSize.x,playerSize.y); break; case EAST: textureCoordsRect = - new IntRect(0+animationAddition,96,playerSize.x,playerSize.y); + new IntRect(0+(animationFrame*32),96,playerSize.x,playerSize.y); break; case WEST: textureCoordsRect = - new IntRect(0+animationAddition,48,playerSize.x,playerSize.y); + new IntRect(0+(animationFrame*32),48,playerSize.x,playerSize.y); break; } return textureCoordsRect; } public void draw(RenderTarget target, RenderStates states) { - if(animated) { - animationCounter++; - if(animationCounter >= 10) { - animationCounter = 0; - animationFrame++; - if(animationFrame >= 4) { - animationFrame = 0; - animated = false; + Vector2f spritePosition = new Vector2f(0,0); + Vector2i currentPlayerPosition = playerLoc.getPosition(); + if(currentAction == PlayerAction.MOVING) { + if(frameCounter >= animationSpeed) { + frameCounter = 0; + currentAction = PlayerAction.NONE; + playerLoc = newPlayerLoc; + newPlayerLoc = null; + Vector2i newPlayerPosition = playerLoc.getPosition(); + spritePosition = new Vector2f(newPlayerPosition.x, newPlayerPosition.y); + animationFrame = 0; + } else { + float additionX = 0.0f; + float additionY = 0.0f; + switch(playerLoc.getDirection()) { + case NORTH: + additionY = -(1.0f / animationSpeed) * (frameCounter); + break; + case SOUTH: + additionY = (1.0f / animationSpeed) * (frameCounter); + case EAST: + additionX = (1.0f / animationSpeed) * (frameCounter); + break; + case WEST: + additionX = -(1.0f / animationSpeed) * (frameCounter); + break; } + float change = Math.abs(additionX + additionY); + if(change >= 0.f && change < .25f) { + animationFrame = 0; + } else if(change >= .25f && change < .5f) { + animationFrame = 1; + } else if(change >= .5f && change < .75f) { + animationFrame = 2; + } else if(change >= .75f && change <= 1.0f) { + animationFrame = 3; + } + spritePosition = new Vector2f(currentPlayerPosition.x + additionX, + currentPlayerPosition.y + additionY); } + frameCounter++; + } else { + spritePosition = new Vector2f(currentPlayerPosition.x, currentPlayerPosition.y); } - Vector2i playerPosition = playerLoc.getPosition(); - Direction playerDirection = playerLoc.getDirection(); playerSprite.setPosition( - new Vector2f((playerPosition.x*playerSize.x), - (playerPosition.y*playerSize.x) - (playerSize.y-playerSize.x))); + new Vector2f(spritePosition.x*playerSize.x, + (spritePosition.y*playerSize.x) - (playerSize.y - playerSize.x))); + playerSprite.setTextureRect(getTextureCoords()); RenderStates newStates = new RenderStates(playerSpritesheetTexture); playerSprite.draw(target, newStates); diff --git a/src/tfg/PlayerAction.java b/src/tfg/PlayerAction.java new file mode 100644 index 0000000..3628952 --- /dev/null +++ b/src/tfg/PlayerAction.java @@ -0,0 +1,7 @@ +package tfg; + +/** + * Actions the player can carry out. + * @author Ritchie Cunningham + */ +public enum PlayerAction {NONE, MOVING } diff --git a/src/tfg/Tile.java b/src/tfg/Tile.java index dd9fc21..bc5a2ea 100644 --- a/src/tfg/Tile.java +++ b/src/tfg/Tile.java @@ -2,10 +2,14 @@ package tfg; import org.jsfml.system.Vector2f; +/** + * Map tiles for the Map class. + * @author Ritchie Cunningham + */ public enum Tile { WATER, SAND, GRASS; - private final static int tileSize = 32; + private static int tileSize = 32; public static Vector2f getTextureCoords(Tile t) { Vector2f textureCoordinates = new Vector2f(0, 0);