Firstborn/Assets/RPG Creation Kit/Scripts/Cells System/Scripts/Data Structure/Worldspace.cs

175 lines
6.1 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using RPGCreationKit.CellsSystem;
using System.IO;
using System;
namespace RPGCreationKit.CellsSystem
{
/// <summary>
/// Represent a World Space, a collection of Cells.
/// </summary>
public class Worldspace : ScriptableObject
{
public enum WorldSpaceType { Exterior, Interior};
[Header("Data")]
public string worldSpaceID;
public string worldSpaceName;
public WorldSpaceType worldSpaceType;
[Header("Extra")]
public Worldspace parentWorldSpace;
public bool unloadPreviousWorld;
public bool forceToUnloadAllCached;
public float distanceBeforeUnloading;
[Header("Cells Settings")]
public Vector2 cellSize;
public Material SkyboxMaterial;
public Cell[] cells;
#if UNITY_EDITOR
[MenuItem("Assets/Create/RPG Creation Kit/Cell System/New Worldspace")]
public static void CreateNewWorldpsace()
{
Worldspace asset = ScriptableObject.CreateInstance<Worldspace>();
string path = AssetDatabase.GenerateUniqueAssetPath(GetSelectedPathOrFallback() + "/New Worldspace.asset");
AssetDatabase.CreateAsset(asset, path);
AssetDatabase.SaveAssets();
Selection.activeObject = asset;
/*
var AllWindows = Resources.FindObjectsOfTypeAll<CellView>();
bool winFound = false;
for (int i = 0; i < AllWindows.Length; i++)
{
if (AllWindows[i].isReady &&
AllWindows[i].itemObj.FindProperty("ItemID").stringValue == refItem.ItemID)
{
winFound = true;
AllWindows[i].Focus();
}
}
*/
}
#endif
[ContextMenu("Calculate Cells Coordinates")]
public void CalculateCellsCoordinates()
{
if (worldSpaceType == WorldSpaceType.Exterior)
{
for (int i = 0; i < cells.Length; i++)
{
cells[i].cellInWorldCoordinates = new Vector3((cellSize.x) * cells[i].cellCoordinates.x, 0, (cellSize.y) * cells[i].cellCoordinates.y);
#if UNITY_EDITOR
EditorUtility.SetDirty(cells[i]);
#endif
}
}
}
[ContextMenu("Calculate Cells Neighboring")]
public void CalculateCellsNeighboring()
{
if (worldSpaceType == WorldSpaceType.Exterior)
{
for (int i = 0; i < cells.Length; i++)
{
Cell centerCell = cells[i];
// Debug.Log(centerCell.cellName + " | " + centerCell.cellCoordinates);
// Calculate neighboring Cells
Cell[] neighboringCells = new Cell[9];
// Center Cell
neighboringCells[0] = centerCell;
// Top Cell
neighboringCells[1] = GetCell(new Vector2(centerCell.cellCoordinates.x, centerCell.cellCoordinates.y + 1));
// Top-Right Cell
neighboringCells[2] = GetCell(new Vector2(centerCell.cellCoordinates.x + 1, centerCell.cellCoordinates.y + 1));
// Right Cell
neighboringCells[3] = GetCell(new Vector2(centerCell.cellCoordinates.x + 1, centerCell.cellCoordinates.y));
// Down-Right Cell
neighboringCells[4] = GetCell(new Vector2(centerCell.cellCoordinates.x + 1, centerCell.cellCoordinates.y - 1));
// Down
neighboringCells[5] = GetCell(new Vector2(centerCell.cellCoordinates.x, centerCell.cellCoordinates.y - 1));
// Down-Left Cell
neighboringCells[6] = GetCell(new Vector2(centerCell.cellCoordinates.x - 1, centerCell.cellCoordinates.y - 1));
// Left Cell
neighboringCells[7] = GetCell(new Vector2(centerCell.cellCoordinates.x - 1, centerCell.cellCoordinates.y));
// Top-Left Cell
neighboringCells[8] = GetCell(new Vector2(centerCell.cellCoordinates.x - 1, centerCell.cellCoordinates.y + 1));
centerCell.neighboringCells = neighboringCells;
#if UNITY_EDITOR
EditorUtility.SetDirty(cells[i]);
#endif
}
}
else // if it's an interior worldspace, there are no neighboring cells
{
for (int i = 0; i < cells.Length; i++)
Array.Clear(cells[i].neighboringCells, 0, cells[i].neighboringCells.Length);
}
}
private Cell GetCell(Vector2 cellPos)
{
for (int i = 0; i < cells.Length; i++)
{
if (cellPos == cells[i].cellCoordinates)
return cells[i];
}
return null;
}
/// <summary>
/// Returns a cell by providing an ID or NULL if no cell with that ID exists
/// </summary>
/// <param name="_id"></param>
/// <returns></returns>
public Cell GetCellByID(string _id)
{
for(int i = 0; i < cells.Length; i++)
{
if (cells[i].ID == _id)
return cells[i];
}
Debug.LogError("ERROR! Cell: " + _id + " not found. You may have to fix IDs or Update the Databases");
return null;
}
#if UNITY_EDITOR
public static string GetSelectedPathOrFallback()
{
string path = "Assets";
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
{
path = AssetDatabase.GetAssetPath(obj);
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
path = Path.GetDirectoryName(path);
break;
}
}
return path;
}
#endif
}
}