This commit is contained in:
Gerard Gascón 2025-04-24 17:37:25 +02:00
commit 341a877b4a
2338 changed files with 1346408 additions and 0 deletions

View file

@ -0,0 +1,261 @@
//#define _DIFFICULTYDEBUG
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DifficultyEstablisher : MonoBehaviour
{
DungeonGen dung;
[Header("Difficulty Rates")]
[Space(5)]
public bool smartEdit = false;
[Range(0, 100)]
public float easyRate = 0;
float easyRateOriginal;
[Range(0, 100)]
public float mediumRate = 0;
float mediumRateOriginal;
[Range(0, 100)]
public float hardRate = 0;
float hardRateOriginal;
[Header("Difficulty pity progress")]
[Space(5)]
public float easyPityProgress = 0;
public float mediumPityProgress = 0;
public float hardPityProgress = 0;
[Space(10)]
[MinMaxCustomSlider]
public ValueRange mediumRoomsMargin;
[Space(10)]
[MinMaxCustomSlider]
public ValueRange hardRoomsMargin;
//Room amount
[SerializeField]
List<GameObject> easyRooms;
[SerializeField]
List<GameObject> medRooms;
[SerializeField]
List<GameObject> hardRooms;
private void OnValidate()
{
if (smartEdit)
{
float totalRate = easyRate + mediumRate + hardRate;
if (totalRate != 100)
{
easyRate = (easyRate * 100) / (totalRate);
mediumRate = (mediumRate * 100) / (totalRate);
hardRate = (hardRate * 100) / (totalRate);
}
}
#region MinMaxSlider to int
mediumRoomsMargin.MaxValue = Mathf.Round(mediumRoomsMargin.MaxValue);
mediumRoomsMargin.MinValue = Mathf.Round(mediumRoomsMargin.MinValue);
mediumRoomsMargin.MaxLimit = Mathf.Round(mediumRoomsMargin.MaxLimit);
mediumRoomsMargin.MinLimit = Mathf.Round(mediumRoomsMargin.MinLimit);
hardRoomsMargin.MaxValue = Mathf.Round(hardRoomsMargin.MaxValue);
hardRoomsMargin.MinValue = Mathf.Round(hardRoomsMargin.MinValue);
hardRoomsMargin.MaxLimit = Mathf.Round(hardRoomsMargin.MaxLimit);
hardRoomsMargin.MinLimit = Mathf.Round(hardRoomsMargin.MinLimit);
#endregion
mediumRoomsMargin.Name = "Medium room limits";
hardRoomsMargin.Name = "Hard room limits";
}
public static DifficultyEstablisher instance;
private void Awake()
{
instance = this;
dung = GetComponent<DungeonGen>();
easyRateOriginal = easyRate;
mediumRateOriginal = mediumRate;
hardRateOriginal = hardRate;
easyRooms = new List<GameObject>();
medRooms = new List<GameObject>();
hardRooms = new List<GameObject>();
}
[ContextMenu("Progress Med")]
private void ProgressMediumRate()
{
mediumRate += mediumPityProgress * Utils.difficultyFormula(GameMaster.Instance.LevelNumber, GameMaster.Instance.Difficulty);
easyRate = 100 - mediumRate - hardRate;
}
[ContextMenu("Progress Hard")]
private void ProgressHardRate()
{
hardRate += hardPityProgress * Utils.difficultyFormula(GameMaster.Instance.LevelNumber, GameMaster.Instance.Difficulty);
easyRate = 100 - mediumRate - hardRate;
}
Difficulty getRandomDifficulty()
{
float rate = Random.value;
if (rate <= easyRate / 100)
{
ProgressMediumRate();
ProgressHardRate();
return Difficulty.Easy;
}
else if (rate <= (mediumRate / 100 + easyRate / 100))
{
mediumRate = mediumRateOriginal;
ProgressHardRate();
//if (medRooms >= mediumRoomsMargin.MaxValue)
//{
// mediumRate = 0;
// float totalRate = easyRate + hardRooms >= hardRoomsMargin.MaxValue ? 0 : hardRooms;
// easyRate = (easyRate * 100) / (totalRate);
// if (hardRooms < hardRoomsMargin.MaxValue)
// hardRate = (hardRate * 100) / (totalRate);
//}
return Difficulty.Medium;
}
else
{
hardRate = hardRateOriginal;
ProgressMediumRate();
//if (hardRooms >= hardRoomsMargin.MaxValue)
//{
// hardRate = 0;
// float totalRate = easyRate + medRooms >= mediumRoomsMargin.MaxValue ? 0 : medRooms;
// easyRate = (easyRate * 100) / (totalRate);
// if(medRooms < mediumRoomsMargin.MaxValue)
// mediumRate = (mediumRate * 100) / (totalRate);
//}
return Difficulty.Hard;
}
}
void setDungeonDifficulty()
{
foreach (GameObject r in dung.Dungeon)
{
Room localRoom = r.GetComponent<Room>();
if (localRoom.Difficulty == Difficulty.None)
{
localRoom.debugDiff.easyRate = easyRate;
localRoom.debugDiff.mediumRate = mediumRate;
localRoom.debugDiff.hardRate = hardRate;
localRoom.debugDiff.hardEstablished = false;
localRoom.Difficulty = getRandomDifficulty();
switch (localRoom.Difficulty)
{
case Difficulty.Easy:
easyRooms.Add(r);
break;
case Difficulty.Medium:
medRooms.Add(r);
break;
case Difficulty.Hard:
hardRooms.Add(r);
break;
}
}
}
#if _DIFFICULTYDEBUG
Utils.Print("Difficulty scalar: " + Utils.difficultyFormula(GameMaster.Instance.LevelNumber, GameMaster.Instance.Difficulty));
Utils.Print("Before:");
Utils.Print("Easy rooms: " + easyRooms.Count);
Utils.Print("Medium rooms: " + medRooms.Count);
Utils.Print("Hard rooms: " + hardRooms.Count);
#endif
//Medium rooms margin fix
if (medRooms.Count<mediumRoomsMargin.MinValue)
{
int difference = (int)mediumRoomsMargin.MinValue - medRooms.Count;
for (int i=0; i<difference; ++i)
{
GameObject transferedObject = easyRooms[Random.Range(0, Mathf.Max(easyRooms.Count - 1, 0))];
transferedObject.GetComponent<Room>().Difficulty = Difficulty.Medium;
transferedObject.GetComponent<Room>().debugDiff.hardEstablished = true;
medRooms.Add(transferedObject);
easyRooms.Remove(transferedObject);
}
}
else if(medRooms.Count > mediumRoomsMargin.MaxValue)
{
int difference = medRooms.Count - (int)mediumRoomsMargin.MaxValue;
for (int i = 0; i < difference; ++i)
{
GameObject transferedObject = medRooms[Random.Range(0, Mathf.Max(medRooms.Count - 1, 0))];
transferedObject.GetComponent<Room>().Difficulty = Difficulty.Easy;
transferedObject.GetComponent<Room>().debugDiff.hardEstablished = true;
easyRooms.Add(transferedObject);
medRooms.Remove(transferedObject);
}
}
//Hard rooms margin fix
if (hardRooms.Count < hardRoomsMargin.MinValue)
{
int difference = (int)hardRoomsMargin.MinValue - hardRooms.Count;
for (int i = 0; i < difference; ++i)
{
GameObject transferedObject = easyRooms[Random.Range(0, Mathf.Max(easyRooms.Count - 1, 0))];
transferedObject.GetComponent<Room>().Difficulty = Difficulty.Hard;
transferedObject.GetComponent<Room>().debugDiff.hardEstablished = true;
hardRooms.Add(transferedObject);
easyRooms.Remove(transferedObject);
}
}
else if (hardRooms.Count > hardRoomsMargin.MaxValue)
{
int difference = hardRooms.Count - (int)hardRoomsMargin.MaxValue;
for (int i = 0; i < difference; ++i)
{
GameObject transferedObject = hardRooms[Random.Range(0, Mathf.Max(hardRooms.Count - 1, 0))];
transferedObject.GetComponent<Room>().Difficulty = Difficulty.Easy;
transferedObject.GetComponent<Room>().debugDiff.hardEstablished = true;
easyRooms.Add(transferedObject);
hardRooms.Remove(transferedObject);
}
}
#if _DIFFICULTYDEBUG
Utils.Print("After:");
Utils.Print("Easy rooms: " + easyRooms.Count);
Utils.Print("Medium rooms: " + medRooms.Count);
Utils.Print("Hard rooms: " + hardRooms.Count);
#endif
easyRate = easyRateOriginal;
mediumRate = mediumRateOriginal;
hardRate = hardRateOriginal;
}
public void SetDifficulty()
{
//Invoke(nameof(setDungeonDifficulty), 0.2f);
setDungeonDifficulty();
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8f36a910757941146befac5bdf901fe9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,204 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DungeonFiller : MonoBehaviour
{
[System.Serializable]
public struct RoomTypes
{
public List<GameObject> easy;
public List<GameObject> medium;
public List<GameObject> hard;
}
public List<GameObject> dungeonContent;
public GameObject entryRoom;
public GameObject exitRoom;
public RoomTypes one_Entry;
public RoomTypes two_Entries;
public RoomTypes twoL_Entries;
public RoomTypes threeEntries;
public RoomTypes fourEntries;
//Atempt of singleton
public static DungeonFiller instance;
void Awake(){
instance = this;
}
private void Start()
{
dungeonContent = new List<GameObject>();
//Invoke(nameof(FillDungeon), 1f);
}
public void Fill(){
FillDungeon();
}
private void FillDungeon()
{
foreach(GameObject go in GetComponent<DungeonGen>().Dungeon)
{
//Paint all rooms red
//foreach(MeshRenderer mr in go.GetComponentsInChildren<MeshRenderer>())
//{
// mr.material.SetColor("RedChannel", Color.red);
//}
Room r = go.GetComponent<Room>();
//Determine target rotation depending on
Quaternion rotation = Quaternion.identity;
switch (r.RoomID)
{
default:
case 1:
rotation = Quaternion.Euler(0, 0, 0);
break;
case 2:
rotation = Quaternion.Euler(0, 90, 0);
break;
case 3:
rotation = Quaternion.Euler(0, 180, 0);
break;
case 4:
rotation = Quaternion.Euler(0, 270, 0);
break;
case 12:
rotation = Quaternion.Euler(0, 0, 0);
break;
case 13:
rotation = Quaternion.Euler(0, 0, 0);
break;
case 14:
rotation = Quaternion.Euler(0, 270, 0);
break;
case 23:
rotation = Quaternion.Euler(0, 90, 0);
break;
case 24:
rotation = Quaternion.Euler(0, 90, 0);
break;
case 34:
rotation = Quaternion.Euler(0, 180, 0);
break;
case 123:
rotation = Quaternion.Euler(0, 0, 0);
break;
case 124:
rotation = Quaternion.Euler(0, 270, 0);
break;
case 134:
rotation = Quaternion.Euler(0, 180, 0);
break;
case 234:
rotation = Quaternion.Euler(0, 90, 0);
break;
case 1234:
rotation = Quaternion.Euler(0, Random.Range(0, 3)*90, 0);
break;
}
int idLength = 0;
float auxID = r.RoomID;
while (auxID > 1)
{
idLength++;
auxID /= 10;
}
//Determine target prefabs depending on room entries
RoomTypes targetRoomType = new RoomTypes();
switch (idLength)
{
case 0:
case 1:
targetRoomType = one_Entry;
break;
case 2:
{
int num1 = Mathf.FloorToInt(r.RoomID / 10);
int num2 = r.RoomID % 10;
if (Utils.isOdd(num1) ^ Utils.isOdd(num2))
targetRoomType = twoL_Entries;
else
targetRoomType = two_Entries;
}
break;
case 3:
targetRoomType = threeEntries;
break;
case 4:
targetRoomType = fourEntries;
break;
}
//Difficulty
List<GameObject> targetPrefabs=null;
switch (r.Difficulty)
{
case Difficulty.Easy:
targetPrefabs = targetRoomType.easy;
break;
case Difficulty.Medium:
targetPrefabs = targetRoomType.medium;
break;
case Difficulty.Hard:
targetPrefabs = targetRoomType.hard;
break;
default:
break;
}
int z = 0;
if (targetPrefabs != null && targetPrefabs.Count!=0)
{
r.content = Instantiate(targetPrefabs[Random.Range(0, targetPrefabs.Count-1)], r.transform.position, rotation);
dungeonContent.Add(r.content);
}
else
{
int a = 0;
}
}
if (entryRoom != null)
{
Room entrance = GameMaster.Instance.Dungeon.EntryRoom.GetComponent<Room>();
entrance.content = Instantiate(entryRoom, entrance.transform.position, entrance.transform.rotation);
dungeonContent.Add(entrance.content);
}
if (exitRoom != null)
{
Room exit = GameMaster.Instance.Dungeon.ExitRoom.GetComponent<Room>();
Quaternion rotation = Quaternion.identity;
switch (exit.RoomID)
{
default:
case 1:
rotation = Quaternion.Euler(0, 0, 0);
break;
case 2:
rotation = Quaternion.Euler(0, 90, 0);
break;
case 3:
rotation = Quaternion.Euler(0, 180, 0);
break;
case 4:
rotation = Quaternion.Euler(0, 270, 0);
break;
}
exit.content = Instantiate(exitRoom, exit.transform.position, rotation);
dungeonContent.Add(exit.content);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 127a6cef63de33b4abaf3dc7c7d683b9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 865d31958c6b3a946a0ec3623b95e5a3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 20
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAtCamOnStart : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
transform.rotation = Quaternion.LookRotation(-Camera.main.transform.forward);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1451fb7b49debdc44b875474ea37ba77
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,246 @@
//#define _DIFFICULTYDEBUG
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum Gate
{
open, closed, none
}
public enum Difficulty
{
Easy, Medium, Hard, Special, None
}
public class Room : MonoBehaviour
{
public Utils.DifficultyDebug debugDiff;
[SerializeField]
private Difficulty difficulty=Difficulty.None;
public Difficulty Difficulty
{
get
{
return difficulty;
}
set
{
difficulty = value;
#if _DIFFICULTYDEBUG
switch (difficulty)
{
case Difficulty.Easy:
GetComponentInChildren<MeshRenderer>().material.SetColor("RedChannel", Color.green);
break;
case Difficulty.Medium:
GetComponentInChildren<MeshRenderer>().material.SetColor("RedChannel", Color.yellow);
break;
case Difficulty.Hard:
GetComponentInChildren<MeshRenderer>().material.SetColor("RedChannel", Color.red);
break;
case Difficulty.Special:
GetComponentInChildren<MeshRenderer>().material.SetColor("RedChannel", Color.blue);
break;
}
#endif
}
}
[SerializeField]
private int roomID;
public int RoomID
{
get
{
return roomID;
}
}
[Space(10)]
public Gate top = Gate.none;
public Gate right = Gate.none;
public Gate bottom = Gate.none;
public Gate left = Gate.none;
public bool TopWalkable
{
get
{
return (top == Gate.closed || top == Gate.open);
}
}
public bool RightWalkable
{
get
{
return (right == Gate.closed || right == Gate.open);
}
}
public bool BottomWalkable
{
get
{
return (bottom == Gate.closed || bottom == Gate.open);
}
}
public bool LeftWalkable
{
get
{
return (left == Gate.closed || left == Gate.open);
}
}
public GameObject[] rooms;
public GameObject content=null;
public bool OpenEdges()
{
return top==Gate.open || right == Gate.open || bottom == Gate.open || left == Gate.open;
}
private bool checkGates(bool t, bool r, bool b, bool l)
{
return
(t ? top == Gate.open || top==Gate.closed : top == Gate.none) &&
(r ? right == Gate.open || right == Gate.closed : right == Gate.none) &&
(b ? bottom == Gate.open || bottom == Gate.closed : bottom == Gate.none) &&
(l ? left == Gate.open || left == Gate.closed : left == Gate.none);
}
[ContextMenu("Update room")]
public GameObject updateRoom()
{
GameObject aux;
//one open cases
if(checkGates(true, false, false, false))
aux=rooms[0];
else if (checkGates(false, true, false, false))
aux=rooms[1];
else if (checkGates(false, false, true, false))
aux=rooms[2];
else if (checkGates(false, false, false, true))
aux=rooms[3];
//two open cases
else if (checkGates(true, true, false, false))
aux=rooms[4];
else if (checkGates(true, false, true, false))
aux=rooms[5];
else if (checkGates(true, false, false, true))
aux=rooms[6];
else if (checkGates(false, true, true, false))
aux=rooms[7];
else if (checkGates(false, true, false, true))
aux=rooms[8];
else if (checkGates(false, false, true, true))
aux=rooms[9];
//three open cases
else if (checkGates(true, true, true, false))
aux=rooms[10];
else if (checkGates(true, true, false, true))
aux=rooms[11];
else if (checkGates(true, false, true, true))
aux=rooms[12];
else if (checkGates(false, true, true, true))
aux=rooms[13];
//four open cases
else //(checkGates(true, true, true, true))
aux=rooms[14];
if (aux.GetComponent<Room>().RoomID != RoomID)
{
aux = Instantiate(aux, transform);
aux.transform.parent = null;
StartCoroutine(Autodestroy(0.1f));//GameMaster.Instance.ReplaceGameObject(aux, gameObject);
}
return aux;
}
IEnumerator Autodestroy(float time)
{
yield return new WaitForEndOfFrame();
if (Application.isPlaying)
Destroy(gameObject);
else if (Application.isEditor)
DestroyImmediate(gameObject);
}
//deprecated
public void siblingness()
{
Debug.Log("Awake " + gameObject.name);
for (int i = 0; i < 4; ++i)
{
Gate currentGate;
switch (i)
{
default:
case 0:
currentGate = top;
break;
case 1:
currentGate = right;
break;
case 2:
currentGate = bottom;
break;
case 3:
currentGate = left;
break;
}
if (currentGate != Gate.none)
{
Vector3 dir = Vector3.zero;
switch (i)
{
default:
case 0:
dir = Vector3.up;
break;
case 1:
dir = Vector3.right;
break;
case 2:
dir = Vector3.down;
break;
case 3:
dir = Vector3.left;
break;
}
RaycastHit[] rch = Physics.RaycastAll(transform.position, dir, 10);
foreach (RaycastHit r in rch)
{
if (r.collider.gameObject != gameObject && r.collider.gameObject.GetComponent<Room>() != null)
{
Debug.Log(r.collider.gameObject.name + " awaken my master");
switch (i)
{
default:
case 0:
top = r.collider.gameObject.GetComponent<Room>().BottomWalkable ? Gate.closed : Gate.none;
break;
case 1:
right = r.collider.gameObject.GetComponent<Room>().LeftWalkable ? Gate.closed : Gate.none;
break;
case 2:
bottom = r.collider.gameObject.GetComponent<Room>().TopWalkable ? Gate.closed : Gate.none;
break;
case 3:
left = r.collider.gameObject.GetComponent<Room>().RightWalkable ? Gate.closed : Gate.none;
break;
}
}
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 74890c04639ed1f4b8b20eeabe737f58
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoomContent : MonoBehaviour
{
public enum Difficulty
{
Easy, Medium, Hard
}
public enum RoomEntries
{
OneEntry, L_TwoEntries, TwoEntries, ThreeEntries, FourEntries
}
public Difficulty difficulty = Difficulty.Easy;
public RoomEntries entries = RoomEntries.OneEntry;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 202b664a0a2346b4599cf8e4d21d487d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
enum type
{
Debug,
}
public class RoomFloor : MonoBehaviour
{
[SerializeField]
type _type;
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b2e2d66d4d680a8458a20911762924bc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,80 @@
#define _DEBUG
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum Direction
{
_TOP, _RIGHT, _BOTTOM, _LEFT, _MAX
}
public static class Utils
{
public static Vector3 dirToVec(Direction direction)
{
switch (direction)
{
case Direction._TOP:
return new Vector3(0, 0, 1);
case Direction._RIGHT:
return new Vector3(1, 0, 0);
case Direction._BOTTOM:
return new Vector3(0, 0, -1);
case Direction._LEFT:
return new Vector3(-1, 0, 0);
}
return Vector3.one;
}
public const float easyDifficultyK = 200;
public const float mediumDifficultyK = 50;
public const float hardDifficultyK = 5;
public static float difficultyFormula(int levelNumber, float difficultyConstant)
{
return levelNumber / Mathf.Sqrt(levelNumber * levelNumber + difficultyConstant);
}
public static void Print(string text){
#if _DEBUG
Debug.Log(text);
#endif
}
public static bool isOdd(int num)
{
return num % 2 == 0;
}
public static float step(float val, float edge)
{
return val >= edge ? 1 : 0;
}
public static float remap(float value, float from1, float to1, float from2, float to2)
{
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}
public static bool spawnDisk()
{
int maxHp = 15;
int currentHealth=GameMaster.Instance.player.GetComponent<PlayerController>().health.currentHp;
float chance = Mathf.Min(.5f, 1-currentHealth / maxHp);
return chance>Random.value;
}
public static int RAM_cleanseSmall = 5;
public static int RAM_cleanseBig = 7;
[System.Serializable]
public struct DifficultyDebug
{
public bool hardEstablished;
public float easyRate;
public float mediumRate;
public float hardRate;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 44bbb4d79b55ba6439311f0b3a381d9a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: