From afaddd940c7957629eb890db48bee1b5633df115 Mon Sep 17 00:00:00 2001 From: j-weissen Date: Thu, 9 Jun 2022 16:47:17 +0200 Subject: [PATCH] Refactored to ActionInvoker.cs --- Assets/Scripts/ActionInvoker.cs | 123 ++++++++++++++++++++++++ Assets/Scripts/ActionInvoker.cs.meta | 3 + Assets/Scripts/Crop.cs | 138 +++++---------------------- Assets/Scripts/FishingController.cs | 6 +- Assets/Scripts/InventorySlot.cs | 4 +- Assets/Scripts/PlayerController.cs | 2 +- Assets/Scripts/TileBehaviour.cs | 44 +++++---- Assets/Scripts/Tiles/BaseTile.cs | 10 +- Assets/Scripts/Tiles/FarmlandTile.cs | 29 +----- Assets/Scripts/Tiles/GrassTile.cs | 22 +---- Assets/Scripts/Tiles/WaterTile.cs | 22 +---- 11 files changed, 189 insertions(+), 214 deletions(-) create mode 100644 Assets/Scripts/ActionInvoker.cs create mode 100644 Assets/Scripts/ActionInvoker.cs.meta diff --git a/Assets/Scripts/ActionInvoker.cs b/Assets/Scripts/ActionInvoker.cs new file mode 100644 index 0000000..ec2cdc7 --- /dev/null +++ b/Assets/Scripts/ActionInvoker.cs @@ -0,0 +1,123 @@ +using System; +using Tiles; +using UnityEngine; +using UnityEngine.PlayerLoop; +using Random = UnityEngine.Random; + +namespace DefaultNamespace { + public class ActionInvoker { + public static void InvokeAction(GameObject gameObject, UsableItem usableItem) { + Type tileType = gameObject.GetComponent().Tile.GetType(); + ItemContainer ic = ItemContainer.Instance; + + if(tileType == typeof(GrassTile)) { + if(usableItem.ID == ic.GetItemIdByName("Hoe")) { + SetTile(gameObject, new FarmlandTile()); + } else if(usableItem.ID == ic.GetItemIdByName("Shovel")) { + SetTile(gameObject, new WaterTile()); + } + } else if(tileType == typeof(FarmlandTile)) { + if(usableItem.ID == ic.GetItemIdByName("Shovel")) { + SetTile(gameObject, new GrassTile()); + } else if(usableItem.ID == ic.GetItemIdByName("Scythe")) { + HarvestIfPossible(gameObject); + } else if(usableItem.ID == ic.GetItemIdByName("Watering Can")) { + Hydrate(gameObject); + } else if(usableItem.ID == ic.GetItemIdByName("Wheat Seeds")) { + PlantIfPossible(gameObject); + } + } else if(tileType == typeof(WaterTile)) { + if(usableItem.ID == ic.GetItemIdByName("Shovel")) { + SetTile(gameObject, new GrassTile()); + } else if(usableItem.ID == ic.GetItemIdByName("Fishing Rod")) { + Fish(); + } + } + } + + private static void Fish() { + ItemContainer ic = ItemContainer.Instance; + + FishingController fc = FishingController.instance; + if(!fc.Fishing) { + fc.StartFishing(); + } else { + fc.TryCatch(); + } + } + + public static void InvokeDayLightStep(GameObject gameObject) { + Type tileType = gameObject.GetComponent().Tile.GetType(); + + if(tileType == typeof(FarmlandTile)) { + GrowIfHydrated(gameObject); + } + } + + private static void GrowIfHydrated(GameObject gameObject) { + Crop crop = ((FarmlandTile)gameObject.GetComponent().Tile).Crop; + + if(crop.Planted && crop.Hydrated) { + crop.Grow(); + } + UpdateFarmlandSprites(gameObject); + } + + private static void PlantIfPossible(GameObject gameObject) { + Crop crop = ((FarmlandTile)gameObject.GetComponent().Tile).Crop; + if(!crop.Planted) { + crop.Plant(); + Inventory.instance.RemoveItem(ItemContainer.Instance.GetItemByName("Wheat Seeds"), 1); + } + UpdateFarmlandSprites(gameObject); + } + + private static void Hydrate(GameObject gameObject) { + Crop crop = ((FarmlandTile)gameObject.GetComponent().Tile).Crop; + crop.Hydrated = true; + UpdateFarmlandSprites(gameObject); + } + + private static void UpdateFarmlandSprites(GameObject gameObject) { + Crop crop = ((FarmlandTile)gameObject.GetComponent().Tile).Crop; + + SpriteRenderer hydrationSR = gameObject.transform.GetChild(0).GetComponent(); + SpriteRenderer cropSR = gameObject.transform.GetChild(1).GetComponent(); + + if(crop.Planted) { + if(crop.FullyGrown) { + //Debug.Log("sprite fg"); + cropSR.sprite = Crop.FullyGrownCrop; + } else { + //Debug.Log("sprite smallCrop"); + cropSR.sprite = Crop.SmallCrop; + } + } else { + cropSR.sprite = null; + } + + if(crop.Hydrated) { + //Debug.Log("sprite hydrated"); + hydrationSR.color = Crop.HydratedColor; + } else { + //Debug.Log("sprite no hydrated"); + hydrationSR.color = Color.clear; + } + } + + + private static void HarvestIfPossible(GameObject gameObject) { + Crop crop = ((FarmlandTile)gameObject.GetComponent().Tile).Crop; + if(crop.FullyGrown) { + Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Wheat Seeds"), (int)(Random.Range(1, 300))); + Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Wheat"), 1); + crop.ResetPlant(); + UpdateFarmlandSprites(gameObject); + } + } + + private static void SetTile(GameObject gameObject, BaseTile tileType) { + gameObject.GetComponent().Tile = tileType; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActionInvoker.cs.meta b/Assets/Scripts/ActionInvoker.cs.meta new file mode 100644 index 0000000..912bd71 --- /dev/null +++ b/Assets/Scripts/ActionInvoker.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7fc4f22ade5a482eba0e34c4f96fd1d8 +timeCreated: 1654777450 \ No newline at end of file diff --git a/Assets/Scripts/Crop.cs b/Assets/Scripts/Crop.cs index e2ccaa9..72af0e4 100644 --- a/Assets/Scripts/Crop.cs +++ b/Assets/Scripts/Crop.cs @@ -1,139 +1,51 @@ -using System.Security.Cryptography; +using System; +using System.Security.Cryptography; using Tiles; using UnityEngine; -using static CropAction; +using Random = UnityEngine.Random; -enum CropAction { - NextDay, - Hoe, - Scythe, - WateringCan, - Seeds -} - public class Crop { private const int FinalGrowthStage = 4; - private static Sprite _smallCrop; - private static Sprite _fullyGrownCrop; + public static Sprite SmallCrop = BaseTile.GenerateSpriteFromFile("Assets/Farming Asset Pack/Split Assets/farming_tileset_026.png"); + public static Sprite FullyGrownCrop = BaseTile.GenerateSpriteFromFile("Assets/Farming Asset Pack/Split Assets/farming_tileset_039.png"); - private static Color _hydratedColor; + public static Color HydratedColor = new Color(0.5f, 0.5f, 1.0f, 0.269420f); private bool _planted; - - private bool _fullyGrown => (_growthStage >= FinalGrowthStage); - - private bool _hydrated; + public bool Planted => _planted; private int _growthStage; + public bool FullyGrown => (_growthStage >= FinalGrowthStage); - private SpriteRenderer _cropSpriteRenderer; - private SpriteRenderer _hydrationSpriteRenderer; - - public Crop(SpriteRenderer cropSpriteRenderer, SpriteRenderer hydrationSpriteRenderer) { - _planted = false; + private bool _hydrated; + public bool Hydrated + { + get => _hydrated; + set => _hydrated = value; + } + + + public Crop() { + ResetPlant(); _hydrated = false; - - _cropSpriteRenderer = cropSpriteRenderer; - _hydrationSpriteRenderer = hydrationSpriteRenderer; - - _smallCrop = BaseTile.GenerateSpriteFromFile("Assets/Farming Asset Pack/Split Assets/farming_tileset_026.png"); - _fullyGrownCrop = BaseTile.GenerateSpriteFromFile("Assets/Farming Asset Pack/Split Assets/farming_tileset_039.png"); - - _hydratedColor = new Color(0.5f, 0.5f, 1.0f, 0.269420f); - - UpdateSprite(); } - private void Grow() { + public void Plant() { + _planted = true; + } + + public void Grow() { _growthStage++; + Hydrated = false; } - public void DayLightStep() { - ApplyAction(NextDay); - } - - public void Clicked(UsableItem usableItem) { - if(usableItem != null) { - 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) { - if(_planted) { - if(Hoe == action) { - _planted = false; - } else if(Scythe == action) { - if(_fullyGrown) { - Harvest(); - } - } - } else if(!_planted) { - if(Seeds == action) { - _planted = true; - Inventory.instance.RemoveItem(ItemContainer.Instance.GetItemByName("Wheat Seeds"), 1); - } - } - - if(_hydrated) { - if(NextDay == action) { - _hydrated = false; - if(_planted) { - Grow(); - } - } - } else if(!_hydrated) { - if(WateringCan == action) { - _hydrated = true; - } - } - - UpdateSprite(); - } - - private void Harvest() { - AddCropToInventory(); + public void ResetPlant() { _planted = false; _growthStage = 0; } - private void AddCropToInventory() { - Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Wheat Seeds"), (int)(Random.Range(1,300))); - Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Wheat"), 1); - } - - private void UpdateSprite() { - 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" + diff --git a/Assets/Scripts/FishingController.cs b/Assets/Scripts/FishingController.cs index d4269f4..3ae8fc5 100644 --- a/Assets/Scripts/FishingController.cs +++ b/Assets/Scripts/FishingController.cs @@ -97,13 +97,13 @@ public class FishingController : MonoBehaviour { public void TryCatch() { if (_fishing && _catchable) { - Debug.Log("Tried to catch!"); + //Debug.Log("Tried to catch!"); if (_fishingTime <= _maxTime) { - Debug.Log("Caught!"); + //Debug.Log("Caught!"); Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Fish"), Math.Max((int)(1 / (_fishingTime/2)), 1)); ResetFishing(); } else { - Debug.Log("Failed to catch!"); + //Debug.Log("Failed to catch!"); _catchable = false; _fishingTime = 0f; _exMark.SetActive(false); diff --git a/Assets/Scripts/InventorySlot.cs b/Assets/Scripts/InventorySlot.cs index 1a6b02d..1170de6 100644 --- a/Assets/Scripts/InventorySlot.cs +++ b/Assets/Scripts/InventorySlot.cs @@ -20,9 +20,9 @@ public class InventorySlot : ItemStorageSlot, IPointerClickHandler { if(Item) { if(Item.GetType() == typeof(UsableItem)) { ((UsableItem)Item).Select(); - Debug.Log("using " + Item.displayName); + //Debug.Log("using " + Item.displayName); } else { - Debug.Log("Item not usable " + Item.displayName); + //Debug.Log("Item not usable " + Item.displayName); } } } diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 48a8ab7..94f91c2 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -47,7 +47,7 @@ public class PlayerController : MonoBehaviour { _selectedItem = item; Cursor.SetCursor(item.defaultSprite.texture, Vector2.zero, CursorMode.Auto); } else { - Debug.Log("An item requested to select isn't in the inventory" + item); + //Debug.Log("An item requested to select isn't in the inventory" + item); } } diff --git a/Assets/Scripts/TileBehaviour.cs b/Assets/Scripts/TileBehaviour.cs index acda159..cb3bbe3 100644 --- a/Assets/Scripts/TileBehaviour.cs +++ b/Assets/Scripts/TileBehaviour.cs @@ -1,10 +1,20 @@ using System; +using DefaultNamespace; using Tiles; using UnityEngine; public class TileBehaviour : MonoBehaviour { private BaseTile _tile; + public BaseTile Tile { + get => _tile; + set + { + _tile = value; + GetComponent().sprite = _tile.Sprite; + } + } + private SpriteRenderer _hoverIndicatorSpriteRenderer; private static Color _hoverIndicatorColor; @@ -13,9 +23,15 @@ public class TileBehaviour : MonoBehaviour { _hoverIndicatorSpriteRenderer = gameObject.transform.GetChild(2).GetComponent(); SetHoverIndicatorVisibility(false); - SetTile(new GrassTile(gameObject)); + Tile = new GrassTile(); gameObject.transform.GetChild(0).GetComponent().color = Color.clear; _hoverIndicatorColor = new Color(1, 1, 1, 0.3f); + + HouseController.NewDayEvent.AddListener(DayLightStep); + } + + private void DayLightStep() { + ActionInvoker.InvokeDayLightStep(gameObject); } // Update is called once per frame @@ -24,28 +40,14 @@ public class TileBehaviour : MonoBehaviour } - void OnMouseDown() - { - UsableItem usable = null; - BaseTile tileToSetTo = null; - - if (PlayerController.instance.GetSelectedItem() != null) + void OnMouseDown() { + UsableItem usableItem = PlayerController.instance.GetSelectedItem(); + if (usableItem != null) { - usable = PlayerController.instance.GetSelectedItem(); + ActionInvoker.InvokeAction(gameObject, usableItem); } - - tileToSetTo = _tile.Clicked(usable); - - if (tileToSetTo != null) - { - SetTile(tileToSetTo); - } - } - - void SetTile(BaseTile tileToSet) - { - _tile = tileToSet; - GetComponent().sprite = _tile.Sprite; + + } private void OnMouseEnter() diff --git a/Assets/Scripts/Tiles/BaseTile.cs b/Assets/Scripts/Tiles/BaseTile.cs index 451115f..7e7742c 100644 --- a/Assets/Scripts/Tiles/BaseTile.cs +++ b/Assets/Scripts/Tiles/BaseTile.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using DefaultNamespace; using UnityEngine; namespace Tiles { @@ -7,20 +8,13 @@ namespace Tiles { private Sprite _sprite; public Sprite Sprite => _sprite; - protected GameObject _gameObject; - - protected BaseTile(String pathToImageFile, GameObject gameObject) { - _gameObject = gameObject; + protected BaseTile(String pathToImageFile) { _sprite = GenerateSpriteFromFile(pathToImageFile); HouseController.NewDayEvent.AddListener(DayLightStep); } public virtual void DayLightStep() { } - public virtual BaseTile Clicked(UsableItem usable) { - return null; - } - 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 4028eb9..940a949 100644 --- a/Assets/Scripts/Tiles/FarmlandTile.cs +++ b/Assets/Scripts/Tiles/FarmlandTile.cs @@ -3,30 +3,11 @@ namespace Tiles { public class FarmlandTile : BaseTile { private Crop _crop; - - public FarmlandTile(GameObject gameObject) : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_008.png", - gameObject) { - _crop = new Crop(gameObject.transform.GetChild(1).GetComponent(), - gameObject.transform.GetChild(0).GetComponent()); - } - - public override void DayLightStep() { - _crop.DayLightStep(); - } - - public override BaseTile Clicked(UsableItem usable) { - BaseTile rv = null; - if (usable != null) { - base.Clicked(usable); - _crop.Clicked(usable); - - if (ItemContainer.Instance.GetItemIdByName("Shovel") == usable.ID) { - rv = new GrassTile(_gameObject); - } - - - } - return rv; + public Crop Crop => _crop; + + public FarmlandTile() : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_008.png") { + _crop = new Crop(); } + } } \ No newline at end of file diff --git a/Assets/Scripts/Tiles/GrassTile.cs b/Assets/Scripts/Tiles/GrassTile.cs index cfcbbb5..21d8f60 100644 --- a/Assets/Scripts/Tiles/GrassTile.cs +++ b/Assets/Scripts/Tiles/GrassTile.cs @@ -4,29 +4,9 @@ namespace Tiles { public class GrassTile : BaseTile { - public GrassTile(GameObject gameObject) : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_000.png", gameObject) + public GrassTile() : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_000.png") { } - - /// - /// to be invoked when the Tile is clicked, handles the actions following on the click - /// - /// the UsableItem that the Tile was clicked on with - /// a subclass of BaseTile if the Tile has to change, null if it stays the same type - public override BaseTile Clicked(UsableItem usable) { - BaseTile rv = null; - ItemContainer ic = ItemContainer.Instance; - if (usable != null) - { - base.Clicked(usable); - if (usable.ID == ic.GetItemIdByName("Hoe")) { - rv = new FarmlandTile(_gameObject); - } else if (usable.ID == ic.GetItemIdByName("Shovel")) { - rv = new WaterTile(_gameObject); - } - } - return rv; - } } } \ No newline at end of file diff --git a/Assets/Scripts/Tiles/WaterTile.cs b/Assets/Scripts/Tiles/WaterTile.cs index 35682e3..0e87ceb 100644 --- a/Assets/Scripts/Tiles/WaterTile.cs +++ b/Assets/Scripts/Tiles/WaterTile.cs @@ -3,27 +3,7 @@ using UnityEngine; namespace Tiles { public class WaterTile : BaseTile { - public WaterTile(GameObject gameObject) : base("Assets/Farming Asset Pack/Split Assets/water_sprite_00.png", gameObject) { - } - - public override BaseTile Clicked(UsableItem usable) { - base.Clicked(usable); - BaseTile rv = null; - - ItemContainer ic = ItemContainer.Instance; - - if (usable.ID == ic.GetItemIdByName("Fishing Rod")) { - FishingController fc = FishingController.instance; - if (!fc.Fishing) { - fc.StartFishing(); - } else { - fc.TryCatch(); - } - } else if (usable.ID == ic.GetItemIdByName("Shovel")) { - rv = new GrassTile(_gameObject); - } - - return rv; + public WaterTile() : base("Assets/Farming Asset Pack/Split Assets/water_sprite_00.png") { } } } \ No newline at end of file