// Create new location game object
        private GameObject CreateLocationGameObject(int terrain, out DFLocation locationOut)
        {
            locationOut = new DFLocation();

            // Terrain must have a location
            DaggerfallTerrain dfTerrain = terrainArray[terrain].terrainObject.GetComponent <DaggerfallTerrain>();

            if (!dfTerrain.MapData.hasLocation)
            {
                return(null);
            }

            // Get location data
            locationOut = dfUnity.ContentReader.MapFileReader.GetLocation(dfTerrain.MapData.mapRegionIndex, dfTerrain.MapData.mapLocationIndex);
            if (!locationOut.Loaded)
            {
                return(null);
            }

            // Spawn parent game object for new location
            float      height         = dfTerrain.MapData.averageHeight * TerrainScale;
            GameObject locationObject = new GameObject(string.Format("DaggerfallLocation [Region={0}, Name={1}]", locationOut.RegionName, locationOut.Name));

            locationObject.transform.parent   = this.transform;
            locationObject.hideFlags          = HideFlags.HideAndDontSave;
            locationObject.transform.position = terrainArray[terrain].terrainObject.transform.position + new Vector3(0, height, 0);
            DaggerfallLocation dfLocation = locationObject.AddComponent <DaggerfallLocation>() as DaggerfallLocation;

            dfLocation.SetLocation(locationOut, false);

            return(locationObject);
        }
Example #2
0
        // Create new location game object
        private GameObject CreateLocationGameObject(int terrain, out DFLocation locationOut)
        {
            locationOut = new DFLocation();

            // Terrain must have a location
            DaggerfallTerrain dfTerrain = terrainArray[terrain].terrainObject.GetComponent <DaggerfallTerrain>();

            if (!dfTerrain.MapData.hasLocation)
            {
                return(null);
            }

            // Get location data
            locationOut = dfUnity.ContentReader.MapFileReader.GetLocation(dfTerrain.MapData.mapRegionIndex, dfTerrain.MapData.mapLocationIndex);
            if (!locationOut.Loaded)
            {
                return(null);
            }

            // Get sampled position of height as more accurate than scaled average - thanks Nystul!
            Terrain terrainInstance = dfTerrain.gameObject.GetComponent <Terrain>();
            float   scale           = terrainInstance.terrainData.heightmapScale.x;
            float   xSamplePos      = (TerrainHelper.terrainTileDim - 1) / 2.0f; // get center terrain tile of block
            float   ySamplePos      = (TerrainHelper.terrainTileDim - 1) / 2.0f; // get center terrain tile of block
            Vector3 pos             = new Vector3(xSamplePos * scale, 0, ySamplePos * scale);
            float   height          = terrainInstance.SampleHeight(pos + terrainArray[terrain].terrainObject.transform.position);

            // Spawn parent game object for new location
            //float height = dfTerrain.MapData.averageHeight * TerrainScale;
            GameObject locationObject = new GameObject(string.Format("DaggerfallLocation [Region={0}, Name={1}]", locationOut.RegionName, locationOut.Name));

            locationObject.transform.parent = this.transform;
            //locationObject.hideFlags = HideFlags.HideAndDontSave;
            locationObject.transform.position = terrainArray[terrain].terrainObject.transform.position + new Vector3(0, height, 0);
            DaggerfallLocation dfLocation = locationObject.AddComponent <DaggerfallLocation>() as DaggerfallLocation;

            dfLocation.SetLocation(locationOut, false);

            // Raise event
            RaiseOnCreateLocationGameObjectEvent(dfLocation);

            return(locationObject);
        }