Skip to content

Commit

Permalink
Merge pull request #21 from sammyhori/SpriteDirection
Browse files Browse the repository at this point in the history
Set player's sprite to face in direction they are walking
  • Loading branch information
li4x authored Mar 21, 2024
2 parents 4ac7b9c + 645c3cc commit 31c6503
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions core/src/uk/ac/york/student/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public class Player extends Actor implements PlayerScore, InputProcessor {
*/
private Sprite sprite;

/**
* Sprite objects for each direction to face in.
*<p>
* There's no right facing region in the Atlas so the left one is flipped
* when facing right.
*</p>
*/
private final TextureAtlas.AtlasRegion SPRITETOWARDSREGION;
private final TextureAtlas.AtlasRegion SPRITEAWAYREGION;
private final TextureAtlas.AtlasRegion SPRITELEFTREGION;
/**
* TiledMap object representing the current game map.
*/
Expand Down Expand Up @@ -76,7 +86,10 @@ public Player(@NotNull TiledMap map, @NotNull Vector2 startPosition) {
mapScale = Math.max(Gdx.graphics.getWidth() / maxWidth, Gdx.graphics.getHeight() / maxHeight);

// Create a sprite for the player and set its position, opacity, and size
sprite = textureAtlas.createSprite("char3_left");
SPRITETOWARDSREGION = textureAtlas.findRegion("char3_towards");
SPRITEAWAYREGION = textureAtlas.findRegion("char3_away");
SPRITELEFTREGION = textureAtlas.findRegion("char3_left");
sprite = textureAtlas.createSprite("char3_towards");
sprite.setPosition(startPosition.x, startPosition.y);
sprite.setAlpha(1);
sprite.setSize(sprite.getWidth() * mapScale, sprite.getHeight() * mapScale);
Expand Down Expand Up @@ -107,7 +120,7 @@ public void setMap(@NotNull TiledMap map) {
mapScale = Math.max(Gdx.graphics.getWidth() / maxWidth, Gdx.graphics.getHeight() / maxHeight);

// Create a sprite for the player and set its position, opacity, and size
sprite = textureAtlas.createSprite("char3_left");
sprite = textureAtlas.createSprite("char3_towards");
sprite.setAlpha(1);

// Scale the sprite according to the map scale
Expand Down Expand Up @@ -185,24 +198,29 @@ public void move() {

// Move the sprite up if the UP movement is active and the sprite is not at the top of the map
if (Movement.UP.is && (sprite.getY() + sprite.getHeight() < maxHeightScaled)) {
sprite.setRegion(SPRITEAWAYREGION);
sprite.translateY(amount);
setY(sprite.getY());
}

// Move the sprite down if the DOWN movement is active and the sprite is not at the bottom of the map
if (Movement.DOWN.is && (sprite.getY() > 0)) {
sprite.setRegion(SPRITETOWARDSREGION);
sprite.translateY(-amount);
setY(sprite.getY());
}

// Move the sprite left if the LEFT movement is active and the sprite is not at the left edge of the map
if (Movement.LEFT.is && (sprite.getX() > 0)) {
sprite.setRegion(SPRITELEFTREGION);
sprite.translateX(-amount);
setX(sprite.getX());
}

// Move the sprite right if the RIGHT movement is active and the sprite is not at the right edge of the map
if (Movement.RIGHT.is && (sprite.getX() + sprite.getWidth() < maxWidthScaled)) {
sprite.setRegion(SPRITELEFTREGION);
sprite.setFlip(true, false);
sprite.translateX(amount);
setX(sprite.getX());
}
Expand Down

0 comments on commit 31c6503

Please sign in to comment.