From 1f7a3a4595c323543964f600f09534b35ba564fa Mon Sep 17 00:00:00 2001 From: j-weissen Date: Thu, 23 Jun 2022 14:39:00 +0200 Subject: [PATCH 1/4] Only one action can be invoked --- Assets/Scripts/Actions/ActionManager.cs | 2 ++ Assets/Scripts/TileBehaviour.cs | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/Actions/ActionManager.cs b/Assets/Scripts/Actions/ActionManager.cs index 16ba036..f6ee005 100644 --- a/Assets/Scripts/Actions/ActionManager.cs +++ b/Assets/Scripts/Actions/ActionManager.cs @@ -49,6 +49,7 @@ namespace Actions { foreach (ClickActionHandler actionHandler in _clickActionHandlers) { if(actionHandler.Matches(gameObject, usableItem)) { actionHandler.InvokeAction(gameObject); + break; // Ja Herr Professor, Sie sehen richtig. Voller Stolz verwende ich ein break. } } } @@ -58,6 +59,7 @@ namespace Actions { foreach (NextDayActionHandler actionHandler in _nextDayActionHandlers) { if(actionHandler.Matches(gameObject)) { actionHandler.InvokeAction(gameObject); + break; // Gleich noch einmal. Und ich kann nachts immer noch zufrieden schlafen. } } } diff --git a/Assets/Scripts/TileBehaviour.cs b/Assets/Scripts/TileBehaviour.cs index c64ee25..d9c2a05 100644 --- a/Assets/Scripts/TileBehaviour.cs +++ b/Assets/Scripts/TileBehaviour.cs @@ -38,7 +38,6 @@ public class TileBehaviour : MonoBehaviour { } private void NextDay() { - Debug.Log("nextday"); ActionManager.Instance.NextDayAction(gameObject); } From f5e4c8b789128bcc48fcd2e9b340e6728df80cf0 Mon Sep 17 00:00:00 2001 From: j-weissen Date: Thu, 23 Jun 2022 14:41:14 +0200 Subject: [PATCH 2/4] Tiles now in 2D list --- Assets/Scripts/TileController.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/TileController.cs b/Assets/Scripts/TileController.cs index 18dfb7b..c8b55c0 100644 --- a/Assets/Scripts/TileController.cs +++ b/Assets/Scripts/TileController.cs @@ -1,26 +1,30 @@ using System; using System.Collections.Generic; using UnityEngine; +using UnityEngine.Tilemaps; public class TileController : MonoBehaviour { public GameObject tile; public GameObject cameraGameObject; - public List Tiles; + public List> Tiles; // Start is called before the first frame update void Start() { Camera camera = cameraGameObject.GetComponent(); + Tiles = new List>(); Vector3 screen = camera.ViewportToWorldPoint(new Vector3(1, 1, camera.nearClipPlane)); int x = Convert.ToInt32(Math.Ceiling(screen.x)); int y = Convert.ToInt32(Math.Ceiling(screen.y)); for(int xx = -x; xx <= x; xx++) { + List temp = new List(); for(int yy = -y; yy <= y; yy++) { if(tile != null) { - Tiles.Add(Instantiate(tile, new Vector3(xx, yy, 0), Quaternion.identity)); + temp.Add(Instantiate(tile, new Vector3(xx, yy, 0), Quaternion.identity)); } } + Tiles.Add(temp); } } } \ No newline at end of file From 9151d987a7fc743585ada2a8975d43dbb7e6370e Mon Sep 17 00:00:00 2001 From: j-weissen Date: Thu, 23 Jun 2022 14:56:39 +0200 Subject: [PATCH 3/4] Grass Sprites now randomized --- Assets/Scripts/TileBehaviour.cs | 3 +-- Assets/Scripts/TileController.cs | 2 +- Assets/Scripts/Tiles/BaseTile.cs | 4 ++-- Assets/Scripts/Tiles/GrassTile.cs | 20 ++++++++++++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Assets/Scripts/TileBehaviour.cs b/Assets/Scripts/TileBehaviour.cs index d9c2a05..92736a0 100644 --- a/Assets/Scripts/TileBehaviour.cs +++ b/Assets/Scripts/TileBehaviour.cs @@ -6,8 +6,7 @@ using UnityEngine; public class TileBehaviour : MonoBehaviour { private BaseTile _tile; - - public BaseTile Tile { + public virtual BaseTile Tile { get => _tile; set { _tile = value; diff --git a/Assets/Scripts/TileController.cs b/Assets/Scripts/TileController.cs index c8b55c0..20e87ae 100644 --- a/Assets/Scripts/TileController.cs +++ b/Assets/Scripts/TileController.cs @@ -12,8 +12,8 @@ public class TileController : MonoBehaviour { // Start is called before the first frame update void Start() { - Camera camera = cameraGameObject.GetComponent(); Tiles = new List>(); + Camera camera = cameraGameObject.GetComponent(); Vector3 screen = camera.ViewportToWorldPoint(new Vector3(1, 1, camera.nearClipPlane)); int x = Convert.ToInt32(Math.Ceiling(screen.x)); int y = Convert.ToInt32(Math.Ceiling(screen.y)); diff --git a/Assets/Scripts/Tiles/BaseTile.cs b/Assets/Scripts/Tiles/BaseTile.cs index b62c9a5..4d5b667 100644 --- a/Assets/Scripts/Tiles/BaseTile.cs +++ b/Assets/Scripts/Tiles/BaseTile.cs @@ -4,8 +4,8 @@ using UnityEngine; namespace Tiles { public abstract class BaseTile { - private Sprite _sprite; - public Sprite Sprite => _sprite; + protected Sprite _sprite; + public virtual Sprite Sprite => _sprite; protected BaseTile(String pathToImageFile) { _sprite = GenerateSpriteFromFile(pathToImageFile); diff --git a/Assets/Scripts/Tiles/GrassTile.cs b/Assets/Scripts/Tiles/GrassTile.cs index 21d8f60..6c6861d 100644 --- a/Assets/Scripts/Tiles/GrassTile.cs +++ b/Assets/Scripts/Tiles/GrassTile.cs @@ -2,11 +2,23 @@ namespace Tiles { - public class GrassTile : BaseTile - { - public GrassTile() : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_000.png") + public class GrassTile : BaseTile { + private Sprite[] _sprites; + public override Sprite Sprite { - + get + { + int rand = Random.Range(0, _sprites.Length); + return _sprites[rand]; + } + } + public GrassTile() : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_000.png") { + _sprites = new[] + { + GenerateSpriteFromFile("Assets/Farming Asset Pack/Split Assets/farming_tileset_000.png"), + GenerateSpriteFromFile("Assets/Farming Asset Pack/Split Assets/farming_tileset_001.png"), + GenerateSpriteFromFile("Assets/Farming Asset Pack/Split Assets/farming_tileset_002.png") + }; } } } \ No newline at end of file From 5f1a5e5abc53039d47ee4bdbd61c08ca7b42b2ee Mon Sep 17 00:00:00 2001 From: j-weissen Date: Fri, 24 Jun 2022 08:05:57 +0200 Subject: [PATCH 4/4] Added Comments --- Assets/Scripts/Actions/ActionManager.cs | 23 +++++++++++++++++++ Assets/Scripts/Actions/ClickActionHandler.cs | 3 +++ Assets/Scripts/Actions/ClickActionHandlers.cs | 3 ++- .../Scripts/Actions/NextDayActionHandler.cs | 3 +++ .../Scripts/Actions/NextDayActionHandlers.cs | 1 + Assets/Scripts/Crop.cs | 3 +++ Assets/Scripts/TileBehaviour.cs | 18 ++++++++++++++- Assets/Scripts/TileController.cs | 3 +++ Assets/Scripts/Tiles/BaseTile.cs | 9 ++++++++ Assets/Scripts/Tiles/GrassTile.cs | 3 +++ 10 files changed, 67 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Actions/ActionManager.cs b/Assets/Scripts/Actions/ActionManager.cs index f6ee005..41202b8 100644 --- a/Assets/Scripts/Actions/ActionManager.cs +++ b/Assets/Scripts/Actions/ActionManager.cs @@ -3,6 +3,10 @@ using Assets.Scripts.Actions; using UnityEngine; namespace Actions { + /// + /// AcrionManagaer managing Actions. + /// ActionHandler implement either NextDayActionHandler or ClickActionHandler and have to be added to the matching list in the instatiationmethod + /// public class ActionManager { #region Singleton private static ActionManager _instance; @@ -27,10 +31,17 @@ namespace Actions { instantiateNextDayActionHandlers(); } + /// + /// NextDayActionHandlers to be instatiated and added to the corresponding List + /// private void instantiateNextDayActionHandlers() { _nextDayActionHandlers.Add(new FarmlandTileNextDayActionHandler()); } + + /// + /// ClickActionHandlers to be instatiated and added to the corresponding List + /// private void instantiateClickActionHandlers() { _clickActionHandlers.Add(new GrassTileClickHoeActionHandler()); _clickActionHandlers.Add(new GrassTileClickShovelActionHandler()); @@ -45,6 +56,12 @@ namespace Actions { _clickActionHandlers.Add(new WaterTileClickFishingRodActionHandler()); } + /// + /// Used to Invoke ClickActions, all ClickActionHandlers in ClickActionHandlers list are iterated through, + /// only one will be invoked per method call + /// + /// The affected gameObject + /// the current tool public void ClickAction(GameObject gameObject, UsableItem usableItem) { foreach (ClickActionHandler actionHandler in _clickActionHandlers) { if(actionHandler.Matches(gameObject, usableItem)) { @@ -54,6 +71,12 @@ namespace Actions { } } + /// + /// Used to Invoke ClickActions, all ClickActionHandlers in ClickActionHandlers list are iterated through, + /// only one will be invoked per method call + /// + /// The affected gameObject + /// the current tool public void NextDayAction(GameObject gameObject) { Debug.Log("nextday action"); foreach (NextDayActionHandler actionHandler in _nextDayActionHandlers) { diff --git a/Assets/Scripts/Actions/ClickActionHandler.cs b/Assets/Scripts/Actions/ClickActionHandler.cs index 119fa9c..faeb655 100644 --- a/Assets/Scripts/Actions/ClickActionHandler.cs +++ b/Assets/Scripts/Actions/ClickActionHandler.cs @@ -3,6 +3,9 @@ using System.Transactions; using UnityEngine; namespace Actions { + /// + /// Implementing classes handle ClickActions + /// public interface ClickActionHandler { public void InvokeAction(GameObject gameObject); public bool Matches(GameObject gameObject, UsableItem usableItem); diff --git a/Assets/Scripts/Actions/ClickActionHandlers.cs b/Assets/Scripts/Actions/ClickActionHandlers.cs index f6094ea..2e9a80b 100644 --- a/Assets/Scripts/Actions/ClickActionHandlers.cs +++ b/Assets/Scripts/Actions/ClickActionHandlers.cs @@ -2,6 +2,7 @@ using Tiles; using UnityEngine; +/// Definitions of ClickActionHandlers here namespace Assets.Scripts.Actions { public abstract class AbstractTileClickActionHandler : ClickActionHandler { protected BaseTile _tile; @@ -183,7 +184,7 @@ namespace Assets.Scripts.Actions { public override void InvokeAction(GameObject gameObject) { if(crop.FullyGrown) { Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat Seeds"), - (int)(Random.Range(1, 300))); + (int)(Random.Range(1, 3))); Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat"), 1); crop.ResetPlant(); updateFarmlandSprites(gameObject); diff --git a/Assets/Scripts/Actions/NextDayActionHandler.cs b/Assets/Scripts/Actions/NextDayActionHandler.cs index 7e54539..a7e0d17 100644 --- a/Assets/Scripts/Actions/NextDayActionHandler.cs +++ b/Assets/Scripts/Actions/NextDayActionHandler.cs @@ -1,6 +1,9 @@ using UnityEngine; namespace Actions { + /// + /// Implementing classes handle nextDayAction + /// public interface NextDayActionHandler { public void InvokeAction(GameObject gameObject); public bool Matches(GameObject gameObject); diff --git a/Assets/Scripts/Actions/NextDayActionHandlers.cs b/Assets/Scripts/Actions/NextDayActionHandlers.cs index a2fa4ba..152778c 100644 --- a/Assets/Scripts/Actions/NextDayActionHandlers.cs +++ b/Assets/Scripts/Actions/NextDayActionHandlers.cs @@ -2,6 +2,7 @@ using Tiles; using UnityEngine; +/// Definitions of NextDayActionHandlers here namespace Actions { public abstract class AbstractTileNextDayActionHandler : NextDayActionHandler { protected BaseTile _tile; diff --git a/Assets/Scripts/Crop.cs b/Assets/Scripts/Crop.cs index c83387f..609a812 100644 --- a/Assets/Scripts/Crop.cs +++ b/Assets/Scripts/Crop.cs @@ -5,6 +5,9 @@ using UnityEngine; using Random = UnityEngine.Random; +/// +/// Controlling logic of crop on FarmlandTiles +/// public class Crop { private const int FinalGrowthStage = 4; diff --git a/Assets/Scripts/TileBehaviour.cs b/Assets/Scripts/TileBehaviour.cs index 92736a0..0e52b50 100644 --- a/Assets/Scripts/TileBehaviour.cs +++ b/Assets/Scripts/TileBehaviour.cs @@ -4,6 +4,9 @@ using DefaultNamespace; using Tiles; using UnityEngine; +/// +/// Defining the behaviour of tiles +/// public class TileBehaviour : MonoBehaviour { private BaseTile _tile; public virtual BaseTile Tile { @@ -17,7 +20,7 @@ public class TileBehaviour : MonoBehaviour { private SpriteRenderer _hoverIndicatorSpriteRenderer; private static Color _hoverIndicatorColor; - // Start is called before the first frame update + /// Start is called before the first frame update void Start() { Tile = new GrassTile(); @@ -36,10 +39,16 @@ public class TileBehaviour : MonoBehaviour { HouseController.NewDayEvent.AddListener(NextDay); } + /// + /// is called when a NewDayEvent is dispersed + /// private void NextDay() { ActionManager.Instance.NextDayAction(gameObject); } + /// + /// Used to invoke Click Actions + /// void OnMouseDown() { UsableItem usableItem = PlayerController.instance.SelectedItem; if(usableItem != null) { @@ -47,6 +56,9 @@ public class TileBehaviour : MonoBehaviour { } } + /// + /// used to set hover indicator + /// private void OnMouseEnter() { SetHoverIndicatorVisibility(true); } @@ -55,6 +67,10 @@ public class TileBehaviour : MonoBehaviour { SetHoverIndicatorVisibility(false); } + /// + /// Sets the visibility of hover indicator + /// + /// private void SetHoverIndicatorVisibility(bool visible) { if(visible) { _hoverIndicatorSpriteRenderer.color = _hoverIndicatorColor; diff --git a/Assets/Scripts/TileController.cs b/Assets/Scripts/TileController.cs index 20e87ae..8a409fb 100644 --- a/Assets/Scripts/TileController.cs +++ b/Assets/Scripts/TileController.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps; +/// +/// Instatiates Tiles at beginning of the game +/// public class TileController : MonoBehaviour { public GameObject tile; diff --git a/Assets/Scripts/Tiles/BaseTile.cs b/Assets/Scripts/Tiles/BaseTile.cs index 4d5b667..27722de 100644 --- a/Assets/Scripts/Tiles/BaseTile.cs +++ b/Assets/Scripts/Tiles/BaseTile.cs @@ -2,7 +2,11 @@ using System; using System.IO; using UnityEngine; + namespace Tiles { + /// + /// Base class of all Tile types + /// public abstract class BaseTile { protected Sprite _sprite; public virtual Sprite Sprite => _sprite; @@ -14,6 +18,11 @@ namespace Tiles { public virtual void DayLightStep() { } + /// + /// Used to generate Sprites from files + /// + /// Path to the Image file to be Converted to the Sprite + /// the created Sprite 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/GrassTile.cs b/Assets/Scripts/Tiles/GrassTile.cs index 6c6861d..dae99e5 100644 --- a/Assets/Scripts/Tiles/GrassTile.cs +++ b/Assets/Scripts/Tiles/GrassTile.cs @@ -4,6 +4,9 @@ namespace Tiles { public class GrassTile : BaseTile { private Sprite[] _sprites; + /// + /// Overridden Property to randomize tiles between the 3 different sprites + /// public override Sprite Sprite { get