add dynamic spawning

This commit is contained in:
WillJeynes
2026-08-24 13:53:58 +01:00
parent ae2e8daa6f
commit 641564b461
36 changed files with 1774 additions and 18 deletions
+16
View File
@@ -0,0 +1,16 @@
using UnityEngine;
public class DialogManager : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5078a33f9501dab1ba0e2808a8a07810
+28
View File
@@ -0,0 +1,28 @@
using UnityEditor;
using UnityEngine;
public class DoorManager : MonoBehaviour
{
public string type;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnDrawGizmos()
{
#if UNITY_EDITOR
Handles.Label(
transform.position + Vector3.up * 2f,
type
);
#endif
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3194c39c08057cd4badc5a1721e6a498
+28
View File
@@ -0,0 +1,28 @@
using UnityEditor;
using UnityEngine;
public class GuardManager : MonoBehaviour
{
public string type;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnDrawGizmos()
{
#if UNITY_EDITOR
Handles.Label(
transform.position + Vector3.up * 0f,
type
);
#endif
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 20061c4fba047435b84052ea7566f8cf
+69
View File
@@ -0,0 +1,69 @@
using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Utils;
public class LevelLoader : Singleton<LevelLoader>
{
[SerializeField]
private List<GameObjectEntry> DoorPrefabs;
[SerializeField]
private List<GameObjectEntry> GuardPrefabs;
[SerializeField]
private List<TextAsset> levels;
[SerializeField]
private float widthAvail = 50;
public List<DoorManager> Doors = new List<DoorManager>();
public List<GuardManager> Guards = new List<GuardManager>();
void Start()
{
//load level data
var data = JsonUtility.FromJson<LevelData>(levels[0].text);
//shuffle for randomness
data.doors.Shuffle();
data.guards.Shuffle();
//spawn in doors
float spacingDoors = widthAvail / (data.doors.Count + 1);
for(int i = 0; i < data.doors.Count; i++)
{
var doorinstance = Instantiate(DoorPrefabs.ByKey(data.doors[i].type));
doorinstance.transform.position = new Vector3(spacingDoors*(i+1), 0, 0);
var manager = doorinstance.GetComponent<DoorManager>();
manager.type = data.doors[i].action;
Doors.Add(manager);
}
//spawn in guards
float spacingGuards = widthAvail / (data.guards.Count + 1);
for(int i = 0; i < data.guards.Count; i++)
{
var guardinstance = Instantiate(GuardPrefabs.ByKey(data.guards[i].type));
guardinstance.transform.position = new Vector3(spacingGuards*(i+1), 0, -5);
var manager = guardinstance.GetComponent<GuardManager>();
manager.type = data.guards[i].action;
Guards.Add(manager);
}
//dynamic dialog dree
var speechTree = JObject.Parse(levels[0].text)["dialog"] as JObject;
}
void Update()
{
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 742d01e96d3414f0c9d0745f6199adcb
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c20f52b9cebf481292ebed5f2087b504
timeCreated: 1787569729
+28
View File
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Utils
{
[Serializable]
public class GameObjectEntry
{
public string key;
public GameObject value;
}
public static class GameObjectEntryListExtensions
{
public static GameObject ByKey(this List<GameObjectEntry> entries, string key)
{
foreach (var entry in entries)
{
if (entry.key == key)
return entry.value;
}
return null;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2f5a46f036b44cbd9b4bf5d0694327fc
timeCreated: 1787569753
+25
View File
@@ -0,0 +1,25 @@
using System.Collections.Generic;
namespace Utils
{
[System.Serializable]
public class LevelData
{
public List<DoorData> doors;
public List<GuardData> guards;
}
[System.Serializable]
public class DoorData
{
public string type;
public string action;
}
[System.Serializable]
public class GuardData
{
public string type;
public string action;
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2350cfda27c14b5fbb0e296982bf46d9
timeCreated: 1787569794
+18
View File
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using UnityEngine;
namespace Utils
{
public static class ListExtensions
{
public static void Shuffle<T>(this List<T> list)
{
for (int i = list.Count - 1; i > 0; i--)
{
int j = Random.Range(0, i + 1);
(list[i], list[j]) = (list[j], list[i]);
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 79c87d7a89b04996bc35ac4bdb8a95dd
timeCreated: 1787569866
+21
View File
@@ -0,0 +1,21 @@
using UnityEngine;
namespace Utils
{
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
public static T Instance { get; private set; }
protected virtual void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this as T;
DontDestroyOnLoad(gameObject);
}
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1c0c51556b4d40b596303c15a761a80e
timeCreated: 1787575635