Created
June 17, 2019 01:59
-
-
Save SocketWeaver/80b3c8ace0d6eff97bd7fad8d4f210c3 to your computer and use it in GitHub Desktop.
Modify the Health.cs and the PlayerHealth.cs script to use the new "HP" SyncProperty instead of the protected currentHP variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using SWNetwork; | |
using UnityEngine; | |
public class Health : MonoBehaviour | |
{ | |
public int MaxHp = 100; | |
const string HP = "HP"; | |
public NetworkID networkID; | |
public SyncPropertyAgent syncPropertyAgent; | |
void Start() | |
{ | |
networkID = GetComponent<NetworkID>(); | |
syncPropertyAgent = GetComponent<SyncPropertyAgent>(); | |
} | |
public void GotHit(int damage) | |
{ | |
// update the HP SyncProperty when got hit | |
int currentHP = syncPropertyAgent.GetPropertyWithName(HP).GetIntValue(); | |
if (currentHP == 0) | |
{ | |
return; | |
} | |
currentHP = Mathf.Max(currentHP - damage, 0); | |
syncPropertyAgent.Modify(HP, currentHP); | |
} | |
public void OnHpChanged() | |
{ | |
// handles HP OnValueChange event | |
int hp = syncPropertyAgent.GetPropertyWithName(HP).GetIntValue(); | |
HPUpdated(hp); | |
} | |
public void OnHpConflict(SWSyncConflict conflict, SWSyncedProperty property) | |
{ | |
// handles HP OnConflict event | |
int remotePlayerHp = (int)conflict.remoteValue; | |
if (remotePlayerHp != 0) | |
{ | |
int oldLocalPlayerHp = (int)conflict.oldLocalValue; | |
int newLocalPlayerHp = (int)conflict.newLocalValue; | |
int damage = oldLocalPlayerHp - newLocalPlayerHp; | |
remotePlayerHp = remotePlayerHp - damage; | |
if (remotePlayerHp < 0) | |
{ | |
remotePlayerHp = 0; | |
} | |
} | |
property.Resolve(remotePlayerHp); | |
} | |
public void OnHpReady() | |
{ | |
// handles HP OnReady event | |
if (networkID.IsMine) | |
{ | |
int hp = syncPropertyAgent.GetPropertyWithName(HP).GetIntValue(); | |
int version = syncPropertyAgent.GetPropertyWithName(HP).version; | |
if(version == 0) | |
{ | |
syncPropertyAgent.Modify(HP, MaxHp); | |
} | |
else | |
{ | |
HPReady(hp); | |
} | |
} | |
} | |
public virtual void HPUpdated(int hp) { | |
} | |
public virtual void HPReady(int hp) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment