Files
cryptonhym-thunder/Code/Presenters/CharacterBody3DPresenter.cs

86 lines
2.8 KiB
C#

using CryptonymThunder.Code.Extensions;
using GameCore.ECS;
using GameCore.ECS.Interfaces;
using GameCore.Input;
using GameCore.Movement;
using GameCore.Physics;
using Godot;
namespace CryptonymThunder.Code.Presenters;
[GlobalClass]
public partial class CharacterBody3DPresenter : CharacterBody3D, IEntityPresenter, IPresenterComponent
{
[Export] private Node3D _muzzleNode;
private World _world;
private VelocityComponent _velocity;
private PositionComponent _position;
private RotationComponent _rotation;
private CharacterStateComponent _characterState;
private InputStateComponent _inputState;
private Camera3D _camera;
public Entity CoreEntity { get; set; }
public void Initialize(Entity coreEntity, World world)
{
CoreEntity = coreEntity;
_world = world;
_velocity = _world.GetComponent<VelocityComponent>(CoreEntity);
_position = _world.GetComponent<PositionComponent>(CoreEntity);
_rotation = _world.GetComponent<RotationComponent>(CoreEntity);
_inputState = _world.GetComponent<InputStateComponent>(CoreEntity);
_characterState = _world.GetComponent<CharacterStateComponent>(CoreEntity);
_camera = GetNodeOrNull<Camera3D>("CameraPivot/Camera3D");
}
public void SyncToPresentation(float delta)
{
if (_rotation != null)
{
var coreRotation = _rotation.Rotation;
Rotation = new Vector3(Rotation.X, coreRotation.Y, Rotation.Z);
}
if (_velocity == null)
{
_world.Logger.Warn("No VelocityComponent found for CharacterBody3DPresenter.");
return;
}
var godotVelocity = Velocity;
godotVelocity.X = _velocity.DesiredVelocity.X;
godotVelocity.Y = _velocity.DesiredVelocity.Y;
godotVelocity.Z = _velocity.DesiredVelocity.Z;
Velocity = godotVelocity;
MoveAndSlide();
}
public void SyncToCore(float delta)
{
if (_position != null) _position.Position = GlobalPosition.ToGameCore();
if (_velocity != null) _velocity.ActualVelocity = Velocity.ToGameCore();
if (_characterState != null) _characterState.IsOnFloor = IsOnFloor();
if (_inputState != null)
{
if (_camera != null)
{
_inputState.MuzzlePosition = _camera.GlobalPosition.ToGameCore();
_inputState.MuzzleDirection = (-_camera.GlobalTransform.Basis.Z).ToGameCore();
}
else
{
if (_muzzleNode != null)
{
_inputState.MuzzlePosition = _muzzleNode.GlobalPosition.ToGameCore();
}
else
{
_inputState.MuzzlePosition = GlobalPosition.ToGameCore();
}
}
}
}
}