91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace GravityFunctionality
|
|
{
|
|
/// <summary>
|
|
/// Available types of gravity.
|
|
/// Need to match the existing functions!
|
|
/// </summary>
|
|
public enum Gravity
|
|
{
|
|
DownGravity = 0, UpGravity = 1, NoGravity = 2, InwardsGravity = 3, OutwardsGravity = 4
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializable mapping of a color to a gravity.
|
|
/// </summary>
|
|
[Serializable]
|
|
public class GravityColorEntry
|
|
{
|
|
public Gravity gravity;
|
|
public Color color;
|
|
}
|
|
|
|
public static class GravityHelpers
|
|
{
|
|
/// <summary>
|
|
/// Function which returns a gravity zero vector.
|
|
/// </summary>
|
|
private static readonly Func<Transform, Transform, Vector3> _noGravity =
|
|
new((gravitySource, target) => new Vector3());
|
|
|
|
/// <summary>
|
|
/// Function which returns a gravity vector downwards, depending
|
|
/// on the parent transforms rotation.
|
|
/// The parenting transform for a ship is the arena it's in.
|
|
/// </summary>
|
|
private static readonly Func<Transform, Transform, Vector3> _downGravity =
|
|
new((gravitySource, target) =>
|
|
gravitySource.rotation * Vector3.down);
|
|
|
|
/// <summary>
|
|
/// Function which returns a gravity vector upwards, depending
|
|
/// on the parent transforms rotation.
|
|
/// The parenting transform for a ship is the arena it's in.
|
|
/// </summary>
|
|
private static readonly Func<Transform, Transform, Vector3> _upGravity =
|
|
new((gravitySource, target) =>
|
|
gravitySource.rotation * Vector3.up);
|
|
|
|
/// <summary>
|
|
/// Function which returns a gravity vector towards the center of the parenting transform.
|
|
/// The parenting transform for a ship is the arena it's in.
|
|
/// </summary>
|
|
private static readonly Func<Transform, Transform, Vector3> _inwardsGravity =
|
|
new((gravitySource, target) =>
|
|
(-target.position + gravitySource.position).normalized);
|
|
|
|
/// <summary>
|
|
/// Function which returns a gravity vector outwards from the center of the parenting transform.
|
|
/// The parenting transform for a ship is the arena it's in.
|
|
/// </summary>
|
|
private static readonly Func<Transform, Transform, Vector3> _outwardsGravity =
|
|
new((gravitySource, target) =>
|
|
(target.position - gravitySource.position).normalized);
|
|
|
|
/// <summary>
|
|
/// Gets the gravity function belonging to the supplied gravity enum.
|
|
/// </summary>
|
|
/// <param name="gravity">Enum for the gravity function to get.</param>
|
|
/// <returns>A gravity function</returns>
|
|
public static Func<Transform, Transform, Vector3> GetGravityFunction(Gravity gravity)
|
|
{
|
|
switch (gravity)
|
|
{
|
|
case Gravity.DownGravity:
|
|
return _downGravity;
|
|
case Gravity.UpGravity:
|
|
return _upGravity;
|
|
case Gravity.NoGravity:
|
|
return _noGravity;
|
|
case Gravity.OutwardsGravity:
|
|
return _outwardsGravity;
|
|
case Gravity.InwardsGravity:
|
|
return _inwardsGravity;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
} |