2025-09-06 21:01:54 +02:00
|
|
|
|
using UnityEngine;
|
2025-09-24 13:33:43 +00:00
|
|
|
|
using AppleHills.Core.Settings;
|
2025-10-14 15:53:58 +02:00
|
|
|
|
using Core;
|
2025-09-06 21:01:54 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Input
|
|
|
|
|
|
{
|
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
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Saveable data for PlayerTouchController state
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
|
public class PlayerSaveData
|
|
|
|
|
|
{
|
|
|
|
|
|
public Vector3 worldPosition;
|
|
|
|
|
|
public Quaternion worldRotation;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Handles player movement in response to tap and hold input events.
|
|
|
|
|
|
/// Supports both direct and pathfinding movement modes, and provides event/callbacks for arrival/cancellation.
|
2025-12-08 16:46:50 +01:00
|
|
|
|
/// Extends BasePlayerMovementController with save/load and MoveToAndNotify functionality.
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// </summary>
|
2025-12-08 16:46:50 +01:00
|
|
|
|
public class PlayerTouchController : BasePlayerMovementController
|
2025-09-06 21:01:54 +02:00
|
|
|
|
{
|
2025-12-08 16:46:50 +01:00
|
|
|
|
// --- PlayerTouchController-specific features (MoveToAndNotify) ---
|
2025-09-06 21:01:54 +02:00
|
|
|
|
public delegate void ArrivedAtTargetHandler();
|
2025-12-08 16:46:50 +01:00
|
|
|
|
private Coroutine _moveToCoroutine;
|
2025-09-06 21:01:54 +02:00
|
|
|
|
public event ArrivedAtTargetHandler OnArrivedAtTarget;
|
|
|
|
|
|
public event System.Action OnMoveToCancelled;
|
2025-12-08 16:46:50 +01:00
|
|
|
|
private bool _interruptMoveTo;
|
2025-09-06 21:01:54 +02:00
|
|
|
|
|
2025-11-07 15:38:31 +00:00
|
|
|
|
// Save system configuration
|
|
|
|
|
|
public override bool AutoRegisterForSave => true;
|
|
|
|
|
|
// Scene-specific SaveId - each level has its own player state
|
|
|
|
|
|
public override string SaveId => $"{gameObject.scene.name}/PlayerController";
|
|
|
|
|
|
|
2025-12-08 16:46:50 +01:00
|
|
|
|
protected override void LoadSettings()
|
2025-09-06 21:01:54 +02:00
|
|
|
|
{
|
2025-12-08 16:46:50 +01:00
|
|
|
|
var configs = GameManager.GetSettingsObject<IPlayerMovementConfigs>();
|
|
|
|
|
|
_movementSettings = configs.DefaultPlayerMovement;
|
2025-09-06 21:01:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-08 16:46:50 +01:00
|
|
|
|
#region ITouchInputConsumer Overrides (Add InterruptMoveTo)
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnTap(Vector2 worldPosition)
|
2025-09-06 21:01:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
InterruptMoveTo();
|
2025-12-08 16:46:50 +01:00
|
|
|
|
base.OnTap(worldPosition);
|
2025-09-06 21:01:54 +02:00
|
|
|
|
}
|
2025-12-08 16:46:50 +01:00
|
|
|
|
|
|
|
|
|
|
public override void OnHoldStart(Vector2 worldPosition)
|
2025-09-06 21:01:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
InterruptMoveTo();
|
2025-12-08 16:46:50 +01:00
|
|
|
|
base.OnHoldStart(worldPosition);
|
2025-09-06 21:01:54 +02:00
|
|
|
|
}
|
2025-12-08 16:46:50 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
2025-09-06 21:01:54 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Moves the player to a specific target position and notifies via events when arrived or cancelled.
|
|
|
|
|
|
/// This is used by systems like Pickup.cs to orchestrate movement.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void MoveToAndNotify(Vector3 target)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Cancel any previous move-to coroutine
|
2025-12-08 16:46:50 +01:00
|
|
|
|
if (_moveToCoroutine != null)
|
2025-09-06 21:01:54 +02:00
|
|
|
|
{
|
2025-12-08 16:46:50 +01:00
|
|
|
|
StopCoroutine(_moveToCoroutine);
|
2025-09-06 21:01:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-08 16:46:50 +01:00
|
|
|
|
_interruptMoveTo = false;
|
2025-09-08 08:56:11 +02:00
|
|
|
|
// Ensure pathfinding is enabled for MoveToAndNotify
|
2025-12-08 16:46:50 +01:00
|
|
|
|
if (_aiPath != null)
|
2025-09-08 08:56:11 +02:00
|
|
|
|
{
|
2025-12-08 16:46:50 +01:00
|
|
|
|
_aiPath.enabled = true;
|
|
|
|
|
|
_aiPath.canMove = true;
|
|
|
|
|
|
_aiPath.isStopped = false;
|
2025-09-08 08:56:11 +02:00
|
|
|
|
}
|
2025-12-08 16:46:50 +01:00
|
|
|
|
_moveToCoroutine = StartCoroutine(MoveToTargetCoroutine(target));
|
2025-09-06 21:01:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Cancels any in-progress MoveToAndNotify operation and fires the cancellation event.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void InterruptMoveTo()
|
|
|
|
|
|
{
|
2025-12-08 16:46:50 +01:00
|
|
|
|
_interruptMoveTo = true;
|
|
|
|
|
|
_isHolding = false;
|
|
|
|
|
|
_directMoveVelocity = Vector3.zero;
|
|
|
|
|
|
if (Settings.DefaultHoldMovementMode == HoldMovementMode.Direct && _aiPath != null)
|
|
|
|
|
|
_aiPath.enabled = false;
|
2025-09-06 21:01:54 +02:00
|
|
|
|
OnMoveToCancelled?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Coroutine for moving the player to a target position and firing arrival/cancel events.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private System.Collections.IEnumerator MoveToTargetCoroutine(Vector3 target)
|
|
|
|
|
|
{
|
2025-12-08 16:46:50 +01:00
|
|
|
|
if (_aiPath != null)
|
2025-09-06 21:01:54 +02:00
|
|
|
|
{
|
2025-12-08 16:46:50 +01:00
|
|
|
|
_aiPath.destination = target;
|
|
|
|
|
|
_aiPath.maxSpeed = Settings.MoveSpeed;
|
|
|
|
|
|
_aiPath.maxAcceleration = Settings.MaxAcceleration;
|
2025-09-06 21:01:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-08 16:46:50 +01:00
|
|
|
|
while (!_interruptMoveTo)
|
2025-09-06 21:01:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
Vector2 current2D = new Vector2(transform.position.x, transform.position.y);
|
|
|
|
|
|
Vector2 target2D = new Vector2(target.x, target.y);
|
|
|
|
|
|
float dist = Vector2.Distance(current2D, target2D);
|
2025-12-08 16:46:50 +01:00
|
|
|
|
if (dist <= Settings.StopDistance + 0.2f)
|
2025-09-06 21:01:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
yield return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-08 16:46:50 +01:00
|
|
|
|
_moveToCoroutine = null;
|
|
|
|
|
|
if (!_interruptMoveTo)
|
2025-09-06 21:01:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
OnArrivedAtTarget?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
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
|
|
|
|
|
2025-11-07 15:38:31 +00:00
|
|
|
|
#region Save/Load Lifecycle Hooks
|
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
|
|
|
|
|
2025-11-11 08:48:29 +00:00
|
|
|
|
internal override string OnSceneSaveRequested()
|
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
|
|
|
|
{
|
|
|
|
|
|
var saveData = new PlayerSaveData
|
|
|
|
|
|
{
|
|
|
|
|
|
worldPosition = transform.position,
|
|
|
|
|
|
worldRotation = transform.rotation
|
|
|
|
|
|
};
|
|
|
|
|
|
return JsonUtility.ToJson(saveData);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-11 08:48:29 +00:00
|
|
|
|
internal override void OnSceneRestoreRequested(string serializedData)
|
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
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(serializedData))
|
|
|
|
|
|
{
|
|
|
|
|
|
Logging.Debug("[PlayerTouchController] No saved state to restore");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var saveData = JsonUtility.FromJson<PlayerSaveData>(serializedData);
|
|
|
|
|
|
if (saveData != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
transform.position = saveData.worldPosition;
|
|
|
|
|
|
transform.rotation = saveData.worldRotation;
|
|
|
|
|
|
Logging.Debug($"[PlayerTouchController] Restored position: {saveData.worldPosition}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logging.Warning($"[PlayerTouchController] Failed to restore state: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2025-09-06 21:01:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|