Fully refactored action handling
This commit is contained in:
parent
561b56ae84
commit
a5ded24c36
11 changed files with 169 additions and 26 deletions
|
|
@ -289,6 +289,7 @@ GameObject:
|
||||||
m_Component:
|
m_Component:
|
||||||
- component: {fileID: 6840747130279028589}
|
- component: {fileID: 6840747130279028589}
|
||||||
- component: {fileID: 6444954891387505866}
|
- component: {fileID: 6444954891387505866}
|
||||||
|
- component: {fileID: 5996997330531153169}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: HydrationIndicator
|
m_Name: HydrationIndicator
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
|
|
@ -363,6 +364,26 @@ SpriteRenderer:
|
||||||
m_WasSpriteAssigned: 1
|
m_WasSpriteAssigned: 1
|
||||||
m_MaskInteraction: 0
|
m_MaskInteraction: 0
|
||||||
m_SpriteSortPoint: 0
|
m_SpriteSortPoint: 0
|
||||||
|
--- !u!95 &5996997330531153169
|
||||||
|
Animator:
|
||||||
|
serializedVersion: 4
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 2145333408190307379}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_Avatar: {fileID: 0}
|
||||||
|
m_Controller: {fileID: 0}
|
||||||
|
m_CullingMode: 0
|
||||||
|
m_UpdateMode: 0
|
||||||
|
m_ApplyRootMotion: 0
|
||||||
|
m_LinearVelocityBlending: 0
|
||||||
|
m_StabilizeFeet: 0
|
||||||
|
m_WarningMessage:
|
||||||
|
m_HasTransformHierarchy: 1
|
||||||
|
m_AllowConstantClipSamplingOptimization: 1
|
||||||
|
m_KeepAnimatorControllerStateOnDisable: 0
|
||||||
--- !u!1 &4752245148499717901
|
--- !u!1 &4752245148499717901
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using UnityEngine.PlayerLoop;
|
||||||
using Random = UnityEngine.Random;
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
namespace DefaultNamespace {
|
namespace DefaultNamespace {
|
||||||
|
[Obsolete("Use ActionManager.HandleAction() instead", true)]
|
||||||
public class ActionInvoker {
|
public class ActionInvoker {
|
||||||
public static void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
public static void InvokeAction(GameObject gameObject, UsableItem usableItem) {
|
||||||
Type tileType = gameObject.GetComponent<TileBehaviour>().Tile.GetType();
|
Type tileType = gameObject.GetComponent<TileBehaviour>().Tile.GetType();
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ using UnityEngine;
|
||||||
|
|
||||||
namespace Actions {
|
namespace Actions {
|
||||||
public class ActionManager {
|
public class ActionManager {
|
||||||
|
#region Singleton
|
||||||
private static ActionManager _instance;
|
private static ActionManager _instance;
|
||||||
public static ActionManager Instance {
|
public static ActionManager Instance {
|
||||||
get
|
get
|
||||||
|
|
@ -14,33 +15,49 @@ namespace Actions {
|
||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
private List<ActionHandler> _actionHandlers;
|
private List<ClickActionHandler> _clickActionHandlers;
|
||||||
public List<ActionHandler> ActionHandlers => _actionHandlers;
|
private List<NextDayActionHandler> _nextDayActionHandlers;
|
||||||
|
|
||||||
private ActionManager() {
|
private ActionManager() {
|
||||||
_actionHandlers = new List<ActionHandler>();
|
_clickActionHandlers = new List<ClickActionHandler>();
|
||||||
instatiateActionHandlers();
|
_nextDayActionHandlers = new List<NextDayActionHandler>();
|
||||||
|
instantiateClickActionHandlers();
|
||||||
|
instantiateNextDayActionHandlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void instatiateActionHandlers() {
|
private void instantiateNextDayActionHandlers() {
|
||||||
ActionHandlers.Add(new GrassTileHoeActionHandler());
|
_nextDayActionHandlers.Add(new FarmlandTileNextDayActionHandler());
|
||||||
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) {
|
private void instantiateClickActionHandlers() {
|
||||||
foreach (ActionHandler actionHandler in ActionHandlers) {
|
_clickActionHandlers.Add(new GrassTileClickHoeActionHandler());
|
||||||
|
_clickActionHandlers.Add(new GrassTileClickShovelActionHandler());
|
||||||
|
_clickActionHandlers.Add(new GrassTileClickFenceActionHandler());
|
||||||
|
|
||||||
|
_clickActionHandlers.Add(new FarmlandTileClickShovelActionHandler());
|
||||||
|
_clickActionHandlers.Add(new FarmlandTileClickScytheActionHandler());
|
||||||
|
_clickActionHandlers.Add(new FarmlandTileClickWateringCanActionHandler());
|
||||||
|
_clickActionHandlers.Add(new FarmlandTileClickWheatSeedsActionHandler());
|
||||||
|
|
||||||
|
_clickActionHandlers.Add(new WaterTileClickShovelActionHandler());
|
||||||
|
_clickActionHandlers.Add(new WaterTileClickFishingRodActionHandler());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClickAction(GameObject gameObject, UsableItem usableItem) {
|
||||||
|
foreach (ClickActionHandler actionHandler in _clickActionHandlers) {
|
||||||
if(actionHandler.Matches(gameObject, usableItem)) {
|
if(actionHandler.Matches(gameObject, usableItem)) {
|
||||||
actionHandler.InvokeAction(gameObject, usableItem);
|
actionHandler.InvokeAction(gameObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void NextDayAction(GameObject gameObject) {
|
||||||
|
Debug.Log("nextday action");
|
||||||
|
foreach (NextDayActionHandler actionHandler in _nextDayActionHandlers) {
|
||||||
|
if(actionHandler.Matches(gameObject)) {
|
||||||
|
actionHandler.InvokeAction(gameObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
Assets/Scripts/Actions/NextDayActionHandler.cs
Normal file
8
Assets/Scripts/Actions/NextDayActionHandler.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Actions {
|
||||||
|
public interface NextDayActionHandler {
|
||||||
|
public void InvokeAction(GameObject gameObject);
|
||||||
|
public bool Matches(GameObject gameObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Assets/Scripts/Actions/NextDayActionHandler.cs.meta
Normal file
3
Assets/Scripts/Actions/NextDayActionHandler.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 8f87f93d622e4c48b1d3a9e7b8540cb1
|
||||||
|
timeCreated: 1655984072
|
||||||
90
Assets/Scripts/Actions/NextDayActionHandlers.cs
Normal file
90
Assets/Scripts/Actions/NextDayActionHandlers.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
using Assets.Scripts.Actions;
|
||||||
|
using Tiles;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Actions {
|
||||||
|
public abstract class AbstractTileNextDayActionHandler : NextDayActionHandler {
|
||||||
|
protected BaseTile _tile;
|
||||||
|
protected ItemContainer _ic;
|
||||||
|
public virtual void InvokeAction(GameObject gameObject) {
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool Matches(GameObject gameObject) {
|
||||||
|
bool rv = false;
|
||||||
|
_ic = ItemContainer.Instance;
|
||||||
|
try {
|
||||||
|
_tile = gameObject.GetComponent<TileBehaviour>().Tile;
|
||||||
|
rv = true;
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class AbstractFarmlandTileNextDayActionHandler : AbstractTileNextDayActionHandler {
|
||||||
|
protected Crop crop;
|
||||||
|
public override bool Matches(GameObject gameObject) {
|
||||||
|
bool rv = base.Matches(gameObject);
|
||||||
|
try {
|
||||||
|
crop = ((FarmlandTile)_tile).Crop;
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
rv = false;
|
||||||
|
}
|
||||||
|
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 class FarmlandTileNextDayActionHandler : AbstractFarmlandTileNextDayActionHandler {
|
||||||
|
public override void InvokeAction(GameObject gameObject) {
|
||||||
|
if(crop.Planted && crop.Hydrated) {
|
||||||
|
crop.Grow();
|
||||||
|
}
|
||||||
|
crop.Hydrated = false;
|
||||||
|
|
||||||
|
updateFarmlandSprites(gameObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Matches(GameObject gameObject) {
|
||||||
|
bool rv = base.Matches(gameObject);
|
||||||
|
Debug.Log(_tile.ToString());
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Assets/Scripts/Actions/NextDayActionHandlers.cs.meta
Normal file
3
Assets/Scripts/Actions/NextDayActionHandlers.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: a265af555e5f40a69fba299178573640
|
||||||
|
timeCreated: 1655984072
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using Actions;
|
||||||
using DefaultNamespace;
|
using DefaultNamespace;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Object = System.Object;
|
using Object = System.Object;
|
||||||
|
|
@ -59,6 +60,6 @@ public class Animal : MonoBehaviour {
|
||||||
private void OnMouseDown() {
|
private void OnMouseDown() {
|
||||||
//TODO: TEMP!!!!
|
//TODO: TEMP!!!!
|
||||||
Destroy(gameObject);
|
Destroy(gameObject);
|
||||||
ActionInvoker.InvokeAction(gameObject, PlayerController.instance.SelectedItem);
|
//ActionManager.Instance.HandleAction(gameObject, PlayerController.instance.SelectedItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +38,6 @@ public class Crop {
|
||||||
|
|
||||||
public void Grow() {
|
public void Grow() {
|
||||||
_growthStage++;
|
_growthStage++;
|
||||||
Hydrated = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ResetPlant() {
|
public void ResetPlant() {
|
||||||
|
|
|
||||||
|
|
@ -34,18 +34,18 @@ public class TileBehaviour : MonoBehaviour {
|
||||||
_hoverIndicatorColor = new Color(1, 1, 1, 0.3f);
|
_hoverIndicatorColor = new Color(1, 1, 1, 0.3f);
|
||||||
SetHoverIndicatorVisibility(false);
|
SetHoverIndicatorVisibility(false);
|
||||||
|
|
||||||
HouseController.NewDayEvent.AddListener(DayLightStep);
|
HouseController.NewDayEvent.AddListener(NextDay);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DayLightStep() {
|
private void NextDay() {
|
||||||
ActionInvoker.InvokeDayLightStep(gameObject);
|
Debug.Log("nextday");
|
||||||
|
ActionManager.Instance.NextDayAction(gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnMouseDown() {
|
void OnMouseDown() {
|
||||||
UsableItem usableItem = PlayerController.instance.SelectedItem;
|
UsableItem usableItem = PlayerController.instance.SelectedItem;
|
||||||
if(usableItem != null) {
|
if(usableItem != null) {
|
||||||
//ActionInvoker.InvokeAction(gameObject, usableItem);
|
ActionManager.Instance.ClickAction(gameObject, usableItem);
|
||||||
ActionManager.Instance.HandleAction(gameObject, usableItem);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
m_EditorVersion: 2021.3.2f1
|
m_EditorVersion: 2021.3.1f1
|
||||||
m_EditorVersionWithRevision: 2021.3.2f1 (d6360bedb9a0)
|
m_EditorVersionWithRevision: 2021.3.1f1 (3b70a0754835)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue