Ejemplo n.º 1
0
        public IEnumerator GenerateStructureItems()
        {
            yield return(null);

            Transform structureItems = null;
            WIGroup   group          = null;

            State = BuilderState.BuildingItems;
            WorldChunk chunk = null;

            switch (Mode)
            {
            case BuilderMode.Exterior:
            default:
                group          = ParentStructure.StructureGroup;
                chunk          = group.GetParentChunk();
                structureItems = group.gameObject.FindOrCreateChild("_ITEMS_EXT");
                var generateExtItems = StructureBuilder.GenerateExteriorItems(
                    ParentStructure,
                    Template.Exterior,
                    group,
                    structureItems);
                while (generateExtItems.MoveNext())
                {
                    /*if (chunk.TargetMode == ChunkMode.Unloaded) {
                     *              Debug.Log("Cancelling build, chunk is unloading");
                     *              yield break;
                     *      }*/
                    yield return(generateExtItems.Current);
                }
                break;

            case BuilderMode.Interior:
                for (int i = 0; i < InteriorVariants.Count; i++)
                {
                    int interiorVariant = InteriorVariants [i];
                    if (!ParentStructure.StructureGroupInteriors.ContainsKey(interiorVariant))
                    {
                        //Debug.Log ("Didn't find interior variant " + interiorVariant.ToString () + " in " + ParentStructure.name + " - waiting for a bit...");
                        double timeOut = WorldClock.RealTime + 10f;
                        while (ParentStructure != null && !ParentStructure.worlditem.Is(WIActiveState.Invisible) && !ParentStructure.StructureGroupInteriors.ContainsKey(interiorVariant))
                        {
                            //waiting for interior groups to spawn

                            /*if (chunk.TargetMode == ChunkMode.Unloaded) {
                             *              Debug.Log("Cancelling build, chunk is unloading");
                             *              yield break;
                             *      }*/
                            yield return(null);

                            if (WorldClock.RealTime > timeOut)
                            {
                                //Debug.Log ("Timed out waiting for interior group " + interiorVariant.ToString () + " in " + ParentStructure.name);
                                break;
                            }
                        }
                    }

                    try {
                        group          = ParentStructure.StructureGroupInteriors [interiorVariant];
                        structureItems = group.gameObject.FindOrCreateChild("_ITEMS_INT_" + interiorVariant.ToString());
                    } catch (Exception e) {
                        Debug.LogError(e.ToString());
                        continue;
                    }

                    if (!ParentStructure.State.InteriorsLoadedOnce.Contains(interiorVariant) && interiorVariant < Template.InteriorVariants.Count)
                    {
                        /*#if UNITY_EDITOR
                         * Debug.Log ("Generating items for interior variant " + interiorVariant.ToString ());
                         #endif*/
                        var generateIntItems = StructureBuilder.GenerateInteriorItems(
                            ParentStructure,
                            interiorVariant,
                            Template.InteriorVariants [interiorVariant],
                            group,
                            structureItems);
                        while (generateIntItems.MoveNext())
                        {
                            /*if (chunk.TargetMode == ChunkMode.Unloaded) {
                             *              Debug.Log("Cancelling build, unloading");
                             *              yield break;
                             *      }*/
                            yield return(generateIntItems.Current);
                        }
                    }

                    /*#if UNITY_EDITOR
                     * else {
                     *      Debug.Log ("Didn't generate items for interior variant "
                     + interiorVariant.ToString ()
                     + " because it has either been loaded once (" + ParentStructure.State.InteriorsLoadedOnce.Contains (interiorVariant).ToString()
                     + ") or it's out of range (" + (interiorVariant >= Template.InteriorVariants.Count).ToString () + ")");
                     + }
                     #endif*/

                    yield return(null);
                }
                break;

            case BuilderMode.Minor:
                //minor structures don't generate items
                break;
            }
        }
