Fix shop System

add food indicator
This commit is contained in:
Vladimir Koshevarov
2022-11-17 17:24:00 +02:00
parent be3161d7e4
commit c8047bd170
16 changed files with 663 additions and 286 deletions
+15 -15
View File
@@ -1,16 +1,16 @@
namespace Assets.Scripts.Actions
{
public abstract class BaseAction
{
protected int DurationInTicks { get; }
protected int ElapsedTicks { get; }
protected BaseAction(int durationTicks)
{
DurationInTicks = durationTicks;
}
public abstract void ApplyAction(PlayerController playerController);
}
namespace Assets.Scripts.Actions
{
public abstract class BaseAction
{
protected int DurationInTicks { get; }
protected int ElapsedTicks { get; }
protected BaseAction(int durationTicks)
{
DurationInTicks = durationTicks;
}
public abstract void ApplyAction(PlayerManager playerController);
}
}
+5 -5
View File
@@ -5,17 +5,17 @@ namespace Assets.Scripts.Actions
public class Eat : BaseAction, ISellableItem
{
public float Price { get; private set; }
private int _energyPerTick;
public Eat(int duration, int energyPerTick, float price) : base(duration)
private int _energy;
public Eat(int duration, int energy, float price) : base(duration)
{
Price = price;
_energyPerTick = energyPerTick;
_energy = energy;
}
public override void ApplyAction(PlayerController playerController)
public override void ApplyAction(PlayerManager playerController)
{
playerController.foodEnergy.increase(_energyPerTick);
playerController.food.increase(_energy);
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
{
_energyPerTick = energyPerTick;
}
public override void ApplyAction(PlayerController playerController)
public override void ApplyAction(PlayerManager playerController)
{
playerController.energy.increase(_energyPerTick);
}
+17 -17
View File
@@ -1,17 +1,17 @@
namespace Assets.Scripts.Actions
{
public class Work : BaseAction
{
private PlayerController playerController;
private int energyPerTick;
public Work(PlayerController player, int duration, int energyPerTick) : base(duration)
{
this.playerController = player;
this.energyPerTick = energyPerTick;
}
public override void ApplyAction(PlayerController playerController)
{
throw new System.NotImplementedException();
}
}
}
namespace Assets.Scripts.Actions
{
public class Work : BaseAction
{
private PlayerManager playerController;
private int energyPerTick;
public Work(PlayerManager player, int duration, int energyPerTick) : base(duration)
{
this.playerController = player;
this.energyPerTick = energyPerTick;
}
public override void ApplyAction(PlayerManager playerController)
{
throw new System.NotImplementedException();
}
}
}
+23 -23
View File
@@ -1,23 +1,23 @@
using Assets.Scripts.Actions;
namespace Assets.Scripts.Buildings
{
public class Burger : BaseCell
{
protected override void Initialize()
{
_name = "Monolith burger";
}
protected override void BuildOptionsList()
{
_optionsList.Add("Hamburgers - 83$", new Eat(6, 1, 83));
_optionsList.Add("Cheesburger - 94$", new Eat(6, 1, 94));
_optionsList.Add("Astro chicken - 131$", new Eat(6, 1, 131));
_optionsList.Add("Fries - 68$", new Eat(6, 1, 68));
_optionsList.Add("Shakes - 108$", new Eat(6, 1, 108));
_optionsList.Add("Colas - 73$", new Eat(6, 1, 73));
}
}
}
using Assets.Scripts.Actions;
namespace Assets.Scripts.Buildings
{
public class Burger : BaseCell
{
protected override void Initialize()
{
_name = "Monolith burger";
}
protected override void BuildOptionsList()
{
_optionsList.Add("Hamburgers - 83$", new Eat(6, 10, 83));
_optionsList.Add("Cheesburger - 94$", new Eat(6, 1, 94));
_optionsList.Add("Astro chicken - 131$", new Eat(6, 1, 131));
_optionsList.Add("Fries - 68$", new Eat(6, 1, 68));
_optionsList.Add("Shakes - 108$", new Eat(6, 1, 108));
_optionsList.Add("Colas - 73$", new Eat(6, 1, 73));
}
}
}
-47
View File
@@ -1,47 +0,0 @@
using UnityEngine;
using UnityEngine.AI;
public class CharacterMovement : MonoBehaviour
{
public NavMeshAgent player;
public Animator playerAnimator;
public Camera playerCamera;
public ParticleSystem targetDest;
public bool allowMovement=true;
void Start()
{
allowMovement = true;
}
// Update is called once per frame
void Update()
{
if (allowMovement)
{
if (Input.GetMouseButton(0))
{
Ray myRay = playerCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myRay, out RaycastHit hit))
{
targetDest.transform.position = hit.point;
targetDest.Play();
player.SetDestination(hit.point);
}
}
}
else
{
player.SetDestination(player.transform.position);
player.velocity=Vector3.zero;
}
if(player.velocity!=Vector3.zero)
{
playerAnimator.SetBool("IsWalking",true);
}
else if (player.velocity == Vector3.zero)
{
playerAnimator.SetBool("IsWalking", false);
}
}
}
-11
View File
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: e34d78101f2c2b842864fce206379a7c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,61 +1,46 @@
using Assets.Scripts.Actions;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
[System.Serializable]
public class ConversationChangeEvent : UnityEvent<BaseAction> { }
public class ChoiceController : MonoBehaviour
{
public PlayerController playerController;
public KeyValuePair<string, BaseAction> _option;
public ConversationChangeEvent conversationChangeEvent;
// Update is called once per frame
public static ChoiceController AddChoiceButton(Button choiceButtonTemplate, KeyValuePair<string, BaseAction> option, int index)
{
int buttonSpacing = -50;
Button button = Instantiate(choiceButtonTemplate);
button.transform.SetParent(choiceButtonTemplate.transform.parent);
button.transform.localScale = Vector3.one;
button.transform.localPosition = choiceButtonTemplate.transform.localPosition + new Vector3(0, index * buttonSpacing, 0);
button.name = option.Key;
button.gameObject.SetActive(true);
ChoiceController choiceController = button.GetComponent<ChoiceController>();
choiceController._option = option;
return choiceController;
}
private void Start()
{
if (conversationChangeEvent == null)
conversationChangeEvent = new ConversationChangeEvent();
var btn = GetComponent<Button>();
var txt = btn.GetComponentInChildren<TextMeshProUGUI>();
txt.text = _option.Key;
}
public void MakeChoice()
{
if (playerController.TryBuyAction(_option.Value))
{
conversationChangeEvent.Invoke(_option.Value);
}
else
{
YesNoDialogUI.Instance.ShowQuestion("not enouth money",
() =>
{
},
() =>
{
//do nothing
});
}
}
}
using Assets.Scripts.Actions;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
[System.Serializable]
public class ConversationChangeEvent : UnityEvent<BaseAction> { }
public class ChoiceController : MonoBehaviour
{
public KeyValuePair<string, BaseAction> _option;
public ConversationChangeEvent conversationChangeEvent;
// Update is called once per frame
public static ChoiceController AddChoiceButton(Button choiceButtonTemplate, KeyValuePair<string, BaseAction> option, int index)
{
int buttonSpacing = -50;
Button button = Instantiate(choiceButtonTemplate);
button.transform.SetParent(choiceButtonTemplate.transform.parent);
button.transform.localScale = Vector3.one;
button.transform.localPosition = choiceButtonTemplate.transform.localPosition + new Vector3(0, index * buttonSpacing, 0);
button.name = option.Key;
button.gameObject.SetActive(true);
ChoiceController choiceController = button.GetComponent<ChoiceController>();
choiceController._option = option;
return choiceController;
}
private void Start()
{
if (conversationChangeEvent == null)
conversationChangeEvent = new ConversationChangeEvent();
var btn = GetComponent<Button>();
var txt = btn.GetComponentInChildren<TextMeshProUGUI>();
txt.text = _option.Key;
}
public void MakeChoice()
{
conversationChangeEvent.Invoke(_option.Value);
}
}
@@ -1,67 +1,85 @@
using Assets.Scripts.Actions;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ConversationController : MonoBehaviour
{
[SerializeField]
public TextMeshProUGUI _title;
[SerializeField]
public Button _choiceButton;
[SerializeField]
using Assets.Scripts.Actions;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ConversationController : MonoBehaviour
{
[SerializeField]
public TextMeshProUGUI _title;
[SerializeField]
public Button _choiceButton;
[SerializeField]
private Button _closeBtn;
[SerializeField]
CharacterMovement _player;
public ConversationChangeEvent conversationChangeEvent;
private List<ChoiceController> choiceControllers = new();
private void Awake()
{
Hide();
}
public void Change(string title, Dictionary<string, BaseAction> options)
[SerializeField]
PlayerManager _playerManager;
public ConversationChangeEvent conversationChangeEvent;
private List<ChoiceController> choiceControllers = new();
private void Awake()
{
Hide();
}
public void Change(string title, Dictionary<string, BaseAction> options)
{
_closeBtn.onClick.AddListener(() =>
{
Hide();
});
_player.allowMovement = false;
RemoveChoices();
_title.text = title;
gameObject.SetActive(true);
for (var count = 0; count < options.Count; count++)
{
ChoiceController c = ChoiceController.AddChoiceButton(_choiceButton, options.ElementAt(count), count);
choiceControllers.Add(c);
}
StopAllCoroutines();
Time.timeScale = 0;
_choiceButton.gameObject.SetActive(false);
}
private void RemoveChoices()
{
foreach (ChoiceController c in choiceControllers)
Destroy(c.gameObject);
choiceControllers.Clear();
}
public void Hide()
{
RemoveChoices();
gameObject.SetActive(false);
Time.timeScale = 1;
_player.allowMovement = true;
}
}
});
_playerManager.allowMovement = false;
RemoveChoices();
_title.text = title;
gameObject.SetActive(true);
for (var count = 0; count < options.Count; count++)
{
ChoiceController c = ChoiceController.AddChoiceButton(_choiceButton, options.ElementAt(count), count);
c.conversationChangeEvent.AddListener(ApplyActionOnPlayer);
choiceControllers.Add(c);
}
StopAllCoroutines();
Time.timeScale = 0;
_choiceButton.gameObject.SetActive(false);
}
private void ApplyActionOnPlayer(BaseAction action)
{
if (!_playerManager.TryBuyAction(action))
{
YesNoDialogUI.Instance.ShowQuestion("not enouth money",
() =>
{
},
() =>
{
//do nothing
});
}
}
private void RemoveChoices()
{
foreach (ChoiceController c in choiceControllers)
{
c.conversationChangeEvent.RemoveAllListeners();
Destroy(c.gameObject);
}
choiceControllers.Clear();
}
public void Hide()
{
RemoveChoices();
gameObject.SetActive(false);
Time.timeScale = 1;
_playerManager.allowMovement = true;
}
}
+20 -20
View File
@@ -1,20 +1,20 @@
using UnityEngine;
public class GameController : MonoBehaviour
{
public Map GameMap;
public PlayerController Player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
using UnityEngine;
public class GameController : MonoBehaviour
{
public Map GameMap;
public PlayerManager Player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
@@ -2,10 +2,10 @@ using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class GameUIController : MonoBehaviour
public class GameUIManager : MonoBehaviour
{
[SerializeField]
private PlayerController _playerController;
private PlayerManager _playerController;
[SerializeField]
private TextMeshProUGUI _timeText;
@@ -16,6 +16,9 @@ public class GameUIController : MonoBehaviour
[SerializeField]
public Slider _energy;
[SerializeField]
public Slider _food;
// Start is called before the first frame update
private void OnEnable()
{
@@ -31,7 +34,7 @@ public class GameUIController : MonoBehaviour
void Update()
{
_moneyText.text = $"${_playerController.money.Value}";
_energy.value = _playerController.energy.Value;
}
@@ -41,5 +44,8 @@ public class GameUIController : MonoBehaviour
{
_timeText.text = TimeManager.CurrentTime.ToString(@"hh\:mm");
}
_energy.value = _playerController.energy.Value;
_food.value = _playerController.food.Value;
}
}
@@ -1,12 +1,13 @@
using Assets.Scripts.Actions;
using Assets.Scripts.Actions.Interfaces;
using UnityEngine;
using UnityEngine.AI;
public class PlayerController : MonoBehaviour
public class PlayerManager : MonoBehaviour
{
public Stat money = new Stat("Money", 100.0f);
public Stat money = new Stat("Money", 1000.0f);
public Stat rentAccount = new Stat("Rent Account", 0);
public Stat foodEnergy = new Stat("Food Energy", 0);
public Stat food = new Stat("Food Energy", 100);
public Stat energy = new Stat("Energy", 100);
// bank
@@ -40,6 +41,12 @@ public class PlayerController : MonoBehaviour
public Stat tvItem = new Stat("Tv Item", 1500, 1);
public Stat laptopItem = new Stat("Laptop Item", 3000, 1);
public NavMeshAgent player;
public Animator playerAnimator;
public Camera playerCamera;
public ParticleSystem targetDest;
public bool allowMovement = true;
private void OnEnable()
{
TimeManager.OnMinuteChanged += DecreaseEnergy;
@@ -50,17 +57,46 @@ public class PlayerController : MonoBehaviour
TimeManager.OnMinuteChanged -= DecreaseEnergy;
}
// Start is called before the first frame update
void Start()
{
allowMovement = true;
}
// Update is called once per frame
void Update()
{
if (allowMovement)
{
if (Input.GetMouseButton(0))
{
Ray myRay = playerCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myRay, out RaycastHit hit))
{
targetDest.transform.position = hit.point;
targetDest.Play();
player.SetDestination(hit.point);
}
}
}
else
{
player.SetDestination(player.transform.position);
player.velocity = Vector3.zero;
}
if (player.velocity != Vector3.zero)
{
playerAnimator.SetBool("IsWalking", true);
}
else if (player.velocity == Vector3.zero)
{
playerAnimator.SetBool("IsWalking", false);
}
}
public void DecreaseEnergy()
{
food.deduct(1f);
energy.deduct(0.096f); // 24 hours it's 100, 100/1440=~0.096 per minute
}