Skip to content

Commit

Permalink
fixed player hurt rendering between clients
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisj42 committed Jul 29, 2017
1 parent 8b71fec commit f26b3bd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
2 changes: 0 additions & 2 deletions Bugs and things to do.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ Note to self: for more little bugs/ideas/concerns, search code for "TODO"

####BUGS####
####
Player animation for getting hurt doesn't sync between clients.
####
Stairs clump together.
####
Fire particles appear, but... they look totally weird...
Expand Down
46 changes: 25 additions & 21 deletions src/minicraft/entity/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -849,28 +849,40 @@ protected void doHurt(int damage, int attackDir) {

if(Game.isValidServer() && this instanceof RemotePlayer) {
// let the clients deal with it.
Game.server.getAssociatedThread((RemotePlayer)this).sendPlayerHurt(damage, attackDir);
Game.server.broadcastPlayerHurt(eid, damage, attackDir);
return;
}

// apply the appropriate knockback
if (attackDir == 0) yKnockback = +6;
if (attackDir == 1) yKnockback = -6;
if (attackDir == 2) xKnockback = -6;
if (attackDir == 3) xKnockback = +6;
// set invulnerability time
hurtTime = playerHurtTime;

boolean fullPlayer = !(Game.isValidClient() && this != game.player);

int healthDam = 0, armorDam = 0;
Sound.playerHurt.play();
if (curArmor == null) { // no armor
health -= damage; // subtract that amount
} else { // has armor
armorDamageBuffer += damage;
armorDam += damage;

while (armorDamageBuffer >= curArmor.level+1) {
armorDamageBuffer -= curArmor.level+1;
healthDam++;
if(fullPlayer) {
Sound.playerHurt.play();
if (curArmor == null) { // no armor
health -= damage; // subtract that amount
} else { // has armor
armorDamageBuffer += damage;
armorDam += damage;

while (armorDamageBuffer >= curArmor.level+1) {
armorDamageBuffer -= curArmor.level+1;
healthDam++;
}
}
}

// adds a text particle telling how much damage was done to the player, and the armor.
if(armorDam > 0) {
level.add(new TextParticle("" + damage, x, y, Color.get(-1, 333)));
armor -= armorDam;
if(fullPlayer) armor -= armorDam;
if(armor <= 0) {
healthDam -= armor; // adds armor damage overflow to health damage (minus b/c armor would be negative)
armor = 0;
Expand All @@ -880,16 +892,8 @@ protected void doHurt(int damage, int attackDir) {
}
if(healthDam > 0) {
level.add(new TextParticle("" + damage, x, y, Color.get(-1, 504)));
health -= healthDam;
if(fullPlayer) health -= healthDam;
}

// apply the appropriate knockback
if (attackDir == 0) yKnockback = +6;
if (attackDir == 1) yKnockback = -6;
if (attackDir == 2) xKnockback = -6;
if (attackDir == 3) xKnockback = +6;
// set invulnerability time
hurtTime = playerHurtTime;
}

protected String getUpdateString() {
Expand Down
1 change: 1 addition & 0 deletions src/minicraft/entity/RemotePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void tick() {
attackTime--;
if(attackTime == 0) attackItem = null; // null the attackItem once we are done attacking.
}
if (hurtTime > 0) hurtTime--;
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/minicraft/network/MinicraftClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,12 @@ else if(!((RemotePlayer)game.player).shouldTrack(entity.x >> 4, entity.y >> 4))
case HURT:
// the player got attacked.
//if(Game.debug) System.out.println("CLIENT: recieved hurt packet");
int damage = Integer.parseInt(data[0]);
int attackDir = Integer.parseInt(data[1]);
game.player.hurt(damage, attackDir);
int hurteid = Integer.parseInt(data[0]);
int damage = Integer.parseInt(data[1]);
int attackDir = Integer.parseInt(data[2]);
Entity p = Game.getEntity(hurteid);
if (p instanceof Player)
((Player)p).hurt(damage, attackDir);
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions src/minicraft/network/MinicraftServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ public void broadcastNotification(String note, int notetime) {
broadcastData(InputType.NOTIFY, data);
}

public void broadcastPlayerHurt(int eid, int damage, int attackDir) {
for(MinicraftServerThread thread: getThreads())
thread.sendPlayerHurt(eid, damage, attackDir);
}

public void updateGameVars() { updateGameVars(getThreads()); }
public void updateGameVars(MinicraftServerThread sendTo) {
updateGameVars(new MinicraftServerThread[] {sendTo});
Expand Down
4 changes: 2 additions & 2 deletions src/minicraft/network/MinicraftServerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public void sendNotification(String note, int notetime) {
sendData(InputType.NOTIFY, notetime+";"+note);
}

public void sendPlayerHurt(int damage, int attackDir) {
sendData(InputType.HURT, damage+";"+attackDir);
public void sendPlayerHurt(int eid, int damage, int attackDir) {
sendData(InputType.HURT, eid+";"+damage+";"+attackDir);
}

public void updatePlayerActiveItem(Item heldItem) {
Expand Down

0 comments on commit f26b3bd

Please sign in to comment.