private void EditLocationWindow()
        {
            if (GUI.Button(Rect_NewFile, "New Prefab"))
            {
                if (parent != null)
                {
                    DestroyImmediate(parent);
                }

                locationPrefab = new LocationPrefab();
                parent         = new GameObject("Location Prefab");
            }

            if (GUI.Button(Rect_SaveFile, "Save Prefab"))
            {
                string path = EditorUtility.SaveFilePanel("Save as", LocationHelper.locationPrefabFolder, "new location", "txt");
                if (path.Length != 0)
                {
                    LocationHelper.SaveLocationPrefab(locationPrefab, path);
                }
            }

            if (GUI.Button(Rect_LoadFile, "Load Prefab"))
            {
                string path = EditorUtility.OpenFilePanel("Open", LocationHelper.locationPrefabFolder, "txt");

                if (path.Length == 0)
                {
                    return;
                }

                locationPrefab = new LocationPrefab();
                objScene       = new List <GameObject>();
                locationPrefab = LocationHelper.LoadLocationPrefab(path);

                if (locationPrefab == null)
                {
                    return;
                }

                if (parent != null)
                {
                    DestroyImmediate(parent);
                }

                parent = new GameObject("Location Prefab");

                foreach (LocationPrefab.LocationObject obj in locationPrefab.obj)
                {
                    CreateObject(obj);
                    availableIDs[obj.objectID] = true;
                }
            }

            if (parent != null)
            {
                GUI.Box(new Rect(4, 32, 516, 56), "", lightGrayBG);

                GUI.Label(new Rect(16, 40, 64, 16), "Area Y:");
                locationPrefab.height = EditorGUI.IntSlider(new Rect(90, 40, 400, 16), locationPrefab.height, 1, 126);

                GUI.Label(new Rect(16, 64, 64, 16), "Area X:");
                locationPrefab.width = EditorGUI.IntSlider(new Rect(90, 64, 400, 16), locationPrefab.width, 1, 126);

                scrollPosition = GUI.BeginScrollView(new Rect(2, 128, 532, 512), scrollPosition, new Rect(0, 0, 512, 20 + ((objScene.Count + 1) * 60)), false, true);

                for (int i = 0; i < objScene.Count; i++)
                {
                    if (objScene[i] == null)
                    {
                        availableIDs[locationPrefab.obj[i].objectID] = false;
                        objScene.RemoveAt(i);
                        locationPrefab.obj.RemoveAt(i);
                        return;
                    }

                    locationPrefab.obj[i].pos   = new Vector3(objScene[i].transform.localPosition.x, objScene[i].transform.localPosition.y, objScene[i].transform.localPosition.z);
                    locationPrefab.obj[i].rot   = objScene[i].transform.rotation;
                    locationPrefab.obj[i].scale = objScene[i].transform.localScale;

                    if (Selection.Contains(objScene[i]))
                    {
                        GUI.BeginGroup(new Rect(6, 10 + (i * 60), 496, 52), lightGreenBG);
                    }
                    else
                    {
                        GUI.BeginGroup(new Rect(6, 10 + (i * 60), 496, 52), lightGrayBG);
                    }

                    GUI.Label(new Rect(2, 4, 128, 16), "" + objScene[i].name);
                    GUI.Label(new Rect(2, 20, 128, 16), "Name: " + locationPrefab.obj[i].name);
                    GUI.Label(new Rect(2, 36, 128, 16), "ID: " + locationPrefab.obj[i].objectID);

                    GUI.Label(new Rect(136, 4, 256, 16), "Position : " + locationPrefab.obj[i].pos);
                    GUI.Label(new Rect(136, 20, 256, 16), "Rotation : " + locationPrefab.obj[i].rot.eulerAngles);
                    GUI.Label(new Rect(136, 36, 256, 16), "Scale    : " + locationPrefab.obj[i].scale);

                    if (GUI.Button(new Rect(392, 20, 64, 16), "Duplicate"))
                    {
                        int newID = 0;

                        for (int j = 0; j < availableIDs.Length; j++)
                        {
                            if (availableIDs[j] == false)
                            {
                                newID           = j;
                                availableIDs[j] = true;
                                break;
                            }
                        }

                        locationPrefab.obj.Add(new LocationPrefab.LocationObject(locationPrefab.obj[i].type, locationPrefab.obj[i].name, locationPrefab.obj[i].pos, locationPrefab.obj[i].rot, locationPrefab.obj[i].scale));
                        CreateObject(locationPrefab.obj[locationPrefab.obj.Count - 1], true);
                        locationPrefab.obj[locationPrefab.obj.Count - 1].objectID = newID;
                        //locationPrefab.obj.Sort((a, b) => a.objectID.CompareTo(b.objectID));
                    }

                    GUI.color = new Color(0.9f, 0.5f, 0.5f);
                    if (GUI.Button(new Rect(476, 0, 20, 20), "X") || (Event.current.Equals(Event.KeyboardEvent("Delete")) && Selection.Contains(objScene[i])))
                    {
                        availableIDs[locationPrefab.obj[i].objectID] = false;
                        DestroyImmediate(objScene[i]);
                        objScene.RemoveAt(i);
                        locationPrefab.obj.RemoveAt(i);
                        return;
                    }
                    GUI.color = Color.white;

                    if (GUI.Button(new Rect(0, 0, 758, 64), "", emptyBG))
                    {
                        Selection.activeGameObject = objScene[i];
                    }

                    GUI.EndGroup();
                }

                if (GUI.Button(new Rect(6, 10 + (objScene.Count * 60), 496, 52), "Add New Object"))
                {
                    editMode = EditMode.ObjectPicker;
                }

                GUI.EndScrollView();

                //Make sure we always have a ground
                if (ground == null)
                {
                    ground = GameObject.CreatePrimitive(PrimitiveType.Plane);
                }

                //Always make sure that the ground is set correctly
                ground.transform.SetParent(parent.transform);
                ground.name = "Surface";
                ground.transform.localScale    = new Vector3(locationPrefab.width * magicNumberSca, 0, locationPrefab.height * magicNumberSca);
                ground.transform.localRotation = new Quaternion();
                ground.transform.localPosition = new Vector3(-(((locationPrefab.width - 1) * magicNumberLoc) + magicNumberLoc), 0, -(((locationPrefab.height - 1) * magicNumberLoc) + magicNumberLoc));
            }
        }
        void ShowLocationList()
        {
            scrollPosition = GUI.BeginScrollView(new Rect(32, 64, 256, 472), scrollPosition, new Rect(0, 0, 236, 20 + (locationInstaneNames.Count * 24)), false, true);
            listSelector   = GUI.SelectionGrid(new Rect(10, 10, 216, locationInstaneNames.Count * 24), listSelector, locationInstaneNames.ToArray(), 1);

            GUI.EndScrollView();

            if (locationInstance.Count > listSelector)
            {
                GUI.Label(new Rect(312, 64, 96, 16), "Location ID: ");
                GUI.TextField(new Rect(388, 64, 96, 16), "#" + locationInstance[listSelector].locationID.ToString("00000000"));

                if (GUI.Button(new Rect(506, 64, 128, 16), "Generate New ID"))
                {
                    locationInstance[listSelector].UpdateLocationID();
                }

                GUI.Label(new Rect(312, 96, 64, 16), "Name: ");
                locationInstance[listSelector].name = GUI.TextField(new Rect(372, 96, 128, 16), locationInstance[listSelector].name);

                if (GUI.changed)
                {
                    locationInstaneNames[listSelector] = locationInstance[listSelector].name;
                }

                GUI.Label(new Rect(312, 128, 64, 16), "Prefab: ");
                GUI.TextField(new Rect(372, 128, 128, 16), locationInstance[listSelector].prefab);

                if (GUI.Button(new Rect(506, 128, 96, 16), "Select Prefab"))
                {
                    string path = EditorUtility.OpenFilePanel("Open", LocationHelper.locationPrefabFolder, "txt");

                    if (path.Length == 0)
                    {
                        return;
                    }

                    LocationPrefab tmplocpref = LocationHelper.LoadLocationPrefab(path);

                    if (tmplocpref != null)
                    {
                        locationInstance[listSelector].prefab = Path.GetFileNameWithoutExtension(path);
                        locationPrefab[listSelector]          = tmplocpref;
                    }
                }

                if (locationInstance[listSelector].prefab != "" && !File.Exists(Application.dataPath + LocationHelper.locationPrefabFolder + locationInstance[listSelector].prefab + ".txt"))
                {
                    GUI.contentColor = Color.red;
                    GUI.Label(new Rect(614, 128, 128, 16), "Prefab not found!");
                    GUI.contentColor = Color.white;
                }

                GUI.Label(new Rect(312, 160, 64, 16), "Type: ");
                locationInstance[listSelector].type = EditorGUI.Popup(new Rect(372, 160, 128, 16), locationInstance[listSelector].type, locationTypes);

                GUI.Label(new Rect(312, 192, 64, 16), "World X: ");
                locationInstance[listSelector].worldX = EditorGUI.IntSlider(new Rect(372, 192, 256, 16), locationInstance[listSelector].worldX, 3, 998);

                GUI.Label(new Rect(312, 224, 64, 16), "World Y: ");
                locationInstance[listSelector].worldY = EditorGUI.IntSlider(new Rect(372, 224, 256, 16), locationInstance[listSelector].worldY, 3, 498);

                GUI.Label(new Rect(312, 256, 64, 16), "Terrain X: ");
                locationInstance[listSelector].terrainX = EditorGUI.IntSlider(new Rect(372, 256, 256, 16), locationInstance[listSelector].terrainX, 1, 127 - locationPrefab[listSelector].width);

                GUI.Label(new Rect(312, 288, 64, 16), "Terrain Y: ");
                locationInstance[listSelector].terrainY = EditorGUI.IntSlider(new Rect(372, 288, 256, 16), locationInstance[listSelector].terrainY, 1, 127 - locationPrefab[listSelector].height);

                GUI.Label(new Rect(454, 320, 64, 16), "- N -");
                GUI.Box(new Rect(336, 336, 256, 256), "", blackBG);
                GUI.Box(new Rect((336 + (locationInstance[listSelector].terrainX * 2)), (592 - (locationInstance[listSelector].terrainY * 2)) - locationPrefab[listSelector].height * 2, locationPrefab[listSelector].width * 2, locationPrefab[listSelector].height * 2), "", lightGreenBG);
                GUI.Label(new Rect(454, 592, 64, 16), "- S -");
            }

            if (GUI.Button(new Rect(32, 562, 96, 20), "Add Location"))
            {
                locationInstance.Add(new LocationInstance("new Location", 0, "", 0, 0, 0, 0));
                locationInstance[locationInstance.Count - 1].UpdateLocationID();
                locationInstaneNames.Add("new location");
                locationPrefab.Add(new LocationPrefab());
            }

            if (GUI.Button(new Rect(160, 562, 128, 20), "Remove Location"))
            {
                locationInstance.RemoveAt(listSelector);
                locationInstaneNames.RemoveAt(listSelector);
            }
        }
