86 lines
1.6 KiB
C#
86 lines
1.6 KiB
C#
using Godot;
|
|
|
|
public partial class GravityReceiver : Node3D
|
|
{
|
|
public int CheckInterval = 100;
|
|
public bool InGravityZone = false;
|
|
public GravityZone Zone { get; private set; }
|
|
public Node3D Parent { get; private set; }
|
|
|
|
private int checkCounter = 0;
|
|
private Node3D owner;
|
|
private Node3D root;
|
|
private GravityZone oldZone;
|
|
private Node3D oldParent;
|
|
|
|
public override void _Ready()
|
|
{
|
|
owner = GetParent<Node3D>();
|
|
root = GetTree().Root.GetNode<Node3D>("Game");
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
checkCounter++;
|
|
if (checkCounter >= CheckInterval)
|
|
{
|
|
checkCounter = 0;
|
|
if (InGravityZone && owner.GetParent() != Parent)
|
|
{
|
|
owner.Reparent(Parent, true);
|
|
}
|
|
if (!InGravityZone && owner.GetParent() != root)
|
|
{
|
|
owner.Reparent(root, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void EnterGravityZone(GravityZone gravityZone, Node3D newParent)
|
|
{
|
|
if (Zone != null)
|
|
{
|
|
oldZone = Zone;
|
|
oldParent = Parent;
|
|
}
|
|
|
|
InGravityZone = true;
|
|
Zone = gravityZone;
|
|
Parent = newParent;
|
|
}
|
|
|
|
public void ExitGravityZone(GravityZone gravityZone)
|
|
{
|
|
if (gravityZone == Zone)
|
|
{
|
|
if (oldZone != null && oldZone.GetOverlappingBodies().Contains(owner))
|
|
{
|
|
GravityZone tempZone = Zone;
|
|
Node3D tempParent = Parent;
|
|
|
|
InGravityZone = true;
|
|
Zone = oldZone;
|
|
Parent = oldParent;
|
|
|
|
oldZone = tempZone;
|
|
oldParent = tempParent;
|
|
}
|
|
else
|
|
{
|
|
InGravityZone = false;
|
|
Zone = null;
|
|
Parent = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public double GetGravityStrength()
|
|
{
|
|
return Zone.Gravity;
|
|
}
|
|
|
|
public Vector3 GetGravityDirection()
|
|
{
|
|
return Zone.GlobalTransform.Basis.Y;
|
|
}
|
|
}
|