Animals can now be bought in AnimalShop

if bought they will be placed somewhere random on the map
This commit is contained in:
d-hain 2022-06-23 22:18:32 +02:00
parent acd602c85a
commit 441ffb900a
34 changed files with 2387 additions and 265 deletions

View file

@ -1,29 +1,32 @@
using System;
using System.Collections;
using Actions;
using DefaultNamespace;
using UnityEngine;
using Object = System.Object;
using Random = UnityEngine.Random;
public class Animal : MonoBehaviour {
private Rigidbody2D _rigidbody;
private SpriteRenderer _spriteRenderer;
private Sprite _animalSprite;
private Animator _animator;
private int _animMoveID;
public Sprite AnimalSprite => _animalSprite;
public int SellPrice => Convert.ToInt32(price * 0.8);
public Item producedItem;
public int movementSpeed;
public GameObject animalPrefab;
public Sprite defaultSprite;
public Sprite selectedSprite;
public string displayName;
public string description;
public int price;
private void Start() {
_rigidbody = gameObject.GetComponent<Rigidbody2D>();
_spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
_animalSprite = _spriteRenderer.GetComponent<Sprite>();
_animator = gameObject.GetComponent<Animator>();
_animMoveID = Animator.StringToHash("moving");
_spriteRenderer.sprite = defaultSprite;
// Move the Animal in any random direction every 1-5s
InvokeRepeating(nameof(MoveInRandomDirection), 2f, Random.Range(1f, 5f));

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using UnityEditorInternal;
using UnityEngine;
public class ElementStorage<T> : MonoBehaviour {
@ -7,7 +8,7 @@ public class ElementStorage<T> : MonoBehaviour {
public T[] startElements;
/**
* Methods can be added to this and they will get called every time onItemChangedCallback gets Invoked
* Methods can be added to this and they will get called every time onElementChangedCallback gets Invoked
*/
public delegate void OnElementChanged();
public OnElementChanged onElementChangedCallback;

View file

@ -5,7 +5,7 @@ using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ElementStorageSlot<T> : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
public Image icon;
public Image image;
public TextMeshProUGUI amountText;
public T Element => _element;
@ -30,20 +30,22 @@ public class ElementStorageSlot<T> : MonoBehaviour, IPointerEnterHandler, IPoint
}
private void ShowMessage() {
if(_element is Item) { //TODO: add animal description showing
if(_element is Item) {
Item item = (Item)(object)_element;
HoverManager.onMouseHoverDescription(item.description, Input.mousePosition);
} else if(_element is Animal) {
Animal animal = (Animal)(object)_element;
HoverManager.onMouseHoverDescription(animal.description, Input.mousePosition);
}
}
public void ChangeElementSelectedSprite(bool on) {
if(_element is Item) { //TODO: add animal sprite change
Item item = (Item)(object)_element;
if(on) {
icon.sprite = item.selectedSprite;
} else {
icon.sprite = item.defaultSprite;
}
image.sprite = on ? item.selectedSprite : item.defaultSprite;
}else if(_element is Animal) {
Animal animal = (Animal)(object)_element;
image.sprite = on ? animal.selectedSprite : animal.defaultSprite;
}
}
@ -61,12 +63,15 @@ public class ElementStorageSlot<T> : MonoBehaviour, IPointerEnterHandler, IPoint
public void AddElement(T newElement) {
_element = newElement;
if(_element is Item) { //TODO: add animal sprite change
if(_element is Item) {
Item item = (Item)(object)_element;
icon.sprite = item.defaultSprite;
image.sprite = item.defaultSprite;
} else if (_element is Animal) {
Animal animal = (Animal)(object)_element;
image.sprite = animal.defaultSprite;
}
icon.enabled = true;
image.enabled = true;
}
/**
@ -74,8 +79,8 @@ public class ElementStorageSlot<T> : MonoBehaviour, IPointerEnterHandler, IPoint
*/
public virtual void ClearSlot() {
_element = default;
icon.sprite = null;
icon.enabled = false;
image.sprite = null;
image.enabled = false;
amountText.text = "";
}

View file

@ -1,4 +1,5 @@
using System;
using Shop;
using UnityEngine;
public class Inventory : ElementStorage<Item> {
@ -50,7 +51,7 @@ public class Inventory : ElementStorage<Item> {
*/
public void SellItem(Item item, int amount) {
PlayerController.instance.ChangeMoney(item.SellPrice);
Shop.instance.AddElement(item, amount);
ItemShop.instance.AddElement(item, amount);
RemoveElement(item, amount);
}
}

View file

@ -34,7 +34,7 @@ public class InventorySlot : ElementStorageSlot<Item>, IPointerClickHandler {
// When clicked on with right Mouse Button sell the Item
if(eventData.button == PointerEventData.InputButton.Right) {
if(Element) {
_inventory.SellItem(Element, 1); //TODO: wie machen mehr als 1 verkaufen?!
_inventory.SellItem(Element, 1);
}
}
}

View file

@ -20,7 +20,7 @@ public class InventoryUI : MonoBehaviour {
// Set the icon to not be a raycast target for the Description Hovering to work
foreach(InventorySlot slot in _slots) {
slot.icon.raycastTarget = false;
slot.image.raycastTarget = false;
}
}

View file

@ -19,7 +19,7 @@ public class Item : ScriptableObject, IComparable<Item> {
}
public int CompareTo(Item other) {
return this._id - other._id;
return _id - other._id;
}
public void SetID(int id) {

View file

@ -1,49 +0,0 @@
using UnityEngine;
public class Shop : ElementStorage<Item> {
#region Singleton
public static Shop instance;
private void Awake() {
if(instance != null) {
Debug.LogWarning("More than one instance of Shop found");
}
instance = this;
}
#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 RemoveElement(Item item, int amount) {
base.RemoveElement(item, amount);
if(itemWasBought){
_lastBoughtItem = item;
_lastBoughtItemAmount = amount;
}
}
public void UndoLastPurchase() {
if(itemWasBought){
_inventory = Inventory.instance;
_playerController = PlayerController.instance;
if(_lastBoughtItem) {
_playerController.ChangeMoney(_lastBoughtItem.price);
_inventory.RemoveElement(_lastBoughtItem, _lastBoughtItemAmount);
AddElement(_lastBoughtItem, _lastBoughtItemAmount);
itemWasBought = false;
}
}
}
}

3
Assets/Scripts/Shop.meta Normal file
View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f4055d199c2a43b6a92f4a3704e3018a
timeCreated: 1656014917

View file

@ -0,0 +1,21 @@
using UnityEngine;
namespace Shop {
public class AnimalShop : ElementStorage<Animal> {
#region Singleton
public static AnimalShop instance;
private void Awake() {
if(instance != null) {
Debug.LogWarning("More than one instance of AnimalShop found");
}
instance = this;
}
#endregion
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2c06d4c2082cfaa48ab284fbf112290b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,61 @@
using TMPro;
using UnityEngine;
namespace Shop {
public class AnimalShopSlot : ElementStorageSlot<Animal> {
public TextMeshProUGUI nameText;
public TextMeshProUGUI costText;
private AnimalShop _animalShop;
private PlayerController _playerController;
private void Start() {
_animalShop = AnimalShop.instance;
_playerController = PlayerController.instance;
}
/**
* Clears the Shop Slot
*/
public override void ClearSlot() {
nameText.text = "";
costText.text = "";
amountText.text = "";
base.ClearSlot();
}
/**
* Gets called when the Shop Slot is clicked
*/
public override void UseElement() {
if(Element) {
if(_playerController.Money >= Element.price) {
if(Element) {
_playerController.ChangeMoney(-Element.price);
// Debug.Log("Buying Animal: " + Element.displayName);
}
PlaceCowRandomlyOnScreen();
_animalShop.RemoveElement(Element, 1);
} else {
// Debug.Log("Not enough money to buy Animal.");
}
_animalShop.onElementChangedCallback?.Invoke();
}
}
private void PlaceCowRandomlyOnScreen() {
float spawnY = Random.Range
(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y,
Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
float spawnX = Random.Range
(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x,
Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x);
Vector2 spawnPosition = new Vector2(spawnX, spawnY);
Instantiate(Element.animalPrefab, spawnPosition, Quaternion.identity);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 12dc6c16906d3994380888118d0da675
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,53 @@
using UnityEngine;
namespace Shop {
public class ItemShop : ElementStorage<Item> {
#region Singleton
public static ItemShop instance;
private void Awake() {
if(instance != null) {
Debug.LogWarning("More than one instance of ItemShop found");
}
instance = this;
}
#endregion
public bool itemWasBought;
private PlayerController _playerController;
private Inventory _inventory;
private Item _lastBoughtItem;
private int _lastBoughtItemAmount;
/**
* Calls ElementStorage.RemoveItem() and sets 2 Variables to remember the last bought item
*/
public override void RemoveElement(Item item, int amount) {
base.RemoveElement(item, amount);
if(itemWasBought) {
_lastBoughtItem = item;
_lastBoughtItemAmount = amount;
}
}
public void UndoLastPurchase() {
if(itemWasBought) {
_inventory = Inventory.instance;
_playerController = PlayerController.instance;
if(_lastBoughtItem) {
_playerController.ChangeMoney(_lastBoughtItem.price);
_inventory.RemoveElement(_lastBoughtItem, _lastBoughtItemAmount);
AddElement(_lastBoughtItem, _lastBoughtItemAmount);
itemWasBought = false;
}
}
}
}
}

View file

@ -0,0 +1,51 @@
using TMPro;
namespace Shop {
public class ItemShopSlot : ElementStorageSlot<Item> {
public TextMeshProUGUI nameText;
public TextMeshProUGUI costText;
private ItemShop _itemShop;
private Inventory _inventory;
private PlayerController _playerController;
private void Start() {
_itemShop = ItemShop.instance;
_inventory = Inventory.instance;
_playerController = PlayerController.instance;
}
/**
* Clears the Shop Slot
*/
public override void ClearSlot() {
nameText.text = "";
costText.text = "";
amountText.text = "";
base.ClearSlot();
}
/**
* Gets called when the Shop Slot is clicked
*/
public override void UseElement() {
if(Element) {
if(_playerController.Money >= Element.price) {
if(Element) {
_playerController.ChangeMoney(-Element.price);
_itemShop.itemWasBought = true;
// Debug.Log("Buying Item: " + Element.displayName);
}
_inventory.AddElement(Element, 1);
_itemShop.RemoveElement(Element, 1);
} else {
// Debug.Log("Not enough money to buy Item.");
}
_itemShop.onElementChangedCallback?.Invoke();
_inventory.onElementChangedCallback?.Invoke();
}
}
}
}

View file

@ -0,0 +1,45 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Shop {
public class ShopSwitcher : MonoBehaviour {
public Sprite animalShopSprite;
public Sprite itemShopSprite;
public Image switchButtonImage;
public TextMeshProUGUI shopTitleText;
public GameObject viewportItems;
public GameObject viewportAnimals;
public GameObject undoPurchaseButton;
private void Start() {
switchButtonImage.enabled = true;
switchButtonImage.sprite = animalShopSprite;
viewportItems.SetActive(true);
viewportAnimals.SetActive(false);
}
public void SwitchShops() {
// switch Shop Title text and Image of the Button
if(shopTitleText.text.StartsWith("Item")) {
shopTitleText.text = "Animal \nShop";
switchButtonImage.sprite = itemShopSprite;
} else {
shopTitleText.text = "Item \nShop";
switchButtonImage.sprite = animalShopSprite;
}
// switch the shown shops
viewportItems.SetActive(!viewportItems.activeSelf);
viewportAnimals.SetActive(!viewportAnimals.activeSelf);
// turn off undoPurchaseButton when opening Animal Shop because of lack of undoing purchases in the Animal Shop
undoPurchaseButton.SetActive(!undoPurchaseButton.activeSelf);
// remove undo purchase button when switching to an animal shop and fix content alignement
}
}
}

View file

@ -0,0 +1,85 @@
using System.Linq;
using UnityEngine;
namespace Shop {
public class ShopUI : MonoBehaviour {
public Transform itemsParent;
public Transform animalsParent;
public GameObject shopUI;
public GameObject inventoryUI;
private ItemShop _itemShop;
private AnimalShop _animalShop;
private ItemShopSlot[] _itemSlots;
private AnimalShopSlot[] _animalSlots;
private void Start() {
// Get Shop instance and add UpdateUI method to OnItemChanged delegate
_itemShop = ItemShop.instance;
_animalShop = AnimalShop.instance;
_itemShop.onElementChangedCallback += UpdateUI;
_animalShop.onElementChangedCallback += UpdateUI;
// Add all ItemShopSlot GameObjects to _itemSlots and turn off the Shop UI
_itemSlots = itemsParent.GetComponentsInChildren<ItemShopSlot>();
_animalSlots = animalsParent.GetComponentsInChildren<AnimalShopSlot>();
ToggleShop();
// Set the icon to not be a raycast target for the Description Hovering to work
foreach(ItemShopSlot slot in _itemSlots) {
slot.image.raycastTarget = false;
}
foreach(AnimalShopSlot slot in _animalSlots) {
slot.image.raycastTarget = false;
}
UpdateUI();
}
private void Update() {
// When "Shop" button is pressed turn on/off Shop UI
if(Input.GetButtonDown("Shop")) {
ToggleShop();
}
}
/**
* Turn on/off the Shop UI
*/
private void ToggleShop() {
inventoryUI.gameObject.SetActive(!shopUI.activeSelf);
HoverManager.instance.HideDescription();
shopUI.SetActive(!shopUI.activeSelf);
}
/**
* Is called when something in the Shop UI should update
*/
private void UpdateUI() {
// Add all items to the correct slots and clear the ones where no item should be
for(int i = 0; i < _itemSlots.Length; i++) {
// Item Slots
if(i < _itemShop.Elements.Count) {
_itemSlots[i].AddElement(_itemShop.Elements.ElementAt(i).Key);
_itemSlots[i].nameText.text = _itemSlots[i].Element.displayName;
_itemSlots[i].costText.text = _itemSlots[i].Element.price + " µ";
_itemSlots[i].amountText.text = _itemShop.Elements[_itemShop.Elements.ElementAt(i).Key] + " #";
} else {
_itemSlots[i].ClearSlot();
}
// Animal SLots
if(i < _animalShop.Elements.Count) {
_animalSlots[i].AddElement(_animalShop.Elements.ElementAt(i).Key);
_animalSlots[i].nameText.text = _animalSlots[i].Element.displayName;
_animalSlots[i].costText.text = _animalSlots[i].Element.price + " µ";
_animalSlots[i].amountText.text =
_animalShop.Elements[_animalShop.Elements.ElementAt(i).Key] + " #";
} else {
_animalSlots[i].ClearSlot();
}
}
}
}
}

View file

@ -1,50 +0,0 @@
using TMPro;
using UnityEngine;
public class ShopSlot : ElementStorageSlot<Item> {
public TextMeshProUGUI nameText;
public TextMeshProUGUI costText;
private Shop _shop;
private Inventory _inventory;
private PlayerController _playerController;
private void Start() {
_shop = Shop.instance;
_inventory = Inventory.instance;
_playerController = PlayerController.instance;
}
/**
* Clears the Shop Slot
*/
public override void ClearSlot() {
nameText.text = "";
costText.text = "";
amountText.text = "";
base.ClearSlot();
}
/**
* Gets called when the Shop Slot is clicked
*/
public override void UseElement() {
if(Element) {
if(_playerController.Money >= Element.price) {
if(Element) {
_playerController.ChangeMoney(-Element.price);
_shop.itemWasBought = true;
Debug.Log("Buying Item: " + Element.displayName);
}
_inventory.AddElement(Element, 1);
_shop.RemoveElement(Element, 1);
} else {
Debug.Log("Not enough money to buy item.");
}
_shop.onElementChangedCallback?.Invoke();
_inventory.onElementChangedCallback?.Invoke();
}
}
}

View file

@ -1,13 +0,0 @@
using UnityEngine;
public class ShopSwitcher : MonoBehaviour {
public Sprite animalShopSprite;
public Sprite itemShopSprite;
public void SwitchShops() {
// switch text in Shop Title
// switch image on switch Button
// turn off one content and turn on the other
// remove undo purchase button when switching to an animal shop
}
}

View file

@ -1,60 +0,0 @@
using System.Linq;
using UnityEngine;
public class ShopUI : MonoBehaviour {
public Transform itemsParent;
public GameObject shopUI;
public GameObject inventoryUI;
private Shop _shop;
private ShopSlot[] _slots;
private void Start() {
// Get Shop instance and add UpdateUI method to OnItemChanged delegate
_shop = Shop.instance;
_shop.onElementChangedCallback += UpdateUI;
// Add all ShopSlot GameObjects to _slots and turn off the Shop UI
_slots = itemsParent.GetComponentsInChildren<ShopSlot>();
ToggleShop();
// Set the icon to not be a raycast target for the Description Hovering to work
foreach (ShopSlot slot in _slots) {
slot.icon.raycastTarget = false;
}
}
private void Update() {
// When "Shop" button is pressed turn on/off Shop UI
if (Input.GetButtonDown("Shop")) {
ToggleShop();
}
}
/**
* Turn on/off the Shop UI
*/
private void ToggleShop() {
inventoryUI.gameObject.SetActive(!shopUI.activeSelf);
HoverManager.instance.HideDescription();
shopUI.SetActive(!shopUI.activeSelf);
}
/**
* Is called when something in the Shop UI should update
*/
private void UpdateUI() {
// Add all items to the correct slots and clear the ones where no item should be
for (int i = 0; i < _slots.Length; i++) {
if (i < _shop.Elements.Count) {
_slots[i].AddElement(_shop.Elements.ElementAt(i).Key);
_slots[i].nameText.text = _slots[i].Element.displayName;
_slots[i].costText.text = _slots[i].Element.price + " µ";
_slots[i].amountText.text = _shop.Elements[_shop.Elements.ElementAt(i).Key] + " #";
}
else {
_slots[i].ClearSlot();
}
}
}
}