/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ using System; using System.Collections.Generic; using System.Linq; using GameCult.Aetheria.State.Verse; using UnityEngine; using UnityEngine.Rendering.PostProcessing; using float3 = Unity.Mathematics.float3; public sealed class AetheriaUnityCurrentEntityBinder { public ZoneRenderer ZoneRenderer { get; set; } public PostProcessVolume DeathPost { get; set; } public CanvasGroup GameplayUI { get; set; } public AetheriaUnityObservedDockingIndex ObservedDocking { get; set; } public AetheriaUnityCurrentEntityPresentation CurrentEntityPresentation { get; set; } public AetheriaUnityTargetPresentation TargetPresentation { get; set; } public Func GetCurrentEntity { get; set; } public Action SetCurrentEntity { get; set; } public Func GetViewDirection { get; set; } public Action SetViewDirection { get; set; } public Func ResolveZoneRender { get; set; } public Action> ApplyActionBarBindings { get; set; } public Action EnablePlayerInput { get; set; } public Action DisablePlayerInput { get; set; } public Action PlayMusic { get; set; } public Action UpdateTargetPanel { get; set; } public void RestoreBinding(Entity currentEntity) { if (ObservedDocking != null && ObservedDocking.TryResolveDockingBay(currentEntity, out var dockParent, out _)) { SetCurrentEntity?.Invoke(currentEntity); ApplyActionBarBindings?.Invoke(Array.Empty()); CurrentEntityPresentation?.BindDocked( dockParent, null, ResolveZoneRender?.Invoke()); return; } BindUndocked( currentEntity); } public void ClearBinding() { TargetPresentation?.ClearIndicators(); CurrentEntityPresentation?.ClearBinding(); DisablePlayerInput?.Invoke(); Cursor.lockState = CursorLockMode.None; if (GameplayUI != null) GameplayUI.gameObject.SetActive(false); } private void BindUndocked(Entity entity) { if (ZoneRenderer == null || !ZoneRenderer.TryGetEntityInstance(entity, out var entityInstance)) { Debug.LogError($"Attempted to bind to entity {entity?.Name}, but ZoneRenderer has no daemon-indexed instance."); return; } SetCurrentEntity?.Invoke(entity); if (DeathPost != null) DeathPost.weight = 0; if (CultMath.math.length(entity.CultDirection) > .1f) SetViewDirection?.Invoke((float3)AetheriaMath.ToUnityXZ(entity.CultDirection)); CurrentEntityPresentation?.BindUndocked( entity, entityInstance, Array.Empty(), EnablePlayerInput, PlayMusic, SetCurrentEntity, UpdateTargetPanel, () => TargetPresentation?.ReconcileVisibleTargetIndicators(GetCurrentEntity?.Invoke()), ApplyActionBarBindings); } }