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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add AtmosphereVolumeTemplate
  • Loading branch information
LeeTwentyThree committed Dec 18, 2023
commit 8ec9f40b325a12e49cd698ecb7cee23c59a91433
100 changes: 100 additions & 0 deletions Nautilus/Assets/PrefabTemplates/AtmosphereVolumeTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections;
using Nautilus.Utility;
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>
/// 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>
public AtmosphereVolumeTemplate(PrefabInfo info, VolumeShape shape, string overrideBiome, int priority = 10) : base(info)
{
Shape = shape;
OverrideBiome = overrideBiome;
}

/// <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 = LargeWorldEntity.CellLevel.Batch;

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
}
}