void SpawnEntityClient( Entity.Spawn spawn ) { Entity entity = Entity.SpawnEntity( _commonSettings.entityPrefab, spawn, _localIndex ); _entity.Add( entity.GetIndex(), entity ); if( _onEntitySpawn != null ) { _onEntitySpawn.Invoke( entity ); } }
public Entity SpawnEntityServer( NodeIndex owner, EntityPrefabIndex prefabIndex, Vector3 position, Quaternion rotation, out string error ) { if( owner != NodeIndex.SERVER_NODE_INDEX ) { ClientConnection client = _connection.Get( owner ); if( client == null ) { error = $"Cannot spawn entity with ownerClientId {owner.GetClientIndex()}, client not yet connected!"; return null; } } // Generate unique network id uint unique_id = _entityCounter; ++_entityCounter; EntityIndex entityIndex = new EntityIndex( unique_id ); // spawn Entity.Data data = new Entity.Data() { id = entityIndex, owner = owner, prefabIndex = prefabIndex }; Entity.Spawn spawn = new Entity.Spawn( data, position, rotation ); Entity entity = Entity.SpawnEntity( _commonSettings.entityPrefab, spawn, _localIndex ); _entity.Add( entityIndex, entity ); // notify all clients that an entity was created for( int i = 0; i < _connection.GetCount(); ++i ) { string clientSendError; ClientConnection client = _connection.GetAt(i); using( BitWriter writer = GetPooledWriter() ) { spawn.Write( writer ); if( !SendInternal( client.GetId(), InternalMessage.EntityCreate, InternalChannel.Reliable, writer, false, out clientSendError ) ) { error = $"Failed to send spawn to client {i}, error is:\n{clientSendError}\n"; return null; } } } if( _onEntitySpawn != null ) { _onEntitySpawn.Invoke( entity ); } error = null; return entity; }
void OnMessageConnectionApproved( BitReader reader ) { int ourIndex = reader.Packed<Int32>(); _localIndex = new NodeIndex( (uint)ourIndex ); _networkTime = reader.Packed<float>(); // TODO: cozeroff // not sure what we do with the network timestamp for now int networkTimestamp = reader.Packed<Int32>(); // spawn entities already existing in the world int entityCount = reader.Packed<Int32>(); Entity.Spawn s = new Entity.Spawn(); for( int i = 0; i < entityCount; ++i ) { s.Read( reader ); SpawnEntityClient( s ); } _status = Status.Connected; }
void OnEntityCreate( BitReader reader ) { Entity.Spawn spawn = new Entity.Spawn(); spawn.Read( reader ); SpawnEntityClient( spawn ); }