214 lines
6.9 KiB
C#
214 lines
6.9 KiB
C#
using Assets.Scripts;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using Assets.Scripts.InteractableObjects;
|
|
|
|
public class Tile : MonoBehaviour
|
|
{
|
|
Vector3 position;
|
|
System.Random rand = new System.Random();
|
|
TileType tiletype;
|
|
GameObject contentGenerator;
|
|
List<GameObject> aliveEnemies = new List<GameObject>();
|
|
|
|
public void generateTile(Vector3 pos, TileType type)
|
|
{
|
|
tiletype = type;
|
|
SteamWorksHandler.getForestAchievement(type.ToString());
|
|
contentGenerator = GameObject.Find("ContentGenerator");
|
|
setPosition(pos);
|
|
generateContent();
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void generateContent()
|
|
{
|
|
foreach (Vector3 position in getSpawnLocations())
|
|
{
|
|
spawnObject(position);
|
|
}
|
|
}
|
|
|
|
|
|
public List<Vector3> getSpawnLocations()
|
|
{
|
|
List<Vector3> list = new List<Vector3>();
|
|
int objectAmount = rand.Next(40, 151);
|
|
int iterations = 0;
|
|
Vector3 newPoint;
|
|
bool canSpawn;
|
|
while (list.Count < objectAmount)
|
|
{
|
|
iterations++;
|
|
canSpawn = true;
|
|
newPoint = new Vector3(rand.Next(-40, 40) + 100 * position.x, 50, rand.Next(-40, 40) + 100 * position.z);
|
|
foreach (Vector3 vector in list)
|
|
{
|
|
if (Vector3.Distance(vector, newPoint) < 12.5f)
|
|
{
|
|
canSpawn = false;
|
|
break;
|
|
}
|
|
}
|
|
if (canSpawn && !list.Contains(newPoint))
|
|
{
|
|
list.Add(newPoint);
|
|
iterations = 0;
|
|
}
|
|
if (iterations >= 1000)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public void spawnObject(Vector3 position)
|
|
{
|
|
int chance = rand.Next(1, 101);
|
|
|
|
if (chance >= 25)
|
|
{
|
|
GameObject content = contentGenerator.GetComponent<ContentGenerator>().generateContent(tiletype);
|
|
if (content != null)
|
|
{
|
|
GameObject obj = Instantiate(content, position, Quaternion.identity, gameObject.transform);
|
|
if (obj.tag.Contains("Enemy"))
|
|
{
|
|
aliveEnemies.Add(obj);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setPosition(Vector3 position)
|
|
{
|
|
this.position = position;
|
|
}
|
|
|
|
public Vector3 getPosition()
|
|
{
|
|
return position;
|
|
}
|
|
|
|
public void enemyKilled(GameObject enemy)
|
|
{
|
|
aliveEnemies.Remove(enemy);
|
|
Destroy(enemy);
|
|
}
|
|
|
|
public TileType getTileType()
|
|
{
|
|
return tiletype;
|
|
}
|
|
|
|
public void saveTile(string path)
|
|
{
|
|
string result = "{\r\n";
|
|
GameObject obj;
|
|
result = result + FileHandler.generateJSON("tiletype", "\"" + tiletype + "\"") + ",\r\n";
|
|
result = result + FileHandler.generateJSON("tilename", "\"" + name + "\"") + ",\r\n";
|
|
result = result + FileHandler.generateJSON("position", "\"" + position.x + "/" + position.y + "/" + position.z + "\"") + ",\r\n";
|
|
result = result + "\"objects\": {\r\n";
|
|
for (int i = 0; i < gameObject.transform.childCount; i++)
|
|
{
|
|
obj = gameObject.transform.GetChild(i).gameObject;
|
|
result = result + "\"object" + i + "\": {\r\n";
|
|
result = result + FileHandler.generateJSON("position", "\"" + obj.transform.position.x + "/" + obj.transform.position.y + "/" + obj.transform.position.z + "\"") + ",\r\n";
|
|
if (obj.tag.Contains("Enemy"))
|
|
{
|
|
result = result + obj.GetComponent<Enemy>().saveEnemy() + "\r\n}";
|
|
}
|
|
else if (obj.name.ToLower().Contains("house"))
|
|
{
|
|
result = result + obj.transform.Find("Door").GetComponent<Door>().saveHouse() + "\r\n}";
|
|
}
|
|
else if (obj.name.ToLower().Contains("npc"))
|
|
{
|
|
result = result + obj.GetComponent<NPC>().saveNPC() + "\r\n}";
|
|
}
|
|
else
|
|
{
|
|
result = result + FileHandler.generateJSON("objectname", "\"" + obj.name + "\"") + "\r\n}";
|
|
}
|
|
if (i < gameObject.transform.childCount - 1)
|
|
{
|
|
result = result + ",\r\n";
|
|
}
|
|
}
|
|
result = result + "\r\n},";
|
|
FileHandler.saveTile(result, path);
|
|
}
|
|
|
|
public string saveCurrent()
|
|
{
|
|
string result = "";
|
|
result = result + "\"" + position.x + "/" + position.y + "/" + position.z + "\"";
|
|
return result;
|
|
}
|
|
|
|
public void loadTile(JToken json, Vector3 pos)
|
|
{
|
|
var jsonData = JObject.Parse(json["objects"].ToString()).Children();
|
|
List<JToken> tokens = jsonData.Children().ToList();
|
|
contentGenerator = GameObject.Find("ContentGenerator");
|
|
GameObject spawnedObject;
|
|
Vector3 position;
|
|
foreach (JToken obj in tokens)
|
|
{
|
|
if (obj["objectname"] != null)
|
|
{
|
|
if (obj["objectname"].ToString() != "pnlWater")
|
|
{
|
|
spawnedObject = contentGenerator.GetComponent<ContentGenerator>().loadObject(obj);
|
|
}
|
|
else
|
|
{
|
|
spawnedObject = null;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
spawnedObject = contentGenerator.GetComponent<ContentGenerator>().loadEnemy(obj);
|
|
}
|
|
if (spawnedObject != null)
|
|
{
|
|
position = new Vector3(float.Parse(obj["position"].ToString().Split('/')[0]), float.Parse(obj["position"].ToString().Split('/')[1]), float.Parse(obj["position"].ToString().Split('/')[2]));
|
|
spawnedObject = Instantiate(spawnedObject, position, Quaternion.identity);
|
|
spawnedObject.transform.parent = gameObject.transform;
|
|
spawnedObject.transform.localScale *= 10;
|
|
if (spawnedObject.tag.Contains("Enemy"))
|
|
{
|
|
aliveEnemies.Add(spawnedObject);
|
|
if (obj["health"] != null)
|
|
{
|
|
spawnedObject.GetComponent<Enemy>().loadEnemy(obj);
|
|
}
|
|
}
|
|
else if (spawnedObject.name.ToLower().Contains("house"))
|
|
{
|
|
spawnedObject.transform.Find("Door").GetComponent<Door>().loadHouse(obj);
|
|
}
|
|
else if (spawnedObject.tag.ToLower().Contains("npc"))
|
|
{
|
|
spawnedObject.GetComponent<NPC>().loadQuest(obj["receivedQuest"].ToString());
|
|
}
|
|
}
|
|
}
|
|
tiletype = (TileType)Enum.Parse(typeof(TileType), json["tiletype"].ToString());
|
|
setPosition(pos);
|
|
}
|
|
|
|
public List<TileType> getPossibleNeighbours()
|
|
{
|
|
return TileTypeMethods.getPossibleNeighbours(tiletype);
|
|
}
|
|
} |