Beispiel #1
0
        /// <summary>
        ///     Load an item prefab from a bundle and register it in <see cref="AssetManager" />.
        /// </summary>
        /// <param name="assetBundle"></param>
        /// <param name="assetName"></param>
        /// <param name="itemDef"></param>
        public static void LoadItemPrefab(AssetBundle assetBundle, string assetName, ItemDef itemDef)
        {
            var prefab = assetBundle.LoadAsset <GameObject>(assetName);

            AssetManager.RegisterItemPrefab(prefab, itemDef);
        }
Beispiel #2
0
        /// <summary>
        ///     Create a <see cref="Recipe" /> for a prefab based on the <see cref="ItemDef" /> of this custom item
        /// </summary>
        /// <param name="prefab">The item for which the recipe will be created</param>
        /// <param name="itemDef"></param>
        /// <param name="craftingStations">
        ///     List of stations which are allowed to act as the crafting and repair station for this
        ///     item
        /// </param>
        /// <returns></returns>
        private Recipe CreateRecipe(ObjectDB instance, GameObject prefab, ItemDef itemDef)
        {
            var newRecipe = ScriptableObject.CreateInstance <Recipe>();

            newRecipe.name              = $"Recipe_{prefab.name}";
            newRecipe.m_amount          = itemDef.Amount;
            newRecipe.m_minStationLevel = itemDef.MinStationLevel;
            newRecipe.m_item            = prefab.GetComponent <ItemDrop>();
            newRecipe.m_enabled         = itemDef.Enabled;

            // Assign the crafting station for this Recipe if defined in ItemDef
            if (!string.IsNullOrEmpty(itemDef.CraftingStation))
            {
                var craftingStation = CraftingStations.GetValueSafe(itemDef.CraftingStation);
                if (craftingStation == null)
                {
                    Logger.LogWarning($"Could not find crafting station: {itemDef.CraftingStation}");
                    var stationList = string.Join(", ", CraftingStations.Keys);
                    Logger.LogDebug($"Available Stations: {stationList}");
                }
                else
                {
                    newRecipe.m_craftingStation = craftingStation;
                }
            }

            // Assign the repair station for this recipe if defined in ItemDef
            if (!string.IsNullOrEmpty(itemDef.RepairStation))
            {
                var repairStation = CraftingStations.GetValueSafe(itemDef.RepairStation);
                if (repairStation == null)
                {
                    Logger.LogWarning($"Could not find repair station: {itemDef.RepairStation}");
                    var stationList = string.Join(", ", CraftingStations.Keys);
                    Logger.LogDebug($"Available Stations: {stationList}");
                }
                else
                {
                    newRecipe.m_repairStation = repairStation;
                }
            }

            // Create a requirement list and assign instances of the requirement prefabs to it
            var reqs = new List <Piece.Requirement>();

            foreach (var requirement in itemDef.Resources)
            {
                var reqPrefab = instance.GetItemPrefab(requirement.Item);
                if (reqPrefab == null)
                {
                    Logger.LogError($"Could not load requirement item: {requirement.Item}");
                    continue;
                }

                reqs.Add(new Piece.Requirement {
                    m_amount = requirement.Amount, m_resItem = reqPrefab.GetComponent <ItemDrop>()
                });
            }

            newRecipe.m_resources = reqs.ToArray();

            return(newRecipe);
        }