using System;
using UnityEngine;
namespace GravityFunctionality
{
///
/// Available types of gravity.
/// Need to match the existing functions!
///
public enum Gravity
{
DownGravity = 0, UpGravity = 1, NoGravity = 2, InwardsGravity = 3, OutwardsGravity = 4
}
///
/// Serializable mapping of a color to a gravity.
///
[Serializable]
public class GravityColorEntry
{
public Gravity gravity;
public Color color;
}
public static class GravityHelpers
{
///
/// Function which returns a gravity zero vector.
///
private static readonly Func _noGravity =
new((gravitySource, target) => new Vector3());
///
/// 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.
///
private static readonly Func _downGravity =
new((gravitySource, target) =>
gravitySource.rotation * Vector3.down);
///
/// 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.
///
private static readonly Func _upGravity =
new((gravitySource, target) =>
gravitySource.rotation * Vector3.up);
///
/// 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.
///
private static readonly Func _inwardsGravity =
new((gravitySource, target) =>
(-target.position + gravitySource.position).normalized);
///
/// 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.
///
private static readonly Func _outwardsGravity =
new((gravitySource, target) =>
(target.position - gravitySource.position).normalized);
///
/// Gets the gravity function belonging to the supplied gravity enum.
///
/// Enum for the gravity function to get.
/// A gravity function
public static Func 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;
}
}
}
}