Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created May 2, 2011 09:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngryAnt/951371 to your computer and use it in GitHub Desktop.
Save AngryAnt/951371 to your computer and use it in GitHub Desktop.
Player.cs
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (NetworkView))]
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (AnimationController))]
public class Player : MonoBehaviour, System.IComparable
{
public Transform graphicsPrefab;
private AnimationController animationController;
private Vector3 targetPosition, targetVelocity;
private Quaternion targetRotation;
private Color identityColor;
private int score, cargo;
void Start ()
{
animationController = GetComponent<AnimationController> ();
Transform graphics = (Transform)Instantiate (graphicsPrefab, transform.position, transform.rotation);
graphics.parent = transform;
graphics.localPosition = Vector3.zero;
graphics.localRotation = Quaternion.identity;
graphics.localScale = Vector3.one;
animationController.rollTarget = graphics;
if (!networkView.isMine)
{
Debug.Log (transform.root.gameObject.name + " is handled as a remote player");
Destroy (gameObject.GetComponent<PlayerController> ());
Destroy (graphics.GetComponentInChildren<Camera> ().gameObject);
rigidbody.isKinematic = true;
}
else
{
Debug.Log (transform.root.gameObject.name + " is handled as a local player");
}
networkView.observed = this;
targetPosition = transform.position;
targetVelocity = rigidbody.velocity;
targetRotation = transform.rotation;
identityColor = Color.white;
score = cargo = 0;
}
[RPC]
void Initialize (string name, float colorR, float colorG, float colorB)
{
gameObject.name = name;
identityColor = new Color (colorR, colorG, colorB, 1.0f);
}
[RPC]
void SetCargo (int newCargo)
{
cargo = newCargo;
}
[RPC]
void SetScore (int newScore)
{
score = newScore;
}
public string Name
{
get
{
return gameObject.name;
}
}
public Color Color
{
get
{
return identityColor;
}
}
public Vector3 Velocity
{
get
{
return networkView.isMine ? rigidbody.velocity : targetVelocity;
}
}
public int Score
{
get
{
return score;
}
}
public int Cargo
{
get
{
return cargo;
}
}
float kCorrectionSpeed = 10.0f;
void Update ()
{
targetPosition += targetVelocity * Time.deltaTime;
if (!networkView.isMine)
{
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * kCorrectionSpeed);
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * kCorrectionSpeed);
}
}
void LateUpdate ()
{
transform.localScale = Vector3.one;
}
void OnSerializeNetworkView (BitStream stream, NetworkMessageInfo info)
{
targetPosition = transform.position;
stream.Serialize (ref targetPosition);
targetVelocity = rigidbody.velocity;
stream.Serialize (ref targetVelocity);
targetRotation = transform.rotation;
stream.Serialize (ref targetRotation);
// TODO: Apply velocity to target position over the time it took to send the package
}
public int CompareTo (System.Object other)
{
Player player = other as Player;
if (player == null)
{
throw new System.ArgumentException ("Non-player or null comparison");
}
return gameObject.name.CompareTo (player.gameObject.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment