86 lines
2.1 KiB
C#
86 lines
2.1 KiB
C#
using Kryz.CharacterStats.Examples;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class FishingSpotInteractable : Interactable
|
|
{
|
|
[SerializeField] private List<WeightedDrop> dropItems = new List<WeightedDrop>();
|
|
|
|
public bool interacted = false;
|
|
|
|
protected Item drop;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
}
|
|
|
|
public override void Interact(bool melee)
|
|
{
|
|
if (interacted) return;
|
|
|
|
interacted = true;
|
|
|
|
base.Interact(melee);
|
|
|
|
playerController.StartFishing();
|
|
|
|
StartCoroutine(FindFish());
|
|
}
|
|
|
|
IEnumerator FindFish()
|
|
{
|
|
yield return new WaitForSeconds(Random.Range(GameConstants.GameBalancing.MinimumFindFishWaitTime, GameConstants.GameBalancing.MaximumFindFishWaitTime));
|
|
|
|
playerController.GetComponentInChildren<CharacterAnimatorController>().OnPickup();
|
|
}
|
|
|
|
public void OnFishingEnded()
|
|
{
|
|
drop = GetRandomDrop(dropItems);
|
|
|
|
if(drop != null)
|
|
{
|
|
Inventory.Instance.AddItem(drop);
|
|
}
|
|
|
|
Destroy(this.gameObject);
|
|
}
|
|
|
|
public override void OnFocused(Transform playerTransform, PlayerController playerController)
|
|
{
|
|
base.OnFocused(playerTransform, playerController);
|
|
}
|
|
|
|
public override void OnDeFocus()
|
|
{
|
|
base.OnDeFocus();
|
|
}
|
|
|
|
public static Item GetRandomDrop(List<WeightedDrop> items)
|
|
{
|
|
float totalWeight = 0;
|
|
foreach (var weightedItem in items)
|
|
{
|
|
totalWeight += weightedItem.weight;
|
|
}
|
|
|
|
float randomValue = UnityEngine.Random.Range(0f, totalWeight);
|
|
float currentWeight = 0;
|
|
|
|
foreach (var weightedItem in items)
|
|
{
|
|
currentWeight += weightedItem.weight;
|
|
if (randomValue <= currentWeight)
|
|
{
|
|
return weightedItem.drop;
|
|
}
|
|
}
|
|
|
|
// This should never happen if the weights are set up correctly
|
|
Debug.LogWarning("No item selected in weighted drop system. Returning default value.");
|
|
return null;
|
|
}
|
|
}
|