aboutsummaryrefslogtreecommitdiff
path: root/source/resources/types/Recipe.cs
blob: 94dd9ba2ce26e43c6166c4c08deebe9d5bea4358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Collections.Generic;
using Celesteia.Game.Components.Items;

namespace Celesteia.Resources.Types {
    public class Recipe : IResourceType {
        private byte id;
        public readonly string Name;
        public byte GetID() => id;
        public void SetID(byte value) => id = value;

        public Part Result;
        public List<Part> Ingredients;

        public Recipe(Part result, params Part[] ingredients) {
            Result = result;
            Ingredients = new List<Part>(ingredients);
        }

        public bool Craftable(Inventory inventory) {
            foreach (Part ingredient in Ingredients)
                if (!inventory.ContainsAmount(ingredient.Key, ingredient.Amount)) return false;

            return true;
        }

        public void Craft(Inventory inventory) {
            if (!Craftable(inventory)) return;

            foreach (Part ingredient in Ingredients)
                inventory.RemoveAmount(ingredient.Key, ingredient.Amount);

            inventory.AddItem(Result.Stack);
        }
    }

    public struct Part {
        public NamespacedKey Key;
        public int Amount;

        public Part(NamespacedKey key, int amount) {
            Key = key;
            Amount = amount;
        }

        public ItemStack Stack => new ItemStack(Key, Amount);
        public ItemType GetItemType() => ResourceManager.Items.GetResource(Key) as ItemType;
    }
}