Skip to content

Commit

Permalink
0.3.1
Browse files Browse the repository at this point in the history
- Show fish info popups when using the spyglass ability
- Remove spyglass crosshair UI
  • Loading branch information
xen-42 authored Dec 8, 2023
2 parents 470efe4 + 6f4b85b commit e1f4396
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 2 deletions.
14 changes: 14 additions & 0 deletions DredgeVR/Ability/Spyglass/Patches/SpyglassHarvestablePOIPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using HarmonyLib;

namespace DredgeVR.Ability.Spyglass.Patches;

[HarmonyPatch(typeof(HarvestPOI))]
internal class SpyglassHarvestablePOIPatches
{
[HarmonyPostfix]
[HarmonyPatch(nameof(HarvestPOI.Start))]
public static void HarvestPOI_Start(HarvestPOI __instance)
{
__instance.gameObject.AddComponent<SpyglassHarvestPOIUI>();
}
}
40 changes: 40 additions & 0 deletions DredgeVR/Ability/Spyglass/Patches/SpyglassMapStampPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using HarmonyLib;
using UnityEngine;

namespace DredgeVR.Ability.Patches;

// TODO

/*
[HarmonyPatch(typeof(SpyglassMapStamp))]
public static class SpyglassMapStampPatches
{
[HarmonyPrefix]
[HarmonyPatch(nameof(SpyglassMapStamp.LateUpdate))]
public static bool SpyglassMapStamp_LateUpdate(SpyglassMapStamp __instance)
{
// Normal method has these flat on the screen
// We keep them at their world position
__instance.transform.position = __instance.worldPosition + Vector3.up;
// Count them as viewable if within 100m
// TODO: Would probably be good to also actually check if they are in front of the player
__instance.isOnScreen = (__instance.transform.position - GameManager.Instance.Player.transform.position).magnitude < 100;
__instance.stampImage.enabled = __instance.isOnScreen;
__instance.distanceTextField.enabled = __instance.isOnScreen;
if (__instance.isOnScreen)
{
__instance.timeUntilDistanceUpdate -= Time.deltaTime;
if (__instance.timeUntilDistanceUpdate < 0f)
{
__instance.timeUntilDistanceUpdate = __instance.timeBetweenDistanceUpdates;
__instance.UpdatePlayerDistance();
}
}
return false;
}
}
*/
14 changes: 14 additions & 0 deletions DredgeVR/Ability/Spyglass/Patches/SpyglassUIPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using HarmonyLib;

namespace DredgeVR.Ability.Spyglass.Patches;

[HarmonyPatch(typeof(SpyglassUI))]
public static class SpyglassUIPatches
{
[HarmonyPostfix]
[HarmonyPatch(nameof(SpyglassUI.OnPlayerAbilityToggled))]
public static void SpyglassUI_OnPlayerAbilityToggled(SpyglassUI __instance)
{
__instance.crosshairContainer.SetActive(false);
}
}
189 changes: 189 additions & 0 deletions DredgeVR/Ability/Spyglass/SpyglassHarvestPOIUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using DredgeVR.Helpers;
using DredgeVR.VRCamera;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Components;
using UnityEngine.UI;
using Random = UnityEngine.Random;

namespace DredgeVR.Ability.Spyglass;

