Actor spawnActor(Player owner, ushort playerId, int prefabId, SlimMath.Vector3 position) { Actor actor; ushort actorId; bool hasOwner = owner != null; if (!actorIdPool.Acquire(out actorId)) { log.Error("Can't spawn actor, failed to acquire a new actor id"); return(null); } if (hasOwner && owner.Connection == null) { log.Error("Owner supplied, but it has no connection object"); return(null); } Network.IConnection connection = hasOwner ? owner.Connection : null; if (Context.InstantiateActor(connection, prefabId, actorId, playerId, position, out actor)) { if (hasOwner) { owner.OwnedActors.Add(actor); } return(actor); } return(null); }
public override void OnDisconnected(Network.IConnection connection) { if (connection != null && connection.Player != null) { log.Info("Disconnected {0}", connection.Player); playerIdPool.Release(connection.Player.Id); Context.RemovePlayer(connection.Player); } }
public override void OnConnected(Network.IConnection connection) { Context.Time.Start(); connection.Player = Player = Context.CreatePlayer(0, connection); log.Info("Connected"); if (OnClientConnected != null) { OnClientConnected(this); } }
public override void OnConnected(Network.IConnection connection) { Assert.NotNull(connection, "connection"); ushort playerId; if (playerIdPool.Acquire(out playerId)) { log.Info("Connected {0}", Context.CreatePlayer(playerId, connection)); } else { connection.Disconnect(); } }
internal Player(Context context, Network.IConnection connection, ushort playerId) { Assert.NotNull(context, "context"); Assert.NotNull(connection, "connection"); Context = context; Id = playerId; Connection = connection; Connection.Player = this; OwnedActors = new HashSet <Actor>(); SubscribedTo = new HashSet <Actor>(); Spawned = new HashSet <Actor>(); ActorProximityLevels = new ProximityLevel[UInt16.MaxValue]; Stats = new Stats(context); }
internal Player CreatePlayer(ushort playerId, Network.IConnection connection) { Assert.NotNull(connection, "connection"); if (players.ContainsKey(playerId)) { log.Error("A player with id #{0} already exists", playerId); return(null); } Player player = new Player(this, connection, playerId); players.Add(playerId, player); Peer.PlayerJoined(player); Peer.ContextPlugin.PlayerJoined(player); return(player); }
internal bool InstantiateActor(Network.IConnection connection, int prefabId, ushort actorId, ushort playerId, Vector3 position, out Actor instance) { ActorDefinition definition; // On client, verify we always have a connection if (IsClient) { Assert.NotNull(connection, "connection"); } // On server, if the connection is null, playerId MUST equal Player.ServerPlayerId if (IsServer && connection == null && playerId != Player.ServerPlayerId) { log.Error("Connection was null and playerId was not Player.ServerPlayerId"); instance = null; return(false); } // Make sure this id is not in use if (actors.ContainsKey(actorId)) { log.Error("An actor with id #{0} already exists", actorId); instance = null; return(false); } if (!ActorDefinition.ById(prefabId, out definition)) { log.Error("Could not find actor definition with id #{0}", prefabId); instance = null; return(false); } // Create instance instance = Peer.CreateActor(); // Set public properties instance.Role = Peer.ResolveActorRole(playerId); instance.Id = actorId; instance.PlayerId = playerId; instance.Connection = connection; instance.Transform.Position = position; instance.Context = this; instance.Definition = definition; instance.StateStreamer = definition.StateStreamer; // Some actors dont have a state streamer if (instance.StateStreamer != null) { instance.StateStreamer.Actor = instance; } // Copy definition valeus instance.Name = definition.Name; instance.StateStreamRate = definition.StateStreamUpdateRate; instance.TransformSource = definition.TransformSource; instance.SimulationOffset = definition.SimulationOffset; instance.InitBehaviours(definition.Behaviours); instance.Synchronizable.Init(definition.SynchronizedValues); // Only copy collider if we have a spatial partitioner if (HasSpatialPartitioner) { instance.Collider = definition.Collider; // Of we got a collider from the definition if (instance.HasCollider) { // Set actor on collider instance.Collider.Actor = instance; // Update collider (null safe) instance.UpdateCollider(); // Insert into spatial partitioner SpatialPartitioner.Insert(instance.Collider); } } // Add actor to actor index actors.Add(instance.Id, instance); // Allow the peer to modify the actor before it's started Peer.BeforeActorStart(instance); // Start actor instance.InternalStart(); // Call plugin Peer.ContextPlugin.ActorSpawned(instance); // Log log.Info("Instantiated {0} at {1}", instance, position); return(true); }
internal void InstantiateActor(Network.IConnection connection, int prefabId, ushort actorId, ushort playerId, Vector3 position) { Actor actor; InstantiateActor(connection, prefabId, actorId, playerId, position, out actor); }
public override void OnDisconnected(Network.IConnection connection) { disconnect(false); }