Refactoring of the interaction system and preliminary integration of save/load functionality across the game. (#44)
### Interactables Architecture Refactor
- Converted composition to inheritance, moved from component-based to class-based interactables. No more requirement for chain of "Interactable -> Item" etc.
- Created `InteractableBase` abstract base class with common functionality that replaces the old component
- Specialized child classes: `Pickup`, `ItemSlot`, `LevelSwitch`, `MinigameSwitch`, `CombinationItem`, `OneClickInteraction` are now children classes
- Light updates to the interactable inspector, moved some things arround, added collapsible inspector sections in the UI for better editor experience
### State Machine Integration
- Custom `AppleMachine` inheritong from Pixelplacement's StateMachine which implements our own interface for saving, easy place for future improvements
- Replaced all previous StateMachines by `AppleMachine`
- Custom `AppleState` extends from default `State`. Added serialization, split state logic into "EnterState", "RestoreState", "ExitState" allowing for separate logic when triggering in-game vs loading game
- Restores directly to target state without triggering transitional logic
- Migration tool converts existing instances
### Prefab Organization
- Saved changes from scenes into prefabs
- Cleaned up duplicated components, confusing prefabs hierarchies
- Created prefab variants where possible
- Consolidated Environment prefabs and moved them out of Placeholders subfolder into main Environment folder
- Organized item prefabs from PrefabsPLACEHOLDER into proper Items folder
- Updated prefab references - All scene references updated to new locations
- Removed placeholder files from Characters, Levels, UI, and Minigames folders
### Scene Updates
- Quarry scene with major updates
- Saved multiple working versions (Quarry, Quarry_Fixed, Quarry_OLD)
- Added proper lighting data
- Updated all interactable components to new architecture
### Minor editor tools
- New tool for testing cards from an editor window (no in-scene object required)
- Updated Interactable Inspector
- New debug option to opt in-and-out of the save/load system
- Tooling for easier migration
Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Reviewed-on: https://homelab.tailf7f81b.ts.net/tschesky/AppleHillsProduction/pulls/44
2025-11-03 10:12:51 +00:00
|
|
|
|
using UnityEditor;
|
2025-09-10 13:47:59 +02:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using System.IO;
|
2025-09-11 13:00:26 +02:00
|
|
|
|
using Interactions;
|
|
|
|
|
|
using PuzzleS;
|
2025-09-10 13:47:59 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Editor
|
|
|
|
|
|
{
|
|
|
|
|
|
public class PrefabCreatorWindow : EditorWindow
|
|
|
|
|
|
{
|
|
|
|
|
|
private string _prefabName = "NewPrefab";
|
|
|
|
|
|
private string _saveFolderPath = "Assets/Prefabs/Items";
|
|
|
|
|
|
private string _pickupSoFolderPath = "Assets/Data/Items";
|
|
|
|
|
|
private string _puzzleSoFolderPath = "Assets/Data/Puzzles";
|
|
|
|
|
|
private bool _addObjective;
|
|
|
|
|
|
|
|
|
|
|
|
private PickupItemData _pickupData;
|
|
|
|
|
|
private PuzzleStepSO _objectiveData;
|
|
|
|
|
|
private UnityEditor.Editor _soEditor;
|
|
|
|
|
|
|
2025-09-11 13:15:43 +02:00
|
|
|
|
private enum ItemType { None, Pickup, ItemSlot }
|
|
|
|
|
|
private ItemType _itemType = ItemType.None;
|
|
|
|
|
|
|
|
|
|
|
|
private bool _createNext = false;
|
|
|
|
|
|
|
2025-09-24 13:33:43 +00:00
|
|
|
|
[MenuItem("AppleHills/Item Prefab Creator")]
|
2025-09-10 13:47:59 +02:00
|
|
|
|
public static void ShowWindow()
|
|
|
|
|
|
{
|
|
|
|
|
|
var window = GetWindow<PrefabCreatorWindow>("Prefab Creator");
|
|
|
|
|
|
window.minSize = new Vector2(400, 400);
|
|
|
|
|
|
// Set default paths if not already set
|
|
|
|
|
|
if (string.IsNullOrEmpty(window._saveFolderPath))
|
|
|
|
|
|
window._saveFolderPath = "Assets/Prefabs/Items";
|
|
|
|
|
|
if (string.IsNullOrEmpty(window._pickupSoFolderPath))
|
|
|
|
|
|
window._pickupSoFolderPath = "Assets/Data/Items";
|
|
|
|
|
|
if (string.IsNullOrEmpty(window._puzzleSoFolderPath))
|
|
|
|
|
|
window._puzzleSoFolderPath = "Assets/Data/Puzzles";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnGUI()
|
|
|
|
|
|
{
|
|
|
|
|
|
EditorGUILayout.LabelField("Prefab Creator", EditorStyles.boldLabel);
|
|
|
|
|
|
_prefabName = EditorGUILayout.TextField("Prefab Name", _prefabName);
|
|
|
|
|
|
// Prefab save folder
|
|
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
|
|
EditorGUILayout.PrefixLabel("Save Folder");
|
|
|
|
|
|
EditorGUILayout.SelectableLabel(_saveFolderPath, EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
|
|
|
|
|
|
if (GUILayout.Button("Select...", GUILayout.Width(80)))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_saveFolderPath = PrefabEditorUtility.SelectFolder(_saveFolderPath, "Prefabs/Items");
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
|
|
EditorGUILayout.LabelField("Add Components:", EditorStyles.boldLabel);
|
2025-09-11 13:15:43 +02:00
|
|
|
|
|
|
|
|
|
|
// Item type selection
|
|
|
|
|
|
var newType = (ItemType)EditorGUILayout.EnumPopup("Item Type", _itemType);
|
|
|
|
|
|
if (newType != _itemType)
|
|
|
|
|
|
{
|
|
|
|
|
|
_itemType = newType;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool addObjective = EditorGUILayout.Toggle("ObjectiveStepBehaviour", _addObjective);
|
|
|
|
|
|
_addObjective = addObjective;
|
2025-09-10 13:47:59 +02:00
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
|
|
|
2025-09-11 13:15:43 +02:00
|
|
|
|
// Pickup Data (for Pickup or ItemSlot)
|
|
|
|
|
|
if (_itemType == ItemType.Pickup || _itemType == ItemType.ItemSlot)
|
2025-09-10 13:47:59 +02:00
|
|
|
|
{
|
|
|
|
|
|
EditorGUILayout.LabelField("Pickup Data:", EditorStyles.boldLabel);
|
|
|
|
|
|
_pickupData = (PickupItemData)EditorGUILayout.ObjectField("PickupItemData", _pickupData, typeof(PickupItemData), false);
|
|
|
|
|
|
// Pickup SO save folder
|
|
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
|
|
EditorGUILayout.PrefixLabel("Save To");
|
|
|
|
|
|
EditorGUILayout.SelectableLabel(_pickupSoFolderPath, EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
|
|
|
|
|
|
if (GUILayout.Button("Select...", GUILayout.Width(80)))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_pickupSoFolderPath = PrefabEditorUtility.SelectFolder(_pickupSoFolderPath, "Data/Items");
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
|
|
if (_pickupData == null && GUILayout.Button("Create New PickupItemData"))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_pickupData = PrefabEditorUtility.CreateScriptableAsset<PickupItemData>(_prefabName + "_pickup", _pickupSoFolderPath);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
if (_pickupData != null)
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _pickupData);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-11 13:15:43 +02:00
|
|
|
|
// Objective Data
|
2025-09-10 13:47:59 +02:00
|
|
|
|
if (_addObjective)
|
|
|
|
|
|
{
|
|
|
|
|
|
EditorGUILayout.LabelField("Objective Data:", EditorStyles.boldLabel);
|
|
|
|
|
|
_objectiveData = (PuzzleStepSO)EditorGUILayout.ObjectField("PuzzleStepSO", _objectiveData, typeof(PuzzleStepSO), false);
|
|
|
|
|
|
// Puzzle SO save folder
|
|
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
|
|
EditorGUILayout.PrefixLabel("Save To");
|
|
|
|
|
|
EditorGUILayout.SelectableLabel(_puzzleSoFolderPath, EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
|
|
|
|
|
|
if (GUILayout.Button("Select...", GUILayout.Width(80)))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_puzzleSoFolderPath = PrefabEditorUtility.SelectFolder(_puzzleSoFolderPath, "Data/Puzzles");
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
|
|
if (_objectiveData == null && GUILayout.Button("Create New PuzzleStepSO"))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_objectiveData = PrefabEditorUtility.CreateScriptableAsset<PuzzleStepSO>(_prefabName + "_puzzle", _puzzleSoFolderPath);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
if (_objectiveData != null)
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _objectiveData);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
GUILayout.FlexibleSpace();
|
2025-09-11 13:15:43 +02:00
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
2025-09-10 13:47:59 +02:00
|
|
|
|
GUI.enabled = !string.IsNullOrEmpty(_prefabName) && !string.IsNullOrEmpty(_saveFolderPath);
|
2025-09-11 13:15:43 +02:00
|
|
|
|
if (GUILayout.Button("Create Prefab", GUILayout.Height(28)))
|
2025-09-10 13:47:59 +02:00
|
|
|
|
{
|
|
|
|
|
|
CreatePrefab();
|
|
|
|
|
|
}
|
2025-09-11 13:15:43 +02:00
|
|
|
|
_createNext = GUILayout.Toggle(_createNext, "Create Next", GUILayout.Width(100), GUILayout.Height(28));
|
2025-09-10 13:47:59 +02:00
|
|
|
|
GUI.enabled = true;
|
2025-09-11 13:15:43 +02:00
|
|
|
|
EditorGUILayout.EndHorizontal();
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CreatePrefab()
|
|
|
|
|
|
{
|
|
|
|
|
|
var go = new GameObject(_prefabName);
|
Refactoring of the interaction system and preliminary integration of save/load functionality across the game. (#44)
### Interactables Architecture Refactor
- Converted composition to inheritance, moved from component-based to class-based interactables. No more requirement for chain of "Interactable -> Item" etc.
- Created `InteractableBase` abstract base class with common functionality that replaces the old component
- Specialized child classes: `Pickup`, `ItemSlot`, `LevelSwitch`, `MinigameSwitch`, `CombinationItem`, `OneClickInteraction` are now children classes
- Light updates to the interactable inspector, moved some things arround, added collapsible inspector sections in the UI for better editor experience
### State Machine Integration
- Custom `AppleMachine` inheritong from Pixelplacement's StateMachine which implements our own interface for saving, easy place for future improvements
- Replaced all previous StateMachines by `AppleMachine`
- Custom `AppleState` extends from default `State`. Added serialization, split state logic into "EnterState", "RestoreState", "ExitState" allowing for separate logic when triggering in-game vs loading game
- Restores directly to target state without triggering transitional logic
- Migration tool converts existing instances
### Prefab Organization
- Saved changes from scenes into prefabs
- Cleaned up duplicated components, confusing prefabs hierarchies
- Created prefab variants where possible
- Consolidated Environment prefabs and moved them out of Placeholders subfolder into main Environment folder
- Organized item prefabs from PrefabsPLACEHOLDER into proper Items folder
- Updated prefab references - All scene references updated to new locations
- Removed placeholder files from Characters, Levels, UI, and Minigames folders
### Scene Updates
- Quarry scene with major updates
- Saved multiple working versions (Quarry, Quarry_Fixed, Quarry_OLD)
- Added proper lighting data
- Updated all interactable components to new architecture
### Minor editor tools
- New tool for testing cards from an editor window (no in-scene object required)
- Updated Interactable Inspector
- New debug option to opt in-and-out of the save/load system
- Tooling for easier migration
Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Reviewed-on: https://homelab.tailf7f81b.ts.net/tschesky/AppleHillsProduction/pulls/44
2025-11-03 10:12:51 +00:00
|
|
|
|
// Note: No need to add InteractableBase separately - Pickup and ItemSlot inherit from it
|
2025-09-10 13:47:59 +02:00
|
|
|
|
go.AddComponent<BoxCollider>();
|
|
|
|
|
|
int interactableLayer = LayerMask.NameToLayer("Interactable");
|
|
|
|
|
|
if (interactableLayer != -1)
|
|
|
|
|
|
go.layer = interactableLayer;
|
|
|
|
|
|
go.AddComponent<SpriteRenderer>();
|
2025-09-11 13:15:43 +02:00
|
|
|
|
if (_itemType == ItemType.Pickup)
|
2025-09-10 13:47:59 +02:00
|
|
|
|
{
|
|
|
|
|
|
var pickup = go.AddComponent<Pickup>();
|
|
|
|
|
|
pickup.itemData = _pickupData;
|
|
|
|
|
|
}
|
2025-09-11 13:15:43 +02:00
|
|
|
|
else if (_itemType == ItemType.ItemSlot)
|
2025-09-10 13:47:59 +02:00
|
|
|
|
{
|
2025-09-11 13:15:43 +02:00
|
|
|
|
var slot = go.AddComponent<ItemSlot>();
|
|
|
|
|
|
slot.itemData = _pickupData;
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
if (_addObjective)
|
|
|
|
|
|
{
|
|
|
|
|
|
var obj = go.AddComponent<ObjectiveStepBehaviour>();
|
|
|
|
|
|
obj.stepData = _objectiveData;
|
|
|
|
|
|
}
|
|
|
|
|
|
string prefabPath = Path.Combine(_saveFolderPath, _prefabName + ".prefab").Replace("\\", "/");
|
2025-09-11 13:15:43 +02:00
|
|
|
|
var prefab = PrefabUtility.SaveAsPrefabAsset(go, prefabPath);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
DestroyImmediate(go);
|
|
|
|
|
|
AssetDatabase.Refresh();
|
2025-09-11 13:15:43 +02:00
|
|
|
|
Selection.activeObject = prefab;
|
|
|
|
|
|
EditorGUIUtility.PingObject(prefab);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
EditorUtility.DisplayDialog("Prefab Created", $"Prefab saved to {prefabPath}", "OK");
|
2025-09-11 13:15:43 +02:00
|
|
|
|
if (_createNext)
|
|
|
|
|
|
{
|
|
|
|
|
|
_prefabName = "NewPrefab";
|
|
|
|
|
|
_pickupData = null;
|
|
|
|
|
|
_objectiveData = null;
|
|
|
|
|
|
_itemType = ItemType.None;
|
|
|
|
|
|
_addObjective = false;
|
|
|
|
|
|
_soEditor = null;
|
|
|
|
|
|
GUI.FocusControl(null);
|
|
|
|
|
|
Repaint();
|
|
|
|
|
|
}
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|