172 lines
5.5 KiB
C#
172 lines
5.5 KiB
C#
using System.Linq;
|
|
using Data.ScriptableObjects.World;
|
|
using FishNet.Object;
|
|
using Handlers;
|
|
using Managers;
|
|
using Types.Items;
|
|
using UnityEngine;
|
|
|
|
namespace Components.Grid
|
|
{
|
|
public class CropInteractiveComponent : InteractiveComponent
|
|
{
|
|
// Crop state
|
|
public int isWatered;
|
|
public CropInteractable interactable;
|
|
|
|
[SerializeField] private PlayerManager playerManager;
|
|
|
|
private void Start()
|
|
{
|
|
GameTimeManager.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()
|
|
{
|
|
GameTimeManager.OnDayChange -= OnDayChange;
|
|
}
|
|
|
|
|
|
[ServerRpc]
|
|
public void Harvest()
|
|
{
|
|
if (interactable != null && interactable.CropItem != null && interactable.IsHarvestable())
|
|
{
|
|
Debug.Log("Harvesting Crop:" + interactable.CropItem.ItemName);
|
|
// add crop to inventory
|
|
var item = interactable.CropItem.Clone();
|
|
|
|
playerManager.AddItemToInventory(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.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 is { Item: 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 == ToolType.Watering)
|
|
{
|
|
Water();
|
|
return;
|
|
}
|
|
|
|
if (tool.ToolType == ToolType.Harvesting)
|
|
if (interactable.IsHarvestable())
|
|
Harvest();
|
|
|
|
if (tool.ToolType == 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
|
|
}
|
|
}
|
|
} |