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

Biome Handler #511

Merged
merged 19 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Example mod/BiomeHandlerExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using BepInEx;
using Nautilus.Assets;
using Nautilus.Assets.PrefabTemplates;
using Nautilus.Handlers;
using Nautilus.Utility;
using UnityEngine;

namespace Nautilus.Examples;

[BepInPlugin("com.snmodding.nautilus.biomehandler", "Nautilus Biome Example Mod", Nautilus.PluginInfo.PLUGIN_VERSION)]
[BepInDependency("com.snmodding.nautilus")]
public class BiomeHandlerExample : BaseUnityPlugin
{
private void Awake()
{
// Register the new biome into the game
var lilyPadsFogSettings = BiomeUtils.CreateBiomeSettings(new Vector3(20, 5, 6), 0.6f, Color.white, 0.45f,
new Color(0.18f, 0.604f, 0.404f), 0.05f, 20, 1, 1.25f, 20);
#if SUBNAUTICA
BiomeHandler.RegisterBiome("nautilusexamplebiome", lilyPadsFogSettings, new BiomeHandler.SkyReference("SkyKelpForest"));
#elif BELOWZERO
BiomeHandler.RegisterBiome("nautilusexamplebiome", lilyPadsFogSettings, new BiomeHandler.SkyReference("SkyLilyPads"));
#endif

#if SUBNAUTICA
// Add wreck ambience & music
BiomeHandler.AddBiomeMusic("nautilusexamplebiome", AudioUtils.GetFmodAsset("event:/env/music/wreak_ambience_big_music"));
BiomeHandler.AddBiomeAmbience("nautilusexamplebiome", AudioUtils.GetFmodAsset("event:/env/background/wreak_ambience_big"), FMODGameParams.InteriorState.OnlyOutside);
#endif

// Create an atmosphere volume for the biome
PrefabInfo volumePrefabInfo = PrefabInfo.WithTechType("NautilusExampleBiomeSphereVolume");
CustomPrefab volumePrefab = new CustomPrefab(volumePrefabInfo);
AtmosphereVolumeTemplate volumeTemplate = new AtmosphereVolumeTemplate(volumePrefabInfo, AtmosphereVolumeTemplate.VolumeShape.Sphere, "nautilusexamplebiome");
volumePrefab.SetGameObject(volumeTemplate);
volumePrefab.Register();

// Add the biome somewhere to the world
CoordinatedSpawnsHandler.RegisterCoordinatedSpawn(new SpawnInfo(volumePrefabInfo.ClassID, new Vector3(-1400, -80, 600), Quaternion.identity, new Vector3(50, 50, 50)));
}
}
107 changes: 107 additions & 0 deletions Nautilus/Assets/PrefabTemplates/AtmosphereVolumeTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections;
using UnityEngine;

namespace Nautilus.Assets.PrefabTemplates;

/// <summary>
/// A template for Atmosphere Volumes, which are basic invisible triggers for mini-biomes.
/// </summary>
public class AtmosphereVolumeTemplate : PrefabTemplate
{
private const int AtmosphereVolumesLayer = 21;

/// <summary>
/// The shape of this atmosphere volume.
/// </summary>
public VolumeShape Shape { get; set; }
/// <summary>
/// The biome type used by this atmosphere volume.
/// </summary>
public string OverrideBiome { get; set; }
/// <summary>
/// The priority of this atmosphere volume. Atmosphere volumes with higher priorities override those with lower priorities. The default priority is 10.
/// </summary>
public int Priority { get; set; }

/// <summary>
/// Determines the loading distance of this atmosphere volume prefab. Default value is <see cref="LargeWorldEntity.CellLevel.Far"/>. Although vanilla prefabs always use Batch for this, this does not work with our custom systems.
/// </summary>
public LargeWorldEntity.CellLevel CellLevel { get; set; }

/// <summary>
/// Callback that will get called after the prefab is retrieved. Use this to modify or process your prefab further more.
/// </summary>
public System.Action<GameObject> ModifyPrefab { get; set; }

/// <summary>
/// Callback that will get called after the prefab is retrieved. Use this to modify or process your prefab further more asynchronously.
/// </summary>
public System.Func<GameObject, IEnumerator> ModifyPrefabAsync { get; set; }

/// <summary>
/// Creates a new prefab template for an asset bundle.
/// </summary>
/// <param name="info">The prefab info to base this template off of.</param>
/// <param name="shape">The shape of this atmosphere volume.</param>
/// <param name="overrideBiome">The biome type used by this atmosphere volume.</param>
/// <param name="priority">The priority of this atmosphere volume. Atmosphere volumes with higher priorities override those with lower priorities.</param>
/// <param name="cellLevel">Determines the loading distance of this atmosphere volume prefab. Although vanilla prefabs always use Batch for this, this does not work with our custom systems.</param>
public AtmosphereVolumeTemplate(PrefabInfo info, VolumeShape shape, string overrideBiome, int priority = 10, LargeWorldEntity.CellLevel cellLevel = LargeWorldEntity.CellLevel.Far) : base(info)
{
Shape = shape;
OverrideBiome = overrideBiome;
Priority = priority;
CellLevel = cellLevel;
}

/// <summary>
/// Creates an atmosphere volume prefab.
/// </summary>
public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)
{
var prefab = new GameObject(info.ClassID);
prefab.layer = AtmosphereVolumesLayer;

Collider collider = Shape switch
{
VolumeShape.Sphere => prefab.AddComponent<SphereCollider>(),
VolumeShape.Cube => prefab.AddComponent<BoxCollider>(),
VolumeShape.Capsule => prefab.AddComponent<CapsuleCollider>(),
_ => throw new NotImplementedException()
};
collider.isTrigger = true;

prefab.AddComponent<PrefabIdentifier>().ClassId = info.ClassID;
prefab.AddComponent<LargeWorldEntity>().cellLevel = CellLevel;

var atmosphereVolume = prefab.AddComponent<AtmosphereVolume>();
atmosphereVolume.overrideBiome = OverrideBiome;
atmosphereVolume.priority = Priority;

ModifyPrefab?.Invoke(prefab);
if (ModifyPrefabAsync is { })
yield return ModifyPrefabAsync(prefab);

gameObject.Set(prefab);
}

/// <summary>
/// The shape of an atmosphere volume's trigger.
/// </summary>
public enum VolumeShape
{
/// <summary>
/// Sphere with default radius 0.5m (diameter 1m).
/// </summary>
Sphere,
/// <summary>
/// Cube with default dimensions of 1x1x1m.
/// </summary>
Cube,
/// <summary>
/// Capsule with default radius of 0.5m and height of 2m.
/// </summary>
Capsule
}
}
Loading