refactor interact system

interact only after PopupItemMenu button clicked
This commit is contained in:
Vova
2023-11-16 16:11:39 +02:00
parent c2bb35a46d
commit 1774ab5b18
12 changed files with 100 additions and 61 deletions
+1
View File
@@ -7,6 +7,7 @@ public enum PlayerStates { Awake, Sleeping, Eating,Working }
public enum StatsId { Money, RentAccount, Food, Energy, BankAccount, Job, }
public enum Tasks { Move, Interact, Rotate };
public enum TaskStatus { Waiting, InProgress, Complete };
public enum InteractionStatus { Complete, WaitForChoose, InProgress, FarFromPlayer };
public enum AnimationStates
{
[EnumMember(Value = "Idle")]
@@ -1,5 +1,6 @@
using UnityEngine;
using static UnityEditor.Experimental.GraphView.GraphView;
using System;
using System.Collections;
using UnityEngine;
public class BaseInteractableObject : MonoBehaviour
{
@@ -7,15 +8,39 @@ public class BaseInteractableObject : MonoBehaviour
public Transform _interactionPoint;
[SerializeField]
public PopupItemMenu _radialMenuPrefab;
protected Player _player;
public virtual void Interact(Player player)
private InteractionStatus _currentStatus=InteractionStatus.Complete;
public InteractionStatus Interact(Player player)
{
_player=player;
if (_currentStatus == InteractionStatus.Complete)
{
UIManager.Instance.ShowItemsMenu(PopupMenuCallback);
_currentStatus = InteractionStatus.WaitForChoose;
}
else if(_currentStatus!=InteractionStatus.WaitForChoose)
{
if (_player.IsPathComplete(_interactionPoint.position))
{
InteractAction();
_currentStatus = InteractionStatus.Complete;
}
else
{
_currentStatus = InteractionStatus.FarFromPlayer;
}
}
return _currentStatus;
}
public void SpawnMenu()
private void PopupMenuCallback()
{
UIManager.Instance.ShowItemsMenu();
_currentStatus = InteractionStatus.InProgress;
}
protected virtual void InteractAction()
{
}
}
+2 -3
View File
@@ -1,14 +1,13 @@
using System;
using System.Collections;
public class Bed : BaseInteractableObject
{
public override void Interact(Player player)
protected override void InteractAction()
{
base.Interact(player);
_player.SetPlayerAnimation(AnimationStates.Sitting, OnAnimationFinished);
}
private void OnAnimationFinished()
{
UIManager.Instance.ShowTimeSliderDialog("Go to sleep", "Sleep until", OnCancel, OnConfirm);
@@ -11,16 +11,15 @@ public class CashierDesk : BaseInteractableObject
private List<JobInfoSO> _jobsInfoList;
private JobInfoSO _playerJob;
public override void Interact(Player player)
protected override void InteractAction()
{
base.Interact(player);
if (player.IsHoldContainerItem())
if (_player.IsHoldContainerItem())
{
BuyItems();
}
else
{
_playerJob = _jobsInfoList.Where(x => x.JobPosition == player.JobPosition).FirstOrDefault();
_playerJob = _jobsInfoList.Where(x => x.JobPosition == _player.JobPosition).FirstOrDefault();
print($"playerJob is {_playerJob}");
if (_playerJob != null)
{
@@ -31,7 +30,7 @@ public class CashierDesk : BaseInteractableObject
{
print("You don't work here");
}
}
}
}
private void BuyItems()
+3 -3
View File
@@ -14,11 +14,11 @@ public class Door : BaseInteractableObject
print($"Player came from to {_scene}");
Player.Instance.SetPosition(_interactionPoint.position);
}
}
public override void Interact(Player player)
{
base.Interact(player);
protected override void InteractAction()
{
if (!string.IsNullOrEmpty(_exitName))
{
PlayerPrefs.SetString("lastExitName", _exitName.ToLower());
+6 -7
View File
@@ -12,12 +12,11 @@ public class Fridge : BaseInteractableObject
private List<FoodItemSO> _foodObjects = new List<FoodItemSO>();
public override void Interact(Player player)
{
base.Interact(player);
if (player.IsHoldContainerItem())
protected override void InteractAction()
{
if (_player.IsHoldContainerItem())
{
var playerContainer = player.GetContainerItem();
var playerContainer = _player.GetContainerItem();
if (!playerContainer.IsSalebleItems())
{
if (_foodObjects.Count + playerContainer.GetItems().Count <= _containerSO.MaxCapacity)
@@ -27,7 +26,7 @@ public class Fridge : BaseInteractableObject
_foodObjects.Add(item);
}
player.ClearContainerItem();
_player.ClearContainerItem();
Debug.Log($"Fridge have {_foodObjects.Count} pices of food");
}
else
@@ -39,7 +38,7 @@ public class Fridge : BaseInteractableObject
//Eat menu
if (_foodObjects.Count > 0)
{
var hunger = player.Stats[StatsId.Food].MaxValue - player.Stats[StatsId.Food].Value;
var hunger = _player.Stats[StatsId.Food].MaxValue - _player.Stats[StatsId.Food].Value;
var eatingItems = _foodObjects.Count < (hunger / 10) ? _foodObjects.Count : (hunger / 10);
StartCoroutine(EatRoutine(eatingItems));
_foodObjects.RemoveRange(0, (int)eatingItems);
@@ -7,9 +7,8 @@ public class OfficeTable : BaseInteractableObject
[SerializeField]
private JobsListSO _jobPositionsSO;
public override void Interact(Player player)
protected override void InteractAction()
{
base.Interact(player);
UIManager.Instance.ShowJobSelectionDialog("Job agency", null, OnConfirm);
}
@@ -7,10 +7,9 @@ public class ShopingContainer : BaseInteractableObject
[SerializeField]
//private ItemActionsUI _actionsMenu;
public override void Interact(Player player)
protected override void InteractAction()
{
base.Interact(player);
if (!player.IsHoldContainerItem())
if (!_player.IsHoldContainerItem())
{
var transform = Instantiate(_containerSO.prefab, _interactionPoint);
var containerItem = transform.GetComponent<ContainerItem>();
@@ -19,9 +18,7 @@ public class ShopingContainer : BaseInteractableObject
Debug.LogError("Container Item is null");
return;
}
player.SetContainerItem(containerItem);
_player.SetContainerItem(containerItem);
}
}
}
@@ -6,16 +6,14 @@ public class StoreContainer : BaseInteractableObject
[SerializeField]
private SellableItemSO _sellableItemSO;
public override void Interact(Player player)
protected override void InteractAction()
{
base.Interact(player);
if (player.IsHoldContainerItem())
if (_player.IsHoldContainerItem())
{
var clone = Instantiate(_sellableItemSO);
var container = player.GetContainerItem();
var container = _player.GetContainerItem();
container.AddItem(clone);
}
}
}
+24 -21
View File
@@ -24,7 +24,7 @@ public class Player : MonoBehaviour
public Dictionary<StatsId, Stat> Stats;
public JobPositions JobPosition { get; set; }
public EducationSkill Education { get; set; }
public EducationSkill Education { get; set; }
private readonly Queue<PlayerTasks> _tasks = new Queue<PlayerTasks>();
private PlayerTasks _currentTask;
@@ -56,10 +56,10 @@ public class Player : MonoBehaviour
TimeManager.Instance.OnMinuteChanged += UpdateStatsByClock;
_animator.applyRootMotion = true;
_navAgent.updatePosition = false;
_currentActing = PlayerStates.Awake;
}
private void OnDestroy()
@@ -105,18 +105,21 @@ public class Player : MonoBehaviour
_currentTask.UpdateStatus(MoveToPoint());
break;
case Tasks.Interact:
// Show interaction menu
_currentTask.TagretObject.SpawnMenu();
_currentTask = null;
//if (IsPathComplete(_currentTask.TagretObject._interactionPoint.position))
// _currentTask.UpdateStatus(InteractWithObject(_currentTask.TagretObject));
//else
//{
// AddTask(new PlayerTasks(Tasks.Move, _currentTask.TagretObject));
// AddTask(new PlayerTasks(Tasks.Rotate, _currentTask.TagretObject));
// AddTask(_currentTask);
// _currentTask = null;
//}
var result = _currentTask.TagretObject.Interact(this);
switch (result)
{
case InteractionStatus.FarFromPlayer:
AddTask(new PlayerTasks(Tasks.Move, _currentTask.TagretObject));
AddTask(new PlayerTasks(Tasks.Rotate, _currentTask.TagretObject));
AddTask(_currentTask);
_currentTask = null;
break;
case InteractionStatus.Complete:
_currentTask.UpdateStatus(TaskStatus.Complete);
break;
default:
break;
}
break;
}
}
@@ -137,7 +140,7 @@ public class Player : MonoBehaviour
return IsPathComplete(_navAgent.destination) ? TaskStatus.Complete : TaskStatus.InProgress;
}
private bool IsPathComplete(Vector3 destination)
public bool IsPathComplete(Vector3 destination)
{
var dest = new Vector3(destination.x, 0, destination.z);
var pos = new Vector3(_navAgent.transform.position.x, 0, _navAgent.transform.position.z);
@@ -174,11 +177,11 @@ public class Player : MonoBehaviour
return Mathf.Abs(Quaternion.Dot(q1, q2)) >= 1 - precision;
}
private TaskStatus InteractWithObject(BaseInteractableObject interactableObject)
{
interactableObject.Interact(this);
return TaskStatus.Complete;
}
//private TaskStatus InteractWithObject(BaseInteractableObject interactableObject)
//{
// interactableObject.Interact(this);
// return TaskStatus.Complete;
//}
public void SetPlayerAnimation(AnimationStates newState, Action onAnimationFinish)
{
+20 -1
View File
@@ -1,3 +1,5 @@
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
@@ -6,10 +8,27 @@ public class PopupItemMenu : MonoBehaviour
[SerializeField]
private Button _popupMenuItemButtonPrefab;
public void ShowButtons()
public void ShowButtons(Action menuButtonClick)
{
var button=Instantiate(_popupMenuItemButtonPrefab) as Button;
button.transform.SetParent(transform,false);
button.transform.localPosition = new Vector3(0, 100f, 0);
button.onClick.AddListener(() =>
{
menuButtonClick?.Invoke();
Hide();
CloseDialog();
});
}
private void CloseDialog()
{
UIManager.Instance.Unfreeze();
Destroy(this);
}
private void Hide()
{
gameObject.SetActive(false);
}
}
+2 -2
View File
@@ -40,12 +40,12 @@ public class UIManager : MonoBehaviour
jobSelector.ShowJobSelectionDialog(title, onCancel, onConfirm);
}
public void ShowItemsMenu()
public void ShowItemsMenu(Action itemsMenuCallback)
{
var popupMenu = Instantiate(_itemPopupMenuPrefab) as PopupItemMenu;
popupMenu.transform.transform.SetParent(transform, false);
popupMenu.transform.position = Input.mousePosition;
popupMenu.ShowButtons();
popupMenu.ShowButtons(itemsMenuCallback);
}
public void Freeze()