/// <summary>Within scope</summary> /// <param name="buffer">King Buffer</param> private void WithinScope(KingBuffer buffer) { // Sync id var syncId = buffer.ReadShort(); // Has synchronizer if (_syncsById.ContainsKey(syncId)) { return; } // Spawn sync var KingSync = Spawn( buffer.ReadString(), buffer.ReadVector3(), buffer.ReadQuaternion(), syncId ); // Invoke on reading KingSync.InvokeOnReading(buffer); // Spawning OnSpawning.Invoke(KingSync); }
/// <summary>Out of scope</summary> /// <param name="buffer">King Buffer</param> private void OutOfScope(KingBuffer buffer) { // Sync id var syncId = buffer.ReadShort(); // No synchronizer if (!_syncsById.ContainsKey(syncId)) { return; } // Unspawning OnUnpawning.Invoke(_syncsById[syncId]); // Unspawn Unspawn(syncId); }
/// <summary>Within scope</summary> /// <param name="sync">King Sync</param> internal void WithinScope(KingSync sync) { // Ignore if there is no owner if (Owner == null) { return; } // Ignore if in scope if (_syncsById.ContainsKey(sync.SyncId)) { return; } // Add to indexed sync _syncsById.Add(sync.SyncId, sync); // WITHIN SCOPE var buffer = new KingBuffer(); buffer.WriteMessagePacket(KingPacket.WithinScope); // Write node identifier // Owner always receives ID 0 if (sync == this) { buffer.WriteShort(OWNER_ID); } else { buffer.WriteShort(sync.SyncId); } // Write prefab buffer.WriteString(Prefab); // Write full data InvokeOnWriting(buffer, true); // Send Owner.Send(buffer); }
/// <summary>Sync update</summary> /// <param name="buffer">King buffer</param> public void SyncUpdate(KingBuffer buffer) { var packet = buffer.ReadMessagePacket <KingPacket>(); // OUT OF SCOPE if (packet == KingPacket.OutOfScope) { OutOfScope(buffer); } // WITHIN SCOPE if (packet == KingPacket.WithinScope) { WithinScope(buffer); } // SYNC SCENE if (packet == KingPacket.SyncScene) { // As long as you can read while (buffer.CanRead()) { // sync id var syncId = buffer.ReadShort(); // Has synchronizer if (!_syncsById.ContainsKey(syncId)) { break; } // Catching _syncsById[syncId].Catching(buffer); } } }
/// <summary>Out of scope</summary> /// <param name="sync">King Sync</param> internal void OutOfScope(KingSync sync) { // Ignore if there is no owner if (Owner == null) { return; } // Ignore if not in scope if (!_syncsById.ContainsKey(sync.SyncId)) { return; } // Remove from indexed sync _syncsById.Remove(sync.SyncId); // OUT OF SCOPE var buffer = new KingBuffer();; buffer.WriteMessagePacket(KingPacket.OutOfScope); // Write node identifier // Owner always receives ID 0 if (sync == this) { buffer.WriteShort(OWNER_ID); } else { buffer.WriteShort(sync.SyncId); } // Send Owner.Send(buffer); }
/// <summary> /// Unity UPDATE /// </summary> void Update() { // Do nothing if there is no spawn if (!HasSpawn) { return; } // Do nothing until the next synchronization if (SyncTime > Time.realtimeSinceStartup) { return; } SyncTime = Time.realtimeSinceStartup + (1f / SyncRate); // Synchronize and update scope for (int i = 0; i < _syncs.Count; i++) { // Updates the scope for (int j = i; j < _syncs.Count; j++) { // Distance between two syncs var dist = Vector3.Distance( _syncs[i].transform.position, _syncs[j].transform.position ); // Within the scope area if (dist <= ScopeRadius) { // Ignores if neither one has an owner if ( _syncs[i].Owner == null && _syncs[j].Owner == null ) { continue; } // Within scope _syncs[i].WithinScope(_syncs[j]); _syncs[j].WithinScope(_syncs[i]); } // Outside the scope area else { // Ignore if not out of range // Reach here is a bit higher to avoid a sudden return if (dist <= (ScopeRadius + 50)) { continue; } // Out of scope _syncs[i].OutOfScope(_syncs[j]); _syncs[j].OutOfScope(_syncs[i]); } } // Ignore if you do not have an owner if (_syncs[i].Owner == null) { continue; } // SYNC SCENE var buffer = new KingBuffer(); buffer.WriteMessagePacket(KingPacket.SyncScene); // Putting to buffer _syncs[i].Putting(buffer); // Send to owner _syncs[i].Owner.Send(buffer); } }