Merge branch 'jweissen' into develop

# Conflicts:
#	Assets/Scripts/Actions/ActionManager.cs
This commit is contained in:
j-weissen 2022-06-24 08:07:22 +02:00
commit d3a06effc5
10 changed files with 98 additions and 19 deletions

View file

@ -3,6 +3,10 @@ using Assets.Scripts.Actions;
using UnityEngine; using UnityEngine;
namespace Actions { namespace Actions {
/// <summary>
/// AcrionManagaer managing Actions.
/// ActionHandler implement either NextDayActionHandler or ClickActionHandler and have to be added to the matching list in the instatiationmethod
/// </summary>
public class ActionManager { public class ActionManager {
#region Singleton #region Singleton
private static ActionManager _instance; private static ActionManager _instance;
@ -23,17 +27,22 @@ namespace Actions {
private ActionManager() { private ActionManager() {
_clickActionHandlers = new List<ClickActionHandler>(); _clickActionHandlers = new List<ClickActionHandler>();
_nextDayActionHandlers = new List<NextDayActionHandler>(); _nextDayActionHandlers = new List<NextDayActionHandler>();
InstantiateClickActionHandlers(); instantiateClickActionHandlers();
InstantiateNextDayActionHandlers(); instantiateNextDayActionHandlers();
} }
private void InstantiateNextDayActionHandlers() { /// <summary>
/// NextDayActionHandlers to be instatiated and added to the corresponding List
/// </summary>
private void instantiateNextDayActionHandlers() {
_nextDayActionHandlers.Add(new FarmlandTileNextDayActionHandler()); _nextDayActionHandlers.Add(new FarmlandTileNextDayActionHandler());
_nextDayActionHandlers.Add(new ChickenAnimalNextDayActionHandler());
} }
private void InstantiateClickActionHandlers() {
/// <summary>
/// ClickActionHandlers to be instatiated and added to the corresponding List
/// </summary>
private void instantiateClickActionHandlers() {
_clickActionHandlers.Add(new GrassTileClickHoeActionHandler()); _clickActionHandlers.Add(new GrassTileClickHoeActionHandler());
_clickActionHandlers.Add(new GrassTileClickShovelActionHandler()); _clickActionHandlers.Add(new GrassTileClickShovelActionHandler());
_clickActionHandlers.Add(new GrassTileClickFenceActionHandler()); _clickActionHandlers.Add(new GrassTileClickFenceActionHandler());
@ -49,19 +58,33 @@ namespace Actions {
_clickActionHandlers.Add(new CowAnimalClickActionHandler()); _clickActionHandlers.Add(new CowAnimalClickActionHandler());
} }
/// <summary>
/// Used to Invoke ClickActions, all ClickActionHandlers in ClickActionHandlers list are iterated through,
/// only one will be invoked per method call
/// </summary>
/// <param name="gameObject">The affected gameObject</param>
/// <param name="usableItem">the current tool</param>
public void ClickAction(GameObject gameObject, UsableItem usableItem) { public void ClickAction(GameObject gameObject, UsableItem usableItem) {
foreach (ClickActionHandler actionHandler in _clickActionHandlers) { foreach (ClickActionHandler actionHandler in _clickActionHandlers) {
if(actionHandler.Matches(gameObject, usableItem)) { if(actionHandler.Matches(gameObject, usableItem)) {
actionHandler.InvokeAction(gameObject); actionHandler.InvokeAction(gameObject);
break; // Ja Herr Professor, Sie sehen richtig. Voller Stolz verwende ich ein break.
} }
} }
} }
/// <summary>
/// Used to Invoke ClickActions, all ClickActionHandlers in ClickActionHandlers list are iterated through,
/// only one will be invoked per method call
/// </summary>
/// <param name="gameObject">The affected gameObject</param>
/// <param name="usableItem">the current tool</param>
public void NextDayAction(GameObject gameObject) { public void NextDayAction(GameObject gameObject) {
Debug.Log("nextday action"); Debug.Log("nextday action");
foreach (NextDayActionHandler actionHandler in _nextDayActionHandlers) { foreach (NextDayActionHandler actionHandler in _nextDayActionHandlers) {
if(actionHandler.Matches(gameObject)) { if(actionHandler.Matches(gameObject)) {
actionHandler.InvokeAction(gameObject); actionHandler.InvokeAction(gameObject);
break; // Gleich noch einmal. Und ich kann nachts immer noch zufrieden schlafen.
} }
} }
} }

View file

@ -3,6 +3,9 @@ using System.Transactions;
using UnityEngine; using UnityEngine;
namespace Actions { namespace Actions {
/// <summary>
/// Implementing classes handle ClickActions
/// </summary>
public interface ClickActionHandler { public interface ClickActionHandler {
public void InvokeAction(GameObject gameObject); public void InvokeAction(GameObject gameObject);
public bool Matches(GameObject gameObject, UsableItem usableItem); public bool Matches(GameObject gameObject, UsableItem usableItem);

View file

@ -2,6 +2,7 @@
using Tiles; using Tiles;
using UnityEngine; using UnityEngine;
/// Definitions of ClickActionHandlers here
namespace Assets.Scripts.Actions { namespace Assets.Scripts.Actions {
public abstract class AbstractTileClickActionHandler : ClickActionHandler { public abstract class AbstractTileClickActionHandler : ClickActionHandler {
protected BaseTile _tile; protected BaseTile _tile;
@ -185,7 +186,7 @@ namespace Assets.Scripts.Actions {
public override void InvokeAction(GameObject gameObject) { public override void InvokeAction(GameObject gameObject) {
if(crop.FullyGrown) { if(crop.FullyGrown) {
Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat Seeds"), 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); Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat"), 1);
crop.ResetPlant(); crop.ResetPlant();
updateFarmlandSprites(gameObject); updateFarmlandSprites(gameObject);

View file

@ -1,6 +1,9 @@
using UnityEngine; using UnityEngine;
namespace Actions { namespace Actions {
/// <summary>
/// Implementing classes handle nextDayAction
/// </summary>
public interface NextDayActionHandler { public interface NextDayActionHandler {
public void InvokeAction(GameObject gameObject); public void InvokeAction(GameObject gameObject);
public bool Matches(GameObject gameObject); public bool Matches(GameObject gameObject);

View file

@ -2,6 +2,7 @@
using Tiles; using Tiles;
using UnityEngine; using UnityEngine;
/// Definitions of NextDayActionHandlers here
namespace Actions { namespace Actions {
public abstract class AbstractTileNextDayActionHandler : NextDayActionHandler { public abstract class AbstractTileNextDayActionHandler : NextDayActionHandler {
protected BaseTile _tile; protected BaseTile _tile;

View file

@ -5,6 +5,9 @@ using UnityEngine;
using Random = UnityEngine.Random; using Random = UnityEngine.Random;
/// <summary>
/// Controlling logic of crop on FarmlandTiles
/// </summary>
public class Crop { public class Crop {
private const int FinalGrowthStage = 4; private const int FinalGrowthStage = 4;

View file

@ -4,10 +4,12 @@ using DefaultNamespace;
using Tiles; using Tiles;
using UnityEngine; using UnityEngine;
/// <summary>
/// Defining the behaviour of tiles
/// </summary>
public class TileBehaviour : MonoBehaviour { public class TileBehaviour : MonoBehaviour {
private BaseTile _tile; private BaseTile _tile;
public virtual BaseTile Tile {
public BaseTile Tile {
get => _tile; get => _tile;
set { set {
_tile = value; _tile = value;
@ -18,7 +20,7 @@ public class TileBehaviour : MonoBehaviour {
private SpriteRenderer _hoverIndicatorSpriteRenderer; private SpriteRenderer _hoverIndicatorSpriteRenderer;
private static Color _hoverIndicatorColor; private static Color _hoverIndicatorColor;
// Start is called before the first frame update /// Start is called before the first frame update
void Start() { void Start() {
Tile = new GrassTile(); Tile = new GrassTile();
@ -37,11 +39,16 @@ public class TileBehaviour : MonoBehaviour {
HouseController.NewDayEvent.AddListener(NextDay); HouseController.NewDayEvent.AddListener(NextDay);
} }
/// <summary>
/// is called when a NewDayEvent is dispersed
/// </summary>
private void NextDay() { private void NextDay() {
Debug.Log("nextday");
ActionManager.Instance.NextDayAction(gameObject); ActionManager.Instance.NextDayAction(gameObject);
} }
/// <summary>
/// Used to invoke Click Actions
/// </summary>
void OnMouseDown() { void OnMouseDown() {
UsableItem usableItem = PlayerController.instance.SelectedItem; UsableItem usableItem = PlayerController.instance.SelectedItem;
if(usableItem != null) { if(usableItem != null) {
@ -49,6 +56,9 @@ public class TileBehaviour : MonoBehaviour {
} }
} }
/// <summary>
/// used to set hover indicator
/// </summary>
private void OnMouseEnter() { private void OnMouseEnter() {
SetHoverIndicatorVisibility(true); SetHoverIndicatorVisibility(true);
} }
@ -57,6 +67,10 @@ public class TileBehaviour : MonoBehaviour {
SetHoverIndicatorVisibility(false); SetHoverIndicatorVisibility(false);
} }
/// <summary>
/// Sets the visibility of hover indicator
/// </summary>
/// <param name="visible"></param>
private void SetHoverIndicatorVisibility(bool visible) { private void SetHoverIndicatorVisibility(bool visible) {
if(visible) { if(visible) {
_hoverIndicatorSpriteRenderer.color = _hoverIndicatorColor; _hoverIndicatorSpriteRenderer.color = _hoverIndicatorColor;

View file

@ -1,26 +1,33 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Tilemaps;
/// <summary>
/// Instatiates Tiles at beginning of the game
/// </summary>
public class TileController : MonoBehaviour { public class TileController : MonoBehaviour {
public GameObject tile; public GameObject tile;
public GameObject cameraGameObject; public GameObject cameraGameObject;
public List<GameObject> Tiles; public List<List<GameObject>> Tiles;
// Start is called before the first frame update // Start is called before the first frame update
void Start() { void Start() {
Tiles = new List<List<GameObject>>();
Camera camera = cameraGameObject.GetComponent<Camera>(); Camera camera = cameraGameObject.GetComponent<Camera>();
Vector3 screen = camera.ViewportToWorldPoint(new Vector3(1, 1, camera.nearClipPlane)); Vector3 screen = camera.ViewportToWorldPoint(new Vector3(1, 1, camera.nearClipPlane));
int x = Convert.ToInt32(Math.Ceiling(screen.x)); int x = Convert.ToInt32(Math.Ceiling(screen.x));
int y = Convert.ToInt32(Math.Ceiling(screen.y)); int y = Convert.ToInt32(Math.Ceiling(screen.y));
for(int xx = -x; xx <= x; xx++) { for(int xx = -x; xx <= x; xx++) {
List<GameObject> temp = new List<GameObject>();
for(int yy = -y; yy <= y; yy++) { for(int yy = -y; yy <= y; yy++) {
if(tile != null) { 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);
} }
} }
} }

View file

@ -2,10 +2,14 @@ using System;
using System.IO; using System.IO;
using UnityEngine; using UnityEngine;
namespace Tiles { namespace Tiles {
/// <summary>
/// Base class of all Tile types
/// </summary>
public abstract class BaseTile { public abstract class BaseTile {
private Sprite _sprite; protected Sprite _sprite;
public Sprite Sprite => _sprite; public virtual Sprite Sprite => _sprite;
protected BaseTile(String pathToImageFile) { protected BaseTile(String pathToImageFile) {
_sprite = GenerateSpriteFromFile(pathToImageFile); _sprite = GenerateSpriteFromFile(pathToImageFile);
@ -14,6 +18,11 @@ namespace Tiles {
public virtual void DayLightStep() { } public virtual void DayLightStep() { }
/// <summary>
/// Used to generate Sprites from files
/// </summary>
/// <param name="pathToImageFile">Path to the Image file to be Converted to the Sprite</param>
/// <returns>the created Sprite</returns>
static public Sprite GenerateSpriteFromFile(String pathToImageFile) { static public Sprite GenerateSpriteFromFile(String pathToImageFile) {
byte[] data = File.ReadAllBytes(pathToImageFile); byte[] data = File.ReadAllBytes(pathToImageFile);
Texture2D texture = new Texture2D(32, 32, TextureFormat.ARGB32, false); Texture2D texture = new Texture2D(32, 32, TextureFormat.ARGB32, false);

View file

@ -2,11 +2,26 @@
namespace Tiles namespace Tiles
{ {
public class GrassTile : BaseTile public class GrassTile : BaseTile {
{ private Sprite[] _sprites;
public GrassTile() : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_000.png") /// <summary>
/// Overridden Property to randomize tiles between the 3 different sprites
/// </summary>
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")
};
} }
} }
} }