Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void setDown(boolean down) {
}
}

private boolean anyDirectionKeyPressed() {
public boolean anyDirectionKeyPressed() {
return down || up || left || right;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,140 @@
package dk.sdu.mmmi.modulemon.CommonMap.Data.EntityParts;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import dk.sdu.mmmi.modulemon.CommonMap.Data.Entity;
import dk.sdu.mmmi.modulemon.CommonMap.Data.World;
import dk.sdu.mmmi.modulemon.common.data.GameData;

import java.util.Collections;
import java.util.List;
import java.util.PrimitiveIterator;

public class SpritePart implements EntityPart {
private Texture upSprite;
private Texture downSprite;
private Texture leftSprite;
private Texture rightSprite;
private List<Texture> upSprite;
private List<Texture> downSprite;
private List<Texture> leftSprite;
private List<Texture> rightSprite;

private List<Texture> upIdleSprite;
private List<Texture> downIdleSprite;
private List<Texture> leftIdleSprite;
private List<Texture> rightIdleSprite;

private float timer = 0;
private float duration = 0.25f;
private int frameIndex = 0;

private Texture currentSprite;
private List<Texture> currentSpritePool;

public SpritePart(Texture upSprite, Texture downSprite, Texture leftSprite, Texture rightSprite) {
this.upSprite = Collections.singletonList(upSprite);
this.downSprite = Collections.singletonList(downSprite);
this.leftSprite = Collections.singletonList(leftSprite);
this.rightSprite = Collections.singletonList(rightSprite);
}

public SpritePart(
List<Texture> upSprite,
List<Texture> downSprite,
List<Texture> leftSprite,
List<Texture> rightSprite,
List<Texture> upIdleSprite,
List<Texture> downIdleSprite,
List<Texture> leftIdleSprite,
List<Texture> rightIdleSprite) {
this.upSprite = upSprite;
this.downSprite = downSprite;
this.leftSprite = leftSprite;
this.rightSprite = rightSprite;
this.upIdleSprite = upIdleSprite;
this.downIdleSprite = downIdleSprite;
this.leftIdleSprite = leftIdleSprite;
this.rightIdleSprite = rightIdleSprite;
this.currentSpritePool = downIdleSprite;
}

public Texture getUpSprite(boolean isMoving) {
if(!isMoving && upIdleSprite != null){
updateAnimation(upIdleSprite, isMoving);
return upIdleSprite.get(frameIndex);
} else {
updateAnimation(upSprite, isMoving);
return upSprite.get(frameIndex% upSprite.size());
}
}

public Texture getUpSprite() {
return upSprite;
return upSprite.get(0);
}

public void setUpSprite(Texture upSprite) {
this.upSprite = upSprite;
public Texture getDownSprite(boolean isMoving) {
if(!isMoving && downIdleSprite != null){
updateAnimation(downIdleSprite, isMoving);
return downIdleSprite.get(frameIndex);
} else {
updateAnimation(downSprite, isMoving);
return downSprite.get(frameIndex % downSprite.size());
}
}

public Texture getDownSprite() {
return downSprite;
return downSprite.get(0);
}

public void setDownSprite(Texture downSprite) {
this.downSprite = downSprite;
public Texture getLeftSprite(boolean isMoving) {
if(!isMoving && leftIdleSprite != null){
updateAnimation(leftIdleSprite, isMoving);
return leftIdleSprite.get(frameIndex);
} else {
updateAnimation(leftSprite, isMoving);
return leftSprite.get(frameIndex % leftSprite.size());
}
}

public Texture getLeftSprite() {
return leftSprite;
return leftSprite.get(0);
}

public void setLeftSprite(Texture leftSprite) {
this.leftSprite = leftSprite;
public Texture getRightSprite(boolean isMoving) {
if(!isMoving && rightIdleSprite != null){
updateAnimation(rightIdleSprite, isMoving);
return rightIdleSprite.get(frameIndex);
} else {
updateAnimation(rightSprite, isMoving);
return rightSprite.get(frameIndex % rightSprite.size());
}
}

public Texture getRightSprite() {
return rightSprite;
}

public void setRightSprite(Texture rightSprite) {
this.rightSprite = rightSprite;
return rightSprite.get(0);
}

public Texture getCurrentSprite() {
timer += Gdx.graphics.getDeltaTime();
return currentSprite;
}

public void setCurrentSprite(Texture currentSprite) {
this.currentSprite = currentSprite;
}

private void updateAnimation(List<Texture> animationTextures, boolean isMoving) {
if(timer >= duration){
timer = 0;
frameIndex = (frameIndex + 1) % animationTextures.size();
}
if(this.currentSpritePool != null) {
if(!this.currentSpritePool.contains(animationTextures.get(frameIndex % animationTextures.size()))){
timer = isMoving ? 0 : -1;
frameIndex = 0;
}
}
this.currentSpritePool = animationTextures;
}

@Override
public void process(GameData gameData, World world, Entity entity) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ public class PlayerControlSystem implements IEntityProcessingService {
public void process(GameData gameData, World world) {
for (Entity player : world.getEntities(Player.class)) {
MovingPart movingPart = player.getPart(MovingPart.class);

var isMoving = false;
if (movingPart != null) {
movingPart.setLeft(gameData.getKeys().isDown(LEFT));
movingPart.setRight(gameData.getKeys().isDown(RIGHT));
movingPart.setUp(gameData.getKeys().isDown(UP));
movingPart.setDown(gameData.getKeys().isDown(DOWN));
isMoving = movingPart.anyDirectionKeyPressed();
}

Collection<EntityPart> entityParts = player.getParts();
for (EntityPart entityPart : entityParts) {
entityPart.process(gameData, world, player);
}

updateShape(player);
updateShape(player, isMoving);
}
}

private void updateShape(Entity entity) {
private void updateShape(Entity entity, boolean isMoving) {

SpritePart spritePart = entity.getPart(SpritePart.class);
PositionPart positionPart = entity.getPart(PositionPart.class);
Expand All @@ -43,16 +44,16 @@ private void updateShape(Entity entity) {
Texture result = null;
switch (positionPart.getDirection()) {
case EAST:
result = spritePart.getRightSprite();
result = spritePart.getRightSprite(isMoving);
break;
case WEST:
result = spritePart.getLeftSprite();
result = spritePart.getLeftSprite(isMoving);
break;
case NORTH:
result = spritePart.getUpSprite();
result = spritePart.getUpSprite(isMoving);
break;
case SOUTH:
result = spritePart.getDownSprite();
result = spritePart.getDownSprite(isMoving);
break;
default:
System.out.println(("Did not match any direction"));
Expand Down
40 changes: 31 additions & 9 deletions src/main/java/dk/sdu/mmmi/modulemon/Player/PlayerPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import dk.sdu.mmmi.modulemon.CommonMap.Data.World;
import dk.sdu.mmmi.modulemon.CommonMap.Services.IGamePluginService;
import dk.sdu.mmmi.modulemon.common.data.GameKeys;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.*;

public class PlayerPlugin implements IGamePluginService {
private static final int TILE_SIZE = 64;
Expand Down Expand Up @@ -47,11 +46,16 @@ private Entity createPlayer(GameData gameData) {
player.add(positionPart);
player.add(new MovingPart());
player.add(new InteractPart(positionPart, 1));
Texture upSprite = AssetLoader.getInstance().getTextureAsset("/assets/main-char-up5.png", Player.class);
Texture downSprite = AssetLoader.getInstance().getTextureAsset("/assets/main-char-down5.png", Player.class);
Texture leftSprite = AssetLoader.getInstance().getTextureAsset("/assets/main-char-left5.png", Player.class);
Texture rightSprite = AssetLoader.getInstance().getTextureAsset("/assets/main-char-right5.png", Player.class);
player.add(new SpritePart(upSprite, downSprite, leftSprite, rightSprite));
List<Texture> upSprite = getWalkingAnimation("up");
List<Texture> downSprites = getWalkingAnimation("down");
List<Texture> leftSprite = getWalkingAnimation("left");
List<Texture> rightSprite = getWalkingAnimation("right");

List<Texture> upIdleSprites = getIdleAnimation("up", 3);
List<Texture> downIdleSprites = getIdleAnimation("down", 3);
List<Texture> leftIdleSprites = getIdleAnimation("left", 3);
List<Texture> rightIdleSprites = getIdleAnimation("right", 3);
player.add(new SpritePart(upSprite, downSprites, leftSprite, rightSprite, upIdleSprites, downIdleSprites, leftIdleSprites, rightIdleSprites));
Queue<String> playerLines = new LinkedList<>();
playerLines.add("Alright, lets battle!");
player.add(new TextDisplayPart(playerLines));
Expand All @@ -60,6 +64,24 @@ private Entity createPlayer(GameData gameData) {
return player;
}

private static List<Texture> getIdleAnimation(String direction, int frameCount){
return getAnimation(direction, "idle", frameCount);
}

private static List<Texture> getWalkingAnimation(String direction){
return getAnimation(direction, "walking", 8);
}

@NotNull
private static List<Texture> getAnimation(String direction, String action, int frameCount) {
List<Texture> sprites = new ArrayList<>();
for (int i = 1; i <= frameCount; i++) {
Texture sprite = AssetLoader.getInstance().getTextureAsset("/assets/" + action + "Animations/main-char-" + direction + "-" + action + "" + i +".png", Player.class);
sprites.add(sprite);
}
return sprites;
}

private void addMonsterTeam(Entity entity, GameData gameData) {
List<IMonster> monsterList = new ArrayList<>();
if(gameData != null && gameData.getKeys().isDown(GameKeys.LEFT_CTRL))
Expand Down
Binary file modified src/main/resources/assets/main-char-down5.png