Added project
This commit is contained in:
78
Platformer/Assets/Scripts/ItemBase.cs
Normal file
78
Platformer/Assets/Scripts/ItemBase.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "New Item Database", menuName = "Databases/Items")]
|
||||
public class ItemBase : ScriptableObject {
|
||||
|
||||
[SerializeField, HideInInspector] private List<Item> items;
|
||||
[SerializeField] private Item currentItem;
|
||||
private int currentIndex;
|
||||
|
||||
public void CreateItem() {
|
||||
if (items == null) {
|
||||
items = new List<Item>();
|
||||
}
|
||||
var item = new Item();
|
||||
items.Add(item);
|
||||
currentItem = item;
|
||||
currentIndex = items.Count - 1;
|
||||
}
|
||||
|
||||
public void RemoveItem() {
|
||||
if (items == null || currentItem == null) {
|
||||
return;
|
||||
}
|
||||
items.Remove(currentItem);
|
||||
if (items.Count > 0) {
|
||||
currentItem = items[0];
|
||||
} else {
|
||||
CreateItem();
|
||||
}
|
||||
currentIndex = 0;
|
||||
}
|
||||
|
||||
public void NextItem() {
|
||||
if (currentIndex + 1 < items.Count) {
|
||||
currentIndex++;
|
||||
currentItem = items[currentIndex];
|
||||
}
|
||||
}
|
||||
|
||||
public void PrevItem() {
|
||||
if (currentIndex > 0) {
|
||||
currentIndex--;
|
||||
currentItem = items[currentIndex];
|
||||
}
|
||||
}
|
||||
|
||||
public Item GetItemOfId(int id) {
|
||||
return items.Find(t => t.Id == id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Item {
|
||||
|
||||
[SerializeField] private int id;
|
||||
public int Id => id;
|
||||
|
||||
[SerializeField] private string itemName;
|
||||
public string ItemName => itemName;
|
||||
|
||||
[SerializeField] private string description;
|
||||
public string Description => description;
|
||||
|
||||
[SerializeField] private Sprite icon;
|
||||
public Sprite Icon => icon;
|
||||
|
||||
[SerializeField] private BuffType type;
|
||||
public BuffType Type => type;
|
||||
|
||||
[SerializeField] private float value;
|
||||
public float Value => value;
|
||||
|
||||
public override string ToString() {
|
||||
return $"[id: {id}, itemName: {itemName}, description: {description}, type: {type.ToString()}, value {value}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user