public
Last active

Player.cs

  • Download Gist
Player.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.