From 0d49c92c1573073a53d97408f6f0a410753b7fd1 Mon Sep 17 00:00:00 2001 From: dhain Date: Tue, 10 May 2022 00:01:57 +0200 Subject: [PATCH] extended usable Items with UsableItem --- Assets/Scripts/Item.cs | 15 ++++++++++----- Assets/Scripts/Items/FishingRod.cs | 7 +------ Assets/Scripts/Items/Hoe.cs | 2 +- Assets/Scripts/Items/Scythe.cs | 2 +- Assets/Scripts/Items/WateringCan.cs | 2 +- Assets/Scripts/Items/WheatSeed.cs | 2 +- Assets/Scripts/PlayerController.cs | 15 +++++---------- Assets/Scripts/UsableItem.cs | 2 +- 8 files changed, 21 insertions(+), 26 deletions(-) diff --git a/Assets/Scripts/Item.cs b/Assets/Scripts/Item.cs index dcdac71..fe7449d 100644 --- a/Assets/Scripts/Item.cs +++ b/Assets/Scripts/Item.cs @@ -1,17 +1,22 @@ +using System; using UnityEngine; -public class Item : MonoBehaviour { - private string displayName; - private string description; - private int id; //TODO: create an actual ID System that makes snens +public class Item : MonoBehaviour, IComparable { + private readonly string displayName; + private readonly string description; + private readonly int id; //TODO: create an actual ID System that makes snens public SpriteRenderer spriteRenderer; public Sprite selectedSprite; public Sprite defaultSprite; - + public Item(string displayName, string description, int id) { this.displayName = displayName; this.description = description; this.id = id; spriteRenderer.sprite ??= defaultSprite; // defaultSprite is set in UnityEditor } + + public int CompareTo(Item other) { + return this.id - other.id; + } } diff --git a/Assets/Scripts/Items/FishingRod.cs b/Assets/Scripts/Items/FishingRod.cs index 1906e76..3797b64 100644 --- a/Assets/Scripts/Items/FishingRod.cs +++ b/Assets/Scripts/Items/FishingRod.cs @@ -1,12 +1,7 @@ using UnityEngine; namespace Items { - public class FishingRod : Item, IUsable { + public class FishingRod : UsableItem { public FishingRod() : base("Fishing Rod", "Can be used to fish fishy fish.", 1) { } - - public void select() { - - } - } } diff --git a/Assets/Scripts/Items/Hoe.cs b/Assets/Scripts/Items/Hoe.cs index b6af5a1..b7db958 100644 --- a/Assets/Scripts/Items/Hoe.cs +++ b/Assets/Scripts/Items/Hoe.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using UnityEngine; namespace Items { - public class Hoe : Item { + public class Hoe : UsableItem { public Hoe() : base("Hoe", "Used to hoe the ground into farmland.", 2){} } } diff --git a/Assets/Scripts/Items/Scythe.cs b/Assets/Scripts/Items/Scythe.cs index 2c72eb9..a5b4189 100644 --- a/Assets/Scripts/Items/Scythe.cs +++ b/Assets/Scripts/Items/Scythe.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using UnityEngine; namespace Items { - public class Scythe : Item { + public class Scythe : UsableItem { public Scythe() : base("Scythe", "Used to cut down crops.", 3){} } } diff --git a/Assets/Scripts/Items/WateringCan.cs b/Assets/Scripts/Items/WateringCan.cs index 947c741..3bd37a1 100644 --- a/Assets/Scripts/Items/WateringCan.cs +++ b/Assets/Scripts/Items/WateringCan.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using UnityEngine; namespace Items { - public class WateringCan : Item { + public class WateringCan : UsableItem { public WateringCan():base("Watering Can", "Used to water planted crops.", 4){} } } diff --git a/Assets/Scripts/Items/WheatSeed.cs b/Assets/Scripts/Items/WheatSeed.cs index 471bd53..8d90cfb 100644 --- a/Assets/Scripts/Items/WheatSeed.cs +++ b/Assets/Scripts/Items/WheatSeed.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using UnityEngine; namespace Items { - public class WheatSeed : Item { + public class WheatSeed : UsableItem { public WheatSeed() : base("Wheat Seeds", "When planted on farmland, wheat will grow.", 6){} } } diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 5f0e243..db6dac9 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -6,33 +6,28 @@ public class PlayerController : MonoBehaviour { private List inventory; private int money; private UsableItem selectedItem; - + private static PlayerController instance; - + public int startMoney = 100; public static PlayerController getInstance() { return instance; } - + // Start is called before the first frame update - void Start() - { + void Start() { inventory ??= new List(); money = startMoney; instance = this; } // Update is called once per frame - void Update() - { - + void Update() { } public void setSelectedItem(UsableItem item) { selectedItem = item; - - } } diff --git a/Assets/Scripts/UsableItem.cs b/Assets/Scripts/UsableItem.cs index 009ee97..60fa969 100644 --- a/Assets/Scripts/UsableItem.cs +++ b/Assets/Scripts/UsableItem.cs @@ -20,6 +20,6 @@ public class UsableItem : Item, IUsable } public void select() { - throw new System.NotImplementedException(); + PlayerController.getInstance().setSelectedItem(this); } }