181 lines
7.2 KiB
C#
181 lines
7.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using KinematicCharacterController;
|
|
using BITKit.StateMachine;
|
|
using Net.Client;
|
|
using Net.Share;
|
|
using Unity.Mathematics;
|
|
|
|
namespace BITFALL.Entites
|
|
{
|
|
public partial class EntityKinematicMovement : EntityInputComponent, IHealthCallback, IEntityMovement
|
|
{
|
|
[Header(Constant.Header.Settings)]
|
|
public float moveSpeed = 2.5f;
|
|
[Header(Constant.Header.Components)]
|
|
public KinematicCharacterMotor motor;
|
|
[Header(Constant.Header.Reference)]
|
|
[SerializeReference, SubclassSelector] public References _velocity;
|
|
[SerializeReference, SubclassSelector] public References _speed;
|
|
[SerializeReference, SubclassSelector] public References _vertical;
|
|
[SerializeReference, SubclassSelector] public References _horizontal;
|
|
[SerializeReference, SubclassSelector] public References _MoveHorizontal;
|
|
[SerializeReference, SubclassSelector] public References _MoveVertical;
|
|
[SerializeReference, SubclassSelector] public References _sqrMagnitude;
|
|
[SerializeReference, SubclassSelector] public References _direction;
|
|
[SerializeReference, SubclassSelector] public References _rootVelocity;
|
|
[SerializeReference, SubclassSelector] public References _jump;
|
|
[SerializeReference, SubclassSelector] public References _isGrounded;
|
|
[SerializeReference, SubclassSelector] public References _isRunning;
|
|
[SerializeReference, SubclassSelector] public References _isSprint;
|
|
[SerializeReference, SubclassSelector] public References _isSliding;
|
|
[SerializeReference, SubclassSelector] public References _isProneing;
|
|
[SerializeReference, SubclassSelector] public References _isCrouched;
|
|
[SerializeReference, SubclassSelector] public References _cameraPosition;
|
|
[SerializeReference, SubclassSelector] public References _setLookAt;
|
|
[SerializeReference, SubclassSelector] public References _cameraRotation;
|
|
[SerializeReference, SubclassSelector] public References _lookRotation;
|
|
[SerializeReference, SubclassSelector] public References _cancel;
|
|
[SerializeReference, SubclassSelector] public References _ads;
|
|
[SerializeReference, SubclassSelector] public References _angularVelocity;
|
|
[Header(Constant.Header.Input)]
|
|
public InputActionReference viewAction;
|
|
public InputActionReference movementAction;
|
|
public InputActionReference runAction;
|
|
public InputActionReference jumpAction;
|
|
public InputActionReference crouchAction;
|
|
public InputActionGroup inputActionGroup = new();
|
|
[Header(Constant.Header.State)]
|
|
public ExpectState<bool> runState;
|
|
public ExpectState<bool> sprintState;
|
|
public ExpectState<bool> crouchState;
|
|
public ExpectState<bool> groundState;
|
|
public ExpectState<bool> jumpState;
|
|
public ExpectState<bool> vaultState;
|
|
[Header(Constant.Header.Gameobjects)]
|
|
public Transform cameraRoot;
|
|
public List<Collider> ignoreColliders = new();
|
|
[Header(Constant.Header.InternalVariables)]
|
|
public Vector3 moveInput;
|
|
public Vector3 lookInput;
|
|
public Vector3 matchTargetPosition;
|
|
public float currentGravity;
|
|
ClientBase client;
|
|
IStateMachine<IKinematicMovementState> stateMachine => this as IStateMachine<IKinematicMovementState>;
|
|
public float _moveSpeed => (runState.being, sprintState.being, crouchState.being) switch
|
|
{
|
|
(_, _, true) => 1,
|
|
(true, false, _) => moveSpeed * 2f,
|
|
(_, true, false) => moveSpeed * 3f,
|
|
_ => moveSpeed,
|
|
};
|
|
public override void Initialize(IEntity entity)
|
|
{
|
|
base.Initialize(entity);
|
|
entity.Set<IEntityMovement>(this);
|
|
}
|
|
public override void OnAwake()
|
|
{
|
|
lookInput = MathV.TransientRotationAxis(transform.eulerAngles);
|
|
}
|
|
public override void OnStart()
|
|
{
|
|
foreach (var state in characterStates)
|
|
{
|
|
StateDictonary.Add(state.GetType(), state);
|
|
}
|
|
KinematicCharacterSystem.EnsureCreation();
|
|
motor.CharacterController = this;
|
|
KinematicCharacterSystem.CharacterMotors.Add(motor);
|
|
|
|
inputActionGroup.RegisterCallback(viewAction, OnView);
|
|
inputActionGroup.RegisterCallback(movementAction, OnMovement);
|
|
inputActionGroup.RegisterCallback(runAction, OnRun);
|
|
inputActionGroup.RegisterCallback(jumpAction, OnJump);
|
|
inputActionGroup.RegisterCallback(crouchAction, OnCrouch);
|
|
|
|
entity.AddListener<IMovementCancelAction>(CancelMovement);
|
|
}
|
|
public override void OnSpawn()
|
|
{
|
|
if (isLocalPlayer)
|
|
stateMachine.TransitionState(characterStates[0]);
|
|
}
|
|
public override void OnFixedUpdate(float deltaTime)
|
|
{
|
|
if (isSpawned)
|
|
{
|
|
/* client.SendRT(nameof(EntityProxyMovement.RpcSyncMovement)
|
|
, entity.Id,
|
|
(Net.Vector3)motor.Velocity,
|
|
(Net.Vector3)motor.TransientPosition); */
|
|
// FGame.ClientAllRpc(
|
|
// nameof(IEntityMovement.SyncMovement),
|
|
// entity.Id,
|
|
// (float3)motor.Velocity,
|
|
// (float3)motor.TransientPosition,
|
|
// (quaternion)motor.TransientRotation,
|
|
// motor.GroundingStatus.FoundAnyGround
|
|
// );
|
|
}
|
|
}
|
|
public override void OnUpdate(float deltaTime)
|
|
{
|
|
if (isLocalPlayer)
|
|
{
|
|
var offset = new Vector3(0, 1.2f * (crouchState.being ? 0.64f : 1), 0.2f);
|
|
var lerp = Vector3.Lerp(
|
|
cameraRoot.localPosition,
|
|
offset,
|
|
5 * deltaTime
|
|
);
|
|
cameraRoot.localPosition = lerp;
|
|
}
|
|
|
|
}
|
|
public override void OnLateUpdate(float deltaTime)
|
|
{
|
|
if (isLocalPlayer)
|
|
{
|
|
var rotation = Quaternion.Euler(lookInput);
|
|
|
|
|
|
cameraRoot.rotation = rotation;
|
|
|
|
entity.Set(_cameraRotation, rotation);
|
|
}
|
|
}
|
|
public override void OnDestroyComponent()
|
|
{
|
|
KinematicCharacterSystem.CharacterMotors.Remove(motor);
|
|
}
|
|
|
|
void IHealthCallback.OnSetAlive(bool alive)
|
|
{
|
|
if (isLocalPlayer)
|
|
{
|
|
inputActionGroup.allowInput.SetElements(nameof(EntityHealth), alive);
|
|
inputActionGroup.allowInput.SetDisableElements(nameof(EntityHealth), !alive);
|
|
}
|
|
}
|
|
|
|
void IHealthCallback.OnSetHP(int hp)
|
|
{
|
|
}
|
|
|
|
void CancelMovement(IMovementCancelAction action)
|
|
{
|
|
stateMachine?.CurrentState?.OnCancelMovement(action);
|
|
}
|
|
|
|
void IEntityMovement.SyncMovement(Vector3 velocity, Vector3 position, Quaternion rotation, bool isGrounded)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|