ActionHandlers working for Clicks on Tiles
This commit is contained in:
parent
e160867e7e
commit
5d4bf8c940
12 changed files with 352 additions and 15 deletions
9
Assets/Scripts/Actions/ActionHandler.cs
Normal file
9
Assets/Scripts/Actions/ActionHandler.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Actions {
|
||||
public interface ActionHandler {
|
||||
public void InvokeAction(GameObject gameObject, UsableItem usableItem);
|
||||
public bool Matches(GameObject gameObject, UsableItem usableItem);
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Actions/ActionHandler.cs.meta
Normal file
3
Assets/Scripts/Actions/ActionHandler.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ebe75c40781c442db6456a1e901f61bc
|
||||
timeCreated: 1655937551
|
||||
271
Assets/Scripts/Actions/ActionHandlers.cs
Normal file
271
Assets/Scripts/Actions/ActionHandlers.cs
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
using Actions;
|
||||
using Tiles;
|
||||
using TMPro.EditorUtilities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.Scripts.Actions {
|
||||
public abstract class AbstractTileActionHandler : ActionHandler {
|
||||
protected BaseTile _tile;
|
||||
protected int _usableItemId;
|
||||
protected ItemContainer _ic;
|
||||
public virtual void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = false;
|
||||
_ic = ItemContainer.Instance;
|
||||
try {
|
||||
_tile = gameObject.GetComponent<TileBehaviour>().Tile;
|
||||
rv = true;
|
||||
}
|
||||
catch {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
public abstract class AbstractGrassTileActionHandler : AbstractTileActionHandler {
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (_tile.GetType() == typeof(GrassTile));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
public abstract class AbstractFarmlandTileActionHandler : AbstractTileActionHandler {
|
||||
protected Crop crop;
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().Tile).Crop;
|
||||
rv = (_tile.GetType() == typeof(FarmlandTile));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
protected void updateFarmlandSprites(GameObject gameObject) {
|
||||
SpriteRenderer hydrationSpriteRenderer = null;
|
||||
SpriteRenderer cropSpriteRenderer = null;
|
||||
|
||||
foreach (Transform transChild in gameObject.transform.GetComponentInChildren<Transform>()) {
|
||||
if(transChild.gameObject.name.Equals("HydrationIndicator")) {
|
||||
hydrationSpriteRenderer = transChild.gameObject.GetComponent<SpriteRenderer>();
|
||||
}
|
||||
if(transChild.gameObject.name.Equals("Crop")) {
|
||||
cropSpriteRenderer = transChild.gameObject.GetComponent<SpriteRenderer>();
|
||||
}
|
||||
}
|
||||
|
||||
if(crop.Planted) {
|
||||
if(crop.FullyGrown) {
|
||||
//Debug.Log("sprite fg");
|
||||
cropSpriteRenderer.sprite = Crop.FullyGrownCrop;
|
||||
} else {
|
||||
//Debug.Log("sprite smallCrop");
|
||||
cropSpriteRenderer.sprite = Crop.SmallCrop;
|
||||
}
|
||||
} else {
|
||||
cropSpriteRenderer.sprite = null;
|
||||
}
|
||||
|
||||
if(crop.Hydrated) {
|
||||
//Debug.Log("sprite hydrated");
|
||||
hydrationSpriteRenderer.color = Crop.HydratedColor;
|
||||
} else {
|
||||
//Debug.Log("sprite no hydrated");
|
||||
hydrationSpriteRenderer.color = Color.clear;
|
||||
}
|
||||
}
|
||||
}
|
||||
public abstract class AbstractWaterTileActionHandler : AbstractTileActionHandler {
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (_tile.GetType() == typeof(WaterTile));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class AbstractAnimalActionHandler : ActionHandler {
|
||||
public virtual void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = false;
|
||||
try {
|
||||
gameObject.GetComponent<Animal>();
|
||||
rv = true;
|
||||
}
|
||||
catch {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public class GrassTileHoeActionHandler : AbstractGrassTileActionHandler {
|
||||
public override void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
gameObject.GetComponent<TileBehaviour>().Tile = new FarmlandTile();
|
||||
}
|
||||
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (usableItem.ID == _ic.GetItemIdByName("Hoe"));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public class GrassTileShovelActionHandler : AbstractGrassTileActionHandler {
|
||||
public override void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
gameObject.GetComponent<TileBehaviour>().Tile = new WaterTile();
|
||||
}
|
||||
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (usableItem.ID == _ic.GetItemIdByName("Shovel"));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public class GrassTileFenceActionHandler : AbstractGrassTileActionHandler {
|
||||
public override void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
SpriteRenderer fenceRenderer = null;
|
||||
BoxCollider2D fenceCollider = null;
|
||||
foreach (Transform transChild in gameObject.GetComponentsInChildren<Transform>()) {
|
||||
if(transChild.gameObject.name.Equals("Fence")) {
|
||||
fenceRenderer = transChild.gameObject.GetComponent<SpriteRenderer>();
|
||||
fenceCollider = transChild.gameObject.GetComponent<BoxCollider2D>();
|
||||
}
|
||||
}
|
||||
|
||||
if(fenceRenderer && fenceCollider) {
|
||||
if(!fenceRenderer.enabled || !fenceCollider.enabled) {
|
||||
fenceRenderer.enabled = true;
|
||||
Debug.Log("aaaaaaaaaaaaaaaaaaaaa");
|
||||
fenceRenderer.color = new Color(1, 1, 1, 1);
|
||||
fenceCollider.enabled = true;
|
||||
Inventory.instance.RemoveElement(ItemContainer.Instance.GetItemByName("Fence"), 1);
|
||||
}
|
||||
} else {
|
||||
Debug.LogError("Fence Renderer or Fence Collider is null");
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (usableItem.ID == _ic.GetItemIdByName("Fence"));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public class FarmlandTileShovelActionHandler : AbstractFarmlandTileActionHandler {
|
||||
public override void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
gameObject.GetComponent<TileBehaviour>().Tile = new GrassTile();
|
||||
}
|
||||
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (usableItem.ID == _ic.GetItemIdByName("Shovel"));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public class FarmlandTileScytheActionHandler : AbstractFarmlandTileActionHandler {
|
||||
public override void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
if(crop.FullyGrown) {
|
||||
Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat Seeds"),
|
||||
(int)(Random.Range(1, 300)));
|
||||
Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat"), 1);
|
||||
crop.ResetPlant();
|
||||
updateFarmlandSprites(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (usableItem.ID == _ic.GetItemIdByName("Scythe"));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public class FarmlandTileWateringCanActionHandler : AbstractFarmlandTileActionHandler {
|
||||
public override void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
crop.Hydrated = true;
|
||||
updateFarmlandSprites(gameObject);
|
||||
}
|
||||
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (usableItem.ID == _ic.GetItemIdByName("Watering Can"));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public class FarmlandTileWheatSeedsActionHandler : AbstractFarmlandTileActionHandler {
|
||||
public override void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
if(!crop.Planted) {
|
||||
crop.Plant();
|
||||
Inventory.instance.RemoveElement(ItemContainer.Instance.GetItemByName("Wheat Seeds"), 1);
|
||||
}
|
||||
|
||||
updateFarmlandSprites(gameObject);
|
||||
}
|
||||
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (usableItem.ID == _ic.GetItemIdByName("Wheat Seeds"));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public class WaterTileShovelActionHandler : AbstractWaterTileActionHandler {
|
||||
public override void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
gameObject.GetComponent<TileBehaviour>().Tile = new GrassTile();
|
||||
}
|
||||
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (usableItem.ID == _ic.GetItemIdByName("Shovel"));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public class WaterTileFishingRodActionHandler : AbstractWaterTileActionHandler {
|
||||
public override void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||
ItemContainer ic = ItemContainer.Instance;
|
||||
|
||||
FishingController fc = FishingController.instance;
|
||||
if(!fc.Fishing) {
|
||||
fc.StartFishing();
|
||||
} else {
|
||||
fc.TryCatch();
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Matches(GameObject gameObject, UsableItem usableItem) {
|
||||
bool rv = base.Matches(gameObject, usableItem);
|
||||
if(rv) {
|
||||
rv = (usableItem.ID == _ic.GetItemIdByName("Fishing Rod"));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Actions/ActionHandlers.cs.meta
Normal file
3
Assets/Scripts/Actions/ActionHandlers.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c504c0b09a314fafb60775de09451040
|
||||
timeCreated: 1655939308
|
||||
152
Assets/Scripts/Actions/ActionInvoker.cs
Normal file
152
Assets/Scripts/Actions/ActionInvoker.cs
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
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(usableItem.ID == ic.GetItemIdByName("Fence")) {
|
||||
SetFence(gameObject);
|
||||
}
|
||||
} 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();
|
||||
}
|
||||
crop.Hydrated = false;
|
||||
|
||||
UpdateFarmlandSprites(gameObject);
|
||||
}
|
||||
|
||||
private static void PlantIfPossible(GameObject gameObject) {
|
||||
Crop crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().Tile).Crop;
|
||||
if(!crop.Planted) {
|
||||
crop.Plant();
|
||||
Inventory.instance.RemoveElement(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.AddElement(ItemContainer.Instance.GetItemByName("Wheat Seeds"),
|
||||
(int)(Random.Range(1, 300)));
|
||||
Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat"), 1);
|
||||
crop.ResetPlant();
|
||||
UpdateFarmlandSprites(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetTile(GameObject gameObject, BaseTile tileType) {
|
||||
gameObject.GetComponent<TileBehaviour>().Tile = tileType;
|
||||
}
|
||||
|
||||
private static void SetFence(GameObject gameObject) {
|
||||
SpriteRenderer fenceRenderer = null;
|
||||
BoxCollider2D fenceCollider = null;
|
||||
foreach(Transform transChild in gameObject.GetComponentsInChildren<Transform>()) {
|
||||
if(transChild.gameObject.name.Equals("Fence")) {
|
||||
fenceRenderer = transChild.gameObject.GetComponent<SpriteRenderer>();
|
||||
fenceCollider = transChild.gameObject.GetComponent<BoxCollider2D>();
|
||||
}
|
||||
}
|
||||
|
||||
if(fenceRenderer && fenceCollider) {
|
||||
if(!fenceRenderer.enabled || !fenceCollider.enabled) {
|
||||
fenceRenderer.enabled = true;
|
||||
Debug.Log("aaaaaaaaaaaaaaaaaaaaa");
|
||||
fenceRenderer.color = new Color(1, 1, 1, 1);
|
||||
fenceCollider.enabled = true;
|
||||
Inventory.instance.RemoveElement(ItemContainer.Instance.GetItemByName("Fence"), 1);
|
||||
}
|
||||
} else {
|
||||
Debug.LogError("Fence Renderer or Fence Collider is null");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Actions/ActionInvoker.cs.meta
Normal file
3
Assets/Scripts/Actions/ActionInvoker.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7fc4f22ade5a482eba0e34c4f96fd1d8
|
||||
timeCreated: 1654777450
|
||||
48
Assets/Scripts/Actions/ActionManager.cs
Normal file
48
Assets/Scripts/Actions/ActionManager.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
using System.Collections.Generic;
|
||||
using Assets.Scripts.Actions;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Actions {
|
||||
public class ActionManager {
|
||||
private static ActionManager _instance;
|
||||
public static ActionManager Instance {
|
||||
get
|
||||
{
|
||||
if(_instance == null) {
|
||||
_instance = new ActionManager();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
private List<ActionHandler> _actionHandlers;
|
||||
public List<ActionHandler> ActionHandlers => _actionHandlers;
|
||||
|
||||
private ActionManager() {
|
||||
_actionHandlers = new List<ActionHandler>();
|
||||
instatiateActionHandlers();
|
||||
}
|
||||
|
||||
private void instatiateActionHandlers() {
|
||||
ActionHandlers.Add(new GrassTileHoeActionHandler());
|
||||
ActionHandlers.Add(new GrassTileShovelActionHandler());
|
||||
ActionHandlers.Add(new GrassTileFenceActionHandler());
|
||||
|
||||
ActionHandlers.Add(new FarmlandTileShovelActionHandler());
|
||||
ActionHandlers.Add(new FarmlandTileScytheActionHandler());
|
||||
ActionHandlers.Add(new FarmlandTileWateringCanActionHandler());
|
||||
ActionHandlers.Add(new FarmlandTileWheatSeedsActionHandler());
|
||||
|
||||
ActionHandlers.Add(new WaterTileShovelActionHandler());
|
||||
ActionHandlers.Add(new WaterTileFishingRodActionHandler());
|
||||
}
|
||||
|
||||
public void HandleAction(GameObject gameObject, UsableItem usableItem) {
|
||||
foreach (ActionHandler actionHandler in ActionHandlers) {
|
||||
if(actionHandler.Matches(gameObject, usableItem)) {
|
||||
actionHandler.InvokeAction(gameObject, usableItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Actions/ActionManager.cs.meta
Normal file
3
Assets/Scripts/Actions/ActionManager.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1cb4e375aa8d4ab59b1854274c0c12e3
|
||||
timeCreated: 1655938224
|
||||
Loading…
Add table
Add a link
Reference in a new issue