This commit is contained in:
Janis Meister
2023-03-06 15:52:26 +01:00
parent 95b70a77db
commit d26c1fe607
6 changed files with 96 additions and 43 deletions

View File

@@ -52,6 +52,7 @@ public class Field : Building
fieldController.GetComponent<FieldController>().SetField(this);
TimeManager.OnDayChanged += DayInterval;
TimeManager.OnMonthChanged += MonthInterval;
}
public override void OnPlace()
@@ -90,12 +91,18 @@ public class Field : Building
state = FieldState.DEAD;
else
{
daysSincePlanted++;
if (daysSincePlanted >= crop.daysToGrow)
{
state = FieldState.HARVESTABLE;
}
SetSprite(crop.sprites[daysSincePlanted]);
// SetSprite(crop.Sprites[daysSincePlanted]);
}
}
}
private void MonthInterval()
{
if (state == FieldState.GROWING)
{
if (crop.SeasonToHarvest == TimeManager.CurrentSeason)
{
state = FieldState.HARVESTABLE;
}
}
}
@@ -109,7 +116,7 @@ public class Field : Building
daysSincePlanted = 0;
state = FieldState.GROWING;
crop = newCrop;
SetSprite(crop.sprites[0]);
SetSprite(crop.Sprites[0]);
}
else
{
@@ -122,7 +129,7 @@ public class Field : Building
{
if (state == FieldState.HARVESTABLE)
{
FarmManager.Instance.HarvestInventory.Add(crop.harvest, size.x * size.y);
FarmManager.Instance.HarvestInventory.Add(crop.HarvestItem, size.x * size.y);
state = FieldState.EMPTY;
SetSprite(null);
}

View File

@@ -41,29 +41,25 @@ public class Inventory<TItem> where TItem : Item
{
int remainingCount = count;
// check if the item is stackable
if (item.stackable)
// look for an existing stack of the same item in the inventory
for (int i = 0; i < items.Length; i++)
{
// look for an existing stack of the same item in the inventory
for (int i = 0; i < items.Length; i++)
InventoryItem<TItem> invItem = items[i];
if (invItem != null && invItem.item == item && invItem.count < maxStackSize)
{
InventoryItem<TItem> invItem = items[i];
if (invItem != null && invItem.item == item && invItem.count < maxStackSize)
// add as many items as possible to the stack
int space = maxStackSize - invItem.count;
int toAdd = Mathf.Min(space, remainingCount);
invItem.count += toAdd;
remainingCount -= toAdd;
// exit the loop if all items have been added
if (remainingCount == 0)
{
// add as many items as possible to the stack
int space = maxStackSize - invItem.count;
int toAdd = Mathf.Min(space, remainingCount);
invItem.count += toAdd;
remainingCount -= toAdd;
// exit the loop if all items have been added
if (remainingCount == 0)
{
return 0;
}
return 0;
}
}
}

View File

@@ -6,7 +6,35 @@ using System.Collections.Generic;
[CreateAssetMenu(fileName = "Crop", menuName = "Harvestdale/Items/Crop", order = 0)]
public class Crop : Item
{
public int daysToGrow;
public List<Sprite> sprites = new List<Sprite>();
public Harvest harvest;
[SerializeField]
private List<Sprite> sprites = new List<Sprite>();
[SerializeField]
private Harvest harvestItem;
[SerializeField]
private Season seasonToPlant;
[SerializeField]
private Season seasonToHarvest;
public List<Sprite> Sprites
{
get { return sprites; }
}
public Harvest HarvestItem
{
get { return harvestItem; }
}
public Season SeasonToPlant
{
get { return seasonToPlant; }
}
public Season SeasonToHarvest
{
get { return seasonToHarvest; }
}
}

View File

@@ -1,9 +1,14 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
[CreateAssetMenu(fileName = "Harvest", menuName = "Harvestdale/Items/Harvest", order = 0)]
public class Harvest : Item
{
[SerializeField]
private int price;
public int Price
{
get { return price; }
}
}

View File

@@ -3,21 +3,28 @@ using UnityEngine;
[CreateAssetMenu(fileName = "Item", menuName = "Harvestdale/Items/Item", order = 0)]
public class Item : ScriptableObject
{
public string uuid;
public string itemName;
[SerializeField]
private string uuid;
public Sprite sprite;
[SerializeField]
private string itemName;
[Tooltip("Tools will set this to false on Awake()")]
public bool stackable = true;
public int maxStackSize = 100;
[SerializeField]
private Sprite icon;
private void OnEnable()
public string UUID
{
if (!stackable)
{
maxStackSize = 1;
}
get { return uuid; }
}
public string ItemName
{
get { return itemName; }
}
public Sprite Icon
{
get { return icon; }
}
}

View File

@@ -1,6 +1,14 @@
using UnityEngine;
using System;
public enum Season
{
SPRING = 1,
SUMMER = 2,
AUTUMN = 3,
WINTER = 4
}
public class TimeManager : MonoBehaviour
{
public static Action OnMinuteChanged;
@@ -16,6 +24,8 @@ public class TimeManager : MonoBehaviour
public static int Month { get; private set; }
public static int Year { get; private set; }
public static Season CurrentSeason => (Season)Month;
[SerializeField]
[Range(.05f, 10f)]
private float minuteToRealTime = .05f;