cvr-props/Assets/ABI.CCK/Components/CVRWorld.cs

360 lines
12 KiB
C#
Raw Normal View History

2023-01-22 16:38:23 +01:00
using System;
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.Serialization;
2023-01-22 16:38:23 +01:00
#if CCK_ADDIN_HIGHLIGHT_PLUS
using HighlightPlus;
#endif
namespace ABI.CCK.Components
{
[ExecuteInEditMode]
[AddComponentMenu("ChilloutVR/CVR World")]
[HelpURL("https://developers.abinteractive.net/cck/components/world/")]
public class CVRWorld : MonoBehaviour, ICCK_Component
2023-01-22 16:38:23 +01:00
{
#region Editor Methods
public void Reset()
{
if (GetComponent<CVRAssetInfo>() != null) return;
CVRAssetInfo info = gameObject.AddComponent<CVRAssetInfo>();
info.type = CVRAssetInfo.AssetType.World;
}
#endregion
#region CVRWorldEnums
2023-01-22 16:38:23 +01:00
public enum SpawnRule
{
Sequential = 1,
Random = 2
2023-01-22 16:38:23 +01:00
}
2023-01-22 16:38:23 +01:00
public enum RespawnBehaviour
{
Respawn = 1,
Destroy = 2
2023-01-22 16:38:23 +01:00
}
#endregion
#region General Settings
2023-01-22 16:38:23 +01:00
public GameObject referenceCamera;
public GameObject[] spawns = Array.Empty<GameObject>();
public SpawnRule spawnRule = SpawnRule.Random;
public int respawnHeightY = -25;
public RespawnBehaviour objectRespawnBehaviour = RespawnBehaviour.Respawn;
// Currently unused
[HideInInspector] public CVRWarpPoint[] warpPoints = Array.Empty<CVRWarpPoint>();
#endregion
#region AdvSettings World Rules
2023-01-22 16:38:23 +01:00
public bool useAdvancedSettings = false;
2023-01-22 16:38:23 +01:00
public bool allowSpawnables = true;
public bool allowPortals = true;
public bool allowFlying = true;
public bool showNamePlates = true;
[HideInInspector] public bool enableBuilder = true; // unused
#endregion
#region AdvSettings World Graphics
#if CCK_ADDIN_HIGHLIGHT_PLUS
public HighlightProfile highlightProfile;
#endif
[Range(60f, 120f)]
public float fov = 60f;
public bool enableZoom = true;
public bool enableDepthNormals;
[FormerlySerializedAs("allowCustomFarClippingPlane")]
public bool allowExtremeFarClippingPlane = false;
#endregion
#region AdvSettings Movement Modifiers
2023-01-22 16:38:23 +01:00
public float baseMovementSpeed = 2f;
2023-01-22 16:38:23 +01:00
public float sprintMultiplier = 2f;
public float strafeMultiplier = 1f;
public float crouchMultiplier = 0.5f;
public float proneMultiplier = 0.3f;
public float flyMultiplier = 5f;
public float inAirMovementMultiplier = 1f;
public float gravity = 18f;
public float objectGravity = 9.81f;
2023-01-22 16:38:23 +01:00
public float jumpHeight = 1f;
/// <summary>
/// Friction of the air. This will be applied to the character when moving on air, including falling, being
/// pulled by gravity, or even when floating in 0 gravity.
/// </summary>
[SerializeField] public float airFriction = 0.3f;
/// <summary>
/// Mode to use to align players with the current gravity direction.
/// </summary>
[SerializeField]
public GravityZone.PlayerAlignmentMode playerGravityAlignmentMode = GravityZone.DefaultGravityAlignmentMode;
/// <summary>
/// Value used for the gravity player alignment speed when using the custom alignment mode.
/// </summary>
[SerializeField]
public float playerGravityCustomAlignmentValue = GravityZone.DefaultGravityCustomAlignmentValue;
#endregion
#region AdvSettings Collision Matrix
public bool useCustomCollisionMatrix = false;
public List<CVRCollisionListWrapper> collisionMatrix = new List<CVRCollisionListWrapper>
{
//Internal
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
//Content
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[]
{ true, true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(
new[] { true, true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[] { true, true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[] { true, true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[] { true, true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[] { true, true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[] { true, true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[] { true, true, true, true, true, true }),
new CVRCollisionListWrapper(new[] { true, true, true, true, true }),
new CVRCollisionListWrapper(new[] { true, true, true, true }),
new CVRCollisionListWrapper(new[] { true, true, true }),
new CVRCollisionListWrapper(new[] { true, true }),
new CVRCollisionListWrapper(new[] { true })
};
#endregion
#region Object Library
2023-01-22 16:38:23 +01:00
public List<CVRObjectCatalogCategory> objectCatalogCategories = new List<CVRObjectCatalogCategory>();
public List<CVRObjectCatalogEntry> objectCatalogEntries = new List<CVRObjectCatalogEntry>();
#endregion
#region Public Methods
public void CaptureCollisionMatrix()
2023-01-22 16:38:23 +01:00
{
for (int i = 0; i <= 31; i++)
{
for (int j = 0; j <= Math.Min(31 - i, 15); j++)
{
collisionMatrix[i][j] = !Physics.GetIgnoreLayerCollision(i, 31 - j);
}
}
2023-01-22 16:38:23 +01:00
}
#endregion
#region Method Calls
public void SetBaseMovementSpeed(float value)
2023-01-22 16:38:23 +01:00
{
baseMovementSpeed = value;
ApplyMovementSettings();
}
2023-01-22 16:38:23 +01:00
public void SetSprintMultiplier(float value)
{
sprintMultiplier = value;
ApplyMovementSettings();
}
public void SetStrafeMultiplier(float value)
{
strafeMultiplier = value;
ApplyMovementSettings();
}
public void SetCrouchMultiplier(float value)
{
crouchMultiplier = value;
ApplyMovementSettings();
}
public void SetProneMultiplier(float value)
{
proneMultiplier = value;
ApplyMovementSettings();
}
public void SetFlyMultiplier(float value)
{
flyMultiplier = value;
ApplyMovementSettings();
}
public void SetInAirMovementMultiplier(float value)
{
inAirMovementMultiplier = value;
ApplyMovementSettings();
}
public void SetJumpHeight(float value)
{
jumpHeight = value;
ApplyMovementSettings();
}
public void SetAirFriction(float value)
{
airFriction = value;
ApplyMovementSettings();
}
public void SetGravity(float value)
{
gravity = value;
ApplyMovementSettings();
}
public void SetObjectGravity(float value)
{
objectGravity = value;
ApplyMovementSettings();
}
public void SetPlayerGravityAlignmentMode(int value)
{
// Use `int` so they can be used via method calls
if (Enum.IsDefined(typeof(GravityZone.PlayerAlignmentMode), value))
2023-01-22 16:38:23 +01:00
{
playerGravityAlignmentMode = (GravityZone.PlayerAlignmentMode)value;
ApplyMovementSettings();
2023-01-22 16:38:23 +01:00
}
}
public void SetPlayerGravityCustomAlignmentValue(float value)
{
playerGravityCustomAlignmentValue = value;
ApplyMovementSettings();
2023-01-22 16:38:23 +01:00
}
/// <summary>
/// Applies the current CVRWorld movement setting values to the character controller.
/// </summary>
public void ApplyMovementSettings() { }
#endregion
#region UnityEvents
private void OnValidate()
2023-01-22 16:38:23 +01:00
{
// using Range attribute turns it into an ugly slider, enforced by client
playerGravityCustomAlignmentValue = Mathf.Clamp(playerGravityCustomAlignmentValue, 0, GravityZone.MaxDegreesPerSecond);
2023-01-22 16:38:23 +01:00
}
#endregion
2023-01-22 16:38:23 +01:00
}
#region Object Library Classes
[Serializable]
2023-01-22 16:38:23 +01:00
public class CVRObjectCatalogCategory
{
public string id;
public string name;
public Texture2D image;
}
[Serializable]
2023-01-22 16:38:23 +01:00
public class CVRObjectCatalogEntry
{
public string name;
public GameObject prefab;
public Texture2D preview;
public string categoryId = "";
public string guid = "";
}
#endregion
#region CVRCollisionListWrapper Class
[Serializable]
public class CVRCollisionListWrapper
{
public List<bool> collisionList = new List<bool>();
public CVRCollisionListWrapper(IEnumerable<bool> boolList)
{
foreach (bool b in boolList)
{
collisionList.Add(b);
}
}
public bool this[int key]
{
get => collisionList[key];
set => collisionList[key] = value;
}
public int Count
{
get => collisionList.Count;
}
}
#endregion
}