From 1ed296be27e87af14334a1c380d4b777802990b7 Mon Sep 17 00:00:00 2001 From: Peter Klooster Date: Tue, 19 Nov 2024 16:01:24 +0100 Subject: [PATCH] Fixes #306 and #307 --- .../Goap/Capabilities/TestCapability.asset | 23 +++++++ .../Capabilities/TestCapability.asset.meta | 8 +++ .../Elements/ListElementBase.cs | 61 ++++++++++++------- .../Elements/SensorList.cs | 37 +++++------ 4 files changed, 91 insertions(+), 38 deletions(-) create mode 100644 Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Goap/Capabilities/TestCapability.asset create mode 100644 Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Goap/Capabilities/TestCapability.asset.meta diff --git a/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Goap/Capabilities/TestCapability.asset b/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Goap/Capabilities/TestCapability.asset new file mode 100644 index 00000000..42da03f0 --- /dev/null +++ b/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Goap/Capabilities/TestCapability.asset @@ -0,0 +1,23 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 875fbfa2d1734285b353878b31467c98, type: 3} + m_Name: TestCapability + m_EditorClassIdentifier: + goals: [] + actions: [] + worldSensors: [] + targetSensors: [] + multiSensors: [] + generatorScriptable: {fileID: 0} + references: + version: 2 + RefIds: [] diff --git a/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Goap/Capabilities/TestCapability.asset.meta b/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Goap/Capabilities/TestCapability.asset.meta new file mode 100644 index 00000000..f4fcdfa6 --- /dev/null +++ b/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Goap/Capabilities/TestCapability.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a14fa8c1bdb52e14a951ecc9fbc3169b +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Package/Editor/CrashKonijn.Goap.Editor/Elements/ListElementBase.cs b/Package/Editor/CrashKonijn.Goap.Editor/Elements/ListElementBase.cs index ce3c6cdb..5a624f74 100644 --- a/Package/Editor/CrashKonijn.Goap.Editor/Elements/ListElementBase.cs +++ b/Package/Editor/CrashKonijn.Goap.Editor/Elements/ListElementBase.cs @@ -30,7 +30,7 @@ public void Rebuild() { return; } - + for (var i = 0; i < this.property.arraySize; i++) { this.CreateElement(i); @@ -38,20 +38,20 @@ public void Rebuild() if (this.buttonContainer is not null) return; - + this.buttonContainer = new VisualElement(); this.buttonContainer.style.flexDirection = FlexDirection.Row; this.buttonContainer.style.justifyContent = Justify.FlexEnd; var addButton = new Button(this.AddItem) { - text = "+" + text = "+", }; this.buttonContainer.Add(addButton); var removeButton = new Button(this.RemoveSelectedItem) { - text = "-" + text = "-", }; this.buttonContainer.Add(removeButton); @@ -60,16 +60,19 @@ public void Rebuild() private void CreateElement(int i) { + if (i < 0 || i >= this.list.Count) + return; + var value = this.list[i]; var prop = this.property.GetArrayElementAtIndex(i); - + var element = this.CreateListItem(prop, value); element.RegisterCallback(evt => { this.selectedItemIndex = i; }); this.elementsRoot.Add(element); - + this.BindListItem(prop, element, value, i); } @@ -79,46 +82,62 @@ private void BindListItem(SerializedProperty property, VisualElement element, in { this.BindListItem(property, element as TRenderType, this.list[index], index); } - + protected abstract void BindListItem(SerializedProperty property, TRenderType element, TItemType item, int index); - + private void AddItem() { // Add your item to the scriptable's list // Example: scriptable.items.Add(newItem); this.property.arraySize++; - + // var element = this.property.GetArrayElementAtIndex(this.property.arraySize -1); // element.managedReferenceValue = new TItemType(); - + this.list.Add(new TItemType()); - - this.CreateElement(this.property.arraySize -1); + + this.CreateElement(this.property.arraySize - 1); this.CloseAll(); - - this.GetFoldable(this.property.arraySize -1).Foldout.value = true; - - // this.Rebuild(); // Refresh the ListView + + var foldable = this.GetFoldable(this.property.arraySize - 1); + + if (foldable is null) + return; + + foldable.Foldout.value = true; + + this.Rebuild(); // Refresh the ListView } private void RemoveSelectedItem() { + if (this.selectedItemIndex < 0 || this.selectedItemIndex >= this.property.arraySize) + return; + this.list.RemoveAt(this.selectedItemIndex); - + this.Rebuild(); } private void CloseAll() { - for (int i = 0; i < this.property.arraySize; i++) + for (var i = 0; i < this.property.arraySize; i++) { - this.GetFoldable(i).Foldout.value = false; + var foldable = this.GetFoldable(i); + + if (foldable is null) + continue; + + foldable.Foldout.value = false; } } - + private IFoldable GetFoldable(int index) { + if (index < 0 || index >= this.elementsRoot.childCount) + return null; + return this.elementsRoot.ElementAt(index) as IFoldable; } } @@ -127,4 +146,4 @@ public interface IFoldable { public Foldout Foldout { get; } } -} \ No newline at end of file +} diff --git a/Package/Editor/CrashKonijn.Goap.Editor/Elements/SensorList.cs b/Package/Editor/CrashKonijn.Goap.Editor/Elements/SensorList.cs index 05577684..9d4283e6 100644 --- a/Package/Editor/CrashKonijn.Goap.Editor/Elements/SensorList.cs +++ b/Package/Editor/CrashKonijn.Goap.Editor/Elements/SensorList.cs @@ -16,7 +16,7 @@ public SensorList(SerializedObject serializedObject, CapabilityConfigScriptable { this.scriptable = scriptable; this.generator = generator; - + this.Rebuild(); } @@ -28,7 +28,7 @@ protected override CapabilitySensorElement CreateListItem(SerializedProperty pro protected override void BindListItem(SerializedProperty property, CapabilitySensorElement element, TSensorType item, int index) { element.Foldout.text = this.GetName(item); - + this.Bind(element, item); } @@ -54,7 +54,7 @@ private void BindSensor(CapabilitySensorElement element, CapabilityWorldSensor i { element.Foldout.text = this.GetName(item); }); - + element.KeyField.Bind(this.scriptable, item.worldKey, this.generator.GetWorldKeys(), classRef => { element.Foldout.text = this.GetName(item); @@ -67,7 +67,7 @@ private void BindSensor(CapabilitySensorElement element, CapabilityTargetSensor { element.Foldout.text = this.GetName(item); }); - + element.KeyField.Bind(this.scriptable, item.targetKey, this.generator.GetTargetKeys(), classRef => { element.Foldout.text = this.GetName(item); @@ -80,24 +80,27 @@ private void BindSensor(CapabilitySensorElement element, CapabilityMultiSensor i { element.Foldout.text = this.GetName(item); }); - - var sensors = this.GetMultiSensors(item.sensor, this.generator.GetMultiSensors()); + + var sensors = this.GetMultiSensors(item.sensor, this.generator.GetMultiSensors()); element.LabelField.text = "- " + string.Join("\n- ", sensors); - + element.Foldout.text = $"{item} ({sensors.Length})"; } public string[] GetMultiSensors(ClassRef classRef, Script[] scripts) { var match = classRef.GetMatch(scripts); - + if (match.status == ClassRefStatus.None) return Array.Empty(); - + + if (match.script == null) + return Array.Empty(); + // Create instance of type var instance = (IMultiSensor) Activator.CreateInstance(match.script.Type); - + return instance.GetSensors(); } @@ -110,7 +113,7 @@ public string GetName(CapabilitySensor item) if (item is CapabilityWorldSensor worldSensor) { - var scopes = new List(){ worldSensor.worldKey.Name }; + var scopes = new List() { worldSensor.worldKey.Name }; scopes.AddRange(this.GetScopes(worldSensor.sensor, this.generator.GetWorldSensors())); return $"{worldSensor.sensor.Name} ({string.Join(", ", scopes)})"; @@ -118,7 +121,7 @@ public string GetName(CapabilitySensor item) if (item is CapabilityTargetSensor targetSensor) { - var scopes = new List(){ targetSensor.targetKey.Name }; + var scopes = new List() { targetSensor.targetKey.Name }; scopes.AddRange(this.GetScopes(targetSensor.sensor, this.generator.GetTargetSensors())); return $"{targetSensor.sensor.Name} ({string.Join(", ", scopes)})"; @@ -133,19 +136,19 @@ private string[] GetScopes(ClassRef classRef, Script[] scripts) if (status == ClassRefStatus.None) return Array.Empty(); - + if (status == ClassRefStatus.Empty) return Array.Empty(); - + var scopes = new List(); - + if (typeof(ILocalSensor).IsAssignableFrom(match.Type)) scopes.Add("local"); - + if (typeof(IGlobalSensor).IsAssignableFrom(match.Type)) scopes.Add("global"); return scopes.ToArray(); } } -} \ No newline at end of file +}