evershade/Assets/Scripts/Handlers/CropHandler.cs

181 lines
6.4 KiB
C#

using Components;
using UnityEngine;
using UnityEngine.EventSystems;
public class CropHandler : MonoBehaviour, IPointerDownHandler, IPointerClickHandler,
IPointerEnterHandler, IPointerExitHandler
{
public Material hoverMaterial;
private Collider2D _collider2D;
private Collider2D _localCollider2D;
private Material _originalMaterial;
private GameObject _player;
private Renderer _renderer;
private void Start()
{
_player = GameObject.Find("Player");
_localCollider2D = GetComponent<Collider2D>();
_collider2D = _player.GetComponent<Collider2D>();
_renderer = GetComponent<Renderer>();
if (_renderer == null)
_renderer = GetComponentInChildren<Renderer>();
if (_renderer == null)
Debug.LogError("CROP: No Renderer found on CropHandler");
Debug.Log("CROP: CropHandler Start Complete");
}
private void Update()
{
// if the crop is not visible, return
if (!_renderer.isVisible)
return;
// if the crop is visible and the player is above the crop, set the sortingOrder to 4
// Get the bounds of the player and the crop
var playerBounds = _collider2D.bounds;
var cropBounds = _localCollider2D.bounds;
if (playerBounds.min.y > cropBounds.min.y && playerBounds.min.y < cropBounds.max.y)
// Debug.Log("CROP: OnTriggerEnter2D: Bottom of player is inside crop");
_renderer.sortingOrder = 4;
// Check if the bottom of the player is inside the crop
if (playerBounds.max.y > cropBounds.min.y && playerBounds.max.y < cropBounds.max.y)
// Debug.Log("CROP: OnTriggerEnter2D: Top of player is inside crop");
_renderer.sortingOrder = 0;
// middle of the player is inside the crop
if (playerBounds.center.y > cropBounds.min.y && playerBounds.center.y < cropBounds.max.y)
// Debug.Log("CROP: OnTriggerEnter2D: Middle of player is inside crop");
_renderer.sortingOrder = 0;
}
/*private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("CROP: OnTriggerEnter2D");
Debug.Log("CROP: OnTriggerEnter2D: " + other.name);
if (other.CompareTag("Player"))
// if the player is coming from above the crop down the crop should be above the player
if (other.transform.position.y < transform.position.y)
{
Debug.Log("CROP: OnTriggerEnter2D: Player is below crop");
_renderer.sortingOrder = 4;
}
else
{
Debug.Log("CROP: OnTriggerEnter2D: Player is above crop");
_renderer.sortingOrder = 0;
}
// If the player is coming from the right of the crop, the crop should be above the player
if (other.transform.position.x > transform.position.x)
{
Debug.Log("CROP: OnTriggerEnter2D: Player is to the right of crop");
_renderer.sortingOrder = 4;
}
else
{
Debug.Log("CROP: OnTriggerEnter2D: Player is to the left of crop");
_renderer.sortingOrder = 0;
}
}*/
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("CROP: OnTriggerEnter2D");
Debug.Log("CROP: OnTriggerEnter2D: " + other.name);
if (other.CompareTag("Player"))
{
_player = _player;
// Get the bounds of the player and the crop
var playerBounds = other.bounds;
var cropBounds = GetComponent<Collider2D>().bounds;
// Check if the top of the player is inside the crop
if (playerBounds.min.y > cropBounds.min.y && playerBounds.min.y < cropBounds.max.y)
{
Debug.Log("CROP: OnTriggerEnter2D: Bottom of player is inside crop");
_renderer.sortingOrder = 4;
}
// Check if the bottom of the player is inside the crop
if (playerBounds.max.y > cropBounds.min.y && playerBounds.max.y < cropBounds.max.y)
{
Debug.Log("CROP: OnTriggerEnter2D: Top of player is inside crop");
_renderer.sortingOrder = 0;
}
}
}
private void OnTriggerExit(Collider other)
{
// reset the sortingOrder
_renderer.sortingOrder = 0;
_player = null;
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("CROP: OnPointerClick");
var cropComponent = GetComponent<CropComponent>();
// if no tool is equipped
if (!cropComponent.isToolRequired())
{
if (!cropComponent.isHarvestable())
return;
cropComponent.Harvest();
return;
}
// should check what tool is equipped and active on the Hotbar.
var activeSlot = ToolbarManager.Instance.ActiveSlot;
// check if slot is tool
if (activeSlot.item is ToolItem tool)
// if the toolType is in the list from the crop
if (cropComponent.crop.validToolTypes.Contains(tool.toolType))
{
// check what tool it is and what action to take water or harvest
if (tool.toolType == ToolItem.ToolType.Watering)
{
cropComponent.Water();
return;
}
if (tool.toolType == ToolItem.ToolType.Harvesting)
{
if (!cropComponent.isHarvestable())
return;
cropComponent.Harvest();
}
}
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("CROP: OnPointerDown");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("CROP: OnPointerEnter");
// change the material of the crop to show it is selected
if (_renderer != null && hoverMaterial != null)
_originalMaterial = _renderer.material;
_renderer.material = hoverMaterial; // Change to hover material
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("CROP: OnPointerExit");
// change the material of the crop back to the original
if (_renderer != null && _originalMaterial != null)
Debug.Log("CROP: OnPointerExit: Changing Material");
_renderer.material = _originalMaterial; // Change to original material
}
}