168 lines
5.4 KiB
C#
168 lines
5.4 KiB
C#
using Assets.Scripts;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class WorldGenerator : MonoBehaviour
|
|
{
|
|
public GameObject player;
|
|
public GameObject tile;
|
|
Dictionary<Vector3, GameObject> tiles;
|
|
GameObject currentTile;
|
|
NoiseGenerator noise;
|
|
int cityAmount = 0;
|
|
int maxCityAmount = 0;
|
|
System.Random rand = new System.Random();
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
createSpawn();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
tiles = new Dictionary<Vector3, GameObject>();
|
|
noise = new NoiseGenerator();
|
|
cityAmount = 10;
|
|
maxCityAmount = 10;
|
|
}
|
|
|
|
public bool gameWon()
|
|
{
|
|
return cityAmount <= 0;
|
|
}
|
|
|
|
public void resetGame(int cityAmount)
|
|
{
|
|
if (tiles != null)
|
|
{
|
|
foreach (GameObject tile in tiles.Values)
|
|
{
|
|
Destroy(tile);
|
|
}
|
|
}
|
|
player.transform.position = new Vector3(0, 50, 0);
|
|
player.transform.rotation = Quaternion.identity;
|
|
OnEnable();
|
|
this.cityAmount = cityAmount;
|
|
maxCityAmount = cityAmount;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
resetPlayer();
|
|
}
|
|
|
|
void resetPlayer()
|
|
{
|
|
if (player.transform.position.y <= -20)
|
|
{
|
|
Vector3 position = new Vector3(currentTile.transform.position.x, 50, currentTile.transform.position.z);
|
|
player.transform.SetPositionAndRotation(position, player.transform.rotation);
|
|
}
|
|
}
|
|
|
|
public void createSpawn()
|
|
{
|
|
if (!tiles.ContainsKey(new Vector3(0, 0, 0)))
|
|
{
|
|
Vector3 pos = new Vector3(0, 0, 0);
|
|
Vector3 mapPos = new Vector3(pos.x * 100, 0, pos.z * 100);
|
|
GameObject newTile = Instantiate(tile, mapPos, Quaternion.identity);
|
|
noise.applyNoise(newTile, tiles, pos);
|
|
newTile.GetComponent<Tile>().generateTile(pos, (TileType)Enum.Parse(typeof(TileType), newTile.name.Split("_")[0]));
|
|
tiles.Add(pos, newTile);
|
|
currentTile = newTile;
|
|
}
|
|
|
|
}
|
|
|
|
public void createTile(Vector3 requiredPosition)
|
|
{
|
|
Vector3 pos = currentTile.GetComponent<Tile>().getPosition() + requiredPosition;
|
|
if (!tiles.ContainsKey(pos) && pos.y == 0)
|
|
{
|
|
Vector3 mapPos = new Vector3(pos.x * 100, 0, pos.z * 100);
|
|
GameObject newTile = Instantiate(tile, mapPos, Quaternion.identity);
|
|
|
|
noise.applyNoise(newTile, tiles, pos);
|
|
newTile.GetComponent<Tile>().generateTile(pos, (TileType)Enum.Parse(typeof(TileType), newTile.name.Split("_")[0]));
|
|
tiles.Add(pos, newTile);
|
|
}
|
|
}
|
|
|
|
public void changeCurrentTile(GameObject newCurrent)
|
|
{
|
|
currentTile = newCurrent;
|
|
}
|
|
|
|
public GameObject getCurrentTile()
|
|
{
|
|
return currentTile;
|
|
}
|
|
|
|
public string saveGame()
|
|
{
|
|
string result = "";
|
|
int counter = 0;
|
|
string savePath = "";
|
|
result = result + FileHandler.generateJSON("cityAmount", cityAmount) + ",\r\n";
|
|
result = result + FileHandler.generateJSON("maxCityAmount", maxCityAmount) + ",\r\n";
|
|
result = result + "\"currentTile\": " + currentTile.GetComponent<Tile>().saveCurrent() + ",\r\n";
|
|
result = result + "\"map\": {\r\n";
|
|
foreach (GameObject tile in tiles.Values)
|
|
{
|
|
savePath = "./save/tile" + counter + ".json";
|
|
result = result + "\"tile" + counter + "\": \"" + savePath + "\"";
|
|
tile.GetComponent<Tile>().saveTile(savePath);
|
|
noise.saveTile(tile, savePath);
|
|
if (counter < tiles.Count - 1)
|
|
{
|
|
result = result + ",\r\n";
|
|
}
|
|
counter++;
|
|
}
|
|
result = result + "\r\n}";
|
|
return result;
|
|
}
|
|
|
|
public void loadWorld(JToken json)
|
|
{
|
|
resetGame(0);
|
|
cityAmount = (int)json["cityAmount"];
|
|
maxCityAmount = (int)json["maxCityAmount"];
|
|
string[] vectorParts = json["currentTile"].ToString().Split('/');
|
|
Vector3 current = new Vector3(float.Parse(vectorParts[0]), float.Parse(vectorParts[1]), float.Parse(vectorParts[2]));
|
|
GameObject loadedTile;
|
|
Vector3 mapPos;
|
|
Vector3 pos;
|
|
foreach (JProperty tilePath in json["map"])
|
|
{
|
|
JToken jsonData = JObject.Parse(FileHandler.loadTile(tilePath.Value.ToString()));
|
|
vectorParts = jsonData["position"].ToString().Split('/');
|
|
pos = new Vector3(float.Parse(vectorParts[0]), float.Parse(vectorParts[1]), float.Parse(vectorParts[2]));
|
|
mapPos = new Vector3(pos.x * 100, 0, pos.z * 100);
|
|
loadedTile = Instantiate(tile, mapPos, Quaternion.identity);
|
|
loadedTile.GetComponent<Tile>().loadTile(jsonData, pos);
|
|
noise.loadTile(loadedTile, jsonData["vertices"], jsonData["colors"]);
|
|
loadedTile.name = jsonData["tilename"].ToString();
|
|
tiles.Add(pos, loadedTile);
|
|
}
|
|
currentTile = tiles[current];
|
|
Vector3 position = new Vector3(currentTile.transform.position.x, 5, currentTile.transform.position.z);
|
|
player.transform.SetPositionAndRotation(position, player.transform.rotation);
|
|
}
|
|
|
|
public int getCityAmount()
|
|
{
|
|
return maxCityAmount;
|
|
}
|
|
}
|