public Actor SpawnActor <T>(SlimMath.Vector3 position) where T : ActorDefinition { ActorDefinition definition; if (ActorDefinition.ByType(typeof(T), out definition)) { return(spawnActor(null, Player.ServerPlayerId, definition.Id, position)); } else { log.Warn("No actor definition of type {0} found", typeof(T)); return(null); } }
public Actor SpawnActor <T>(Player player, SlimMath.Vector3 position) where T : ActorDefinition { if (Verify.Authenticated(player)) { ActorDefinition definition; if (ActorDefinition.ByType(typeof(T), out definition)) { return(spawnActor(player, player.Id, definition.Id, position)); } else { log.Warn("No actor definition of type {0} found", typeof(T)); return(null); } } return(null); }
Context(Peer peer) { Assert.NotNull(peer, "peer"); Client = peer as Client; Server = peer as Server; IsServer = peer is Server; IsClient = peer is Client; Peer = peer; Peer.ContextPlugin.Context = this; Scheduler = new Scheduler(this); NetworkQueue = new HashSet <Network.IConnection>(); SynchronizationQueue = new HashSet <Synchronizable>(); // Time = new TimeManager(peer); // Stats = new Stats(this); // Register packet handlers RegisterPacketHandler(new SynchronizableHandler(), HeaderBytes.Synchronizable); RegisterPacketHandler(RPC = new RPCDispatcher(this), HeaderBytes.RemoteProcedureCall); RegisterPacketHandler(StateStreamHandler = new StateStreamHandler(), HeaderBytes.ActorStateStream); // Register player events registerEventsForHandler <Actor>(ActorEventHandler = new EventHandlerActor(this)); registerEventsForHandler <Player>(PlayerEventHandler = new EventHandlerPlayer(this)); // Create spatial partitioner SpatialPartitioner = createSpatialPartitioner(); // Plugin callback Peer.ContextPlugin.ContextStarted(); // Init actor definitions ActorDefinition.Init(this); }
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); }
public static bool ByType(Type type, out ActorDefinition definition) { return byType.TryGetValue(type, out definition); }
public static bool ById(int id, out ActorDefinition definition) { return byId.TryGetValue(id, out definition); }
public static bool ByType(Type type, out ActorDefinition definition) { return(byType.TryGetValue(type, out definition)); }
public static bool ById(int id, out ActorDefinition definition) { return(byId.TryGetValue(id, out definition)); }