Created
May 28, 2013 10:08
-
-
Save AngryAnt/5661770 to your computer and use it in GitHub Desktop.
Example of a server-side managed pickup base behaviour for Unity networking.
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 UnityEngine; | |
using System.Collections; | |
[RequireComponent (typeof (Collider))] | |
[RequireComponent (typeof (NetworkView))] | |
public abstract class Pickup : MonoBehaviour | |
{ | |
bool available; | |
protected bool Available | |
{ | |
get | |
{ | |
return available; | |
} | |
set | |
{ | |
if (value == available) | |
{ | |
return; | |
} | |
networkView.RPC ("SetAvailable", RPCMode.Others, value); | |
SetAvailable (value); | |
} | |
} | |
[RPC] | |
void SetAvailable (bool value) | |
{ | |
available = value; | |
OnFlipAvailable (); | |
} | |
[RPC] | |
void GetState (NetworkMessageInfo messageInfo) | |
{ | |
networkView.RPC ("SetAvailable", messageInfo.sender, available); | |
} | |
protected abstract bool OnPickup (Player player); | |
protected virtual void OnFlipAvailable () | |
{} | |
void Reset () | |
{ | |
networkView.stateSynchronization = NetworkStateSynchronization.None; | |
networkView.observed = this; | |
collider.isTrigger = true; | |
} | |
void Start () | |
{ | |
if (!networkView.isMine) | |
{ | |
networkView.RPC ("GetState", networkView.owner); | |
} | |
} | |
void OnTriggerEnter (Collider other) | |
{ | |
if (!available || !networkView.isMine) | |
{ | |
return; | |
} | |
Player player = other.transform.root.GetComponentInChildren<Player> (); | |
if (player != null && OnPickup (player)) | |
{ | |
Available = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment