[Fix] Animation
[Add] Sound.
This commit is contained in:
parent
79e08a2da5
commit
e1203e42b0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
bin/tfg/PlayerAction.class
Normal file
BIN
bin/tfg/PlayerAction.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
res/stuck.wav
Normal file
BIN
res/stuck.wav
Normal file
Binary file not shown.
@ -8,6 +8,10 @@ import org.jsfml.window.event.Event;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TFG Game's main class.
|
||||||
|
* @author Ritchie Cunningham
|
||||||
|
*/
|
||||||
public class Game {
|
public class Game {
|
||||||
private RenderWindow renderWindow = new RenderWindow();
|
private RenderWindow renderWindow = new RenderWindow();
|
||||||
private final String renderWindowTitle = "TFG Game";
|
private final String renderWindowTitle = "TFG Game";
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package tfg;
|
package tfg;
|
||||||
import org.jsfml.system.Vector2i;
|
import org.jsfml.system.Vector2i;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains a position and direction.
|
||||||
|
* @author Ritchie Cunningham
|
||||||
|
*/
|
||||||
public class Location implements Cloneable {
|
public class Location implements Cloneable {
|
||||||
private Vector2i locPosition = new Vector2i(0,0);
|
private Vector2i locPosition = new Vector2i(0,0);
|
||||||
private Direction locDirection;
|
private Direction locDirection;
|
||||||
@ -62,6 +66,7 @@ public class Location implements Cloneable {
|
|||||||
newLocation.subtractPosition(new Vector2i(1,0));
|
newLocation.subtractPosition(new Vector2i(1,0));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
newLocation.setDirection(d);
|
||||||
return newLocation;
|
return newLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,11 @@ import java.io.IOException;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Arrays;
|
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 {
|
public class Map implements Drawable {
|
||||||
private Vector2i mapDimensions = new Vector2i(0, 0);
|
private Vector2i mapDimensions = new Vector2i(0, 0);
|
||||||
private Tile[][] tileArray;
|
private Tile[][] tileArray;
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package tfg;
|
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.Drawable;
|
||||||
import org.jsfml.graphics.IntRect;
|
import org.jsfml.graphics.IntRect;
|
||||||
import org.jsfml.graphics.RenderStates;
|
import org.jsfml.graphics.RenderStates;
|
||||||
@ -12,24 +15,34 @@ import org.jsfml.system.Vector2i;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Player Class
|
||||||
|
* @author Ritchie Cunningham
|
||||||
|
*/
|
||||||
public class Player implements Drawable {
|
public class Player implements Drawable {
|
||||||
private Location playerLoc;
|
private Location playerLoc;
|
||||||
private Texture playerSpritesheetTexture = new Texture();
|
private Texture playerSpritesheetTexture = new Texture();
|
||||||
private Sprite playerSprite = new Sprite();
|
private Sprite playerSprite = new Sprite();
|
||||||
private Map currentMap;
|
private Map currentMap;
|
||||||
private final Vector2i playerSize = new Vector2i(32, 48);
|
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 int animationFrame = 0;
|
||||||
private boolean animated = false;
|
private SoundBuffer cannotMoveBuffer = new SoundBuffer();
|
||||||
|
private Sound cannotMove = new Sound();
|
||||||
|
|
||||||
public Player() {
|
public Player() {
|
||||||
playerLoc = new Location(0, 0);
|
playerLoc = new Location(0, 0);
|
||||||
try {
|
try {
|
||||||
playerSpritesheetTexture.loadFromFile(Paths.get("res/player.png"));
|
playerSpritesheetTexture.loadFromFile(Paths.get("res/player.png"));
|
||||||
|
cannotMoveBuffer.loadFromFile(Paths.get("res/stuck.wav"));
|
||||||
} catch(IOException ex) {
|
} catch(IOException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
playerSprite = new Sprite(playerSpritesheetTexture);
|
playerSprite = new Sprite(playerSpritesheetTexture);
|
||||||
|
cannotMove.setBuffer(cannotMoveBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeMap(Map m) {
|
public void changeMap(Map m) {
|
||||||
@ -41,11 +54,15 @@ public class Player implements Drawable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void move(Direction d) {
|
public void move(Direction d) {
|
||||||
|
if(currentAction == PlayerAction.NONE) {
|
||||||
Location newLoc = playerLoc.getRelativeLocation(d);
|
Location newLoc = playerLoc.getRelativeLocation(d);
|
||||||
if(currentMap.isValidLocation(newLoc)) {
|
|
||||||
animated = true;
|
|
||||||
playerLoc = newLoc;
|
|
||||||
playerLoc.setDirection(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() {
|
public IntRect getTextureCoords() {
|
||||||
IntRect textureCoordsRect = new IntRect(0,0,0,0);
|
IntRect textureCoordsRect = new IntRect(0,0,0,0);
|
||||||
int animationAddition = animationFrame*32;
|
|
||||||
|
|
||||||
switch(playerLoc.getDirection()) {
|
switch(playerLoc.getDirection()) {
|
||||||
case NORTH:
|
case NORTH:
|
||||||
textureCoordsRect =
|
textureCoordsRect =
|
||||||
new IntRect(0+animationAddition, 144, playerSize.x, playerSize.y);
|
new IntRect(0+(animationFrame*32), 144, playerSize.x, playerSize.y);
|
||||||
break;
|
break;
|
||||||
case SOUTH:
|
case SOUTH:
|
||||||
textureCoordsRect =
|
textureCoordsRect =
|
||||||
new IntRect(0+animationAddition,0,playerSize.x,playerSize.y);
|
new IntRect(0+(animationFrame*32),0,playerSize.x,playerSize.y);
|
||||||
break;
|
break;
|
||||||
case EAST:
|
case EAST:
|
||||||
textureCoordsRect =
|
textureCoordsRect =
|
||||||
new IntRect(0+animationAddition,96,playerSize.x,playerSize.y);
|
new IntRect(0+(animationFrame*32),96,playerSize.x,playerSize.y);
|
||||||
break;
|
break;
|
||||||
case WEST:
|
case WEST:
|
||||||
textureCoordsRect =
|
textureCoordsRect =
|
||||||
new IntRect(0+animationAddition,48,playerSize.x,playerSize.y);
|
new IntRect(0+(animationFrame*32),48,playerSize.x,playerSize.y);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return textureCoordsRect;
|
return textureCoordsRect;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(RenderTarget target, RenderStates states) {
|
public void draw(RenderTarget target, RenderStates states) {
|
||||||
if(animated) {
|
Vector2f spritePosition = new Vector2f(0,0);
|
||||||
animationCounter++;
|
Vector2i currentPlayerPosition = playerLoc.getPosition();
|
||||||
if(animationCounter >= 10) {
|
if(currentAction == PlayerAction.MOVING) {
|
||||||
animationCounter = 0;
|
if(frameCounter >= animationSpeed) {
|
||||||
animationFrame++;
|
frameCounter = 0;
|
||||||
if(animationFrame >= 4) {
|
currentAction = PlayerAction.NONE;
|
||||||
|
playerLoc = newPlayerLoc;
|
||||||
|
newPlayerLoc = null;
|
||||||
|
Vector2i newPlayerPosition = playerLoc.getPosition();
|
||||||
|
spritePosition = new Vector2f(newPlayerPosition.x, newPlayerPosition.y);
|
||||||
animationFrame = 0;
|
animationFrame = 0;
|
||||||
animated = false;
|
} 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(
|
playerSprite.setPosition(
|
||||||
new Vector2f((playerPosition.x*playerSize.x),
|
new Vector2f(spritePosition.x*playerSize.x,
|
||||||
(playerPosition.y*playerSize.x) - (playerSize.y-playerSize.x)));
|
(spritePosition.y*playerSize.x) - (playerSize.y - playerSize.x)));
|
||||||
|
|
||||||
playerSprite.setTextureRect(getTextureCoords());
|
playerSprite.setTextureRect(getTextureCoords());
|
||||||
RenderStates newStates = new RenderStates(playerSpritesheetTexture);
|
RenderStates newStates = new RenderStates(playerSpritesheetTexture);
|
||||||
playerSprite.draw(target, newStates);
|
playerSprite.draw(target, newStates);
|
||||||
|
7
src/tfg/PlayerAction.java
Normal file
7
src/tfg/PlayerAction.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package tfg;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Actions the player can carry out.
|
||||||
|
* @author Ritchie Cunningham
|
||||||
|
*/
|
||||||
|
public enum PlayerAction {NONE, MOVING }
|
@ -2,10 +2,14 @@ package tfg;
|
|||||||
|
|
||||||
import org.jsfml.system.Vector2f;
|
import org.jsfml.system.Vector2f;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map tiles for the Map class.
|
||||||
|
* @author Ritchie Cunningham
|
||||||
|
*/
|
||||||
public enum Tile {
|
public enum Tile {
|
||||||
WATER, SAND, GRASS;
|
WATER, SAND, GRASS;
|
||||||
|
|
||||||
private final static int tileSize = 32;
|
private static int tileSize = 32;
|
||||||
|
|
||||||
public static Vector2f getTextureCoords(Tile t) {
|
public static Vector2f getTextureCoords(Tile t) {
|
||||||
Vector2f textureCoordinates = new Vector2f(0, 0);
|
Vector2f textureCoordinates = new Vector2f(0, 0);
|
||||||
|
Loading…
Reference in New Issue
Block a user