222 lines
8.1 KiB
C#
222 lines
8.1 KiB
C#
using System.Linq;
|
|
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");
|
|
|
|
if (hoverMaterial == null)
|
|
hoverMaterial = Resources.Load<Material>("Materials/Sprites/HighlightPixelOutline");
|
|
}
|
|
|
|
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 = AppConfig.PLAYER_LAYER + 1;
|
|
// 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 = AppConfig.PLAYER_LAYER - 1;
|
|
|
|
// middle of the player is inside the crop
|
|
if (playerBounds.center.y > cropBounds.min.y && playerBounds.center.y < cropBounds.max.y)
|
|
_renderer.sortingOrder = AppConfig.PLAYER_LAYER + 1;
|
|
|
|
var overlapArea = Mathf.Max(0,
|
|
Mathf.Min(playerBounds.max.x, cropBounds.max.x) -
|
|
Mathf.Max(playerBounds.min.x, cropBounds.min.x))
|
|
* Mathf.Max(0,
|
|
Mathf.Min(playerBounds.max.y, cropBounds.max.y) -
|
|
Mathf.Max(playerBounds.min.y, cropBounds.min.y));
|
|
|
|
// Calculate the total area of the player's collider
|
|
var playerArea = playerBounds.size.x * playerBounds.size.y;
|
|
|
|
// Calculate the percentage of the player's collider that is inside the crop's collider
|
|
var overlapPercentage = overlapArea / playerArea;
|
|
|
|
// Adjust the opacity of the crop sprite based on the overlap percentage
|
|
var color = _renderer.material.color;
|
|
color.a = Mathf.Clamp(1 - overlapPercentage, 0.5f, 1); // Adjust the range as needed
|
|
_renderer.material.color = color;*/
|
|
}
|
|
|
|
|
|
// private void Update()
|
|
// {
|
|
// // Get the bounds of the player and the crop
|
|
// var playerBounds = _collider2D.bounds;
|
|
// var cropBounds = _localCollider2D.bounds;
|
|
//
|
|
// // Calculate the overlap area
|
|
// }
|
|
|
|
/*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"))
|
|
{
|
|
// 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 = 101;
|
|
}
|
|
|
|
// 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 = 99;
|
|
}
|
|
}*/
|
|
}
|
|
|
|
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<CropInteractiveComponent>();
|
|
|
|
if (cropComponent == null)
|
|
{
|
|
Debug.LogError("CROP: OnPointerClick: CropInteractiveComponent is null");
|
|
return;
|
|
}
|
|
|
|
if (cropComponent.interactable == null)
|
|
{
|
|
Debug.LogError("CROP: OnPointerClick: CropInteractable is null");
|
|
return;
|
|
}
|
|
|
|
// if no tool is equipped
|
|
if (!cropComponent.interactable.IsToolRequired())
|
|
{
|
|
if (!cropComponent.interactable.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.interactable.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.interactable.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
|
|
}
|
|
} |