void Instantiate(NetStream stream) { if (stream.ReadBool()) Die(); if (!gameObject.activeSelf) gameObject.SetActive(true); float posX = stream.ReadFloat(); float posZ = stream.ReadFloat(); transform.position = new Vector3(posX, 0, posZ); }
void ReadSync(NetStream syncStream) { Vector3 position = new Vector3(syncStream.ReadFloat(), transform.position.y, syncStream.ReadFloat()); Vector2 velocity = syncStream.ReadVector2(); lastPos = position; lastVel = new Vector3(velocity.x, 0, velocity.y); if (Time.time - lastTime > 1.2) { if (Vector3.Distance(transform.position, lastPos) > 2f) { transform.position = lastPos; } } lastTime = Time.time; positionDiff = transform.position - position; }
void ReadSync(NetStream syncStream) { Vector3 position = syncStream.ReadVector3(); float yRot = syncStream.ReadFloat(); Vector2 velocity = syncStream.ReadVector2(); lastTime = 0f; lastPos = position; lastRot = Quaternion.Euler(0, yRot, 0); lastVel = new Vector3(velocity.x, 0, velocity.y); if (teleport) { if (Vector3.Distance(transform.position, lastPos) > 2f) { transform.position = lastPos; transform.rotation = lastRot; } teleport = false; } positionDiff = transform.position - position; }
private static object ReadParam(NetStream stream, Type type) { if (type == typeof(bool)) { return(stream.ReadBool()); } if (type == typeof(byte)) { return(stream.ReadByte()); } if (type == typeof(short)) { return(stream.ReadShort()); } if (type == typeof(ushort)) { return(stream.ReadUShort()); } if (type == typeof(int)) { return(stream.ReadInt()); } if (type == typeof(uint)) { return(stream.ReadUInt()); } if (type == typeof(float)) { return(stream.ReadFloat()); } if (type == typeof(long)) { return(stream.ReadLong()); } if (type == typeof(ulong)) { return(stream.ReadULong()); } if (type == typeof(double)) { return(stream.ReadDouble()); } if (type == typeof(string)) { return(stream.ReadString()); } if (type == typeof(Vector2)) { return(stream.ReadVector2()); } if (type == typeof(Vector3)) { return(stream.ReadVector3()); } if (type == typeof(Quaternion)) { return(stream.ReadQuaternion()); } if (type == typeof(bool[])) { ushort length = stream.ReadUShort(); bool[] arr = new bool[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadBool(); } return(arr); } if (type == typeof(byte[])) { ushort length = stream.ReadUShort(); byte[] arr = new byte[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadByte(); } return(arr); } if (type == typeof(ushort[])) { ushort length = stream.ReadUShort(); ushort[] arr = new ushort[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadUShort(); } return(arr); } if (type == typeof(int[])) { ushort length = stream.ReadUShort(); int[] arr = new int[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadInt(); } return(arr); } if (type == typeof(uint[])) { ushort length = stream.ReadUShort(); uint[] arr = new uint[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadUInt(); } return(arr); } if (type == typeof(float[])) { ushort length = stream.ReadUShort(); float[] arr = new float[length]; for (int i = 0; i < length; i++) { arr[i] = stream.ReadFloat(); } return(arr); } if (type == typeof(string[])) { ushort length = stream.ReadUShort(); string[] arr = new string[length]; for (int i = 0; i < arr.Length; i++) { arr[i] = stream.ReadString(); } return(arr); } if (type == typeof(char[])) { ushort length = stream.ReadUShort(); char[] arr = new char[length]; for (int i = 0; i < arr.Length; i++) { arr[i] = stream.ReadChar(); } return(arr); } if (type == typeof(NetMessage)) { return(ReadNetMessage(stream)); } if (type == typeof(NetMessage[])) { byte length = stream.ReadByte(); NetMessage[] arr = new NetMessage[length]; for (int i = 0; i < length; i++) { arr[i] = ReadNetMessage(stream); } return(arr); } if (type == typeof(NetConnection)) { return(stream.Connection); } if (type == typeof(IPAddress)) { byte[] array = new byte[4]; stream.ReadByteArray(array); return(new IPAddress(array)); } if (type == typeof(IPEndPoint)) { if (!stream.ReadBool()) { return(null); } return(new IPEndPoint((IPAddress)ReadParam(stream, typeof(IPAddress)), stream.ReadUShort())); } if (type == typeof(NetStream)) { return(stream.ReadNetStream()); } if (type == typeof(NetZone)) { return(new NetZone { Id = stream.ReadUInt(), ServerEndpoint = stream.ReadBool() ? (IPEndPoint)ReadParam(stream, typeof(IPEndPoint)) : null, Position = stream.ReadVector3(), ViewIdMin = stream.ReadInt(), ViewIdMax = stream.ReadInt() }); } if (HasType(type)) { return(Read(stream, type)); } // We don't know how to deserialize this type throw new Exception("Deserializer not implemented for type: " + type); }