Example #1
0
        //======================================================
        //      Protected methods
        //======================================================
        /// <summary>
        /// Finds the closest Plant to the player within the collider radius.
        /// </summary>
        /// <param name="player">The player, defining the center and radius of the search operation.</param>
        /// <param name="Plants">An enumerable list of Plants which is searched.</param>
        /// <returns>The closest Plant instance to the player, or null if no Plant was found within the radius.</returns>
        protected static PlantInstanceTemplate FindClosestPlantRequiringInstance(LocalPlayer player, IEnumerable <PlantInstanceTemplate> plants)
        {
            PlantInstanceTemplate result = PlantInstanceTemplate.Empty;
            float minDistance            = float.MaxValue;

            var enumerator = plants.GetEnumerator();

            while (enumerator.MoveNext())
            {
                //foreach (var plant in plants) {
                var plant = enumerator.Current;
                if (!plant.HasInstance && plant.ReadyToBePlanted)
                {
                    Vector3 dst             = plant.Position - player.Position;
                    float   distanceSquared = dst.x * dst.x + dst.y * dst.y + dst.z * dst.z;

                    if (distanceSquared < minDistance)
                    {
                        minDistance = distanceSquared;
                        result      = plant;
                    }
                }
            }
            return(result);
        }
Example #2
0
        public void Initialize()
        {               //this just creates a basic chunk object
            if (mInitialized)
            {
                return;
            }

            mChunkName          = ChunkName(State);
            mChunkDataDirectory = ChunkDataDirectory(mChunkName);
            gameObject.name     = mChunkName;
            //initialize assumes that the chunk state has been loaded
            transform.position            = State.TileOffset;
            ChunkGroup                    = WIGroups.GetOrAdd(gameObject, mChunkName, WIGroups.Get.World, null);
            ChunkGroup.Props.IgnoreOnSave = true;

            Transforms.WorldItems.gameObject.SetActive(true);
            Transforms.AboveGroundWorldItems.gameObject.SetActive(true);
            Transforms.BelowGroundWorldItems.gameObject.SetActive(true);
            Transforms.AboveGroundStaticDistant.gameObject.SetActive(true);

            Mods.Get.Runtime.LoadMod <ChunkTriggerData> (ref TriggerData, mChunkDataDirectory, "Triggers");
            Mods.Get.Runtime.LoadMod <ChunkNodeData> (ref NodeData, mChunkDataDirectory, "Nodes");
            //Mods.Get.Runtime.LoadMod <ChunkSceneryData> (ref SceneryData, mChunkDataDirectory, "Scenery");
            Mods.Get.Runtime.LoadMod <ChunkTerrainData> (ref TerrainData, mChunkDataDirectory, "Terrain");

            /*for (int i = 0; i < SceneryData.AboveGround.RiverNames.Count; i++) {
             *      //Debug.Log("Loading river " + SceneryData.AboveGround.RiverNames[i]);
             *      River river = null;
             *      if (Mods.Get.Runtime.LoadMod <River> (ref river, "River", SceneryData.AboveGround.RiverNames [i])) {
             *              Rivers.Add (river);
             *      }
             * }*/

            CalculateBounds();

            mChunkScale.Set(State.SizeX, Globals.ChunkMaximumYBounds, State.SizeZ);

            //load tree data
            if (Mods.Get.Runtime.LoadMod <ChunkTreeData> (ref TreeData, mChunkDataDirectory, "Trees"))
            {
                //update our tree instances with our offset and create our quad tree
                //make sure not to use the TreeInstances convenience property
                for (int i = 0; i < TreeData.TreeInstances.Length; i++)
                {
                    TreeInstanceTemplate tit = TreeData.TreeInstances [i];
                    tit.ParentChunk = this;
                    tit.ChunkOffset = ChunkOffset;
                    tit.ChunkScale  = ChunkScale;
                }
                TreeInstanceQuad = new QuadTree <TreeInstanceTemplate> (
                    ChunkBounds,
                    Math.Max(TreeInstances.Length / QuadTreeMaxContentScaler, QuadTreeMaxContentMinimum),
                    TreeData.TreeInstances);
            }

            //load plant data
            //make sure not to use the PlantInstances convenience property
            //it will return an empty array
            if (Mods.Get.Runtime.LoadMod <ChunkPlantData> (ref PlantData, mChunkDataDirectory, "Plants"))
            {
                for (int i = 0; i < PlantData.PlantInstances.Length; i++)
                {
                    PlantInstanceTemplate pit = PlantData.PlantInstances [i];
                    pit.HasInstance = false;
                    pit.ChunkOffset = ChunkOffset;
                    pit.ChunkScale  = ChunkScale;
                    pit.ParentChunk = this;
                }
                PlantInstanceQuad = new QuadTree <PlantInstanceTemplate> (
                    ChunkBounds,
                    Math.Max(PlantData.PlantInstances.Length / QuadTreeMaxContentScaler, QuadTreeMaxContentMinimum),
                    PlantData.PlantInstances);
            }

            //Dictionary <string,Texture2D> matChunkMaps = new Dictionary <string, Texture2D> ();
            for (int groundIndex = 0; groundIndex < TerrainData.TextureTemplates.Count; groundIndex++)
            {
                TerrainTextureTemplate ttt = TerrainData.TextureTemplates [groundIndex];
                Texture2D Diffuse          = null;
                if (Mats.Get.GetTerrainGroundTexture(ttt.DiffuseName, out Diffuse))
                {
                    ChunkDataMaps.Add("Ground" + groundIndex.ToString(), Diffuse);
                }
            }

            ChunkDataMaps.Add("ColorOverlay", PrimaryTerrain.materialTemplate.GetTexture("_CustomColorMap") as Texture2D);
            ChunkDataMaps.Add("Splat1", PrimaryTerrain.materialTemplate.GetTexture("_Splat2") as Texture2D);
            ChunkDataMaps.Add("Splat2", PrimaryTerrain.materialTemplate.GetTexture("_Splat2") as Texture2D);

            Texture2D chunkMap = null;

            //Debug.Log ("Getting terrain color overlay in " + Name);

            /*if (Mods.Get.Runtime.ChunkMap (ref chunkMap, Name, "ColorOverlay")) {
             *                  ChunkDataMaps.Add ("ColorOverlay", chunkMap);
             *          }*/
            if (GameWorld.Get.ChunkMap(ref chunkMap, Name, "AboveGroundTerrainType"))
            {
                ChunkDataMaps.Add("AboveGroundTerrainType", chunkMap);
            }
            if (GameWorld.Get.ChunkMap(ref chunkMap, Name, "BelowGroundTerrainType"))
            {
                ChunkDataMaps.Add("BelowGroundTerrainType", chunkMap);
            }
            if (GameWorld.Get.ChunkMap(ref chunkMap, Name, "RegionData"))
            {
                ChunkDataMaps.Add("RegionData", chunkMap);
            }

            /*if (Mods.Get.Runtime.ChunkMap (ref chunkMap, Name, "Splat1")) {
             *                  ChunkDataMaps.Add ("Splat1", chunkMap);
             *          }
             *          if (Mods.Get.Runtime.ChunkMap (ref chunkMap, Name, "Splat2")) {
             *                  ChunkDataMaps.Add ("Splat2", chunkMap);
             *          }*/

            //now start coroutines that load the nodes
            CreateNodesAndTriggers();

            //activate the main terrain
            PrimaryTerrain.gameObject.layer = Globals.LayerNumSolidTerrain;
            PrimaryTerrain.enabled          = true;
            PrimaryCollider = PrimaryTerrain.GetComponent <TerrainCollider>();

            //set the static objects
            DetailPrototype[] details = PrimaryTerrain.terrainData.detailPrototypes;
            for (int i = 0; i < details.Length; i++)
            {
                if (details[i].usePrototypeMesh)
                {
                    if (details[i].renderMode == DetailRenderMode.VertexLit)
                    {
                        details[i].renderMode = DetailRenderMode.Grass;
                    }
                    if (details[i].prototype == null)
                    {
                        Debug.Log("DETAIL " + i + " WAS NULL IN CHUNK " + name);
                    }
                    else if (details[i].prototype.name.Contains("Static"))
                    {
                        details[i].dryColor     = Colors.Alpha(details[i].dryColor, 0f);
                        details[i].healthyColor = Colors.Alpha(details[i].healthyColor, 0f);
                    }
                }
            }
            PrimaryTerrain.terrainData.detailPrototypes = details;

            //remove plant instance prefab, replace it with an empty one
            TreePrototype[] treePrototypes = PrimaryTerrain.terrainData.treePrototypes;
            for (int i = 0; i < treePrototypes.Length; i++)
            {
                if (treePrototypes[i].prefab == Plants.Get.PlantInstancePrefab)
                {
                    treePrototypes[i].prefab = Plants.Get.RuntimePlantInstancePrefab;
                }
            }
            PrimaryTerrain.terrainData.treePrototypes = treePrototypes;

            if (ColliderTemplates != null)
            {
                Array.Clear(ColliderTemplates, 0, ColliderTemplates.Length);
                ColliderTemplates = null;
                Plants.Get.GetTerrainPlantPrototypes(treePrototypes, ref ColliderTemplates);
            }

            /*if (!GameManager.Get.NoTreesMode) {
             *  TreePrototype[] treePrototypes = null;
             *  if (ColliderTemplates != null) {
             *      Array.Clear(ColliderTemplates, 0, ColliderTemplates.Length);
             *      ColliderTemplates = null;
             *  }
             *  //Debug.Log("Getting tree prototypes for " + Name);
             *  Plants.Get.GetTerrainPlantPrototypes(TerrainData.TreeTemplates, TreeData.TreeInstances, ref treePrototypes, ref ColliderTemplates);
             *  //PrimaryTerrain.terrainData.treePrototypes = treePrototypes;
             * }*/

            //turn everything off initially
            Transforms.AboveGroundStaticImmediate.gameObject.SetActive(false);
            Transforms.AboveGroundStaticAdjascent.gameObject.SetActive(false);
            Transforms.AboveGroundStaticDistant.gameObject.SetActive(false);
            Transforms.BelowGroundStatic.gameObject.SetActive(false);

            mInitialized = true;
        }