Ejemplo n.º 2
0
        public IEnumerable <SpawnPoint> GetSpawnPoints(SpawnerStateSetting setting, Location location, WIGroup spawnGroup, PlayerBase player, System.Random random)
        {
            //figure out how many we need
            setting.NumAttempts       = 0;
            setting.NumFailedAttempts = 0;

            int numExistingObjects = spawnGroup.NumChildItemsByCategory(setting.CategoryName, setting.MaxSpawnedObjects);             //TODO make sure this works
            int numObjectsToSpawn  = random.Next(setting.MinSpawnedObjects, setting.MaxSpawnedObjects) - numExistingObjects;

            if (numObjectsToSpawn <= 0)
            {
                //we don't need any more right now
                                #if UNITY_EDITOR
                Debug.Log("Skipping spawning in " + name + " because num objects to spawn is <= 0");
                                #endif
                yield break;
            }
            bool succeededThisAttempt = false;

            switch (setting.Type)
            {
            case SpawnerType.Characters:
                //when we're spawning characters, we need action nodes
                WorldChunk             chunk      = spawnGroup.GetParentChunk();
                List <ActionNodeState> nodeStates = null;
                if (chunk.GetNodesForLocation(location.worlditem.StaticReference.FullPath, out nodeStates))
                {
                    for (int i = 0; i < nodeStates.Count; i++)
                    {
                        mNextSpawnPoint.nodeState = nodeStates [i];
                        yield return(mNextSpawnPoint);
                    }
                }
                else
                {
                    nodeStates = new List <ActionNodeState> ();
                    Debug.Log("Didn't find spawn points at location, so we're going to create some");
                    //if we can't find any nodes for this location
                    //that means we've never spawned here before
                    //we need to create some nodes
                    for (int i = 0; i < numObjectsToSpawn; i++)
                    {
                        //are we totally done?
                        if (setting.NumFailedAttempts >= setting.MaxFailedAttempts)
                        {
                            yield break;
                        }
                        //if not, try to get another spawn point
                        if (spawnGroup.Props.Interior)
                        {
                            succeededThisAttempt = GetInteriorSpawnPoint(ref mNextSpawnPoint, setting, location, spawnGroup);
                        }
                        else if (spawnGroup.Props.TerrainType == LocationTerrainType.AboveGround)
                        {
                            succeededThisAttempt = GetAboveGroundSpawnPoint(ref mNextSpawnPoint, setting, location, spawnGroup);
                        }
                        else
                        {
                            succeededThisAttempt = GetBelowGroundSpawnPoint(ref mNextSpawnPoint, setting, location, spawnGroup);
                        }
                        //did we do it?
                        if (succeededThisAttempt)
                        {
                            //hooray we created a new action node
                            setting.NumAttempts++;
                            //turn this spawn point into an action node
                            ActionNodeState nodeState = new ActionNodeState();
                            nodeState.Name  = location.worlditem.FileName + "-Spawn-" + nodeStates.Count.ToString();
                            nodeState.Type  = ActionNodeType.Generic;
                            nodeState.Users = ActionNodeUsers.AnyOccupant;
                            nodeState.Transform.CopyFrom(mNextSpawnPoint.Transform);
                            mNextSpawnPoint.nodeState = nodeState;
                            ActionNode node = chunk.SpawnActionNode(spawnGroup, nodeState, location.worlditem.tr);
                            yield return(mNextSpawnPoint);
                        }
                        else
                        {
                            //boo, we failed
                            setting.NumFailedAttempts++;
                        }
                    }
                }
                nodeStates.Clear();
                nodeStates = null;
                break;

            default:
                //when we're spawning other things, we just need spawn points
                //ok, try to spawn this stuff
                for (int i = 0; i < numObjectsToSpawn; i++)
                {
                    //are we totally done?
                    if (setting.NumFailedAttempts >= setting.MaxFailedAttempts)
                    {
                        yield break;
                    }
                    //if not, try to get another spawn point
                    if (spawnGroup.Props.Interior)
                    {
                        succeededThisAttempt = GetInteriorSpawnPoint(ref mNextSpawnPoint, setting, location, spawnGroup);
                    }
                    else if (spawnGroup.Props.TerrainType == LocationTerrainType.AboveGround)
                    {
                        succeededThisAttempt = GetAboveGroundSpawnPoint(ref mNextSpawnPoint, setting, location, spawnGroup);
                    }
                    else
                    {
                        succeededThisAttempt = GetBelowGroundSpawnPoint(ref mNextSpawnPoint, setting, location, spawnGroup);
                    }
                    //did we do it?
                    if (succeededThisAttempt)
                    {
                        //hooray, we got another spawn point
                        setting.NumAttempts++;
                        yield return(mNextSpawnPoint);
                    }
                    else
                    {
                        //boo, we failed
                        setting.NumFailedAttempts++;
                        yield return(SpawnPoint.Empty);
                    }
                }
                break;
            }
            yield break;
        }