mirror of
https://github.com/DerTyp7/harvestdale-unity.git
synced 2025-10-30 13:07:10 +01:00
add field functinallity
This commit is contained in:
@@ -2,9 +2,40 @@ using UnityEngine;
|
||||
|
||||
public class FarmManager : MonoBehaviour
|
||||
{
|
||||
public Inventory<Crop> cropInventory;
|
||||
|
||||
// Singleton
|
||||
public static FarmManager Instance { get; private set; }
|
||||
|
||||
[SerializeField]
|
||||
private Inventory<Crop> cropInventory;
|
||||
|
||||
[SerializeField]
|
||||
private Inventory<Harvest> harvestInventory;
|
||||
|
||||
public Inventory<Crop> CropInventory
|
||||
{
|
||||
get { return cropInventory; }
|
||||
}
|
||||
|
||||
public Inventory<Harvest> HarvestInventory
|
||||
{
|
||||
get { return harvestInventory; }
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
cropInventory = new Inventory<Crop>(2, 200);
|
||||
harvestInventory = new Inventory<Harvest>(2, 200);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
// Contains logic for the field
|
||||
// Checks if a plant is dead, growing or harvestable
|
||||
// Determines what sprite to show
|
||||
// Handles planting and harvesting
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
public enum FieldState
|
||||
@@ -10,57 +15,133 @@ public enum FieldState
|
||||
|
||||
public class Field : Building
|
||||
{
|
||||
public Crop crop;
|
||||
[Header("Children Objects")]
|
||||
[SerializeField]
|
||||
private SpriteRenderer cropSpriteRenderer;
|
||||
|
||||
public SpriteRenderer currentCropSpriteRenderer;
|
||||
public int daysSincePlanted;
|
||||
public bool isWatered = false;
|
||||
public FieldState state = FieldState.EMPTY;
|
||||
[SerializeField]
|
||||
private SpriteRenderer backgroundSpriteRenderer;
|
||||
|
||||
[SerializeField]
|
||||
private GameObject fieldController;
|
||||
|
||||
[Header("Field Properties")]
|
||||
[SerializeField]
|
||||
private Crop crop;
|
||||
|
||||
private Vector2Int size;
|
||||
private int daysSincePlanted;
|
||||
private bool isWatered = false;
|
||||
|
||||
private FieldState state = FieldState.EMPTY;
|
||||
|
||||
public FieldState State
|
||||
{
|
||||
get { return state; }
|
||||
}
|
||||
|
||||
public bool IsWatered
|
||||
{
|
||||
get { return isWatered; }
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
TimeManager.OnDayChanged += AddDay;
|
||||
size = DeterminSize();
|
||||
SetFieldControllerPosition();
|
||||
fieldController.GetComponent<FieldController>().SetField(this);
|
||||
|
||||
TimeManager.OnDayChanged += DayInterval;
|
||||
}
|
||||
|
||||
public override void OnPlace()
|
||||
{
|
||||
EmptyField();
|
||||
}
|
||||
|
||||
private void SetFieldControllerPosition()
|
||||
{
|
||||
if (fieldController)
|
||||
{
|
||||
fieldController.transform.localPosition = new Vector3(size.x, size.y / 2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2Int DeterminSize()
|
||||
{
|
||||
return new Vector2Int(Mathf.FloorToInt(backgroundSpriteRenderer.size.x), Mathf.FloorToInt(backgroundSpriteRenderer.size.y));
|
||||
}
|
||||
|
||||
private void SetSprite(Sprite sprite)
|
||||
{
|
||||
if (currentCropSpriteRenderer)
|
||||
if (cropSpriteRenderer)
|
||||
{
|
||||
currentCropSpriteRenderer.sprite = sprite;
|
||||
currentCropSpriteRenderer.drawMode = SpriteDrawMode.Tiled;
|
||||
currentCropSpriteRenderer.size = new Vector2(10, 10); // TODO: Make this dynamic
|
||||
cropSpriteRenderer.sprite = sprite;
|
||||
cropSpriteRenderer.drawMode = SpriteDrawMode.Tiled;
|
||||
cropSpriteRenderer.size = size;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void AddDay()
|
||||
private void DayInterval()
|
||||
{
|
||||
if (crop && isPlaced && state != FieldState.DEAD)
|
||||
if (state == FieldState.GROWING)
|
||||
{
|
||||
if (!isWatered)
|
||||
state = FieldState.DEAD;
|
||||
else
|
||||
{
|
||||
daysSincePlanted++;
|
||||
if (daysSincePlanted >= crop.daysToGrow)
|
||||
{
|
||||
state = FieldState.HARVESTABLE;
|
||||
}
|
||||
SetSprite(crop.sprites[daysSincePlanted]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#region Field controlling
|
||||
public void Plant(Crop newCrop)
|
||||
{
|
||||
daysSincePlanted = 0;
|
||||
state = FieldState.GROWING;
|
||||
crop = newCrop;
|
||||
SetSprite(crop.sprites[0]);
|
||||
bool enoughItemsInInventory = FarmManager.Instance.CropInventory.RemoveExactAmount(newCrop, size.x * size.y);
|
||||
if (enoughItemsInInventory)
|
||||
{
|
||||
daysSincePlanted = 0;
|
||||
state = FieldState.GROWING;
|
||||
crop = newCrop;
|
||||
SetSprite(crop.sprites[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Not enough items in inventory");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void OnPlace()
|
||||
public void Harvest()
|
||||
{
|
||||
Plant(null);
|
||||
if (state == FieldState.HARVESTABLE)
|
||||
{
|
||||
FarmManager.Instance.HarvestInventory.Add(crop.harvest, size.x * size.y);
|
||||
state = FieldState.EMPTY;
|
||||
SetSprite(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void Water()
|
||||
{
|
||||
isWatered = true;
|
||||
}
|
||||
|
||||
public void UnWater()
|
||||
{
|
||||
isWatered = false;
|
||||
}
|
||||
|
||||
public void EmptyField()
|
||||
{
|
||||
state = FieldState.EMPTY;
|
||||
SetSprite(null);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -7,8 +7,7 @@ public abstract class Interactable : MonoBehaviour
|
||||
[Range(0f, 10f)]
|
||||
private float radius = 2f; // radius of interaction
|
||||
|
||||
[SerializeField]
|
||||
private string interactText = "Interact"; // text to display when player is in range
|
||||
public abstract string interactText { get; } // text to display when player is in range
|
||||
|
||||
[SerializeField]
|
||||
private string interactTextOutOfRange = "Out of range"; // text to display when player is out of range
|
||||
|
||||
@@ -11,6 +11,10 @@ public class Inventory<TItem> where TItem : Item
|
||||
[SerializeField]
|
||||
private InventoryItem<TItem>[] items;
|
||||
|
||||
public InventoryItem<TItem>[] Items
|
||||
{
|
||||
get { return items; }
|
||||
}
|
||||
public Inventory(int maxSlots = 1, int maxStackSize = 100)
|
||||
{
|
||||
this.maxSlots = maxSlots;
|
||||
@@ -89,7 +93,7 @@ public class Inventory<TItem> where TItem : Item
|
||||
|
||||
|
||||
// removes an item from the inventory, returns the quantity of items which could not be removed
|
||||
public int Remove(Item item, int count)
|
||||
public int Remove(TItem item, int count)
|
||||
{
|
||||
int remainingCount = count;
|
||||
|
||||
@@ -113,17 +117,48 @@ public class Inventory<TItem> where TItem : Item
|
||||
// exit the loop if all items have been removed
|
||||
if (remainingCount == 0)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return the quantity of items which could not be removed
|
||||
|
||||
return remainingCount;
|
||||
}
|
||||
|
||||
public int GetCountOfItem(TItem item)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
// look for an existing stack of the item in the inventory
|
||||
for (int i = 0; i < items.Length; i++)
|
||||
{
|
||||
InventoryItem<TItem> invItem = items[i];
|
||||
if (invItem != null && invItem.item == item)
|
||||
{
|
||||
count += invItem.count;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
// Remove the exact amount of items from the inventory
|
||||
// If the amount of items in the inventory is less than the amount to remove, it will remove nothing and return false
|
||||
public bool RemoveExactAmount(TItem item, int count)
|
||||
{
|
||||
if (GetCountOfItem(item) < count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Remove(item, count);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void SwapItems(int index1, int index2)
|
||||
{
|
||||
InventoryItem<TItem> temp = items[index1];
|
||||
|
||||
@@ -8,4 +8,5 @@ public class Crop : Item
|
||||
{
|
||||
public int daysToGrow;
|
||||
public List<Sprite> sprites = new List<Sprite>();
|
||||
public Harvest harvest;
|
||||
}
|
||||
9
Assets/Scripts/Items/Harvest.cs
Normal file
9
Assets/Scripts/Items/Harvest.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
[CreateAssetMenu(fileName = "Harvest", menuName = "Harvestdale/Items/Harvest", order = 0)]
|
||||
public class Harvest : Item
|
||||
{
|
||||
}
|
||||
11
Assets/Scripts/Items/Harvest.cs.meta
Normal file
11
Assets/Scripts/Items/Harvest.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 01eac50f7d5dd124e94a48f3e4428435
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,7 +1,5 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
[CreateAssetMenu(fileName = "Item", menuName = "Harvestdale/Items/Item", order = 0)]
|
||||
public class Item : ScriptableObject
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user