Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic tutorial on buildables #433

Open
LeeTwentyThree opened this issue Jul 4, 2023 · 0 comments
Open

Basic tutorial on buildables #433

LeeTwentyThree opened this issue Jul 4, 2023 · 0 comments

Comments

@LeeTwentyThree
Copy link
Member

LeeTwentyThree commented Jul 4, 2023

Describe the feature
Tutorial on a clone buildable and a buildable with custom assets. Provide examples.

Purpose
Nobody knows how to do this, because there aren't any good examples.

Example code:

Based on a vanilla model:

using Nautilus.Crafting;
using Nautilus.Assets;
using Nautilus.Assets.Gadgets;
using Nautilus.Utility;
using UnityEngine;
using Nautilus.Assets.PrefabTemplates;

// ...

public static class BuildableTallLocker
{
    public static PrefabInfo Info { get; } = PrefabInfo.WithTechType("TallLocker", "Tall Locker", "A tall locker.")
        // set the icon to that of the vanilla locker:
        .WithIcon(SpriteManager.Get(TechType.Locker));

    public static void Register()
    {
        // create prefab:
        CustomPrefab prefab = new CustomPrefab(Info);

        // copy the model of a vanilla wreck piece (which looks like a taller locker):
        CloneTemplate lockerClone = new CloneTemplate(Info, "cd34fecd-794c-4a0c-8012-dd81b77f2840");

        // modify the cloned model:
        lockerClone.ModifyPrefab += obj =>
        {
            // allow it to be placced inside bases and submarines on the ground, and can be rotated:
            ConstructableFlags constructableFlags = ConstructableFlags.Inside | ConstructableFlags.Rotatable | ConstructableFlags.Ground | ConstructableFlags.Submarine;

            // find the object that holds the model:
            GameObject model = obj.transform.Find("submarine_locker_04").gameObject;

            // this line is only necessary for the tall locker so that the door is also part of the model:
            obj.transform.Find("submarine_locker_03_door_01").parent = model.transform;

            // add all components necessary for it to be built:
            PrefabUtils.AddConstructable(obj, Info.TechType, constructableFlags, model);

            // allow it to be opened as a storage container:
            PrefabUtils.AddStorageContainer(obj, "StorageRoot", "TallLocker", 3, 8, true);
        };

        // assign the created clone model to the prefab itself:
        prefab.SetGameObject(lockerClone);

        // assign it to the correct tab in the builder tool:
        prefab.SetPdaGroupCategory(TechGroup.InteriorModules, TechCategory.InteriorModule);

        // set recipe:
        prefab.SetRecipe(new RecipeData(new CraftData.Ingredient(TechType.Titanium, 3), new CraftData.Ingredient(TechType.Glass, 1)));

        // finally, register it into the game:
        prefab.Register();
    }
}

With Asset Bundle:

using Nautilus.Crafting;
using Nautilus.Assets;
using Nautilus.Assets.Gadgets;

// ...

public static class BuildableDecoration
{
    public static PrefabInfo Info { get; } = PrefabInfo.WithTechType("BuildableDecoration", "Custom decoration", "A basic buildable object.");

    public static void Register()
    {
        CustomPrefab prefab = new CustomPrefab(Info);
        prefab.SetGameObject(GetGameObject);
        prefab.SetPdaGroupCategory(TechGroup.ExteriorModules, TechCategory.ExteriorModule);
        prefab.SetRecipe(new RecipeData(new CraftData.Ingredient(TechType.Titanium, 2), new CraftData.Ingredient(TechType.FiberMesh, 2)));
        prefab.Register();
    }

    private static GameObject GetGameObject()
    {
        GameObject gameObject = Plugin.assetBundle.LoadAsset<GameObject>("NameOfAssetFromBundle");

        PrefabUtils.AddBasicComponents(gameObject, Info.ClassID, Info.TechType, LargeWorldEntity.CellLevel.Medium);

        Constructable cstr = PrefabUtils.AddConstructable(gameObject, Info.TechType,
            ConstructableFlags.Outside | ConstructableFlags.AllowedOnConstructable | ConstructableFlags.Ground | ConstructableFlags.Rotatable,
            gameObject.transform.Find("model").gameObject);

        MaterialUtils.ApplySNShaders(gameObject);

        return gameObject;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant