summaryrefslogtreecommitdiffhomepage
path: root/Assets/Scripts/CustomGravity.cs
blob: feebab290e5712743179fd9bf8d0affd305303d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Gravity Source Manager
//	This is a component holds all the gravity sources
//	and provides a simple api for physics objects to
//	query and get net gravity effects.
public class CustomGravity : MonoBehaviour
{
	static List<GravitySource> sources = new List<GravitySource>();

	public static Vector3 GetGravity(Vector3 position, out Vector3 upAxis)
	{
		Vector3 g = Vector3.zero;
		for(int i = 0; i < sources.Count; i++)
			g += sources[i].GetGravity(position);
		upAxis = -g.normalized;
		return g;
	}
	public static Vector3 GetGravity(Vector3 position)
	{
		Vector3 g = Vector3.zero;
		for(int i = 0; i < sources.Count; i++)
			g += sources[i].GetGravity(position);
		return g;
	}

	public static Vector3 GetUpAxis(Vector3 position)
	{
		return -GetGravity(position).normalized;
	}

	public static void Register(GravitySource source)
	{
		Debug.Assert(
				!sources.Contains(source),
				"WARNING: This source is already registered!",
				source
				);
		sources.Add(source);
	}
	public static void Unregister(GravitySource source)
	{
		Debug.Assert(
				sources.Contains(source),
				"WARNING: Trying to unregister unknown gravity source!",
				source
				);
		sources.Remove(source);
	}
}