Skip to content

Instantly share code, notes, and snippets.

@lucasvdiepen
Created November 22, 2022 11:11
Show Gist options
  • Select an option

  • Save lucasvdiepen/18a30720853bbc8562908f430ffacc5a to your computer and use it in GitHub Desktop.

Select an option

Save lucasvdiepen/18a30720853bbc8562908f430ffacc5a to your computer and use it in GitHub Desktop.
Cosmetics Structure
public class CosmeticsApplier : MonoBehaviour
{
public ItemType[] applyPriority;
private void Start()
{
ApplySelectedItems();
}
private void ApplySelectedItems()
{
List<ItemBase> selectedItems = FindObjectOfType<ItemManager>().GetAllSelectedItems();
//Apply priority first
foreach(ItemType priorityType in applyPriority)
{
for(int i = 0; i < selectedItems.Count; i++)
{
if(selectedItems[i].itemType == priorityType)
{
selectedItems[i].Apply(gameObject);
selectedItems.RemoveAt(i);
break;
}
}
}
//Apply the rest here
foreach(ItemBase item in selectedItems)
{
item.Apply(gameObject);
}
}
}
public abstract class ItemBase : ScriptableObject
{
public int itemID = -1;
[HideInInspector] public ItemType itemType;
protected ItemBase(ItemType _itemType)
{
itemType = _itemType;
}
public abstract void Apply(GameObject player);
}
[CreateAssetMenu(fileName = "NewColor", menuName = "Player Items/New Color")]
public class ItemColor : ItemBase
{
public Color color;
public ItemColor() : base(ItemType.Color) { }
public override void Apply(GameObject player)
{
SpriteRenderer spriteRenderer = player.FindComponentInChildWithTag<SpriteRenderer>("Shape");
spriteRenderer.color = color;
}
}
[CreateAssetMenu(fileName = "NewShape", menuName = "Player Items/New Shape")]
public class ItemShape : ItemBase
{
public GameObject shape;
public ItemShape() : base(ItemType.Shape) { }
public override void Apply(GameObject player)
{
Instantiate(shape, player.transform);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment