Skip to content

Commit

Permalink
Merge branch 'master' into Issue367
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPurple6411 committed Jan 2, 2024
2 parents 042afec + d27d803 commit ac486d2
Show file tree
Hide file tree
Showing 26 changed files with 1,422 additions and 412 deletions.
8 changes: 8 additions & 0 deletions CLA/Signatures.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
"created_at": "2023-11-24T08:52:03Z",
"repoId": 123711758,
"pullRequestNo": 505
},
{
"name": "tornac1234",
"id": 24827220,
"comment_id": 1873536486,
"created_at": "2024-01-02T00:09:02Z",
"repoId": 123711758,
"pullRequestNo": 523
}
]
}
44 changes: 44 additions & 0 deletions Example mod/BiomeHandlerExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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)));

// Add this biome to the "biome" command
ConsoleCommandsHandler.AddBiomeTeleportPosition("nautilusexamplebiome", new Vector3(-1400, -80, 600));
}
}
4 changes: 2 additions & 2 deletions Nautilus/Assets/Gadgets/GadgetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ public static ICustomPrefab SetSpawns(this ICustomPrefab customPrefab, params Sp
{
customPrefab.AddOnRegister(() =>
{
foreach ((Vector3 position, Vector3 eulerAngles) in spawnLocations)
foreach (var spawnLocation in spawnLocations)
{
CoordinatedSpawnsHandler.RegisterCoordinatedSpawn(new SpawnInfo(customPrefab.Info.ClassID, position, eulerAngles));
CoordinatedSpawnsHandler.RegisterCoordinatedSpawn(new SpawnInfo(customPrefab.Info.ClassID, spawnLocation.Position, spawnLocation.EulerAngles, spawnLocation.Scale));
}
});

Expand Down
6 changes: 2 additions & 4 deletions Nautilus/Assets/ModPrefabRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class ModPrefabRequest: IPrefabRequest

private readonly PrefabInfo prefabInfo;

private CoroutineTask<GameObject> task;
private IEnumerator task;

private TaskResult<GameObject> taskResult;

Expand All @@ -37,7 +37,7 @@ private void Init()
return;
}

task = new CoroutineTask<GameObject>(PrefabHandler.GetPrefabAsync(taskResult, prefabInfo, factory), taskResult);
task = PrefabHandler.GetPrefabAsync(taskResult, prefabInfo, factory);
}

public object Current
Expand Down Expand Up @@ -66,14 +66,12 @@ public bool MoveNext()

public void Reset()
{
Init();
task.Reset();
Done = false;
}

public void Release()
{
ModPrefabCache.RemovePrefabFromCache(prefabInfo.ClassID);
taskResult = null;
task = null;
Done = false;
Expand Down
117 changes: 117 additions & 0 deletions Nautilus/Assets/PrefabTemplates/AtmosphereVolumeTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using System.Collections;
using Nautilus.MonoBehaviours;
using UnityEngine;

namespace Nautilus.Assets.PrefabTemplates;

/// <summary>
/// A template for Atmosphere Volumes, which are basic invisible triggers for mini-biomes. Atmosphere volumes can affect fog, music, ambient sounds and even the player's swim speed.
/// </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>
/// Whether this atmosphere volume can be entered while inside a vehicle or not. For unknown reasons, this is NOT true for base game volumes. However, in this template, it is true by default.
/// </summary>
public bool CanEnterWhileInsideVehicle { get; set; } = true;

/// <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;

if (CanEnterWhileInsideVehicle)
{
prefab.AddComponent<AtmosphereVolumeTriggerFix>().atmosphereVolume = atmosphereVolume;
}

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
}
}
19 changes: 18 additions & 1 deletion Nautilus/Assets/SpawnLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,21 @@ namespace Nautilus.Assets;
/// </summary>
/// <param name="Position">The world position.</param>
/// <param name="EulerAngles">Euler angles for the rotation the spawned object will appear with.</param>
public record SpawnLocation(Vector3 Position, Vector3 EulerAngles = default);
public record SpawnLocation(Vector3 Position, Vector3 EulerAngles = default)
{
/// <summary>
/// The scale that the object is spawned at. If default (0, 0, 0) will be resolved to (1, 1, 1).
/// </summary>
public Vector3 Scale { get; init; }

/// <summary>
/// Defines the spawn location with world position and optional euler angles. Used in the Coordinated Spawns system.
/// </summary>
/// <param name="Position">The world position.</param>
/// <param name="EulerAngles">Euler angles for the rotation the spawned object will appear with.</param>
/// <param name="Scale">The scale that the object is spawned at. If default (0, 0, 0) will be resolved to (1, 1, 1).</param>
public SpawnLocation(Vector3 Position, Vector3 EulerAngles, Vector3 Scale) : this(Position, EulerAngles)
{
this.Scale = Scale;
}
}
Loading

0 comments on commit ac486d2

Please sign in to comment.