Refactored to ActionInvoker.cs

This commit is contained in:
j-weissen 2022-06-09 16:47:17 +02:00
parent c06d161006
commit afaddd940c
11 changed files with 189 additions and 214 deletions

View file

@ -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<TileBehaviour>().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<TileBehaviour>().Tile.GetType();
if(tileType == typeof(FarmlandTile)) {
GrowIfHydrated(gameObject);
}
}
private static void GrowIfHydrated(GameObject gameObject) {
Crop crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().Tile).Crop;
if(crop.Planted && crop.Hydrated) {
crop.Grow();
}
UpdateFarmlandSprites(gameObject);
}
private static void PlantIfPossible(GameObject gameObject) {
Crop crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().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<TileBehaviour>().Tile).Crop;
crop.Hydrated = true;
UpdateFarmlandSprites(gameObject);
}
private static void UpdateFarmlandSprites(GameObject gameObject) {
Crop crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().Tile).Crop;
SpriteRenderer hydrationSR = gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>();
SpriteRenderer cropSR = gameObject.transform.GetChild(1).GetComponent<SpriteRenderer>();
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<TileBehaviour>().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<TileBehaviour>().Tile = tileType;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7fc4f22ade5a482eba0e34c4f96fd1d8
timeCreated: 1654777450

View file

@ -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" +

View file

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

View file

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

View file

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

View file

@ -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<SpriteRenderer>().sprite = _tile.Sprite;
}
}
private SpriteRenderer _hoverIndicatorSpriteRenderer;
private static Color _hoverIndicatorColor;
@ -13,9 +23,15 @@ public class TileBehaviour : MonoBehaviour
{
_hoverIndicatorSpriteRenderer = gameObject.transform.GetChild(2).GetComponent<SpriteRenderer>();
SetHoverIndicatorVisibility(false);
SetTile(new GrassTile(gameObject));
Tile = new GrassTile();
gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>().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<SpriteRenderer>().sprite = _tile.Sprite;
}
private void OnMouseEnter()

View file

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

View file

@ -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<SpriteRenderer>(),
gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>());
}
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();
}
}
}

View file

@ -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")
{
}
/// <summary>
/// to be invoked when the Tile is clicked, handles the actions following on the click
/// </summary>
/// <param name="usable">the UsableItem that the Tile was clicked on with</param>
/// <returns>a subclass of BaseTile if the Tile has to change, null if it stays the same type</returns>
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;
}
}
}

View file

@ -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") {
}
}
}