Skip to content

Commit

Permalink
Add flag support to Player class
Browse files Browse the repository at this point in the history
  • Loading branch information
kireiiiiiiii committed Jan 10, 2025
1 parent d6d51a9 commit 41a09ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions client/app/src/main/java/flaggi/ui/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public class Player implements Renderable {
////////////////

private int[] pos = new int[2];
private boolean visible = true, inverted = false;
private boolean visible = true, inverted = false, hasFlag = false;
private int id, health;
private String name, animationFrame, localPlayerSkinName;
private Sprite sprite;
private Sprite avatar, flag;

/////////////////
// Constructor
Expand All @@ -96,9 +96,13 @@ public Player(int[] pos, String name, String skinName, int id) {

// ---- Set animation
this.localPlayerSkinName = skinName;
this.sprite.setAnimation(localPlayerSkinName + "_idle");
this.sprite.setFps(2);
this.sprite.play();
this.avatar.setAnimation(localPlayerSkinName + "_idle");
this.avatar.setFps(2);
this.avatar.play();

this.flag.setAnimation("flag_blue");
this.flag.setFps(2);
this.flag.play();
}

/**
Expand All @@ -113,17 +117,21 @@ public Player(int[] pos, String name, String skinName, int id) {
public Player(int[] pos, String name, int id, String animationFrame) {

// ---- Set variables
this.sprite = new Sprite();
this.avatar = new Sprite();
this.flag = new Sprite();
this.flag.addAnimation(Arrays.asList("flag_blue"), "flag_blue");
this.flag.addAnimation(Arrays.asList("flag_red"), "flag_red");
this.flag.setAnimation("flag_red");
this.animationFrame = animationFrame;
this.pos = pos;
this.name = name;
this.id = id;

// ---- Initialize skins
if (playerAnimationsLibrary == null) {
addAllPlayerAnimations();
addAllAvatarAnimations();
}
this.sprite.setAnimations(playerAnimationsLibrary);
this.avatar.setAnimations(playerAnimationsLibrary);
}

/////////////////
Expand All @@ -134,7 +142,7 @@ public Player(int[] pos, String name, int id, String animationFrame) {
* Add all the downloaded sprite animations.
*
*/
private static void addAllPlayerAnimations() {
private static void addAllAvatarAnimations() {
playerAnimationsLibrary = new HashMap<>();
String[] skinTextures = FileUtil.listDirectoriesInJar("sprites/player");

Expand All @@ -161,17 +169,21 @@ private static void addAllPlayerAnimations() {
@Override
public void render(Graphics2D g, int[] size, int[] offset, Container focusCycleRootAncestor) {

if (this.hasFlag) {
this.flag.render(g, this.pos[0], this.pos[1], focusCycleRootAncestor, this.inverted);
}

// Render the player sprite
if (!isEnemy()) {
this.sprite.render(g, this.pos[0], this.pos[1], focusCycleRootAncestor, this.inverted);
this.avatar.render(g, this.pos[0], this.pos[1], focusCycleRootAncestor, this.inverted);
offset = new int[] { 0, 0 };

} else {
String[] parsedFrameData = parseAnimationFrame(this.animationFrame);
String animationName = parsedFrameData[0];
int animationFrame = Integer.parseInt(parsedFrameData[1]);
boolean inverted = Boolean.parseBoolean(parsedFrameData[2]);
this.sprite.render(g, this.pos[0] + offset[0], this.pos[1] + offset[1], focusCycleRootAncestor, animationName, animationFrame, inverted);
this.avatar.render(g, this.pos[0] + offset[0], this.pos[1] + offset[1], focusCycleRootAncestor, animationName, animationFrame, inverted);
}

// Render the nametag
Expand Down Expand Up @@ -289,7 +301,7 @@ public int getId() {
* "current-animation-name:current-frame".
*/
public String getAnimationFrame() {
return this.sprite.getAnimationFrame() + ":" + this.inverted;
return this.avatar.getAnimationFrame() + ":" + this.inverted;
}

/**
Expand All @@ -312,23 +324,31 @@ public void setFacingRight(boolean invert) {
this.inverted = invert;
}

public void hasFlag(boolean hasFlag) {
this.hasFlag = hasFlag;
}

public void setHasFlag(boolean hasFlag) {
this.hasFlag = hasFlag;
}

/**
* Switches the player animation.
*
* @param name - name of the player animation. (skin name not needed)
*/
public void switchAnimation(String name) {
String animationName = this.localPlayerSkinName + "_" + name;
if (this.sprite.getAnimationFrame().split(":")[0].equals(animationName)) {
if (this.avatar.getAnimationFrame().split(":")[0].equals(animationName)) {
return;
}
this.sprite.setAnimation(animationName);
this.avatar.setAnimation(animationName);
switch (name) {
case "idle":
this.sprite.setFps(2);
this.avatar.setFps(2);
break;
case "walk_side":
this.sprite.setFps(4);
this.avatar.setFps(4);
default:
break;
}
Expand Down
Binary file added client/app/src/main/resources/sprites/flag_blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/app/src/main/resources/sprites/flag_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 41a09ee

Please sign in to comment.