add field functinallity

This commit is contained in:
Janis
2023-03-05 17:03:16 +01:00
parent 71447ac83e
commit 95b70a77db
15 changed files with 215 additions and 102 deletions

View File

@@ -9,11 +9,11 @@ MonoBehaviour:
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 00ac127aa1678874aa5c9f1381b6b47a, type: 3}
m_Script: {fileID: 11500000, guid: 01eac50f7d5dd124e94a48f3e4428435, type: 3}
m_Name: Apple
m_EditorClassIdentifier:
uuid: apple
uuid: harvest_apple
itemName: Apple
sprite: {fileID: -992969038, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
sprite: {fileID: 0}
stackable: 1
maxStackSize: 100

View File

@@ -1,19 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 00ac127aa1678874aa5c9f1381b6b47a, type: 3}
m_Name: Banana
m_EditorClassIdentifier:
uuid: banana
itemName: Banana
sprite: {fileID: 1603187126, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
stackable: 1
maxStackSize: 100

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: cbbbe8384f7ec2845b4145744d51ecf8
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -17,7 +17,7 @@ MonoBehaviour:
sprite: {fileID: -992969038, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
stackable: 0
maxStackSize: 1
daysToGrow: 6
daysToGrow: 1
sprites:
- {fileID: -1568493089, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
- {fileID: 1918686608, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
@@ -25,3 +25,4 @@ MonoBehaviour:
- {fileID: -1058307475, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
- {fileID: -1664452952, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
- {fileID: 1402415594, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
harvest: {fileID: 11400000, guid: 601d7a33a6c338d49a35134d5c772940, type: 2}

View File

@@ -1,19 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ab99e668aa1ba5a469eef680f3bb4b45, type: 3}
m_Name: Hoe
m_EditorClassIdentifier:
uuid: Dia Hoe
itemName:
sprite: {fileID: 0}
stackable: 0
maxStackSize: 1

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 030984201821ba840872812944c9dc13
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -150,7 +150,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 122753038}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 9.95, y: 5.15, z: 0}
m_LocalPosition: {x: 1.71, y: 4.66, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
@@ -222,10 +222,9 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
radius: 3
interactText: Interact
interactTextOutOfRange: Out of range
field: {fileID: 0}
crop: {fileID: 0}
field: {fileID: 1047402457}
testCrop: {fileID: 11400000, guid: 4ce577b0996dc314ab1b42f42614e3a2, type: 2}
--- !u!61 &122753042
BoxCollider2D:
m_ObjectHideFlags: 0
@@ -554,7 +553,7 @@ SpriteRenderer:
m_SortingLayerID: 949894591
m_SortingLayer: -1
m_SortingOrder: 1
m_Sprite: {fileID: 109301499, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
m_Sprite: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
@@ -562,7 +561,7 @@ SpriteRenderer:
m_Size: {x: 10, y: 10}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_WasSpriteAssigned: 0
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!4 &518922043
@@ -573,7 +572,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 518922041}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0.1, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
@@ -959,7 +958,7 @@ SpriteRenderer:
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 2
m_Size: {x: 10, y: 10}
m_Size: {x: 14, y: 10}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
@@ -1314,6 +1313,10 @@ MonoBehaviour:
maxSlots: 0
maxStackSize: 0
items: []
harvestInventory:
maxSlots: 0
maxStackSize: 0
items: []
--- !u!1 &783176647
GameObject:
m_ObjectHideFlags: 0
@@ -57128,14 +57131,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 53c8af14dae8dbe4e95cda372289f89e, type: 3}
m_Name:
m_EditorClassIdentifier:
isPlaced: 0
placeableObject: {fileID: 0}
TESTCROP: {fileID: 0}
isPlaced: 1
placeableObject: {fileID: 11400000, guid: a29dd64dd331a6342b05e81d10b9b099, type: 2}
cropSpriteRenderer: {fileID: 518922042}
backgroundSpriteRenderer: {fileID: 531540403}
fieldController: {fileID: 122753038}
crop: {fileID: 0}
currentCropSprite: {fileID: 0}
daysSincePlanted: 0
isWatered: 1
state: 0
--- !u!1 &1507699183
GameObject:
m_ObjectHideFlags: 0

View File

@@ -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);
}
}

View File

@@ -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
}

View File

@@ -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

View File

@@ -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];

View File

@@ -8,4 +8,5 @@ public class Crop : Item
{
public int daysToGrow;
public List<Sprite> sprites = new List<Sprite>();
public Harvest harvest;
}

View 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
{
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 01eac50f7d5dd124e94a48f3e4428435
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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
{