ActionHandlers working for Clicks on Tiles
This commit is contained in:
parent
e160867e7e
commit
5d4bf8c940
12 changed files with 352 additions and 15 deletions
|
|
@ -265,9 +265,9 @@ SpriteRenderer:
|
|||
m_AutoUVMaxDistance: 0.5
|
||||
m_AutoUVMaxAngle: 89
|
||||
m_LightmapParameters: {fileID: 0}
|
||||
m_SortingLayerID: 0
|
||||
m_SortingLayer: 0
|
||||
m_SortingOrder: 0
|
||||
m_SortingLayerID: -278359049
|
||||
m_SortingLayer: 2
|
||||
m_SortingOrder: 1
|
||||
m_Sprite: {fileID: 7482667652216324306, guid: 311925a002f4447b3a28927169b83ea6, type: 3}
|
||||
m_Color: {r: 1, g: 1, b: 1, a: 0.41568628}
|
||||
m_FlipX: 0
|
||||
|
|
|
|||
3
Assets/Scripts/Actions.meta
Normal file
3
Assets/Scripts/Actions.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2b4085d6a6194801836ce20f5e5857d1
|
||||
timeCreated: 1655939250
|
||||
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
|
||||
|
|
@ -62,6 +62,7 @@ namespace DefaultNamespace {
|
|||
if(crop.Planted && crop.Hydrated) {
|
||||
crop.Grow();
|
||||
}
|
||||
crop.Hydrated = false;
|
||||
|
||||
UpdateFarmlandSprites(gameObject);
|
||||
}
|
||||
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
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using Actions;
|
||||
using DefaultNamespace;
|
||||
using Tiles;
|
||||
using UnityEngine;
|
||||
|
|
@ -26,6 +27,9 @@ public class TileBehaviour : MonoBehaviour {
|
|||
_hoverIndicatorSpriteRenderer = transChild.gameObject.GetComponent<SpriteRenderer>();
|
||||
_hoverIndicatorSpriteRenderer.color = Color.clear;
|
||||
}
|
||||
if(transChild.gameObject.name.Equals("HydrationIndicator")) {
|
||||
transChild.gameObject.GetComponent<SpriteRenderer>().color = Color.clear;
|
||||
}
|
||||
}
|
||||
_hoverIndicatorColor = new Color(1, 1, 1, 0.3f);
|
||||
SetHoverIndicatorVisibility(false);
|
||||
|
|
@ -40,7 +44,8 @@ public class TileBehaviour : MonoBehaviour {
|
|||
void OnMouseDown() {
|
||||
UsableItem usableItem = PlayerController.instance.SelectedItem;
|
||||
if(usableItem != null) {
|
||||
ActionInvoker.InvokeAction(gameObject, usableItem);
|
||||
//ActionInvoker.InvokeAction(gameObject, usableItem);
|
||||
ActionManager.Instance.HandleAction(gameObject, usableItem);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -137,21 +137,12 @@
|
|||
"dependencies": {},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.nuget.newtonsoft-json": {
|
||||
"version": "3.0.2",
|
||||
"depth": 2,
|
||||
"source": "registry",
|
||||
"dependencies": {},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.services.core": {
|
||||
"version": "1.3.1",
|
||||
"version": "1.0.1",
|
||||
"depth": 1,
|
||||
"source": "registry",
|
||||
"dependencies": {
|
||||
"com.unity.modules.unitywebrequest": "1.0.0",
|
||||
"com.unity.nuget.newtonsoft-json": "3.0.2",
|
||||
"com.unity.modules.androidjni": "1.0.0"
|
||||
"com.unity.modules.unitywebrequest": "1.0.0"
|
||||
},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue