you can now undo a purchase after buying an item from the shop
This commit is contained in:
parent
efc7a3deaf
commit
64b25a2029
12 changed files with 359 additions and 56 deletions
|
|
@ -5,11 +5,13 @@ public class InventorySlot : ItemStorageSlot {
|
|||
* Gets called when the Inventory Slot is clicked
|
||||
*/
|
||||
public override void UseItem() {
|
||||
if(Item.GetType() == typeof(UsableItem)) {
|
||||
((UsableItem) Item).Select();
|
||||
Debug.Log("using " + Item.displayName);
|
||||
} else {
|
||||
Debug.Log("Item not usable " + Item.displayName);
|
||||
if(Item){
|
||||
if(Item.GetType() == typeof(UsableItem)) {
|
||||
((UsableItem)Item).Select();
|
||||
Debug.Log("using " + Item.displayName);
|
||||
} else {
|
||||
Debug.Log("Item not usable " + Item.displayName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -40,12 +40,13 @@ public class InventoryUI : MonoBehaviour {
|
|||
* Is called when something in the Inventory UI should update
|
||||
*/
|
||||
private void UpdateUI() {
|
||||
// Add all items to the correct slots and clear the ones where no item should be
|
||||
// Add all items to the correct slots, clear the ones where no item should be and set the number of how many items are in the slot
|
||||
for(int i = 0; i < _slots.Length; i++) {
|
||||
if(i < _inventory.items.Count) {
|
||||
_slots[i].AddItem(_inventory.items.ElementAt(i).Key);
|
||||
if(_inventory.items[_inventory.items.ElementAt(i).Key] > 1) {
|
||||
_slots[i].amountText.text = "" + _inventory.items[_inventory.items.ElementAt(i).Key];
|
||||
_slots[i].amountText.text = "" + _inventory.items[_inventory.items.ElementAt(i).Key];
|
||||
if(_inventory.items[_inventory.items.ElementAt(i).Key] == 1) {
|
||||
_slots[i].amountText.text = "";
|
||||
}
|
||||
} else {
|
||||
_slots[i].ClearSlot();
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ public class Item : ScriptableObject, IComparable<Item> {
|
|||
public Sprite selectedSprite;
|
||||
public Sprite defaultSprite;
|
||||
public int cost;
|
||||
public int SellPrice => Convert.ToInt32(cost * 0.8);
|
||||
|
||||
public Item(string displayName, string description, int id) {
|
||||
this.displayName = displayName;
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public class ItemStorage : MonoBehaviour {
|
|||
/**
|
||||
* Removes the specified amount of items in the Item Storage
|
||||
*/
|
||||
public void RemoveItem(Item item, int amount) {
|
||||
public virtual void RemoveItem(Item item, int amount) {
|
||||
if(items[item]-amount <= 0) {
|
||||
items.Remove(item);
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -30,11 +30,15 @@ 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() {
|
||||
_money = startMoney;
|
||||
_inventory = Inventory.instance;
|
||||
moneyTextMeshProUGUI.text = _money + "µ";
|
||||
|
||||
onMoneyChangedCallback += UpdateMoneyUI;
|
||||
}
|
||||
|
||||
public void SetSelectedItem(UsableItem item) {
|
||||
|
|
@ -52,6 +56,10 @@ public class PlayerController : MonoBehaviour {
|
|||
|
||||
public void ChangeMoney(int amount) {
|
||||
_money += amount;
|
||||
onMoneyChangedCallback?.Invoke();
|
||||
}
|
||||
|
||||
private void UpdateMoneyUI() {
|
||||
moneyTextMeshProUGUI.text = _money + "µ";
|
||||
}
|
||||
}
|
||||
|
|
@ -14,4 +14,36 @@ public class Shop : ItemStorage {
|
|||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public bool itemWasBought;
|
||||
|
||||
private PlayerController _playerController;
|
||||
private Inventory _inventory;
|
||||
private Item _lastBoughtItem;
|
||||
private int _lastBoughtItemAmount;
|
||||
|
||||
/**
|
||||
* Calls ItemStorage.RemoveItem() and sets 2 Variables to remember the last bought item
|
||||
*/
|
||||
public override void RemoveItem(Item item, int amount) {
|
||||
base.RemoveItem(item, amount);
|
||||
if(itemWasBought){
|
||||
_lastBoughtItem = item;
|
||||
_lastBoughtItemAmount = amount;
|
||||
}
|
||||
}
|
||||
|
||||
public void UndoLastPurchase() {
|
||||
if(itemWasBought){
|
||||
_inventory = Inventory.instance;
|
||||
_playerController = PlayerController.instance;
|
||||
|
||||
if(_lastBoughtItem) {
|
||||
_playerController.ChangeMoney(_lastBoughtItem.cost);
|
||||
_inventory.RemoveItem(_lastBoughtItem, _lastBoughtItemAmount);
|
||||
AddItem(_lastBoughtItem, _lastBoughtItemAmount);
|
||||
itemWasBought = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -32,13 +32,14 @@ public class ShopSlot : ItemStorageSlot {
|
|||
public override void UseItem() {
|
||||
if(Item) {
|
||||
if(_playerController.Money >= Item.cost) {
|
||||
_inventory.AddItem(Item, 1);
|
||||
_shop.RemoveItem(Item, 1);
|
||||
if(Item) {
|
||||
_playerController.ChangeMoney(-Item.cost);
|
||||
_shop.itemWasBought = true;
|
||||
|
||||
Debug.Log("Buying Item: " + Item.displayName);
|
||||
}
|
||||
_inventory.AddItem(Item, 1);
|
||||
_shop.RemoveItem(Item, 1);
|
||||
} else {
|
||||
Debug.Log("Not enough money to buy item.");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ using UnityEngine;
|
|||
public class ShopUI : MonoBehaviour {
|
||||
public Transform itemsParent;
|
||||
public GameObject shopUI;
|
||||
public bool shopIsOpen;
|
||||
public GameObject inventoryUI;
|
||||
|
||||
private Shop _shop;
|
||||
|
|
@ -17,7 +16,6 @@ public class ShopUI : MonoBehaviour {
|
|||
|
||||
// Add all ShopSlot GameObjects to _slots and turn off the Shop UI
|
||||
_slots = itemsParent.GetComponentsInChildren<ShopSlot>();
|
||||
shopIsOpen = false;
|
||||
ToggleShop();
|
||||
|
||||
// Set the icon to not be a raycast target for the Description Hovering to work
|
||||
|
|
@ -29,7 +27,6 @@ public class ShopUI : MonoBehaviour {
|
|||
private void Update() {
|
||||
// When "Shop" button is pressed turn on/off Shop UI
|
||||
if (Input.GetButtonDown("Shop")) {
|
||||
shopIsOpen = true;
|
||||
ToggleShop();
|
||||
}
|
||||
}
|
||||
|
|
@ -38,7 +35,7 @@ public class ShopUI : MonoBehaviour {
|
|||
* Turn on/off the Shop UI
|
||||
*/
|
||||
private void ToggleShop() {
|
||||
inventoryUI.gameObject.SetActive(!shopUI.activeSelf);
|
||||
// TODO: geht ned oda so: inventoryUI.gameObject.SetActive(!shopUI.activeSelf);
|
||||
shopUI.SetActive(!shopUI.activeSelf);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,47 +2,35 @@ using System;
|
|||
using Tiles;
|
||||
using UnityEngine;
|
||||
|
||||
public class TileBehaviour : MonoBehaviour
|
||||
{
|
||||
public class TileBehaviour : MonoBehaviour {
|
||||
private BaseTile _tile;
|
||||
private SpriteRenderer _hoverIndicatorSpriteRenderer;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
void Start() {
|
||||
//_hoverIndicatorSpriteRenderer = gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>();
|
||||
//SetHoverIndicatorVisibility(false);
|
||||
SetTile(new GrassTile());
|
||||
|
||||
|
||||
HouseController.NewDayEvent.AddListener(_tile.DayLightStep);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
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;
|
||||
}
|
||||
|
|
@ -61,4 +49,4 @@ public class TileBehaviour : MonoBehaviour
|
|||
{
|
||||
_hoverIndicatorSpriteRenderer.enabled = visible;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue