Initial commit

This commit is contained in:
LadyAliceMargatroid 2024-07-25 21:46:18 -07:00
parent 1af2b57c1c
commit eebf00988e
30 changed files with 1883 additions and 21 deletions

View file

@ -0,0 +1,72 @@
using DunGen;
using DunGenPlus.Components.DoorwayCleanupScripting;
using DunGenPlus.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components {
public class DoorwayCleanup : MonoBehaviour, IDungeonCompleteReceiver {
[Header("Doorway References")]
[Tooltip("The doorway reference.")]
public Doorway doorway;
[Tooltip("The connectors scene objects of the doorway.\n\nHighly advise to empty the corresponding list in the doorway.")]
public List<GameObject> connectors;
[Tooltip("The blockers scene objects of the doorway.\n\nHighly advise to empty the corresponding list in the doorway.")]
public List<GameObject> blockers;
[Tooltip("The doorway gameobject target for the DoorwayCleanupScripts. Can be null.")]
public GameObject doorwayGameObject;
[Header("Overrides")]
[Tooltip("Mainly for code purposes. Forces the connectors to be active.")]
public bool overrideConnector;
[Tooltip("Mainly for code purposes. Forces the blockers to be active.")]
public bool overrideBlocker;
[Tooltip("Mainly for code purposes. Forces the doorway gameobject to be disabled.")]
public bool overrideNoDoorway;
public void OnDungeonComplete(Dungeon dungeon) {
SetBlockers(true);
DoorwayManager.AddDoorwayCleanup(this);
}
public void Cleanup(){
// start up like in original
SwitchConnectorBlocker(doorway.ConnectedDoorway != null);
var cleanupList = GetComponentsInChildren<DoorwayCleanupScript>();
foreach(var c in cleanupList) c.Cleanup(this);
if (overrideNoDoorway) SwitchDoorwayGameObject(false);
// clean up like in original
foreach(var c in connectors){
if (!c.activeSelf) UnityEngine.Object.DestroyImmediate(c, false);
}
foreach(var b in blockers){
if (!b.activeSelf) UnityEngine.Object.DestroyImmediate(b, false);
}
}
public void SetBlockers(bool state){
foreach(var b in blockers) b.SetActive(state);
}
public void SwitchConnectorBlocker(bool isConnector){
if (overrideConnector) isConnector = true;
if (overrideBlocker) isConnector = false;
foreach(var c in connectors) c.SetActive(isConnector);
foreach(var b in blockers) b.SetActive(!isConnector);
}
public void SwitchDoorwayGameObject(bool isActive){
doorwayGameObject?.SetActive(isActive);
}
}
}

View file

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components.DoorwayCleanupScripting {
public class DCSConnectorBlockerSpawnedPrefab : DoorwayCleanupScript {
public enum Action { SwitchToConnector, SwitchToBlocker };
[Header("Calls switch action\nif Doorway instantiates a Connector/Blocker prefab with the target's name")]
[Header("Switch Action")]
public Action switchAction;
[Header("Target")]
public GameObject target;
public override void Cleanup(DoorwayCleanup parent) {
var result = false;
foreach(Transform t in parent.doorway.transform){
if (t.gameObject.activeSelf && t.name.Contains(target.name)) {
result = true;
break;
}
}
if (result) {
parent.SwitchConnectorBlocker(switchAction == Action.SwitchToConnector);
}
}
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components.DoorwayCleanupScripting {
public class DCSRemoveDoorwayConnectedDoorway : DoorwayCleanupScriptDoorwayCompare {
[Header("Removes Doorway Gameobject\nif the neighboring doorway's priority matches the operation comparison")]
[Header("Operation Comparison")]
public int doorwayPriority;
public Operation operation = Operation.Equal;
public override void Cleanup(DoorwayCleanup parent) {
var doorway = parent.doorway;
if (doorway.connectedDoorway == null) return;
var result = GetOperation(operation).Invoke(doorway.connectedDoorway, doorwayPriority);
if (result) {
parent.SwitchDoorwayGameObject(false);
}
}
}
}

View file

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components.DoorwayCleanupScripting {
public class DCSRemoveDoorwaySpawnedPrefab : DoorwayCleanupScript {
[Header("Removes Doorway Gameobject\nif Doorway instantiates a Connector/Blocker prefab with the target's name")]
[Header("Target")]
public GameObject target;
public override void Cleanup(DoorwayCleanup parent) {
var result = false;
foreach(Transform t in parent.doorway.transform){
if (t.gameObject.activeSelf && t.name.Contains(target.name)) {
result = true;
break;
}
}
if (result) {
parent.SwitchDoorwayGameObject(false);
}
}
}
}

View file

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components.DoorwayCleanupScripting {
public class DCSRemoveGameObjectsConnectedDoorway : DoorwayCleanupScriptDoorwayCompare {
[Header("Removes target GameObjects\nif the neighboring doorway's priority matches the operation comparison")]
[Header("Operation Comparison")]
public int doorwayPriority;
public Operation operation = Operation.Equal;
[Header("Targets")]
public List<GameObject> targets;
public override void Cleanup(DoorwayCleanup parent) {
var doorway = parent.doorway;
if (doorway.connectedDoorway == null) return;
var result = GetOperation(operation).Invoke(doorway.connectedDoorway, doorwayPriority);
if (result) {
foreach(var t in targets) t.SetActive(false);
}
}
}
}

View file

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components.DoorwayCleanupScripting {
public abstract class DoorwayCleanupScript : MonoBehaviour {
public abstract void Cleanup(DoorwayCleanup parent);
}
}

View file

@ -0,0 +1,44 @@
using DunGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DunGenPlus.Components.DoorwayCleanupScripting {
public abstract class DoorwayCleanupScriptDoorwayCompare : DoorwayCleanupScript {
public enum Operation { Equal, NotEqual, LessThan, GreaterThan }
public Func<Doorway, int, bool> GetOperation(Operation operation){
switch(operation){
case Operation.Equal:
return EqualOperation;
case Operation.NotEqual:
return NotEqualOperation;
case Operation.LessThan:
return LessThanOperation;
case Operation.GreaterThan:
return GreaterThanOperation;
}
return null;
}
public bool EqualOperation(Doorway other, int doorwayPriority){
return other.DoorPrefabPriority == doorwayPriority;
}
public bool NotEqualOperation(Doorway other, int doorwayPriority){
return other.DoorPrefabPriority != doorwayPriority;
}
public bool LessThanOperation(Doorway other, int doorwayPriority){
return other.DoorPrefabPriority < doorwayPriority;
}
public bool GreaterThanOperation(Doorway other, int doorwayPriority){
return other.DoorPrefabPriority > doorwayPriority;
}
}
}

View file

@ -0,0 +1,64 @@
using DunGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components {
public class DoorwaySisters : MonoBehaviour {
private Doorway _self;
public Doorway Self {
get {
if (_self == null) {
_self = GetComponent<Doorway>();
}
return _self;
}
}
[Tooltip("The list of 'sister' doorways.\n\nUseDoorwaySisters must be toggled in DunGenExtender for this component to be used.\n\nThis doorway will not generate if it's an intersecting doorway, any of it's 'sister' doorways are generated, and both this doorway and the 'sister' doorway lead to the same tile.")]
public List<Doorway> sisters;
void OnValidate(){
var sis = sisters.Select(s => s.GetComponent<DoorwaySisters>());
foreach(var s in sis) {
if (s == null) continue;
s.TryAddSisterDoorway(Self);
}
}
public void TryAddSisterDoorway(Doorway doorway){
if (sisters.Contains(doorway)) return;
sisters.Add(doorway);
}
public void OnDrawGizmosSelected(){
var center = transform.position + Vector3.up;
if (sisters == null) return;
foreach(var sis in sisters){
var target = sis.transform.position + Vector3.up;
var comp = sis.GetComponent<DoorwaySisters>();
var self = Self;
if (self == null) {
Gizmos.color = Color.magenta;
} else if (comp == null || comp.sisters == null){
Gizmos.color = Color.yellow;
} else if (!comp.sisters.Contains(self)) {
Gizmos.color = Color.red;
} else {
Gizmos.color = Color.green;
}
Gizmos.DrawLine(center, target);
Gizmos.DrawSphere((center + target) * 0.5f, 0.25f);
}
}
}
}

View file

@ -0,0 +1,36 @@
using DunGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components {
public class MainRoomDoorwayGroups : MonoBehaviour {
[System.Serializable]
public class DoorwayList {
[Tooltip("For organizing purposes. Has no effect.")]
public string name;
[Tooltip("The group of doorways.")]
public List<Doorway> doorways;
public bool Contains(Doorway target) {
return doorways.Contains(target);
}
}
[Tooltip("When an additional main path is being generated, it will get the doorway used for the previous main path, find it's corresponding group below, and prevents the dungeon generation from using that group's doorways until the main paths are all generated.\n\nIf you want this feature, this must be attached to the tile that will act as the MainRoomTilePrefab.\n\nThis is designed for the scenario where you would like the main paths to be generated more evenly throughout the MainRoomTilePrefab.")]
public List<DoorwayList> doorwayLists;
public List<Doorway> doorwayListFirst => doorwayLists.Count > 0 ? doorwayLists[0].doorways : null;
public List<Doorway> GrabDoorwayGroup(Doorway target){
foreach(var a in doorwayLists){
if (a.Contains(target)) return a.doorways;
}
return null;
}
}
}

View file

@ -0,0 +1,50 @@
using DunGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace DunGenPlus.Components.Props
{
public class SpawnSyncedObjectCycle : MonoBehaviour, IDungeonCompleteReceiver {
public static int cycle;
public static Dictionary<int, int> cycleDictionary;
[Tooltip("The SpawnSyncedObject reference.\n\nWhen the dungeon generation finishes, the spawnPrefab of the referenced SpawnSyncedObject will change to one of the Props based on a cycle. The starting value is random.\n\nThis is designed for the scenario where you have multiple very similar networked gameobjects that serve the same purpose, and you just want them all to spawn equally for diversity sake.")]
public SpawnSyncedObject Spawn;
[Tooltip("The unique id for this script's cycle.\n\nWhen the dungeon generation finishes, a random cycle value is calculated for each Id. Each script will reference their Id's corresponding cycle value to determine their Prop, and advance the cycle value by 1.")]
public int Id;
[Tooltip("The list of props that would selected based on a cycle.")]
public List<GameObject> Props = new List<GameObject>();
void Reset(){
Spawn = GetComponent<SpawnSyncedObject>();
}
public static void UpdateCycle(int value){
Plugin.logger.LogInfo($"Updating SpawnSyncedObject start cycle to {value}");
cycle = value;
cycleDictionary = new Dictionary<int, int>();
}
public int GetCycle(int id){
if (!cycleDictionary.TryGetValue(id, out var value)){
value = cycle;
cycleDictionary.Add(id, value);
}
cycleDictionary[id] = value + 1;
Plugin.logger.LogInfo($"Cycle{id}: {value}");
return value;
}
public void OnDungeonComplete(Dungeon dungeon) {
var index = GetCycle(Id) % Props.Count;
var prefab = Props[index];
Spawn.spawnPrefab = prefab;
}
}
}