Files
project-reset/Assets/Scripts/Items/ItemContainer.cs
2025-09-09 17:58:52 -04:00

101 lines
3.2 KiB
C#

using System;
using System.Collections;
using Reset.Core.Tools;
using Sirenix.OdinInspector;
using Unity.Netcode;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Reset.Items{
public class ItemContainer : NetworkBehaviour, IInteractable{
private bool available = true;
public GameObject itemDrop;
public int minItemsToSpawn;
public int maxItemsToSpawn;
public float cooldown;
public GameObject spawnPoint;
public Vector3 spawnRotationAngle;
public float spawnForce;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start(){
}
// Update is called once per frame
void Update(){
}
[Button]
public void Interact(){
if (available){
StartCoroutine(SpawnItems());
}
available = false;
if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsConnectedClient) {
SetAvailabilityRpc(false);
}
}
IEnumerator SpawnItems(){
for (int i = 0; i < Random.Range(minItemsToSpawn, maxItemsToSpawn); i++) {
GameObject newDrop = Instantiate(itemDrop);
newDrop.transform.position = spawnPoint.transform.position;
newDrop.AddComponent<ItemDrop>();
Vector3 randomSpawnDirection = new Vector3(
Random.Range(-spawnRotationAngle.x, spawnRotationAngle.x),
Random.Range(-spawnRotationAngle.y, spawnRotationAngle.y),
Random.Range(-spawnRotationAngle.z, spawnRotationAngle.z)
);
Vector3 outputSpawnDirection = Quaternion.Euler(randomSpawnDirection) * spawnPoint.transform.forward;
// outputSpawnDirection = transform.TransformDirection(outputSpawnDirection);
DebugOverlayDrawer.ChangeValue("ItemContainer", "Last Random Spawn Direction", randomSpawnDirection);
DebugOverlayDrawer.ChangeValue("ItemContainer", "Output Spawn Direction", outputSpawnDirection);
newDrop.GetComponent<Rigidbody>().AddForce(outputSpawnDirection * spawnForce, ForceMode.VelocityChange);
yield return new WaitForSeconds(.2f);
}
StartCoroutine(WaitForCooldown());
}
IEnumerator WaitForCooldown(){
yield return new WaitForSeconds(cooldown);
if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsConnectedClient) {
SetAvailabilityRpc(true);
}
available = true;
}
public void CancelInteract(){
throw new NotImplementedException();
}
[Rpc(SendTo.Everyone)]
void SetAvailabilityRpc(bool isAvailable){
Debug.Log($"I made this {isAvailable}!");
available = isAvailable;
}
public void OnObserverDetected(EnvironmentObserver observer){
return;
}
}
}