Crops working
This commit is contained in:
parent
22a4abf927
commit
7ceb7e4bcb
10 changed files with 388 additions and 117 deletions
|
|
@ -1,50 +1,189 @@
|
|||
using UnityEngine;
|
||||
using System.Security.Cryptography;
|
||||
using Tiles;
|
||||
using UnityEngine;
|
||||
using static CropAction;
|
||||
|
||||
|
||||
enum CropAction
|
||||
{
|
||||
NextDay,
|
||||
Hoe,
|
||||
Scythe,
|
||||
WateringCan,
|
||||
Seeds
|
||||
}
|
||||
|
||||
|
||||
public class Crop
|
||||
{
|
||||
private const int DaysUntilFinished = 4;
|
||||
private const int FinalGrowthStage = 4;
|
||||
|
||||
private bool _fullyGrown;
|
||||
public bool FullyGrown => _fullyGrown;
|
||||
private static Sprite _smallCrop;
|
||||
private static Sprite _fullyGrownCrop;
|
||||
|
||||
private bool _markedForDeletion;
|
||||
public bool MarkedForDeletion => _markedForDeletion;
|
||||
private static Color _hydratedColor;
|
||||
|
||||
private bool _dead;
|
||||
public bool IsDead => _dead;
|
||||
private bool _planted;
|
||||
|
||||
private bool _fullyGrown => (_growthStage >= FinalGrowthStage);
|
||||
|
||||
private bool _hydrated;
|
||||
|
||||
private int _daysGrown;
|
||||
private int _growthStage;
|
||||
|
||||
public Crop()
|
||||
private SpriteRenderer _cropSpriteRenderer;
|
||||
private SpriteRenderer _hydrationSpriteRenderer;
|
||||
|
||||
public Crop(SpriteRenderer cropSpriteRenderer, SpriteRenderer hydrationSpriteRenderer)
|
||||
{
|
||||
Debug.Log("Crop created");
|
||||
_fullyGrown = false;
|
||||
_markedForDeletion = false;
|
||||
_daysGrown = 0;
|
||||
_planted = false;
|
||||
_hydrated = false;
|
||||
|
||||
_cropSpriteRenderer = cropSpriteRenderer;
|
||||
_hydrationSpriteRenderer = hydrationSpriteRenderer;
|
||||
|
||||
_smallCrop = BaseTile.GenerateSpriteFromFile("Assets/Farming Asset Pack/Split Assets/farming_tileset_027.png");
|
||||
_fullyGrownCrop = BaseTile.GenerateSpriteFromFile("Assets/Farming Asset Pack/Split Assets/farming_tileset_040.png");
|
||||
|
||||
_hydratedColor = new Color(0.5f, 0.5f, 1.0f, 0.5f);
|
||||
|
||||
UpdateSprite();
|
||||
}
|
||||
|
||||
private void Grow()
|
||||
{
|
||||
Debug.Log("Crop grown");
|
||||
_daysGrown++;
|
||||
_growthStage++;
|
||||
}
|
||||
|
||||
public void DayLightStep(bool hydrated)
|
||||
public void DayLightStep()
|
||||
{
|
||||
if (_daysGrown >= DaysUntilFinished)
|
||||
{
|
||||
_fullyGrown = true;
|
||||
}
|
||||
Debug.Log("Crop.DayLightStep");
|
||||
ApplyAction(NextDay);
|
||||
}
|
||||
|
||||
if (!hydrated)
|
||||
public void Clicked(UsableItem usableItem)
|
||||
{
|
||||
Debug.Log("Crop.Clicked UsableItem " + usableItem);
|
||||
if (usableItem != null)
|
||||
{
|
||||
_markedForDeletion = true;
|
||||
}
|
||||
else if (!_fullyGrown)
|
||||
{
|
||||
Grow();
|
||||
ItemContainer ic = ItemContainer.Instance;
|
||||
if (ic.GetItemIdByName("Hoe") == usableItem.id)
|
||||
{
|
||||
ApplyAction(Hoe);
|
||||
}
|
||||
else if (ic.GetItemIdByName("Scythe") == usableItem.id)
|
||||
{
|
||||
ApplyAction(Scythe);
|
||||
}
|
||||
else if (ic.GetItemIdByName("Wheat Seeds") == usableItem.id)
|
||||
{
|
||||
ApplyAction(Seeds);
|
||||
}
|
||||
else if (ic.GetItemIdByName("Watering Can") == usableItem.id)
|
||||
{
|
||||
ApplyAction(WateringCan);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyAction(CropAction action)
|
||||
{
|
||||
Debug.Log("ApplyAction: CropAction " + action);
|
||||
if (_planted)
|
||||
{
|
||||
if (Hoe == action)
|
||||
{
|
||||
_planted = false;
|
||||
}
|
||||
else if (Scythe == action)
|
||||
{
|
||||
if (_fullyGrown)
|
||||
{
|
||||
Harvest();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!_planted)
|
||||
{
|
||||
if (Seeds == action)
|
||||
{
|
||||
_planted = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (_hydrated)
|
||||
{
|
||||
if (NextDay == action)
|
||||
{
|
||||
_hydrated = false;
|
||||
if (_planted)
|
||||
{
|
||||
Grow();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!_hydrated)
|
||||
{
|
||||
if (WateringCan == action)
|
||||
{
|
||||
_hydrated = true;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateSprite();
|
||||
}
|
||||
|
||||
private void Harvest()
|
||||
{
|
||||
|
||||
AddCropToInventory();
|
||||
_planted = false;
|
||||
_growthStage = 0;
|
||||
}
|
||||
|
||||
private void AddCropToInventory()
|
||||
{
|
||||
Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Wheat Seeds"), 3);
|
||||
}
|
||||
|
||||
private void UpdateSprite()
|
||||
{
|
||||
Dump();
|
||||
if (_planted)
|
||||
{
|
||||
if (_fullyGrown)
|
||||
{
|
||||
//Debug.Log("sprite fg");
|
||||
_cropSpriteRenderer.sprite = _fullyGrownCrop;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Debug.Log("sprite smallCrop");
|
||||
_cropSpriteRenderer.sprite = _smallCrop;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_cropSpriteRenderer.sprite = null;
|
||||
}
|
||||
|
||||
if (_hydrated)
|
||||
{
|
||||
//Debug.Log("sprite hydrated");
|
||||
_hydrationSpriteRenderer.color = _hydratedColor;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Debug.Log("sprite no hydrated");
|
||||
_hydrationSpriteRenderer.color = Color.clear;
|
||||
}
|
||||
}
|
||||
|
||||
private void Dump()
|
||||
{
|
||||
Debug.Log("age: " + _growthStage + "\n" +
|
||||
"hydrated: " + _hydrated + "\n" +
|
||||
"planted: " + _planted);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue