Пример #1
0
        GameObject IPrefabPool.Instantiate(PrefabId prefabId, Vector3 position, Quaternion rotation)
        {
            GameObject go;

            go =
                (GameObject)GameObject.Instantiate(((IPrefabPool)this).LoadPrefab(prefabId), position, rotation);
            go.GetComponent <AscensionEntity>().enabled = true;

            return(go);
        }
Пример #2
0
        public static GameObject Find(PrefabId id)
        {
            if (lookup == null || instance == null)
            {
                LoadInstance();
                UpdateLookup();
            }

            GameObject prefab;

            if (lookup.TryGetValue(id, out prefab))
            {
                return(prefab);
            }
            else
            {
                NetLog.Error("Could not find game object for {0}", id);
                return(null);
            }
        }
Пример #3
0
        public static Entity CreateFor(GameObject instance, PrefabId prefabId, TypeId serializerId, EntityFlags flags)
        {
            Entity eo;

            eo                                 = new Entity();
            eo.UnityObject                     = instance.GetComponent <AscensionEntity>();
            eo.UpdateRate                      = eo.UnityObject.updateRate;
            eo.AutoFreezeProxyFrames           = eo.UnityObject.autoFreezeProxyFrames;
            eo.AllowFirstReplicationWhenFrozen = eo.UnityObject.allowFirstReplicationWhenFrozen;
            eo.AutoRemoveChildEntities         = eo.UnityObject.autoRemoveChildEntities;
            eo.PrefabId                        = prefabId;
            eo.Flags                           = flags;

            if (prefabId.Value == 0)
            {
                eo.Flags  |= EntityFlags.SCENE_OBJECT;
                eo.SceneId = eo.UnityObject.SceneGuid;
            }

            if (eo.UnityObject.persistThroughSceneLoads)
            {
                eo.Flags |= EntityFlags.PERSIST_ON_LOAD;
            }
            if (eo.UnityObject.clientPredicted)
            {
                eo.Flags |= EntityFlags.CONTROLLER_LOCAL_PREDICTION;
            }

            // create serializer
            eo.Serializer = Factory.NewSerializer(serializerId);
            eo.Serializer.OnCreated(eo);


            // done
            return(eo);
        }
Пример #4
0
        private bool ReadUpdate(Packet packet)
        {
            if (packet.ReadBool() == false)
            {
                return(false);
            }
            // grab networkid
            NetworkId     networkId        = packet.ReadNetworkId();
            bool          isController     = packet.ReadBool();
            IMessageRider controlToken     = packet.ReadToken();
            bool          destroyRequested = packet.ReadBool();

            // we're destroying this proxy
            if (destroyRequested)
            {
                EntityProxy   proxy;
                IMessageRider detachToken = packet.ReadToken();

                if (_incommingDict.TryGetValue(networkId, out proxy))
                {
                    if (proxy.Entity.HasControl)
                    {
                        proxy.Entity.ReleaseControlInternal(controlToken);
                    }

                    DestroyIncommingProxy(proxy, detachToken);
                }
                else
                {
                    NetLog.Warn("Received destroy of {0} but no such proxy was found", networkId);
                }
            }
            else
            {
                IMessageRider attachToken = null;

                bool isSceneObject   = false;
                bool createRequested = packet.ReadBool();

                UniqueId   sceneId       = UniqueId.None;
                PrefabId   prefabId      = new PrefabId();
                TypeId     serializerId  = new TypeId();
                Vector3    spawnPosition = new Vector3();
                Quaternion spawnRotation = new Quaternion();

                if (createRequested)
                {
                    attachToken = packet.ReadToken();

                    prefabId      = packet.ReadPrefabId();
                    serializerId  = packet.ReadTypeId();
                    spawnPosition = packet.ReadVector3();
                    spawnRotation = packet.ReadQuaternion();
                    isSceneObject = packet.ReadBool();

                    if (isSceneObject)
                    {
                        sceneId = packet.ReadUniqueId();
                    }
                }

                Entity      entity = null;
                EntityProxy proxy  = null;

                if (createRequested && (_incommingDict.ContainsKey(networkId) == false))
                {
                    // create entity
                    if (isSceneObject)
                    {
                        GameObject go = Core.FindSceneObject(sceneId);

                        if (!go)
                        {
                            NetLog.Warn("Could not find scene object with {0}", sceneId);
                            go = Core.PrefabPool.Instantiate(prefabId, spawnPosition, spawnRotation);
                        }

                        entity = Entity.CreateFor(go, prefabId, serializerId, EntityFlags.SCENE_OBJECT);
                    }
                    else
                    {
                        GameObject go = Core.PrefabPool.LoadPrefab(prefabId);

                        // prefab checks (if applicable)
                        if (go)
                        {
                            if (Core.IsServer && !go.GetComponent <AscensionEntity>().allowInstantiateOnClient)
                            {
                                throw new AscensionException(
                                          "Received entity of prefab {0} from client at {1}, but this entity is not allowed to be instantiated from clients",
                                          go.name, connection.RemoteEndPoint);
                            }
                        }

                        NetLog.Warn("Creating instance of {0}", prefabId);
                        entity = Entity.CreateFor(prefabId, serializerId, spawnPosition, spawnRotation);
                    }

                    entity.Source    = connection;
                    entity.SceneId   = sceneId;
                    entity.NetworkId = networkId;

                    // handle case where we are given control (it needs to be true during the initialize, read and attached callbacks)
                    if (isController)
                    {
                        entity.Flags |= EntityFlags.HAS_CONTROL;
                    }

                    // initialize entity
                    entity.Initialize();

                    // create proxy
                    proxy            = entity.CreateProxy();
                    proxy.NetworkId  = networkId;
                    proxy.Connection = connection;

                    // register proxy
                    _incommingDict.Add(proxy.NetworkId, proxy);

                    // read packet
                    entity.Serializer.Read(connection, packet, packet.Frame);

                    // attach entity
                    proxy.Entity.AttachToken = attachToken;
                    proxy.Entity.Attach();

                    // assign control properly
                    if (isController)
                    {
                        proxy.Entity.Flags &= ~EntityFlags.HAS_CONTROL;
                        proxy.Entity.TakeControlInternal(controlToken);
                    }

                    // log debug info
                    NetLog.Debug("Received {0} from {1}", entity, connection);

                    // update last received frame
                    proxy.Entity.LastFrameReceived = AscensionNetwork.Frame;
                    proxy.Entity.Freeze(false);

                    // notify user
                    GlobalEventListenerBase.EntityReceivedInvoke(proxy.Entity.UnityObject);
                }
                else
                {
                    // find proxy
                    proxy = _incommingDict[networkId];

                    if (proxy == null)
                    {
                        throw new AscensionException("Couldn't find entity for {0}", networkId);
                    }

                    // update control state yes/no
                    if (proxy.Entity.HasControl ^ isController)
                    {
                        if (isController)
                        {
                            proxy.Entity.TakeControlInternal(controlToken);
                        }
                        else
                        {
                            proxy.Entity.ReleaseControlInternal(controlToken);
                        }
                    }

                    // read update
                    proxy.Entity.Serializer.Read(connection, packet, packet.Frame);
                    proxy.Entity.LastFrameReceived = AscensionNetwork.Frame;
                    proxy.Entity.Freeze(false);
                }
            }

            return(true);
        }
Пример #5
0
 public static void WritePrefabId(this BasePacket stream, PrefabId id)
 {
     stream.WriteIntVB(id.Value);
 }
Пример #6
0
 /// <summary>
 /// Create a new entity in the simuation from a prefab
 /// </summary>
 public static AscensionEntity Instantiate(PrefabId prefabId, IMessageRider token, Vector3 position, Quaternion rotation)
 {
     VerifyIsRunning();
     return(Instantiate(Core.PrefabPool.LoadPrefab(prefabId), token, position, rotation));
 }
Пример #7
0
 /// <summary>
 /// Create a new entity in the simuation from a prefab
 /// </summary>
 public static AscensionEntity Instantiate(PrefabId prefabId, Vector3 position, Quaternion rotation)
 {
     VerifyIsRunning();
     return(Instantiate(prefabId, null, position, rotation));
 }
Пример #8
0
 /// <summary>
 /// Create a new entity in the simuation from a prefab
 /// </summary>
 public static AscensionEntity Instantiate(PrefabId prefabId, IMessageRider token)
 {
     VerifyIsRunning();
     return(Instantiate(prefabId, token, Vector3.zero, Quaternion.identity));
 }
Пример #9
0
 /// <summary>
 /// Create a new entity in the simuation from a prefab
 /// </summary>
 public static AscensionEntity Instantiate(PrefabId prefabId)
 {
     VerifyIsRunning();
     return(Instantiate(prefabId, null, Vector3.zero, Quaternion.identity));
 }
Пример #10
0
 public static Entity CreateFor(GameObject instance, PrefabId prefabId, TypeId serializerId)
 {
     return(CreateFor(instance, prefabId, serializerId, EntityFlags.ZERO));
 }
Пример #11
0
 public static Entity CreateFor(PrefabId prefabId, TypeId serializerId, Vector3 position, Quaternion rotation)
 {
     return(CreateFor(Core.PrefabPool.Instantiate(prefabId, position, rotation), prefabId, serializerId));
 }
Пример #12
0
 public static bool Diff(PrefabId a, PrefabId b)
 {
     return(a != b);
 }
Пример #13
0
 GameObject IPrefabPool.LoadPrefab(PrefabId prefabId)
 {
     return(PrefabDatabase.Find(prefabId));
 }