public void BuildTiles(LandPlatform platform, Vector2 center) { this.center = center; var blueprint = platform; tileSize = ResourceManager.GetAsset <GameObject>(blueprint.prefabs[0]).GetComponent <SpriteRenderer>().bounds.size.x; var cols = blueprint.columns; var rows = blueprint.rows; Offset = new Vector2 { x = center.x - tileSize * (cols - 1) / 2F, y = center.y + tileSize * (rows - 1) / 2F }; // TODO: read new data from file, for each platform areas = new List <Rect>(); var tiles = new List <GroundPlatform.Tile>(); if (blueprint != null && blueprint.prefabs.Length > 0) { // Create tile objects for (int i = 0; i < blueprint.tilemap.Length; i++) { var pos = new Vector3 { x = Offset.x + tileSize * (i % cols), y = Offset.y - tileSize * (i / cols), z = 0 }; if (blueprint.tilemap[i] > -1) { var obj = Instantiate(ResourceManager.GetAsset <GameObject>(blueprint.prefabs[blueprint.tilemap[i]]), pos, Quaternion.identity); obj.transform.localEulerAngles = new Vector3(0, 0, 90 * blueprint.rotations[i]); obj.GetComponent <SpriteRenderer>().color = color; obj.transform.parent = transform; tiles.Add( new GroundPlatform.Tile() { pos = new Vector2Int(i % cols, i / cols), type = (byte)blueprint.tilemap[i], rotation = (byte)blueprint.rotations[i], directions = new Dictionary <Vector2Int, byte>(), colliders = obj.GetComponentsInChildren <Collider2D>() }); } } groundPlatforms = DivideToPlatforms(tiles); } }
public void LoadSectorFile(string path) { // Update passed path during load time for main saves updating to newer versions if (path.Contains("main") || path == "") { path = jsonPath; } resourcePath = path; if (System.IO.Directory.Exists(path)) { try { // resource pack loading if (!ResourceManager.Instance.LoadResources(path) && testResourcePath != null) { ResourceManager.Instance.LoadResources(testResourcePath); } foreach (var canvas in Directory.GetFiles(path + "\\Canvases")) { if (canvas.Contains(".meta")) { continue; } if (canvas.Contains(".taskdata")) { taskManager.AddCanvasPath(canvas); continue; } if (canvas.Contains(".sectordata")) { taskManager.AddCanvasPath(canvas); continue; } if (canvas.Contains(".dialoguedata")) { dialogueSystem.AddCanvasPath(canvas); continue; } } // sector and world handling string[] files = Directory.GetFiles(path); current = null; sectors = new List <Sector>(); minimapSectorBorders = new Dictionary <Sector, LineRenderer>(); foreach (string file in files) { if (file.Contains(".meta") || file.Contains("ResourceData.txt")) { continue; } // parse world data if (file.Contains(".worlddata")) { string worlddatajson = System.IO.File.ReadAllText(file); WorldData wdata = ScriptableObject.CreateInstance <WorldData>(); JsonUtility.FromJsonOverwrite(worlddatajson, wdata); spawnPoint = wdata.initialSpawn; if (player.cursave == null || player.cursave.timePlayed == 0) { player.transform.position = player.spawnPoint = player.havenSpawnPoint = spawnPoint; if (wdata.defaultBlueprintJSON != null && wdata.defaultBlueprintJSON != "") { if (player.cursave != null) { player.cursave.currentPlayerBlueprint = wdata.defaultBlueprintJSON; } Debug.Log("Default blueprint set"); } } if (characters == null || characters.Length == 0) { characters = wdata.defaultCharacters; } else { // if there were added characters into the map since the last save, they must be added // into the existing data List <WorldData.CharacterData> charList = new List <WorldData.CharacterData>(characters); foreach (var defaultChar in wdata.defaultCharacters) { Debug.Log(defaultChar.ID); if (!charList.TrueForAll(ch => ch.ID != defaultChar.ID)) { charList.Add(defaultChar); } else { // We want to make sure names and IDs match at all times since these won't be changeable // by the player and the game needs these to match charList.Find(ch => ch.ID == defaultChar.ID).name = defaultChar.name; } } characters = charList.ToArray(); } PartIndexScript.index = wdata.partIndexDataArray; continue; } if (ResourceManager.fileNames.Contains(file)) { continue; } string sectorjson = System.IO.File.ReadAllText(file); SectorCreatorMouse.SectorData data = JsonUtility.FromJson <SectorCreatorMouse.SectorData>(sectorjson); // Debug.Log("Platform JSON: " + data.platformjson); // Debug.Log("Sector JSON: " + data.sectorjson); Sector curSect = ScriptableObject.CreateInstance <Sector>(); JsonUtility.FromJsonOverwrite(data.sectorjson, curSect); if (data.platformjson != "") // If the file has old platform data { LandPlatform plat = ScriptableObject.CreateInstance <LandPlatform>(); JsonUtility.FromJsonOverwrite(data.platformjson, plat); plat.name = curSect.name + "Platform"; curSect.platform = plat; } // render the borders on the minimap var border = new GameObject("MinimapSectorBorder - " + curSect.sectorName).AddComponent <LineRenderer>(); border.material = ResourceManager.GetAsset <Material>("white_material"); border.gameObject.layer = 8; border.positionCount = 4; border.startWidth = 0.5f; border.endWidth = 0.5f; border.loop = true; border.SetPositions(new Vector3[] { new Vector3(curSect.bounds.x, curSect.bounds.y, 0), new Vector3(curSect.bounds.x + curSect.bounds.w, curSect.bounds.y, 0), new Vector3(curSect.bounds.x + curSect.bounds.w, curSect.bounds.y - curSect.bounds.h, 0), new Vector3(curSect.bounds.x, curSect.bounds.y - curSect.bounds.h, 0) }); border.enabled = player.cursave.sectorsSeen.Contains(curSect.sectorName); border.startColor = border.endColor = new Color32((byte)135, (byte)135, (byte)135, (byte)255); minimapSectorBorders.Add(curSect, border); sectors.Add(curSect); } player.SetIsInteracting(false); jsonMode = false; sectorLoaded = true; return; } catch (System.Exception e) { Debug.LogError(e); }; } else if (System.IO.File.Exists(path)) { try { string sectorjson = System.IO.File.ReadAllText(path); SectorCreatorMouse.SectorData data = JsonUtility.FromJson <SectorCreatorMouse.SectorData>(sectorjson); Debug.Log("Platform JSON: " + data.platformjson); Debug.Log("Sector JSON: " + data.sectorjson); Sector curSect = ScriptableObject.CreateInstance <Sector>(); JsonUtility.FromJsonOverwrite(data.sectorjson, curSect); if (data.platformjson != "") // If the file has old platform data { LandPlatform plat = ScriptableObject.CreateInstance <LandPlatform>(); JsonUtility.FromJsonOverwrite(data.platformjson, plat); plat.name = curSect.name + "Platform"; curSect.platform = plat; } current = curSect; sectors = new List <Sector>(); sectors.Add(curSect); Debug.Log("Success! File loaded from " + path); jsonMode = false; sectorLoaded = true; player.SetIsInteracting(false); loadSector(); return; } catch (System.Exception e) { Debug.LogError(e); } } Debug.LogError("Could not find valid sector in " + path); jsonMode = false; player.SetIsInteracting(false); loadSector(); sectorLoaded = true; }
public void FromJSON() { string path = GameObject.Find("JSONPath").GetComponentInChildren <InputField>().text; if (System.IO.File.Exists(path)) { SectorData data = JsonUtility.FromJson <SectorData>(System.IO.File.ReadAllText(path)); Sector sectorDataWrapper = ScriptableObject.CreateInstance <Sector>(); JsonUtility.FromJsonOverwrite(data.sectorjson, sectorDataWrapper); LandPlatform platformDataWrapper = ScriptableObject.CreateInstance <LandPlatform>(); JsonUtility.FromJsonOverwrite(data.platformjson, platformDataWrapper); type = sectorDataWrapper.type; currentColor = sectorDataWrapper.backgroundColor; sctName = sectorDataWrapper.sectorName; UpdateColors(); int cols = platformDataWrapper.columns; int rows = platformDataWrapper.rows; x = sectorDataWrapper.bounds.x; y = sectorDataWrapper.bounds.y; width = sectorDataWrapper.bounds.w; height = sectorDataWrapper.bounds.h; sectorProps.ToggleActive(); sectorProps.transform.Find("Beginning X").GetComponentInChildren <InputField>().text = x.ToString(); sectorProps.transform.Find("Beginning Y").GetComponentInChildren <InputField>().text = y.ToString(); sectorProps.transform.Find("Height").GetComponentInChildren <InputField>().text = height.ToString(); sectorProps.transform.Find("Width").GetComponentInChildren <InputField>().text = width.ToString(); sectorProps.transform.Find("Sector Name").GetComponentInChildren <InputField>().text = sctName; sectorProps.transform.Find("Sector Type").GetComponent <Dropdown>().value = (int)sectorDataWrapper.type; sectorProps.ToggleActive(); var rend = GameObject.Find("SectorBorders").GetComponent <LineRenderer>(); rend.SetPositions(new Vector3[] { new Vector3(x, y, 0), new Vector3(x + width, y, 0), new Vector3(x + width, y + height, 0), new Vector3(x, y + height, 0) }); center = new Vector3 { x = this.x + width / 2F, y = this.y + height / 2F }; Vector2 offset = new Vector2 { x = center.x - tileSize * (cols - 1) / 2, y = center.y + tileSize * (rows - 1) / 2 }; for (int i = 0; i < platformDataWrapper.tilemap.Length; i++) { switch (platformDataWrapper.tilemap[i]) { case -1: break; default: PlaceableObject obj = new PlaceableObject(); obj.type = ObjectTypes.Platform; obj.placeablesIndex = platformDataWrapper.tilemap[i]; obj.rotation = platformDataWrapper.rotations[i]; Vector3 pos = new Vector3 { x = offset.x + (i % cols) * tileSize, y = offset.y - (i / cols) * tileSize, z = 0 }; obj.pos = pos; obj.obj = Instantiate(placeables[platformDataWrapper.tilemap[i]].obj, pos, Quaternion.identity); obj.obj.transform.localEulerAngles = new Vector3(0, 0, 90 * platformDataWrapper.rotations[i]); objects.Add(obj); break; } } foreach (Sector.LevelEntity ent in sectorDataWrapper.entities) { PlaceableObject obj = new PlaceableObject(); obj.pos = ent.position; obj.faction = ent.faction; obj.shellcoreJSON = ent.blueprintJSON; obj.ID = ent.ID; obj.assetID = ent.assetID; obj.type = ObjectTypes.Other; obj.vendingID = ent.vendingID; for (int i = 0; i < placeables.Length; i++) { if (placeables[i].assetID == obj.assetID) { obj.placeablesIndex = i; break; } } obj.obj = Instantiate(placeables[obj.placeablesIndex].obj, obj.pos, Quaternion.identity); obj.obj.name = ent.name; if (GetIsFactable(obj)) { foreach (SpriteRenderer renderer in obj.obj.GetComponentsInChildren <SpriteRenderer>()) { renderer.color = FactionManager.GetFactionColor(obj.faction); } if (obj.obj.GetComponentsInChildren <SpriteRenderer>().Length > 1) { obj.obj.GetComponent <SpriteRenderer>().color = Color.white; } } objects.Add(obj); } } else { Debug.Log($"File {path} doesn't exist."); } }
public void ToJSON() { if (string.IsNullOrEmpty(sctName)) { Debug.Log("Name your damn sector!"); return; } Sector sct = ScriptableObject.CreateInstance <Sector>(); sct.sectorName = sctName; sct.type = type; sct.backgroundColor = currentColor; LandPlatform platform = ScriptableObject.CreateInstance <LandPlatform>(); Vector3 firstTilePos = new Vector3 { x = center.x, y = center.y }; Vector3 lastTilePos = new Vector3 { x = center.x, y = center.y }; foreach (PlaceableObject ojs in objects) { if (ojs.type == ObjectTypes.Platform) { Vector3 tilePos = ojs.obj.transform.position; if (tilePos.x < firstTilePos.x) { firstTilePos.x = tilePos.x; } if (tilePos.y > firstTilePos.y) { firstTilePos.y = tilePos.y; } if (tilePos.x > lastTilePos.x) { lastTilePos.x = tilePos.x; } if (tilePos.y < lastTilePos.y) { lastTilePos.y = tilePos.y; } } } int columns = Mathf.CeilToInt(Mathf.Max(Mathf.Abs(firstTilePos.x - center.x) + 1, Mathf.Abs(lastTilePos.x - center.x) + 1) / tileSize) * 2; int rows = Mathf.CeilToInt(Mathf.Max(Mathf.Abs(firstTilePos.y - center.y) + 1, Mathf.Abs(lastTilePos.y - center.y) + 1) / tileSize) * 2; if (Mathf.RoundToInt(center.x - cursorOffset.x) % Mathf.RoundToInt(tileSize) == 0) { columns++; } if (Mathf.RoundToInt(center.y - cursorOffset.y) % Mathf.RoundToInt(tileSize) == 0) { rows++; } Vector2 offset = new Vector2 { x = center.x + -(columns - 1) * tileSize / 2, y = center.y + (rows - 1) * tileSize / 2 }; platform.rows = rows; platform.columns = columns; platform.tilemap = new int[rows * columns]; platform.rotations = new int[rows * columns]; platform.prefabs = new string[] { "4 Entry", "3 Entry", "2 Entry", "1 Entry", "0 Entry", "Junction" }; for (int i = 0; i < platform.tilemap.Length; i++) { platform.tilemap[i] = -1; } IntRect rect = new IntRect(); List <string> targetIDS = new List <string>(); List <Sector.LevelEntity> ents = new List <Sector.LevelEntity>(); rect.x = x; rect.y = y; rect.w = width; rect.h = height; sct.bounds = rect; int ID = 0; foreach (PlaceableObject oj in objects) { if (oj.type != ObjectTypes.Platform) { Sector.LevelEntity ent = new Sector.LevelEntity(); ent.ID = (ID++).ToString(); ent.faction = oj.faction; ent.position = oj.obj.transform.position; ent.assetID = oj.assetID; ent.vendingID = oj.vendingID; if (oj.isTarget) { targetIDS.Add(ent.ID); } ent.name = oj.obj.name; if (ent.assetID == "shellcore_blueprint") { targetIDS.Add(ent.ID); ent.blueprintJSON = oj.shellcoreJSON; } ents.Add(ent); } else if (oj.type == ObjectTypes.Platform) { int[] coordinates = new int[2]; coordinates[1] = Mathf.RoundToInt((oj.obj.transform.position.x - offset.x) / tileSize); coordinates[0] = -Mathf.RoundToInt((oj.obj.transform.position.y - offset.y) / tileSize); platform.tilemap[coordinates[1] + platform.columns * coordinates[0]] = oj.placeablesIndex; platform.rotations[coordinates[1] + platform.columns * coordinates[0]] = oj.rotation; } } sct.entities = ents.ToArray(); sct.targets = targetIDS.ToArray(); sct.backgroundColor = currentColor; SectorData data = new SectorData(); data.sectorjson = JsonUtility.ToJson(sct); data.platformjson = JsonUtility.ToJson(platform); string output = JsonUtility.ToJson(data); var sectorsDir = System.IO.Path.Combine(Application.streamingAssetsPath, "Sectors"); if (!System.IO.Directory.Exists(sectorsDir)) { System.IO.Directory.CreateDirectory(sectorsDir); } string path = System.IO.Path.Combine(sectorsDir, sct.sectorName); System.IO.File.WriteAllText(path, output); System.IO.Path.ChangeExtension(path, ".json"); mainMenu.ToggleActive(); successBox.ToggleActive(); Debug.Log("JSON written to location: " + path); }