Пример #1
0
 /// <summary>
 /// Spawns the cloth with the specifeid cloth data on the server, syncing it to clients
 /// </summary>
 /// <param name="clothData">cloth data describing the cloth, should be a subtype of BaseClothData</param>
 /// <param name="worldPosition">world position to appear at. Defaults to HiddenPos (hidden / invisible)</param>
 /// <param name="CVT">variant type to spawn this cloth as, defaults to Default</param>
 /// <param name="variantIndex">variant index to spawn this cloth as, defaults to -1</param>
 /// <param name="prefabOverride">prefab to use instead of this cloth's default</param>
 /// <param name="rotation">rotation to spawn with, defaults to Quaternion.identity</param>
 /// <param name="parent">Parent to spawn under, defaults to no parent. Most things
 /// should always be spawned under the Objects transform in their matrix. Many objects (due to RegisterTile)
 /// usually take care of properly parenting themselves when spawned so in many cases you can leave it null.</param>
 /// <param name="count">number of instances to spawn, defaults to 1</param>
 /// <param name="scatterRadius">radius to scatter the spawned instances by from their spawn position. Defaults to
 /// null (no scatter).</param>
 /// <returns></returns>
 public static SpawnResult ServerCloth(BaseClothData clothData, Vector3?worldPosition = null,
                                       ClothingVariantType CVT = ClothingVariantType.Default, int variantIndex = -1, GameObject prefabOverride = null,
                                       Transform parent        = null, Quaternion?rotation = null,
                                       int count = 1, float?scatterRadius                  = null)
 {
     return(Server(
                SpawnInfo.Cloth(clothData, worldPosition, CVT, variantIndex, prefabOverride, parent, rotation, count, scatterRadius)));
 }
Пример #2
0
    /// <summary>
    /// FOR DEV / TESTING ONLY! Simulates destroying and recreating an item by putting it in the pool and taking it back
    /// out again. If item is not pooled, simply destroys and recreates it as if calling Despawn and then Spawn
    /// Can use this to validate that the object correctly re-initializes itself after spawning -
    /// no state should be left over from its previous incarnation.
    /// </summary>
    /// <returns>the re-created object</returns>
    public static GameObject ServerPoolTestRespawn(GameObject target)
    {
        var poolPrefabTracker = target.GetComponent <PoolPrefabTracker>();

        if (poolPrefabTracker == null)
        {
            //destroy / create using normal approach with no pooling
            Logger.LogWarningFormat("Object {0} has no pool prefab tracker, thus cannot be pooled. It will be destroyed / created" +
                                    " without going through the pool.", Category.ItemSpawn, target.name);

            //determine prefab
            var position = target.TileWorldPosition();
            var prefab   = DeterminePrefab(target);
            if (prefab == null)
            {
                Logger.LogErrorFormat("Object {0} at {1} cannot be respawned because it has no PoolPrefabTracker and its name" +
                                      " does not match a prefab name, so we cannot" +
                                      " determine the prefab to instantiate. Please fix this object so that it" +
                                      " has an attached PoolPrefabTracker or so its name matches the prefab it was created from.", Category.ItemSpawn, target.name, position);
                return(null);
            }

            Despawn.ServerSingle(target);
            return(ServerPrefab(prefab, position.To3Int()).GameObject);
        }
        else
        {
            //destroy / create with pooling
            //save previous position
            var worldPos   = target.TileWorldPosition();
            var transform  = target.GetComponent <IPushable>();
            var prevParent = target.transform.parent;

            //this simulates going into the pool
            Despawn._ServerFireDespawnHooks(DespawnResult.Single(DespawnInfo.Single(target)));

            if (transform != null)
            {
                transform.VisibleState = false;
            }

            //this simulates coming back out of the pool
            target.SetActive(true);

            target.transform.parent   = prevParent;
            target.transform.position = worldPos.To3Int();

            var cnt = target.GetComponent <CustomNetTransform>();
            if (cnt)
            {
                cnt.ReInitServerState();
                cnt.NotifyPlayers();                 //Sending out clientState for already spawned items
            }

            SpawnInfo spawnInfo = null;
            //cloth or prefab?
            var clothing = target.GetComponent <Clothing>();
            var prefab   = DeterminePrefab(target);
            if (clothing != null)
            {
                spawnInfo = SpawnInfo.Cloth(clothing.clothingData, worldPos.To3Int(), clothing.Type,
                                            clothing.Variant, prefab);
            }
            else
            {
                spawnInfo = SpawnInfo.Prefab(prefab, worldPos.To3Int());
            }

            _ServerFireClientServerSpawnHooks(SpawnResult.Single(spawnInfo, target));
            return(target);
        }
    }