Crops working

This commit is contained in:
j-weissen 2022-06-08 23:32:24 +02:00
parent 22a4abf927
commit 7ceb7e4bcb
10 changed files with 388 additions and 117 deletions

View file

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

View file

@ -2,35 +2,46 @@ using System;
using Tiles;
using UnityEngine;
public class TileBehaviour : MonoBehaviour {
public class TileBehaviour : MonoBehaviour
{
private BaseTile _tile;
private SpriteRenderer _hoverIndicatorSpriteRenderer;
// Start is called before the first frame update
void Start() {
void Start()
{
//_hoverIndicatorSpriteRenderer = gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>();
//SetHoverIndicatorVisibility(false);
SetTile(new GrassTile());
HouseController.NewDayEvent.AddListener(_tile.DayLightStep);
SetTile(new GrassTile(gameObject));
gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>().color = Color.clear;
}
void OnMouseDown() {
// Update is called once per frame
void Update()
{
}
void OnMouseDown()
{
UsableItem usable = null;
BaseTile tileToSetTo = null;
if(PlayerController.instance.GetSelectedItem() != null) {
if (PlayerController.instance.GetSelectedItem() != null)
{
usable = PlayerController.instance.GetSelectedItem();
}
tileToSetTo = _tile.Clicked(usable);
if(tileToSetTo != null) {
if (tileToSetTo != null)
{
SetTile(tileToSetTo);
}
}
void SetTile(BaseTile tileToSet) {
void SetTile(BaseTile tileToSet)
{
_tile = tileToSet;
GetComponent<SpriteRenderer>().sprite = _tile.Sprite;
}
@ -49,4 +60,4 @@ public class TileBehaviour : MonoBehaviour {
{
_hoverIndicatorSpriteRenderer.enabled = visible;
}
}
}

View file

@ -9,33 +9,26 @@ namespace Tiles
private Sprite _sprite;
public Sprite Sprite => _sprite;
protected BaseTile(String pathToImageFile)
protected GameObject _gameObject;
protected BaseTile(String pathToImageFile, GameObject gameObject)
{
this._gameObject = gameObject;
this._sprite = GenerateSpriteFromFile(pathToImageFile);
HouseController.NewDayEvent.AddListener(DayLightStep);
}
protected void Start()
{
}
protected void Update()
{
}
public void DayLightStep()
public virtual void DayLightStep()
{
}
public virtual BaseTile Clicked(UsableItem usable)
{
Debug.Log(usable.ToString() + " used on " + this.ToString());
return null;
}
static protected Sprite GenerateSpriteFromFile(String pathToImageFile)
static public Sprite GenerateSpriteFromFile(String pathToImageFile)
{
byte[] data = File.ReadAllBytes(pathToImageFile);
Texture2D texture = new Texture2D(32, 32, TextureFormat.ARGB32, false);

View file

@ -6,52 +6,23 @@ namespace Tiles
{
private Crop _crop;
public FarmlandTile() : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_008.png")
public FarmlandTile(GameObject gameObject) : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_008.png", gameObject)
{
_crop = null;
_crop = new Crop(gameObject.transform.GetChild(1).GetComponent<SpriteRenderer>(),
gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>());
}
public override void DayLightStep()
{
_crop.DayLightStep();
}
public new void DayLightStep()
{
}
public new BaseTile Clicked(UsableItem usable)
public override BaseTile Clicked(UsableItem usable)
{
base.Clicked(usable);
ItemContainer ic = ItemContainer.Instance;
if (usable.id == ic.GetItemIdByName("Hoe"))
{
Debug.Log("Farmland hydrated");
//_hydrated = true;
}
_crop.Clicked(usable);
if (usable.id == ic.GetItemIdByName("Wheat Seed") && _crop == null)
{
Plant();
}
if (usable.id == ic.GetItemIdByName("Scythe") && _crop != null && _crop.FullyGrown)
{
Harvest();
}
return null;
}
private void Harvest()
{
Debug.Log("Farmland harvested");
// add wheat to inventory
_crop = null;
}
private void Plant()
{
Debug.Log("Farmland planted");
// wheatSeeds-- in inventory
_crop = new Crop();
}
}
}

View file

@ -4,7 +4,7 @@ namespace Tiles
{
public class GrassTile : BaseTile
{
public GrassTile() : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_000.png")
public GrassTile(GameObject gameObject) : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_000.png", gameObject)
{
}
@ -21,7 +21,7 @@ namespace Tiles
base.Clicked(usable);
if (usable.id == ItemContainer.Instance.GetItemIdByName(new string("Hoe")))
{
rv = new FarmlandTile();
rv = new FarmlandTile(_gameObject);
}
}
return rv;

View file

@ -4,7 +4,7 @@ namespace Tiles
{
public class WaterTile : BaseTile
{
public WaterTile() : base(null)
public WaterTile() : base(null, null)
{
}