Refactor code

This commit is contained in:
Vladimir Koshevarov
2023-02-28 16:19:40 +02:00
parent dad45af7a9
commit b71fa6a9eb
13 changed files with 65 additions and 71 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ namespace Assets.Scripts.Actions
public void ApplyAction(Player playerController) public void ApplyAction(Player playerController)
{ {
playerController.PlayerStats[StatsId.Food].increase(_energy); playerController.Stats[StatsId.Food].increase(_energy);
} }
} }
} }
+1 -1
View File
@@ -14,7 +14,7 @@ namespace Assets.Scripts.Actions
public void ApplyAction(Player playerController) public void ApplyAction(Player playerController)
{ {
playerController.PlayerStats[StatsId.Energy].increase(_energyPerTick); playerController.Stats[StatsId.Energy].increase(_energyPerTick);
} }
} }
} }
@@ -59,7 +59,7 @@ public class ConversationController : MonoBehaviour
_selectedAction = action; _selectedAction = action;
if (action is ISellable) if (action is ISellable)
{ {
_btnApply.interactable = Player.Instance.PlayerStats[StatsId.Money].Value >= (action as ISellable).Price; _btnApply.interactable = Player.Instance.Stats[StatsId.Money].Value >= (action as ISellable).Price;
} }
} }
+3 -3
View File
@@ -30,7 +30,7 @@ public class GameUIManager : MonoBehaviour
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
_moneyText.text = $"${Player.Instance.PlayerStats[StatsId.Money].Value}"; _moneyText.text = $"${Player.Instance.Stats[StatsId.Money].Value}";
} }
@@ -42,7 +42,7 @@ public class GameUIManager : MonoBehaviour
_timeText.text = TimeManager.CurrentTime.ToString(@"hh\:mm"); _timeText.text = TimeManager.CurrentTime.ToString(@"hh\:mm");
} }
_energy.value = (float)Player.Instance.PlayerStats[StatsId.Energy].Value; _energy.value = (float)Player.Instance.Stats[StatsId.Energy].Value;
_food.value = (float)Player.Instance.PlayerStats[StatsId.Food].Value; _food.value = (float)Player.Instance.Stats[StatsId.Food].Value;
} }
} }
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 949ddc438e1560f4193667beb4e04308
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -7,7 +7,6 @@ using System.Runtime.Serialization;
using UnityEngine; using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
public class BlockingAnimation : Attribute public class BlockingAnimation : Attribute
{ {
@@ -43,7 +42,7 @@ public class Player : MonoBehaviour
private AnimationStates _currentAnimation; private AnimationStates _currentAnimation;
private Vector3 _groundDeltaPosition; private Vector3 _groundDeltaPosition;
public Dictionary<StatsId, Stat> PlayerStats; public Dictionary<StatsId, Stat> Stats;
public IWorkPlace WorkPlace { get; set; } public IWorkPlace WorkPlace { get; set; }
private readonly Queue<PlayerTasks> _tasks = new Queue<PlayerTasks>(); private readonly Queue<PlayerTasks> _tasks = new Queue<PlayerTasks>();
@@ -51,7 +50,6 @@ public class Player : MonoBehaviour
private const string WALK_VELOCITY = "WalkVelocity"; private const string WALK_VELOCITY = "WalkVelocity";
private bool _isInAnimation = false;
private void Awake() private void Awake()
{ {
if (Instance != null) if (Instance != null)
@@ -66,7 +64,7 @@ public class Player : MonoBehaviour
TimeManager.OnMinuteChanged += UpdateStatsByClock; TimeManager.OnMinuteChanged += UpdateStatsByClock;
_isAllowMovement = true; _isAllowMovement = true;
_navAgent.updatePosition = false; _navAgent.updatePosition = false;
PlayerStats = GameManager.Instance.PlayerStats; Stats = PlayerStats.CreateInitialStats();
} }
private void OnDestroy() private void OnDestroy()
@@ -197,21 +195,21 @@ public class Player : MonoBehaviour
public void UpdateStatsByClock() public void UpdateStatsByClock()
{ {
PlayerStats[StatsId.Food].deduct(0.034m); // 48 hours it's 100, 100/2880=~0.034 per minute Stats[StatsId.Food].deduct(0.034m); // 48 hours it's 100, 100/2880=~0.034 per minute
if (_currentAnimation != AnimationStates.Sleeping) if (_currentAnimation != AnimationStates.Sleeping)
{ {
PlayerStats[StatsId.Energy].deduct(0.1m); // 24 hours it's 100, 100/1440=~0.096 per minute Stats[StatsId.Energy].deduct(0.1m); // 24 hours it's 100, 100/1440=~0.096 per minute
} }
else else
{ {
PlayerStats[StatsId.Energy].increase(1m); Stats[StatsId.Energy].increase(1m);
} }
} }
public void BuyAction(IPlayerAction action) public void BuyAction(IPlayerAction action)
{ {
PlayerStats[StatsId.Money].deduct(((ISellable)action).Price); Stats[StatsId.Money].deduct(((ISellable)action).Price);
action.ApplyAction(this); action.ApplyAction(this);
} }
@@ -1,27 +1,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine;
public enum StatsId { Money, RentAccount, Food, Energy, BankAccount, Job, } public enum StatsId { Money, RentAccount, Food, Energy, BankAccount, Job, }
public class GameManager : MonoBehaviour public class PlayerStats
{ {
public static GameManager Instance { get; private set; } public static Dictionary<StatsId, Stat> CreateInitialStats()
public Dictionary<StatsId, Stat> PlayerStats;
// Start is called before the first frame update
void Awake()
{ {
Instance = this; return new Dictionary<StatsId, Stat>()
InitPlayerStats();
}
private void InitPlayerStats()
{
PlayerStats = new Dictionary<StatsId, Stat>()
{ {
{StatsId.Money, new Stat("Money", 100.0m)}, {StatsId.Money, new Stat("Money", 100.0m)},
{StatsId.RentAccount, new Stat("Rent Account", 0)}, {StatsId.RentAccount, new Stat("Rent Account", 0)},
{StatsId.Food, new Stat("Food Energy", 100) }, {StatsId.Food, new Stat("Food Energy", 50) },
{StatsId.Energy,new Stat("Energy", 100) }, {StatsId.Energy,new Stat("Energy", 100) },
{StatsId.BankAccount,new Stat("Bank Account", 0) }, {StatsId.BankAccount,new Stat("Bank Account", 0) },
{StatsId.Job, new Stat("Unemployed", 0) }, {StatsId.Job, new Stat("Unemployed", 0) },
@@ -1,41 +1,41 @@
public class Stat public class Stat
{ {
public string Name { get; set; } public string Name { get; set; }
public decimal Value { get; set; } public decimal Value { get; set; }
public decimal Price { get; set; } public decimal Price { get; set; }
public decimal Quantity { get; set; } public decimal Quantity { get; set; }
public Stat(string name, decimal startValue) public Stat(string name, decimal startValue)
{ {
Name = name; Name = name;
Value = startValue; Value = startValue;
} }
public Stat(string name, decimal price, decimal quantity) public Stat(string name, decimal price, decimal quantity)
{ {
Name = name; Name = name;
Price = price; Price = price;
Quantity = quantity; Quantity = quantity;
} }
public void increase(decimal byAmount) public void increase(decimal byAmount)
{ {
Value += byAmount; Value += byAmount;
} }
public bool deduct(decimal amount) public bool deduct(decimal amount)
{ {
if (Value >= amount) if (Value >= amount)
{ {
Value -= amount; Value -= amount;
return true; return true;
} }
return false; return false;
} }
public void forceDeduct(decimal amount) public void forceDeduct(decimal amount)
{ {
Value -= amount; Value -= amount;
} }
} }