Skip to content

Commit

Permalink
Added Safety Checks to EnemyWaveEntryDrawer
Browse files Browse the repository at this point in the history
  • Loading branch information
nickc01 committed Dec 18, 2024
1 parent c89a668 commit 1654c16
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 41 deletions.
92 changes: 52 additions & 40 deletions WeaverCore.Editor/Colosseum/EnemyWaveEntryDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using WeaverCore.Components.Colosseum;

[CustomPropertyDrawer(typeof(EnemyWaveEntry))]
public class EnemyWaveEntryDrawer : PropertyDrawer
public class WaveEntryDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Expand All @@ -28,23 +28,39 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
float lineHeight = EditorGUIUtility.singleLineHeight;
float y = position.y;

// Enemy Name dropdown
// Enemy Name field
SerializedProperty enemyNameProp = property.FindPropertyRelative("enemyName");
Rect enemyRect = new Rect(position.x, y, position.width, lineHeight);
string[] enemyOptions = GetEnemyOptions(challenge);
string[] aliases = GetEnemyAliases(challenge, enemyOptions);
int enemyIndex = Mathf.Max(0, System.Array.IndexOf(enemyOptions, enemyNameProp.stringValue));
enemyIndex = EditorGUI.Popup(enemyRect, "Enemy", enemyIndex, aliases);
enemyNameProp.stringValue = enemyOptions[enemyIndex];
int enemyIndex = System.Array.IndexOf(enemyOptions, enemyNameProp.stringValue);

if (enemyIndex < 0) // Not in the options
{
DrawInvalidTextField(enemyRect, enemyNameProp, "Enemy not found in Colosseum");
}
else
{
enemyIndex = EditorGUI.Popup(enemyRect, "Enemy", enemyIndex, aliases);
enemyNameProp.stringValue = enemyOptions[enemyIndex];
}
y += lineHeight + EditorGUIUtility.standardVerticalSpacing;

// Spawn Location dropdown
// Spawn Location field
SerializedProperty spawnLocationNameProp = property.FindPropertyRelative("spawnLocationName");
Rect spawnRect = new Rect(position.x, y, position.width, lineHeight);
string[] spawnOptions = GetSpawnLocationOptions(challenge);
int spawnIndex = Mathf.Max(0, System.Array.IndexOf(spawnOptions, spawnLocationNameProp.stringValue));
spawnIndex = EditorGUI.Popup(spawnRect, "Spawn Location", spawnIndex, spawnOptions);
spawnLocationNameProp.stringValue = spawnOptions[spawnIndex];
int spawnIndex = System.Array.IndexOf(spawnOptions, spawnLocationNameProp.stringValue);

if (spawnIndex < 0) // Not in the options
{
DrawInvalidTextField(spawnRect, spawnLocationNameProp, "Location not found in Colosseum");
}
else
{
spawnIndex = EditorGUI.Popup(spawnRect, "Spawn Location", spawnIndex, spawnOptions);
spawnLocationNameProp.stringValue = spawnOptions[spawnIndex];
}
y += lineHeight + EditorGUIUtility.standardVerticalSpacing;

// Delay Before Spawn
Expand All @@ -59,19 +75,15 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
EditorGUI.PropertyField(prioritizedRect, isPrioritizedProp);
y += lineHeight + EditorGUIUtility.standardVerticalSpacing;

// Entry Color
SerializedProperty entryColorProp = property.FindPropertyRelative("entryColor");

var value = entryColorProp.colorValue;

var first = wave.entries.FirstOrDefault(e => e.entryColor == value);
var last = wave.entries.LastOrDefault(e => e.entryColor == value);



if (value == default || (last != first && value == last.entryColor && value == first.entryColor))
{
value = new Color(Random.value, Random.value, Random.value);

entryColorProp.colorValue = value;
}

Expand All @@ -86,6 +98,24 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
EditorGUI.EndProperty();
}

private void DrawInvalidTextField(Rect rect, SerializedProperty prop, string errorMessage)
{
// Set color to red
Color previousColor = GUI.color;
GUI.color = Color.red;

// Draw the text field
Rect fieldRect = new Rect(rect.x, rect.y, rect.width * 0.7f, rect.height);
prop.stringValue = EditorGUI.TextField(fieldRect, prop.displayName, prop.stringValue);

// Reset color
GUI.color = previousColor;

// Draw the error message
Rect labelRect = new Rect(rect.x + fieldRect.width + 5, rect.y, rect.width * 0.3f - 5, rect.height);
EditorGUI.LabelField(labelRect, errorMessage, EditorStyles.miniLabel);
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) * 5;
Expand All @@ -94,7 +124,6 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
private string[] GetEnemyAliases(ColosseumRoomManager challenge, string[] names)
{
string[] aliases = new string[names.Length];

for (int i = 0; i < names.Length; i++)
{
string foundAlias = null;
Expand All @@ -106,37 +135,22 @@ private string[] GetEnemyAliases(ColosseumRoomManager challenge, string[] names)
goto Outer;
}
}

Outer:

if (foundAlias != null)
{
aliases[i] = foundAlias + $" (aka {names[i]})";
}
else
{
aliases[i] = names[i];
}
Outer:
aliases[i] = foundAlias != null ? $"{foundAlias} (aka {names[i]})" : names[i];
}

return aliases;
}

private string[] GetEnemyOptions(ColosseumRoomManager challenge)
{
List<string> options = new List<string>();
options.Add("Empty");
List<string> options = new List<string> { "Empty" };
if (challenge.enemyPrefabs != null)
{
foreach (GameObject prefab in challenge.enemyPrefabs)
{
if (prefab != null)
{
options.Add(prefab.name);
}
if (prefab != null) options.Add(prefab.name);
}
}

if (challenge.preloadedEnemies != null)
{
foreach (var preloadedObj in challenge.preloadedEnemies)
Expand All @@ -147,19 +161,17 @@ private string[] GetEnemyOptions(ColosseumRoomManager challenge)
}
}
}

//options.AddRange(challenge.preloadedEnemyNames);
return options.ToArray();
}

private string[] GetSpawnLocationOptions(ColosseumRoomManager challenge)
{
List<string> options = new List<string>();
options.Add("Empty");
List<string> options = new List<string> { "Empty" };
foreach (var spawn in challenge.spawnLocations)
{
options.Add(spawn.name);
}
return options.ToArray();
}
}

45 changes: 44 additions & 1 deletion WeaverCore/Components/Colosseum/ColosseumPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class ColosseumPlatform : MonoBehaviour, IColosseumIdentifier
[NonSerialized]
float _startTime = -1;

//[SerializeField] private bool resetCurves = false;

[NonSerialized]
ColosseumPlatformController platform;

Expand Down Expand Up @@ -119,6 +121,47 @@ private void Awake()
}
}

/*private void SetDefaultCurves()
{
// Initialize expand curve
expandCurve = new AnimationCurve();
expandCurve.AddKey(new Keyframe(0.0f, 0.0f, 1.0f, 1.0f)); // Linear start
expandCurve.AddKey(new Keyframe(0.95f, 1.05f, 0.0f, 0.0f)); // Overshoot at 0.95
expandCurve.AddKey(new Keyframe(1.0f, 1.0f, -1.0f, -1.0f)); // Back to 1.0
// Initialize retract curve
retractCurve = new AnimationCurve();
retractCurve.AddKey(new Keyframe(0.0f, 0.0f, 1.0f, 1.0f)); // Start at 0.0
retractCurve.AddKey(new Keyframe(0.05f, -0.05f, 0.0f, 0.0f)); // Dip below at 0.05
retractCurve.AddKey(new Keyframe(1.0f, 1.0f, 1.0f, 1.0f)); // Linearly return to 1.0
#if UNITY_EDITOR
// Force linear tangents
for (int i = 0; i < expandCurve.keys.Length; i++)
{
AnimationUtility.SetKeyLeftTangentMode(expandCurve, i, AnimationUtility.TangentMode.Linear);
AnimationUtility.SetKeyRightTangentMode(expandCurve, i, AnimationUtility.TangentMode.Linear);
}
for (int i = 0; i < retractCurve.keys.Length; i++)
{
AnimationUtility.SetKeyLeftTangentMode(retractCurve, i, AnimationUtility.TangentMode.Linear);
AnimationUtility.SetKeyRightTangentMode(retractCurve, i, AnimationUtility.TangentMode.Linear);
}
#endif
}
private void OnValidate()
{
if (resetCurves)
{
Debug.Log("Resetting animation curves to default values.");
SetDefaultCurves();
resetCurves = false; // Reset the toggle
}
}*/

IEnumerator TestingRoutine()
{
while (true)
Expand Down Expand Up @@ -259,7 +302,7 @@ IEnumerator TweenObject(Transform obj, Vector3 from, Vector3 to, AnimationCurve
{
for (float t = 0; t < time; t += Time.deltaTime)
{
obj.localPosition = Vector3.Lerp(from, to, curve.Evaluate(t / time));
obj.localPosition = Vector3.LerpUnclamped(from, to, curve.Evaluate(t / time));
yield return null;
}
obj.localPosition = to;
Expand Down

0 comments on commit 1654c16

Please sign in to comment.