day transition is now working

This commit is contained in:
dhain 2022-06-09 16:49:15 +02:00
parent a53857c4d2
commit 7b286bcfd9
13 changed files with 390 additions and 388 deletions

View file

@ -0,0 +1,30 @@
using System;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class DayTransitionManager : MonoBehaviour {
public Animator dayTransitionAnimator;
public TextMeshProUGUI dayCountText;
public GameObject sleepButton;
private void Start() {
HouseController.NewDayEvent.AddListener(NewDay);
}
private void NewDay() {
sleepButton.GetComponent<Button>().enabled = false;
dayTransitionAnimator.gameObject.SetActive(true);
StartCoroutine(PlayTransition());
}
private IEnumerator PlayTransition() {
dayCountText.text = "Day " + HouseController.DayCount;
dayTransitionAnimator.SetTrigger("start");
yield return new WaitForSeconds(3f);
sleepButton.GetComponent<Button>().enabled = true;
dayTransitionAnimator.gameObject.SetActive(false);
}
}

View file

@ -6,9 +6,10 @@ using UnityEngine;
using UnityEngine.Events;
public class HouseController : MonoBehaviour {
private int _dayCount = 1;
private static int _dayCount = 1;
private static UnityEvent _newDayEvent;
public static UnityEvent NewDayEvent => _newDayEvent;
public static int DayCount => _dayCount;
public Canvas menu;
public TextMeshProUGUI dayCountTextMeshProUGUI;
@ -18,8 +19,11 @@ public class HouseController : MonoBehaviour {
ToggleMenu();
}
void Start() {
private void Awake() {
_newDayEvent ??= new UnityEvent();
}
void Start() {
ToggleMenu();
}
@ -31,8 +35,7 @@ public class HouseController : MonoBehaviour {
public void ToggleMenu() {
menu.gameObject.SetActive(!menu.gameObject.activeSelf);
if (Camera.main != null) {
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
float newPosX = pos.x;

View file

@ -3,6 +3,20 @@ using TMPro;
using UnityEngine;
public class HoverManager : MonoBehaviour {
#region Singleton
public static HoverManager instance;
private void Awake() {
if (instance != null) {
Debug.LogWarning("More than one instance of HoverManager found");
}
instance = this;
}
#endregion
public TextMeshProUGUI descriptionText;
public RectTransform descriptionHoverBackground;
@ -41,7 +55,7 @@ public class HoverManager : MonoBehaviour {
/**
* Hide the description Text
*/
private void HideDescription() {
public void HideDescription() {
descriptionText.text = default;
descriptionHoverBackground.gameObject.SetActive(false);
}

View file

@ -16,25 +16,28 @@ public class Inventory : ItemStorage {
#endregion
private const int InventorySpace = 28;
private const int MaxItemStack = 999;
private const int _InventorySpace = 28;
private const int _MaxItemStack = 999;
/**
* Adds the specified amount of items to the Inventory
*/
public override void AddItem(Item item, int amount) {
if (items.Count >= InventorySpace) {
if (items.Count >= _InventorySpace) {
Debug.Log("Not enough inventory space!");
return;
}
// Sell overflowing Items
if (items.ContainsKey(item) && items[item] + amount >= MaxItemStack) {
SellItem(item, amount - (MaxItemStack - items[item]));
amount = MaxItemStack - items[item];
if (items.ContainsKey(item) && items[item] + amount >= _MaxItemStack) {
SellItem(item, amount - (_MaxItemStack - items[item]));
amount = _MaxItemStack - items[item];
}
base.AddItem(item, amount);
}
/**
* Calls ItemStorage.RemoveItem() and deselects the item if removed
*/
public override void RemoveItem(Item item, int amount) {
base.RemoveItem(item, amount);
if (!items.ContainsKey(item) && PlayerController.instance.GetSelectedItem() == item) {
@ -42,6 +45,9 @@ public class Inventory : ItemStorage {
}
}
/**
* Sells the Item for the Item Sell Price and puts it in the Shop for the full price
*/
public void SellItem(Item item, int amount) {
PlayerController.instance.ChangeMoney(item.SellPrice);
Shop.instance.AddItem(item, amount);

View file

@ -4,12 +4,10 @@ using UnityEngine.EventSystems;
public class InventorySlot : ItemStorageSlot, IPointerClickHandler {
private Inventory _inventory;
private Shop _shop;
private PlayerController _playerController;
private void Start() {
_inventory = Inventory.instance;
_shop = Shop.instance;
_playerController = PlayerController.instance;
}
@ -29,22 +27,15 @@ public class InventorySlot : ItemStorageSlot, IPointerClickHandler {
}
}
/**
* Sells the Item for the Item Sell Price and puts it in the Shop if the selling was a mistake
*/
private void SellItem() {
if(Item) {
_inventory.SellItem(Item, 1); //TODO: wie machen mehr als 1 verkaufen?!
}
}
/**
* Gets called when the Inventory Slot gets clicked on
*/
public void OnPointerClick(PointerEventData eventData) {
// When clicked on with right Mouse Button sell the Item
if(eventData.button == PointerEventData.InputButton.Right) {
SellItem();
if(Item) {
_inventory.SellItem(Item, 1); //TODO: wie machen mehr als 1 verkaufen?!
}
}
}
}

View file

@ -1,5 +1,7 @@
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
public class InventoryUI : MonoBehaviour {
public Transform itemsParent;
@ -34,8 +36,12 @@ public class InventoryUI : MonoBehaviour {
*/
private void ToggleInventory() {
inventoryUI.SetActive(!inventoryUI.activeSelf);
HoverManager.instance.HideDescription();
foreach(InventorySlot slot in _slots) {
slot.ChangeItemSelectedSprite(false);
}
}
//TODO: sell Items with right click and when shop is open
/**
* Is called when something in the Inventory UI should update
*/

View file

@ -36,7 +36,7 @@ public class ItemStorageSlot : MonoBehaviour, IPointerEnterHandler, IPointerExit
}
}
private void ChangeItemSelectedSprite(bool on) {
public void ChangeItemSelectedSprite(bool on) {
if(_item) {
if(on) {
icon.sprite = _item.selectedSprite;

View file

@ -31,8 +31,9 @@ public class PlayerController : MonoBehaviour {
public int Money => _money;
public delegate void OnMoneyChanged();
public OnMoneyChanged onMoneyChangedCallback;
// Start is called before the first frame update
private void Start() {
_inventory = Inventory.instance;
@ -51,8 +52,7 @@ public class PlayerController : MonoBehaviour {
}
}
public void DeselectItem()
{
public void DeselectItem() {
_selectedItem = null;
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
@ -63,11 +63,11 @@ public class PlayerController : MonoBehaviour {
public void ChangeMoney(int amount) {
_money += amount;
onMoneyChangedCallback?.Invoke();
}
private void UpdateMoneyUI() {
moneyTextMeshProUGUI.text = _money + "µ";
}
}
}

View file

@ -36,6 +36,7 @@ public class ShopUI : MonoBehaviour {
*/
private void ToggleShop() {
inventoryUI.gameObject.SetActive(!shopUI.activeSelf);
HoverManager.instance.HideDescription();
shopUI.SetActive(!shopUI.activeSelf);
}

View file

@ -2,15 +2,13 @@ using System;
using Tiles;
using UnityEngine;
public class TileBehaviour : MonoBehaviour
{
public class TileBehaviour : MonoBehaviour {
private BaseTile _tile;
private SpriteRenderer _hoverIndicatorSpriteRenderer;
private static Color _hoverIndicatorColor;
// Start is called before the first frame update
void Start()
{
void Start() {
_hoverIndicatorSpriteRenderer = gameObject.transform.GetChild(2).GetComponent<SpriteRenderer>();
SetHoverIndicatorVisibility(false);
SetTile(new GrassTile(gameObject));
@ -19,53 +17,41 @@ public class TileBehaviour : MonoBehaviour
}
// Update is called once per frame
void Update()
{
void Update() {
}
void OnMouseDown()
{
void OnMouseDown() {
UsableItem usable = null;
BaseTile tileToSetTo = null;
if (PlayerController.instance.GetSelectedItem() != null)
{
if(PlayerController.instance.GetSelectedItem() != null) {
usable = PlayerController.instance.GetSelectedItem();
}
tileToSetTo = _tile.Clicked(usable);
if (tileToSetTo != null)
{
if(tileToSetTo != null) {
SetTile(tileToSetTo);
}
}
void SetTile(BaseTile tileToSet)
{
void SetTile(BaseTile tileToSet) {
_tile = tileToSet;
GetComponent<SpriteRenderer>().sprite = _tile.Sprite;
}
private void OnMouseEnter()
{
private void OnMouseEnter() {
SetHoverIndicatorVisibility(true);
}
private void OnMouseExit()
{
private void OnMouseExit() {
SetHoverIndicatorVisibility(false);
}
private void SetHoverIndicatorVisibility(bool visible)
{
if (visible)
{
private void SetHoverIndicatorVisibility(bool visible) {
if(visible) {
_hoverIndicatorSpriteRenderer.color = _hoverIndicatorColor;
}
else
{
} else {
_hoverIndicatorSpriteRenderer.color = Color.clear;
}
}