Beispiel #3
0
        void AddLocation(DaggerfallTerrain daggerTerrain, TerrainData terrainData)
        {
            //Destroy old locations by going through all the child objects, but
            //don't delete the billboard batch (The surrounding vegettion)
            foreach (Transform child in daggerTerrain.gameObject.transform)
            {
                if (!child.GetComponent <DaggerfallBillboardBatch>())
                {
                    Destroy(child.gameObject);
                }
            }

            foreach (LocationInstance loc in locationInstance)
            {
                if (daggerTerrain.MapPixelX != loc.worldX || daggerTerrain.MapPixelY != loc.worldY)
                {
                    continue;
                }

                if (loc.terrainX <= 0 || loc.terrainY <= 0 || (loc.terrainX > 128 || loc.terrainY > 128))
                {
                    Debug.LogWarning("Invalid Location at " + daggerTerrain.MapPixelX + " : " + daggerTerrain.MapPixelY + " : The location pixelX + or/and pixelY must be higher than 0 and lower than 128");
                    continue;
                }

                //Now that we ensured that it is a valid location, then load the locationpreset
                LocationPrefab locationPrefab = LocationHelper.LoadLocationPrefab(Application.dataPath + LocationHelper.locationPrefabFolder + loc.prefab + ".txt");

                if (locationPrefab == null)
                {
                    Debug.LogWarning("Can't find location Preset: " + loc.prefab);
                    continue;
                }

                if ((loc.terrainX + locationPrefab.height > 128 || loc.terrainY + locationPrefab.width > 128))
                {
                    Debug.LogWarning("Invalid Location at " + daggerTerrain.MapPixelX + " : " + daggerTerrain.MapPixelY + " : The locationpreset exist outside the terrain");
                    continue;
                }

                if ((loc.terrainX + locationPrefab.height > 127 || loc.terrainY + locationPrefab.width > 127))
                {
                    Debug.LogWarning("Invalid Location at " + daggerTerrain.MapPixelX + " : " + daggerTerrain.MapPixelY + " : The locationpreset must be 1 pixel away (both X and Y) from the terrainBorder");
                    continue;
                }

                //Smooth the terrain
                if (loc.type == 0)
                {
                    daggerTerrain.MapData.hasLocation = true;
                    //daggerTerrain.MapData.locationName = loc.name;
                    daggerTerrain.MapData.locationRect = new Rect(loc.terrainX, loc.terrainY, locationPrefab.width, locationPrefab.height);

                    int   count            = 0;
                    float tmpAverageHeight = 0;

                    for (int x = loc.terrainX; x <= loc.terrainX + locationPrefab.width; x++)
                    {
                        for (int y = loc.terrainY; y <= loc.terrainY + locationPrefab.height; y++)
                        {
                            tmpAverageHeight += daggerTerrain.MapData.heightmapSamples[y, x];
                            count++;
                        }
                    }

                    daggerTerrain.MapData.averageHeight = tmpAverageHeight /= count;
                    //TerrainHelper.BlendLocationTerrain(ref daggerTerrain.MapData); //orginal alternative

                    for (int x = 1; x <= 127; x++)
                    {
                        for (int y = 1; y <= 127; y++)
                        {
                            daggerTerrain.MapData.heightmapSamples[y, x] = Mathf.Lerp(daggerTerrain.MapData.heightmapSamples[y, x], daggerTerrain.MapData.averageHeight, 1 / (GetDistanceFromRect(daggerTerrain.MapData.locationRect, new Vector2(x, y)) + 1));
                        }
                    }

                    terrainData.SetHeights(0, 0, daggerTerrain.MapData.heightmapSamples);
                }

                foreach (LocationPrefab.LocationObject obj in locationPrefab.obj)
                {
                    if (!LocationHelper.ValidateValue(obj.type, obj.name))
                    {
                        continue;
                    }

                    GameObject go = LocationHelper.LoadObject(obj.type, obj.name, daggerTerrain.gameObject.transform,
                                                              new Vector3((loc.terrainX * TERRAIN_SIZE_MULTI) + obj.pos.x, (daggerTerrain.MapData.averageHeight * TERRAIN_HEIGHT_MAX) + obj.pos.y, (loc.terrainY * TERRAIN_SIZE_MULTI) + obj.pos.z),
                                                              obj.rot,
                                                              new Vector3(obj.scale.x, obj.scale.y, obj.scale.z), loc.locationID, obj.objectID
                                                              );

                    if (go.GetComponent <DaggerfallBillboard>())
                    {
                        float tempY = go.transform.position.y;
                        go.GetComponent <DaggerfallBillboard>().AlignToBase();
                        go.transform.position = new Vector3(go.transform.position.x, tempY + ((go.transform.position.y - tempY) * go.transform.localScale.y), go.transform.position.z);
                    }

                    if (!go.GetComponent <DaggerfallLoot>())
                    {
                        go.isStatic = true;
                    }
                }

                continue;
            }
        }
        void OnGUI()
        {
            if (GUI.Button(Rect_NewFile, "New File"))
            {
                locationInstance     = new List <LocationInstance>();
                locationInstaneNames = new List <string>();
                locationPrefab       = new List <LocationPrefab>();
            }

            if (GUI.Button(Rect_SaveFile, "Save File"))
            {
                string path = EditorUtility.SaveFilePanel("Save as", LocationHelper.locationInstanceFolder, "new location", "txt");
                if (path.Length != 0)
                {
                    LocationHelper.SaveLocationInstance(locationInstance.ToArray(), path);
                }
            }

            if (GUI.Button(Rect_LoadFile, "Load File"))
            {
                string path = EditorUtility.OpenFilePanel("Open", LocationHelper.locationInstanceFolder, "txt");

                if (path.Length == 0)
                {
                    return;
                }

                locationInstance = LocationHelper.LoadLocationInstance(path);

                if (locationInstance == null)
                {
                    return;
                }

                locationInstaneNames = new List <string>();
                locationPrefab       = new List <LocationPrefab>();

                foreach (LocationInstance loc in locationInstance)
                {
                    locationInstaneNames.Add(loc.name);

                    if (loc.prefab != null)
                    {
                        LocationPrefab tmplocpref = LocationHelper.LoadLocationPrefab(Application.dataPath + LocationHelper.locationPrefabFolder + loc.prefab + ".txt");

                        if (tmplocpref != null)
                        {
                            locationPrefab.Add(tmplocpref);
                        }
                        else
                        {
                            locationPrefab.Add(new LocationPrefab());
                        }
                    }
                    else
                    {
                        locationPrefab.Add(new LocationPrefab());
                    }
                }
            }

            if (locationInstance != null)
            {
                ShowLocationList();
            }
        }