159 lines
4.8 KiB
C#
159 lines
4.8 KiB
C#
using System.Linq;
|
|
using Managers;
|
|
using UnityEngine;
|
|
|
|
public class CropInteractiveComponent : InteractiveComponent
|
|
{
|
|
// Crop state
|
|
public int isWatered;
|
|
public CropInteractable interactable;
|
|
|
|
private void Start()
|
|
{
|
|
DayNightCycleManager.OnDayChange += OnDayChange;
|
|
|
|
// attach the crop handler to the crop interactable
|
|
// create crop handler
|
|
gameObject.AddComponent<CropHandler>();
|
|
// attach to this gameobject as its parent
|
|
// cropHandler.transform.parent = transform;
|
|
|
|
// set to first growth stage as seed
|
|
/*interactable.currentGrowthStage = 1;*/
|
|
// set the crop sprite
|
|
spriteRenderer.sprite = interactable.growthSprites[0];
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
DayNightCycleManager.OnDayChange -= OnDayChange;
|
|
}
|
|
|
|
public void Harvest()
|
|
{
|
|
if (interactable != null && interactable.cropItem != null && interactable.IsHarvestable())
|
|
{
|
|
Debug.Log("Harvesting Crop:" + interactable.cropItem.itemName);
|
|
// add crop to inventory
|
|
var item = Instantiate(interactable.cropItem);
|
|
PlayerManager.Instance.AddItemToInventorySlot(item);
|
|
// remove crop from tile
|
|
interactable = null;
|
|
// delete visual
|
|
Destroy(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Crop is null or not harvestable");
|
|
}
|
|
}
|
|
|
|
public void Water()
|
|
{
|
|
// Water the crop
|
|
Debug.Log("Watering Crop");
|
|
interactable.isWatered = true;
|
|
}
|
|
|
|
public void SetCrop(CropInteractable tileCropInteractable)
|
|
{
|
|
interactable = tileCropInteractable;
|
|
// change the crop sprite
|
|
// change the crop material
|
|
if (spriteRenderer != null && interactable != null)
|
|
{
|
|
Debug.Log("CROP: SetCrop: " + interactable.name + " Growth Stage: " +
|
|
interactable.currentGrowthStage);
|
|
spriteRenderer.sprite = interactable.growthSprites[interactable.currentGrowthStage - 1];
|
|
}
|
|
}
|
|
|
|
public override void Interact()
|
|
{
|
|
Debug.Log("Crop Interact");
|
|
// check what tool the player has equipped
|
|
var activeToolbarSlot = PlayerManager.Instance.GetActiveToolbarSlot();
|
|
if (activeToolbarSlot == null)
|
|
{
|
|
// chgeck if tool is required
|
|
if (interactable.IsToolRequired())
|
|
{
|
|
Debug.Log("Tool is required");
|
|
return;
|
|
}
|
|
|
|
if (interactable.IsHarvestable()) Harvest();
|
|
}
|
|
|
|
// check if the item in the slot is a ToolItem
|
|
if (activeToolbarSlot.item is ToolItem tool)
|
|
// check if the toolType is in the list from the crop
|
|
if (interactable.validToolTypes.Contains(tool.toolType))
|
|
{
|
|
// check what tool it is and what action to take water or harvest
|
|
if (tool.toolType == ToolItem.ToolType.Watering)
|
|
{
|
|
Water();
|
|
return;
|
|
}
|
|
|
|
if (tool.toolType == ToolItem.ToolType.Harvesting)
|
|
if (interactable.IsHarvestable())
|
|
Harvest();
|
|
|
|
if (tool.toolType == ToolItem.ToolType.Mining)
|
|
{
|
|
// destroy crop
|
|
interactable = null;
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
if (interactable == null) Debug.Log("Crop is null");
|
|
}
|
|
|
|
public void ChangeSprite(int stage)
|
|
{
|
|
if (interactable != null)
|
|
{
|
|
Debug.Log("Crop InteractiveComponent ChangeSprite");
|
|
spriteRenderer.sprite = interactable.growthSprites[stage];
|
|
}
|
|
}
|
|
|
|
public void OnDayChange()
|
|
{
|
|
if (interactable != null)
|
|
{
|
|
Debug.Log("Crop InteractiveComponent OnDayChange");
|
|
interactable.OnDayChange();
|
|
|
|
if (spriteRenderer == null)
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
if (spriteRenderer != null)
|
|
{
|
|
Debug.Log("Crop sprite renderer is not null Stage:" + interactable.currentGrowthStage);
|
|
var currStage = interactable.currentGrowthStage - 1;
|
|
Debug.Log("Crop sprite renderer is not null currStage:" + currStage);
|
|
if (currStage < 0) currStage = 0;
|
|
ChangeSprite(currStage);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Crop sprite renderer is null");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Crop interactable is null");
|
|
}
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
var cropInteractable = Instantiate(interactable);
|
|
SetCrop(cropInteractable);
|
|
// set position to center of tile
|
|
}
|
|
} |