From dbd1d16a245da68b79222fdd9ed4a7df3a068141 Mon Sep 17 00:00:00 2001 From: Vladimir Koshevarov Date: Tue, 14 Mar 2023 13:36:24 +0200 Subject: [PATCH] sleep increase the energy --- Assets/Scenes/CheapHouse.unity | 20 +++---- Assets/Scripts/Actions/JobPosition.cs | 1 - Assets/Scripts/Actions/Relax.cs | 20 ------- Assets/Scripts/Enums.cs | 2 + Assets/Scripts/InteractableObjects/Bed.cs | 2 + Assets/Scripts/Player/Player.cs | 57 +++++-------------- Assets/Scripts/Player/PlayerHelper.cs | 28 +++++++++ .../PlayerHelper.cs.meta} | 2 +- Assets/Scripts/Player/PlayerStats.cs | 4 +- 9 files changed, 60 insertions(+), 76 deletions(-) delete mode 100644 Assets/Scripts/Actions/Relax.cs create mode 100644 Assets/Scripts/Player/PlayerHelper.cs rename Assets/Scripts/{Actions/Relax.cs.meta => Player/PlayerHelper.cs.meta} (83%) diff --git a/Assets/Scenes/CheapHouse.unity b/Assets/Scenes/CheapHouse.unity index 6c21962f..7b30234d 100644 --- a/Assets/Scenes/CheapHouse.unity +++ b/Assets/Scenes/CheapHouse.unity @@ -1863,7 +1863,7 @@ PrefabInstance: - target: {fileID: 808129671576928264, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_AnchoredPosition.x - value: 20 + value: 0 objectReference: {fileID: 0} - target: {fileID: 808129671576928264, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} @@ -1888,7 +1888,7 @@ PrefabInstance: - target: {fileID: 896272491191485165, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_AnchoredPosition.x - value: 182.005 + value: 0 objectReference: {fileID: 0} - target: {fileID: 896272491191485165, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} @@ -2013,22 +2013,22 @@ PrefabInstance: - target: {fileID: 4307980429506515175, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_AnchorMax.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 4307980429506515175, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_AnchorMin.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 4307980429506515175, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_AnchoredPosition.x - value: 250 + value: 0 objectReference: {fileID: 0} - target: {fileID: 4307980429506515175, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_AnchoredPosition.y - value: -90 + value: 0 objectReference: {fileID: 0} - target: {fileID: 4537241548631189332, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} @@ -2123,7 +2123,7 @@ PrefabInstance: - target: {fileID: 6551138445549304010, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_AnchoredPosition.x - value: 182.005 + value: 0 objectReference: {fileID: 0} - target: {fileID: 6551138445549304010, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} @@ -2248,7 +2248,7 @@ PrefabInstance: - target: {fileID: 7883991513978601488, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_AnchoredPosition.x - value: 182.005 + value: 0 objectReference: {fileID: 0} - target: {fileID: 7883991513978601488, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} @@ -2268,12 +2268,12 @@ PrefabInstance: - target: {fileID: 7949779033593405736, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_SizeDelta.x - value: 364.01 + value: 0 objectReference: {fileID: 0} - target: {fileID: 7949779033593405736, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} propertyPath: m_AnchoredPosition.x - value: 182.005 + value: 0 objectReference: {fileID: 0} - target: {fileID: 7949779033593405736, guid: 8939548f67eb43c4fa321f112c45f83b, type: 3} diff --git a/Assets/Scripts/Actions/JobPosition.cs b/Assets/Scripts/Actions/JobPosition.cs index 65369506..bd748258 100644 --- a/Assets/Scripts/Actions/JobPosition.cs +++ b/Assets/Scripts/Actions/JobPosition.cs @@ -18,7 +18,6 @@ namespace Assets.Scripts.Actions } public void ApplyAction(Player playerController) { - playerController.WorkPlace = this; } } } \ No newline at end of file diff --git a/Assets/Scripts/Actions/Relax.cs b/Assets/Scripts/Actions/Relax.cs deleted file mode 100644 index 053bcbd1..00000000 --- a/Assets/Scripts/Actions/Relax.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Assets.Scripts.Actions.Interfaces; - -namespace Assets.Scripts.Actions -{ - public class Relax : IPlayerAction - { - private int _energyPerTick; - public Relax(int duration, int energyPerTick) - { - _energyPerTick = energyPerTick; - } - - public string Description => throw new System.NotImplementedException(); - - public void ApplyAction(Player playerController) - { - playerController.Stats[StatsId.Energy].increase(_energyPerTick); - } - } -} diff --git a/Assets/Scripts/Enums.cs b/Assets/Scripts/Enums.cs index 48ad6df7..e711423c 100644 --- a/Assets/Scripts/Enums.cs +++ b/Assets/Scripts/Enums.cs @@ -1,5 +1,7 @@ using System.Runtime.Serialization; + +public enum PlayerStates { Awake, Sleeping, Eating } public enum StatsId { Money, RentAccount, Food, Energy, BankAccount, Job, } public enum Tasks { Move, Interact, Rotate }; public enum TaskStatus { Waiting, InProgress, Complete }; diff --git a/Assets/Scripts/InteractableObjects/Bed.cs b/Assets/Scripts/InteractableObjects/Bed.cs index 1b3048cd..8c0f2c29 100644 --- a/Assets/Scripts/InteractableObjects/Bed.cs +++ b/Assets/Scripts/InteractableObjects/Bed.cs @@ -12,11 +12,13 @@ public class Bed : BaseInteractableObject private void OnAnimationFinished() { + _player.SetPlayerActing(PlayerStates.Sleeping); TimeManager.Instance.FastForward(TimeSpan.FromHours(8), OnFastForwardEnd); } private void OnFastForwardEnd() { + _player.SetPlayerActing(PlayerStates.Awake); _player.SetPlayerAnimation(AnimationStates.Standing); } } diff --git a/Assets/Scripts/Player/Player.cs b/Assets/Scripts/Player/Player.cs index 215b1a01..09192575 100644 --- a/Assets/Scripts/Player/Player.cs +++ b/Assets/Scripts/Player/Player.cs @@ -1,9 +1,5 @@ -using Assets.Scripts.Actions.Interfaces; using System; using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Runtime.Serialization; using UnityEngine; using UnityEngine.AI; @@ -21,10 +17,10 @@ public class Player : MonoBehaviour [SerializeField] private Transform _holdPoint; + private PlayerStates _currentActing; private AnimationStates _currentAnimation; public Dictionary Stats; - public IWorkPlace WorkPlace { get; set; } private readonly Queue _tasks = new Queue(); private PlayerTasks _currentTask; @@ -47,12 +43,14 @@ public class Player : MonoBehaviour Instance = this; DontDestroyOnLoad(gameObject); } + private void Start() { TimeManager.OnMinuteChanged += UpdateStatsByClock; _animator.applyRootMotion = true; _navAgent.updatePosition = false; Stats = PlayerStats.CreateInitialStats(); + _currentActing = PlayerStates.Awake; } private void OnDestroy() @@ -62,7 +60,7 @@ public class Player : MonoBehaviour private void Update() { - if (IsBlockingAnimation(_currentAnimation)) + if (PlayerHelper.IsBlockingAnimation(_currentAnimation)) { if (IsAnimationStatePlaying(0)) { @@ -116,7 +114,6 @@ public class Player : MonoBehaviour { _navAgent.Warp(desiredPosition); _navAgent.updatePosition = false; - } private TaskStatus MoveToPoint() @@ -176,20 +173,20 @@ public class Player : MonoBehaviour _OnAnimationFinish = onAnimationFinish; SetPlayerAnimation(newState); } + public void SetPlayerAnimation(AnimationStates newState) { if (newState == _currentAnimation) { return; } - _animator.Play(GetEnumMemberValue(newState)); + _animator.Play(PlayerHelper.GetEnumMemberValue(newState)); _currentAnimation = newState; } - public bool IsAnimationStatePlaying(int animLayer) { - string stateName = GetEnumMemberValue(_currentAnimation); + string stateName = PlayerHelper.GetEnumMemberValue(_currentAnimation); var stateInfo = _animator.GetCurrentAnimatorStateInfo(animLayer); return stateInfo.IsName(stateName) && stateInfo.normalizedTime < 1.0f; @@ -200,17 +197,21 @@ public class Player : MonoBehaviour _tasks.Enqueue(task); } + public void SetPlayerActing(PlayerStates state) + { + _currentActing = state; + } + public void UpdateStatsByClock() { Stats[StatsId.Food].deduct(0.034f); // 48 hours it's 100, 100/2880=~0.034 per minute - if (_currentAnimation != AnimationStates.Sleeping) + if (_currentActing != PlayerStates.Sleeping) { Stats[StatsId.Energy].deduct(0.1f); // 24 hours it's 100, 100/1440=~0.096 per minute } else { - Stats[StatsId.Energy].increase(1f); - + Stats[StatsId.Energy].increase(0.2f); } } @@ -218,41 +219,13 @@ public class Player : MonoBehaviour { Stats[StatsId.Money].deduct(amount); } - - public float Eat() - { - Stats[StatsId.Food].increase(10); - return Stats[StatsId.Food].Value; - } - - private static string GetEnumMemberValue(T value) - where T : struct, IConvertible - { - return typeof(T) - .GetTypeInfo() - .DeclaredMembers - .SingleOrDefault(x => x.Name == value.ToString()) - ?.GetCustomAttribute(false) - ?.Value; - } - - private static bool IsBlockingAnimation(T value) - where T : struct, IConvertible - { - var enumType = typeof(T); - var memInfo = enumType.GetMember(value.ToString()); - var attr = memInfo.FirstOrDefault()?.GetCustomAttributes(false).OfType().FirstOrDefault(); - return attr != null; - } - public void SetContainerItem(ContainerItem containerItem) { containerItem.transform.parent = _holdPoint; containerItem.transform.localPosition = Vector3.zero; _containerItem = containerItem; - - } + public ContainerItem GetContainerItem() { return _containerItem; diff --git a/Assets/Scripts/Player/PlayerHelper.cs b/Assets/Scripts/Player/PlayerHelper.cs new file mode 100644 index 00000000..aa24b8cd --- /dev/null +++ b/Assets/Scripts/Player/PlayerHelper.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization; + +public static class PlayerHelper +{ + + public static string GetEnumMemberValue(T value) + where T : struct, IConvertible + { + return typeof(T) + .GetTypeInfo() + .DeclaredMembers + .SingleOrDefault(x => x.Name == value.ToString()) + ?.GetCustomAttribute(false) + ?.Value; + } + + public static bool IsBlockingAnimation(T value) + where T : struct, IConvertible + { + var enumType = typeof(T); + var memInfo = enumType.GetMember(value.ToString()); + var attr = memInfo.FirstOrDefault()?.GetCustomAttributes(false).OfType().FirstOrDefault(); + return attr != null; + } +} diff --git a/Assets/Scripts/Actions/Relax.cs.meta b/Assets/Scripts/Player/PlayerHelper.cs.meta similarity index 83% rename from Assets/Scripts/Actions/Relax.cs.meta rename to Assets/Scripts/Player/PlayerHelper.cs.meta index 46ad0b30..6dd68f95 100644 --- a/Assets/Scripts/Actions/Relax.cs.meta +++ b/Assets/Scripts/Player/PlayerHelper.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 986e2b29060766347a43dc46e30a648e +guid: 1e0c9736e301f4f4d89a127e9efde6c8 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/Player/PlayerStats.cs b/Assets/Scripts/Player/PlayerStats.cs index 6294002c..3823198d 100644 --- a/Assets/Scripts/Player/PlayerStats.cs +++ b/Assets/Scripts/Player/PlayerStats.cs @@ -9,8 +9,8 @@ public class PlayerStats { {StatsId.Money, new Stat("Money", 100.0f,10000000f)}, {StatsId.RentAccount, new Stat("Rent Account", 0,10f)}, - {StatsId.Food, new Stat("Food Energy", 50,100f) }, - {StatsId.Energy,new Stat("Energy", 100,100f) }, + {StatsId.Food, new Stat("Food Energy", 50f,100f) }, + {StatsId.Energy,new Stat("Energy", 50f,100f) }, //{StatsId.BankAccount,new Stat("Bank Account", 0) }, {StatsId.Job, new Stat("Unemployed", 0, 100f) }, };