From 1654c165ca936ce2f38f5976b238969d87bc1997 Mon Sep 17 00:00:00 2001 From: Nickc01 Date: Wed, 18 Dec 2024 13:11:04 -0600 Subject: [PATCH] Added Safety Checks to EnemyWaveEntryDrawer --- .../Colosseum/EnemyWaveEntryDrawer.cs | 92 +++++++++++-------- .../Components/Colosseum/ColosseumPlatform.cs | 45 ++++++++- 2 files changed, 96 insertions(+), 41 deletions(-) diff --git a/WeaverCore.Editor/Colosseum/EnemyWaveEntryDrawer.cs b/WeaverCore.Editor/Colosseum/EnemyWaveEntryDrawer.cs index b23491f4..9621bb55 100644 --- a/WeaverCore.Editor/Colosseum/EnemyWaveEntryDrawer.cs +++ b/WeaverCore.Editor/Colosseum/EnemyWaveEntryDrawer.cs @@ -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) { @@ -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 @@ -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; } @@ -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; @@ -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; @@ -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 options = new List(); - options.Add("Empty"); + List options = new List { "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) @@ -147,15 +161,12 @@ private string[] GetEnemyOptions(ColosseumRoomManager challenge) } } } - - //options.AddRange(challenge.preloadedEnemyNames); return options.ToArray(); } private string[] GetSpawnLocationOptions(ColosseumRoomManager challenge) { - List options = new List(); - options.Add("Empty"); + List options = new List { "Empty" }; foreach (var spawn in challenge.spawnLocations) { options.Add(spawn.name); @@ -163,3 +174,4 @@ private string[] GetSpawnLocationOptions(ColosseumRoomManager challenge) return options.ToArray(); } } + diff --git a/WeaverCore/Components/Colosseum/ColosseumPlatform.cs b/WeaverCore/Components/Colosseum/ColosseumPlatform.cs index f13ef116..d6f047fa 100644 --- a/WeaverCore/Components/Colosseum/ColosseumPlatform.cs +++ b/WeaverCore/Components/Colosseum/ColosseumPlatform.cs @@ -57,6 +57,8 @@ public class ColosseumPlatform : MonoBehaviour, IColosseumIdentifier [NonSerialized] float _startTime = -1; + //[SerializeField] private bool resetCurves = false; + [NonSerialized] ColosseumPlatformController platform; @@ -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) @@ -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;