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 { 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(CoreEntity); _position = _world.GetComponent(CoreEntity); _rotation = _world.GetComponent(CoreEntity); _inputState = _world.GetComponent(CoreEntity); _characterState = _world.GetComponent(CoreEntity); _camera = GetNode("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) 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 && _camera != null) { _inputState.MuzzlePosition = _camera.GlobalPosition.ToGameCore(); _inputState.MuzzleDirection = (-_camera.GlobalTransform.Basis.Z).ToGameCore(); } } }