sasalka/Code/Gravity/InteractTeleporter.cs
2025-06-08 23:43:22 +03:00

55 lines
1.5 KiB
C#

namespace Sandbox.Gravity;
public sealed class InteractTeleporter : Component, Component.ITriggerListener
{
[Property] public GameObject ToPosGameObject { get; set; }
[Property, Sync] public List<Dedugan> Players { get; set; } = new();
public void OnTriggerEnter( Collider other )
{
var otherEntity = other.GameObject;
if ( otherEntity.Components.TryGet<Dedugan>( out var controller ) )
{
Players.Add( controller );
}
}
public void OnTriggerExit( Collider other )
{
var otherEntity = other.GameObject;
if ( otherEntity.Components.TryGet<Dedugan>( out var controller ) )
{
Players.Remove( controller );
}
}
public void TeleportAll()
{
foreach ( var controller in Players )
{
Teleport( controller );
}
}
[Rpc.Broadcast]
public void Teleport( Dedugan controller )
{
controller.OverrideGravity = WorldRotation.Down;
controller.GameObject.WorldPosition = ToPosGameObject.WorldPosition;
controller.EyeAngles = new Angles( 0, ToPosGameObject.WorldRotation.Yaw(), 0 ).ToRotation();
controller.Renderer.LocalRotation = new Angles( 0, ToPosGameObject.WorldRotation.Yaw(), 0 ).ToRotation();
}
[Rpc.Broadcast]
public void Teleport( Dedugan controller, Vector3 toPos )
{
controller.OverrideGravity = WorldRotation.Down;
controller.GameObject.WorldPosition = toPos;
controller.EyeAngles = new Angles( 0, ToPosGameObject.WorldRotation.Yaw(), 0 ).ToRotation();
controller.Renderer.LocalRotation = new Angles( 0, ToPosGameObject.WorldRotation.Yaw(), 0 ).ToRotation();
}
}