Updated Enemy AI System to use STRIPS
This commit is contained in:
@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEditorInternal;
|
||||
using UnityEngine;
|
||||
using static STRIPS.StripsAction;
|
||||
|
||||
namespace STRIPS
|
||||
{
|
||||
[CustomEditor(typeof(StripsAction))]
|
||||
[CanEditMultipleObjects]
|
||||
public class StripsActionCustomEditor : Editor
|
||||
{
|
||||
|
||||
SerializedProperty associatedMapping;
|
||||
SerializedProperty preConditions;
|
||||
ReorderableList preConditionsList;
|
||||
SerializedProperty functions;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
associatedMapping = serializedObject.FindProperty("associatedMapping");
|
||||
preConditions = serializedObject.FindProperty("preConditions");
|
||||
functions = serializedObject.FindProperty("functions");
|
||||
|
||||
preConditionsList = new ReorderableList(serializedObject, preConditions, true, true, true, true);
|
||||
|
||||
preConditionsList.elementHeightCallback = (int index) =>
|
||||
{
|
||||
// Define the height of each element here. You might want to adjust this based on the complexity of each element.
|
||||
// For simplicity, we're using a fixed height, but you could calculate this dynamically based on the content.
|
||||
return EditorGUIUtility.singleLineHeight * 2.5f + 6; // Example height for two lines and a bit of padding
|
||||
};
|
||||
|
||||
preConditionsList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
|
||||
{
|
||||
var element = preConditions.GetArrayElementAtIndex(index);
|
||||
rect.y += 2;
|
||||
|
||||
var variableNames = GetVariableNames();
|
||||
|
||||
// Adjusting the widths to account for the operator dropdown and ensuring everything fits nicely.
|
||||
float width = rect.width / 2 - 4; // Adjust as necessary for layout
|
||||
|
||||
// Draw the variableName1 as a dropdown
|
||||
var variableName1Prop = element.FindPropertyRelative("variableName1");
|
||||
int selectedIndex = variableNames.IndexOf(variableName1Prop.stringValue);
|
||||
selectedIndex = Mathf.Max(0, selectedIndex); // Ensure the index is at least 0
|
||||
selectedIndex = EditorGUI.Popup(new Rect(rect.x, rect.y, width*1.5f, EditorGUIUtility.singleLineHeight), "Variable Name 1", selectedIndex, variableNames.ToArray());
|
||||
if (variableNames.Count > 0)
|
||||
{
|
||||
variableName1Prop.stringValue = variableNames[selectedIndex];
|
||||
}
|
||||
|
||||
// Draw the operator dropdown
|
||||
EditorGUI.PropertyField(new Rect(rect.x + (width*1.5f) + 8, rect.y, width/2, EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("op"), GUIContent.none);
|
||||
|
||||
// Draw the variableName2 directly as a text field for simplicity
|
||||
EditorGUI.PropertyField(new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight + 4, rect.width, EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("variableName2"), new GUIContent("Variable Name 2"));
|
||||
};
|
||||
|
||||
preConditionsList.drawHeaderCallback = (Rect rect) =>
|
||||
{
|
||||
EditorGUI.LabelField(rect, "PreConditions");
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
serializedObject.Update();
|
||||
|
||||
StripsAction stripsAction = (StripsAction)target;
|
||||
if (stripsAction.associatedMapping == null)
|
||||
{
|
||||
EditorGUILayout.HelpBox("Associated Mapping is null. Please assign an Associated Mapping.", MessageType.Warning);
|
||||
}
|
||||
|
||||
EditorGUILayout.PropertyField(associatedMapping);
|
||||
preConditionsList.DoLayoutList();
|
||||
EditorGUILayout.PropertyField(functions);
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
private List<string> GetVariableNames()
|
||||
{
|
||||
var variableNames = new List<string>();
|
||||
StripsAction stripsAction = (StripsAction)target;
|
||||
if (stripsAction.associatedMapping != null)
|
||||
{
|
||||
var mappingDict = stripsAction.associatedMapping.Mappings;
|
||||
foreach (var mapping in mappingDict)
|
||||
{
|
||||
variableNames.Add(mapping.Key);
|
||||
}
|
||||
}
|
||||
return variableNames;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 088236bfbb7a12c418687cdbd9c644ce
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user