From 7ceb7e4bcb7a0dad6be7033b1008e4c8bdd71451 Mon Sep 17 00:00:00 2001 From: j-weissen Date: Wed, 8 Jun 2022 23:32:24 +0200 Subject: [PATCH] Crops working --- Assets/Farming Asset Pack/Split Assets.meta | 8 - Assets/Resources/BaseTile.prefab | 180 +++++++++++++++++- Assets/Scenes/MainScene.unity | 4 + Assets/Scripts/Crop.cs | 193 +++++++++++++++++--- Assets/Scripts/TileBehaviour.cs | 31 +++- Assets/Scripts/Tiles/BaseTile.cs | 21 +-- Assets/Scripts/Tiles/FarmlandTile.cs | 49 +---- Assets/Scripts/Tiles/GrassTile.cs | 4 +- Assets/Scripts/Tiles/WaterTile.cs | 2 +- Packages/packages-lock.json | 13 +- 10 files changed, 388 insertions(+), 117 deletions(-) delete mode 100644 Assets/Farming Asset Pack/Split Assets.meta diff --git a/Assets/Farming Asset Pack/Split Assets.meta b/Assets/Farming Asset Pack/Split Assets.meta deleted file mode 100644 index 658129b..0000000 --- a/Assets/Farming Asset Pack/Split Assets.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: f8e433f0b06051243aaf8575a862a7e4 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Resources/BaseTile.prefab b/Assets/Resources/BaseTile.prefab index 508482c..c06b95a 100644 --- a/Assets/Resources/BaseTile.prefab +++ b/Assets/Resources/BaseTile.prefab @@ -1,5 +1,89 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1 &1718624093595902244 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 7848957424376950886} + - component: {fileID: 1619632772125140264} + m_Layer: 0 + m_Name: Crop + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &7848957424376950886 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1718624093595902244} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 4752245148499718129} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!212 &1619632772125140264 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1718624093595902244} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: -278359049 + m_SortingLayer: 2 + m_SortingOrder: 0 + m_Sprite: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 0 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 --- !u!1 &1915705303471624000 GameObject: m_ObjectHideFlags: 0 @@ -16,7 +100,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &2275953190273124041 Transform: m_ObjectHideFlags: 0 @@ -30,7 +114,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 4752245148499718129} - m_RootOrder: 0 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!212 &151970701671681920 SpriteRenderer: @@ -84,6 +168,90 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 +--- !u!1 &2145333408190307379 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 6840747130279028589} + - component: {fileID: 6444954891387505866} + m_Layer: 0 + m_Name: HydrationIndicator + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &6840747130279028589 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2145333408190307379} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 4752245148499718129} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!212 &6444954891387505866 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2145333408190307379} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: -2016319409 + m_SortingLayer: 1 + m_SortingOrder: 1 + m_Sprite: {fileID: 7482667652216324306, guid: 311925a002f4447b3a28927169b83ea6, type: 3} + m_Color: {r: 0.49803922, g: 0.49803922, b: 1, a: 0.57254905} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 --- !u!1 &4752245148499717901 GameObject: m_ObjectHideFlags: 0 @@ -111,10 +279,12 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 4752245148499717901} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 2.21, y: 1.43, z: 0} + m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: + - {fileID: 6840747130279028589} + - {fileID: 7848957424376950886} - {fileID: 2275953190273124041} m_Father: {fileID: 0} m_RootOrder: 0 @@ -157,8 +327,8 @@ SpriteRenderer: m_AutoUVMaxDistance: 0.5 m_AutoUVMaxAngle: 89 m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 + m_SortingLayerID: -2016319409 + m_SortingLayer: 1 m_SortingOrder: 0 m_Sprite: {fileID: 0} m_Color: {r: 1, g: 1, b: 1, a: 1} diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index 1deef89..b677d3e 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -7064,6 +7064,10 @@ MonoBehaviour: - {fileID: 11400000, guid: 008a8fdd2c3a95745acafee4087a855d, type: 2} - {fileID: 11400000, guid: 430db451ae959f34b8fba8d8b17276fd, type: 2} - {fileID: 11400000, guid: ea1a26b19bc34a0ba29bad77253c7266, type: 2} + - {fileID: 11400000, guid: d651d57ba97a4246a0094409e29fe56a, type: 2} + - {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2} + - {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2} + - {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2} --- !u!114 &1800469992 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Crop.cs b/Assets/Scripts/Crop.cs index 8d7bf02..56c4a26 100644 --- a/Assets/Scripts/Crop.cs +++ b/Assets/Scripts/Crop.cs @@ -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); + } + } \ No newline at end of file diff --git a/Assets/Scripts/TileBehaviour.cs b/Assets/Scripts/TileBehaviour.cs index b449760..7dc5d94 100644 --- a/Assets/Scripts/TileBehaviour.cs +++ b/Assets/Scripts/TileBehaviour.cs @@ -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(); //SetHoverIndicatorVisibility(false); - SetTile(new GrassTile()); - - HouseController.NewDayEvent.AddListener(_tile.DayLightStep); + SetTile(new GrassTile(gameObject)); + gameObject.transform.GetChild(0).GetComponent().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().sprite = _tile.Sprite; } @@ -49,4 +60,4 @@ public class TileBehaviour : MonoBehaviour { { _hoverIndicatorSpriteRenderer.enabled = visible; } -} \ No newline at end of file +} diff --git a/Assets/Scripts/Tiles/BaseTile.cs b/Assets/Scripts/Tiles/BaseTile.cs index 0b5d34f..208d5bd 100644 --- a/Assets/Scripts/Tiles/BaseTile.cs +++ b/Assets/Scripts/Tiles/BaseTile.cs @@ -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); diff --git a/Assets/Scripts/Tiles/FarmlandTile.cs b/Assets/Scripts/Tiles/FarmlandTile.cs index 7ad967c..01c30a8 100644 --- a/Assets/Scripts/Tiles/FarmlandTile.cs +++ b/Assets/Scripts/Tiles/FarmlandTile.cs @@ -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(), + gameObject.transform.GetChild(0).GetComponent()); + } + + 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(); - } } } \ No newline at end of file diff --git a/Assets/Scripts/Tiles/GrassTile.cs b/Assets/Scripts/Tiles/GrassTile.cs index 56b4fc7..21eb78d 100644 --- a/Assets/Scripts/Tiles/GrassTile.cs +++ b/Assets/Scripts/Tiles/GrassTile.cs @@ -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; diff --git a/Assets/Scripts/Tiles/WaterTile.cs b/Assets/Scripts/Tiles/WaterTile.cs index 37afdd7..c262015 100644 --- a/Assets/Scripts/Tiles/WaterTile.cs +++ b/Assets/Scripts/Tiles/WaterTile.cs @@ -4,7 +4,7 @@ namespace Tiles { public class WaterTile : BaseTile { - public WaterTile() : base(null) + public WaterTile() : base(null, null) { } diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 40769bb..4f0557e 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -137,21 +137,12 @@ "dependencies": {}, "url": "https://packages.unity.com" }, - "com.unity.nuget.newtonsoft-json": { - "version": "3.0.2", - "depth": 2, - "source": "registry", - "dependencies": {}, - "url": "https://packages.unity.com" - }, "com.unity.services.core": { - "version": "1.3.1", + "version": "1.0.1", "depth": 1, "source": "registry", "dependencies": { - "com.unity.modules.unitywebrequest": "1.0.0", - "com.unity.nuget.newtonsoft-json": "3.0.2", - "com.unity.modules.androidjni": "1.0.0" + "com.unity.modules.unitywebrequest": "1.0.0" }, "url": "https://packages.unity.com" },