Add door behavior interface and implementations for animation and linear movement
This commit is contained in:
64
Code/Resources/LerpDoorBehavior.cs
Normal file
64
Code/Resources/LerpDoorBehavior.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using cryptonymthunder.Code.Interfaces;
|
||||
using GameCore.ECS;
|
||||
using Godot;
|
||||
|
||||
namespace CryptonymThunder.Code.Resources;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class LerpDoorBehavior : DoorBehaviorResource, IDoorBehavior
|
||||
{
|
||||
[Export] private NodePath _nodeToMovePath;
|
||||
|
||||
[ExportGroup("Position")]
|
||||
[Export] private Vector3 _closedPosition = Vector3.Zero;
|
||||
[Export] private Vector3 _openPosition = Vector3.Up * 3f;
|
||||
|
||||
[ExportGroup("Rotation")]
|
||||
[Export] private Vector3 _closedRotationDegrees = Vector3.Zero;
|
||||
[Export] private Vector3 _openRotationDegrees = Vector3.Zero;
|
||||
|
||||
private Node3D _nodeToMove;
|
||||
|
||||
public override void Initialize(Node3D doorNode, World world)
|
||||
{
|
||||
if (_nodeToMovePath == null)
|
||||
{
|
||||
_nodeToMove = doorNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
_nodeToMove = doorNode.GetNode<Node3D>(_nodeToMovePath);
|
||||
if (_nodeToMove == null)
|
||||
{
|
||||
world.Logger.Error($"[LerpDoorBehavior] Could not find NodeToMove at path '{_nodeToMovePath}' on door '{doorNode.Name}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void UpdateProgress(float progress, float delta)
|
||||
{
|
||||
if (_nodeToMove == null) return;
|
||||
|
||||
_nodeToMove.RotationDegrees = _closedRotationDegrees.Lerp(_openRotationDegrees, progress);
|
||||
|
||||
var targetPosition = _closedPosition.Lerp(_openPosition, progress);
|
||||
|
||||
if (_nodeToMove is CharacterBody3D doorBody)
|
||||
{
|
||||
if (delta > 0f)
|
||||
{
|
||||
var velocity = (targetPosition - doorBody.Position) / delta;
|
||||
doorBody.Velocity = velocity;
|
||||
doorBody.MoveAndSlide();
|
||||
}
|
||||
else
|
||||
{
|
||||
doorBody.Position = targetPosition;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_nodeToMove.Position = targetPosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user