public static void DestroyNetworkedObject(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source) { MyClientBehaviour client = caller as MyClientBehaviour; uint networkId = stream.ReadUInt(ref context); NetworkId.DestroyNetworked(networkId, ref client.repository); }
public static void PlayerShoot(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source) { MyClientBehaviour client = caller as MyClientBehaviour; //playerId and bullet id uint playerId = stream.ReadUInt(ref context); uint networkId = stream.ReadUInt(ref context); //position float posX = stream.ReadFloat(ref context); float posY = stream.ReadFloat(ref context); float posZ = stream.ReadFloat(ref context); //rotation float rotX = stream.ReadFloat(ref context); float rotY = stream.ReadFloat(ref context); float rotZ = stream.ReadFloat(ref context); //spawn a bullet with the received networkId and position/direction GameObject bullet; uint id = NetworkId.Spawn(Resources.Load("ClientBullet"), ref client.repository, networkId); if (NetworkId.Find(id, out bullet, ref client.repository)) { //set playerId on bullet script, set position and direction of player that's shooting bullet.transform.rotation = Quaternion.Euler(rotX, rotY, rotZ); bullet.transform.position = new Vector3(posX, posY, posZ); } }
public static void PlayerUpdate(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source) { //Debug.Log("Got Player Update from server"); MyClientBehaviour client = caller as MyClientBehaviour; uint numPlayers = stream.ReadUInt(ref context); for (int i = 0; i < numPlayers; ++i) { uint playerIndex = stream.ReadUInt(ref context); float posX = stream.ReadFloat(ref context); float posY = stream.ReadFloat(ref context); float posZ = stream.ReadFloat(ref context); float rotX = stream.ReadFloat(ref context); float rotY = stream.ReadFloat(ref context); float rotZ = stream.ReadFloat(ref context); ClientUpdate clientUpdate; clientUpdate.position.x = posX; clientUpdate.position.y = posY; clientUpdate.position.z = posZ; clientUpdate.euler.x = rotX; clientUpdate.euler.y = rotY; clientUpdate.euler.z = rotZ; client.ApplyClientUpdate(clientUpdate, playerIndex); } }
public static void PlayerJoined(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source) { //got remote player joined from server MyClientBehaviour client = caller as MyClientBehaviour; uint index = stream.ReadUInt(ref context); client.CreateRemoteClientForIndex(index); }
public static void AssignPlayerIndex(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source) { //got player index from server MyClientBehaviour client = caller as MyClientBehaviour; uint index = stream.ReadUInt(ref context); Debug.Log("Assigned player index " + index + " to my client instance"); //TODO: Make this a function? client.playerIndex = index; client.name = "Client" + index; }
public void CreateRemoteClientForIndex(uint index) { Debug.Log("CreateRemoteClientForIndex " + index); //instantiate remoteClient prefab for index if not already existing if (remoteClients.ContainsKey(index)) { Debug.Log("Client Index already exists! " + index); return; } Object prefab = Resources.Load("RemoteClient"); GameObject clientObject = Instantiate(prefab) as GameObject; clientObject.name = "RemoteClient" + index; MyClientBehaviour remoteClient = clientObject.GetComponent <MyClientBehaviour>(); remoteClient.playerIndex = index; remoteClients[index] = remoteClient; }
public static void Ping(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source) { MyClientBehaviour client = caller as MyClientBehaviour; //TODO: get network time, correct offsets, send pong }
private void Start() { clientBehaviour = this.gameObject.GetComponent <MyClientBehaviour>(); this.Initialize(); this.StartAsClient(); }