[RequireComponent(typeof(HarvestPOI))]
public class SpyglassHarvestPOIUI : MonoBehaviour
{
private static GameObject _prefab;

private HarvestPOI _harvestPOI;
private HarvestableItemData _firstHarvestableItem;

private GameObject _container;
private LocalizeStringEvent _itemNameString;
// private TextMeshProUGUI _itemDistanceTextField; //Unused in SpyglassUI
private Image _itemImage;
private Image _invalidEquipmentImage;
private Sprite _hiddenItemSprite;
private HarvestableTypeTagUI _harvestableTypeTagUI;
private LocalizedString _obscuredString;

private float _containerScale = 0.005f;
private bool _shouldShowContainer;

private AbilityData _spyglassAbilityData;
private bool _usingSpyglass;

private float _updateTimer;
public const float UPDATE_TIME = 1f;

private void Awake()
{
GameEvents.Instance.OnPlayerAbilityToggled += this.OnPlayerAbilityToggled;
}

private void OnDestroy()
{
GameEvents.Instance.OnPlayerAbilityToggled -= this.OnPlayerAbilityToggled;
}

public void Start()
{
var spyglassUI = GameManager.Instance.UI.spyglassUI;
_spyglassAbilityData = spyglassUI.spyglassAbilityData;

if (_prefab == null)
{
_prefab = GameObject.Instantiate(spyglassUI.container);
_prefab.SetActive(false);
// Needs to be worldspace
var canvas = _prefab.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
GameObject.DontDestroyOnLoad(_prefab);
}

_harvestPOI = GetComponent<HarvestPOI>();

_container = GameObject.Instantiate(_prefab);
_container.SetParent(transform);
_container.transform.localPosition = Vector3.up;
_container.transform.localScale = Vector3.zero;

// Grab all the components
_itemNameString = _container.transform.Find("Backplate/BackplateInner/NameText").GetComponent<LocalizeStringEvent>();
_itemImage = _container.transform.Find("Backplate/BackplateInner/Image").GetComponent<Image>();
_invalidEquipmentImage = _container.transform.Find("Backplate/BackplateInner/InvalidEquipmentImage").GetComponent<Image>();
_hiddenItemSprite = spyglassUI.hiddenItemSprite;
_harvestableTypeTagUI = _container.transform.Find("Backplate/HarvestableTypeTag").GetComponent<HarvestableTypeTagUI>();
_obscuredString = spyglassUI.obscuredString;

_updateTimer = Random.Range(0, UPDATE_TIME);
}

public void Update()
{
if (!_usingSpyglass)
{
return;
}

// Spread out update checks
_updateTimer -= Time.deltaTime;
if (_updateTimer < 0)
{
_updateTimer += UPDATE_TIME;
CheckPlayerPosition();
}

// Lerp scale based on if active
var targetScale = _shouldShowContainer ? Vector3.one * _containerScale : Vector3.zero;
if (_container.transform.localScale != targetScale)
{
var t = Mathf.Clamp01(Mathf.InverseLerp(_shouldShowContainer ? 0f : _containerScale, targetScale.x, _container.transform.localScale.x) + Time.deltaTime);
_container.transform.localScale = Vector3.Slerp(_container.transform.localScale, targetScale, t);

if (!_shouldShowContainer && _container.transform.localScale == Vector3.zero)
{
_container.SetActive(false);
}
}
}

private void CheckPlayerPosition()
{
// Only show objects within a certain range
if ((GameManager.Instance.Player.transform.position - transform.position).magnitude < 60)
{
_shouldShowContainer = true;

if (!_container.activeInHierarchy)
{
_container.SetActive(true);
}

// First harvestable item can change
UpdateHarvestableItem();

if (_firstHarvestableItem == null)
{
_shouldShowContainer = false;
}
else
{
// TODO: Ideally we'd also check that you're looking towards it
GameManager.Instance.SaveData.SetHasSpiedHarvestCategory(_firstHarvestableItem.harvestableType, true);
GameManager.Instance.AchievementManager.EvaluateAchievement(DredgeAchievementId.ABILITY_SPYGLASS);

// LookAt makes them backwards though
_container.transform.LookAt(VRCameraManager.VRPlayer.transform.position);
_container.transform.Rotate(Vector3.up, 180f);
}
}
else
{
_shouldShowContainer = false;
}
}

private void UpdateHarvestableItem()
{
var currentFirstHarvestableItem = _harvestPOI.Harvestable.GetActiveFirstHarvestableItem();

if (_firstHarvestableItem != currentFirstHarvestableItem)
{
_firstHarvestableItem = currentFirstHarvestableItem;

if (_firstHarvestableItem != null)
{
// Most of this is adapted from SpyglassUI which has a single focused UI for the harvestPOI you're looking at
if (GameManager.Instance.SaveData.GetCaughtCountById(_firstHarvestableItem.id) > 0)
{
_itemNameString.StringReference = _firstHarvestableItem.itemNameKey;
}
else
{
_itemNameString.StringReference = _obscuredString;
}
_itemNameString.StringReference.RefreshString();
_itemImage.sprite = ((_firstHarvestableItem.itemSubtype == ItemSubtype.TRINKET) ? _hiddenItemSprite : _firstHarvestableItem.sprite);
_harvestableTypeTagUI.Init(_firstHarvestableItem.harvestableType);
_invalidEquipmentImage.gameObject.SetActive(!GameManager.Instance.PlayerStats.HarvestableTypes.Contains(_firstHarvestableItem.harvestableType));
}
}
}

private void OnPlayerAbilityToggled(AbilityData abilityData, bool enabled)
{
if (_spyglassAbilityData.name == abilityData.name)
{
_usingSpyglass = enabled;
if (enabled)
{
// Reset them when entering into spyglass
_shouldShowContainer = false;
_container.transform.localScale = Vector3.zero;
}
else
{
_container.SetActive(false);
}
}
}
}
6 changes: 5 additions & 1 deletion DredgeVR/VRUI/VRUIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ private void OnSceneStart(string scene)

private void MakeCanvasWorldSpace(Canvas canvas)
{
// If already world space, skip over it
if (canvas.renderMode == RenderMode.WorldSpace) return;

canvas.renderMode = RenderMode.WorldSpace;
canvas.worldCamera = VRInputModule.Instance.RaycastCamera;
canvas.scaleFactor = 1f;
Expand Down Expand Up @@ -189,11 +192,12 @@ private GameObject CreatePromptContainer(string name, Transform parent)
private void OnGameSceneStart()
{
// Add a component to orient game UI relative to the player camera
// Ignore HarvestablePOIs
foreach (var canvas in GameObject.FindObjectsOfType<Canvas>())
{
// When finding objects of type make sure they are in the game scene, could be on Manager or DontDestroyOnLoad or whatever
// Also ignore canvases that have parent canvases, since they will inherit their position
if (canvas.gameObject.scene.name == "Game" && canvas.isRootCanvas)
if (canvas.gameObject.scene.name == "Game" && canvas.isRootCanvas && canvas.transform.parent?.name != "HarvestPOIs")
{
canvas.gameObject.AddComponent<GameCanvasFixer>();
}
Expand Down
2 changes: 1 addition & 1 deletion DredgeVR/mod_meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Name": "DredgeVR",
"ModGUID": "xen.DredgeVR",
"Version": "0.3.0",
"Version": "0.3.1",
"ModAssembly": "DredgeVR.dll",
"MinWinchVersion": "0.3.0",
"Entrypoint": "DredgeVR.Loader/Initialize",
Expand Down

0 comments on commit e1f4396

Please sign in to comment.