91 lines
3.3 KiB
C#
91 lines
3.3 KiB
C#
using System;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using SzafaKit.Console;
|
|
using SzafaKit.Foundation;
|
|
using SzafaKit.Input;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace SzafaKit.Defaults {
|
|
public class DefaultPlayerController : IController {
|
|
[Serializable]
|
|
public class Config {
|
|
public DefaultPlayer PlayerPrefab;
|
|
|
|
public InputActionReference MoveActionReference;
|
|
public InputActionReference LookActionReference;
|
|
public InputActionReference JumpActionReference;
|
|
public InputActionReference DragObjectActionReference;
|
|
public InputActionReference PrimaryActionReference;
|
|
public InputActionReference SecondaryActionReference;
|
|
}
|
|
|
|
private Config _config;
|
|
|
|
private DefaultPlayer _player;
|
|
|
|
private CVar _sensitivityCVar;
|
|
|
|
public DefaultPlayerController(Config config) {
|
|
_config = config;
|
|
}
|
|
|
|
public async UniTask OnStart(CancellationToken cancellationToken) {
|
|
_player = Object.Instantiate(_config.PlayerPrefab);
|
|
|
|
SzafaEngine.Shared.GetService<InputService>().LockCursor();
|
|
SzafaEngine.Shared.GetService<InputService>().EnableControls();
|
|
|
|
_sensitivityCVar = CVar.Create("fpp_cam_look_sens", "FPP Camera look sensitivity");
|
|
_sensitivityCVar.Set(0.25f);
|
|
|
|
await UniTask.Yield();
|
|
}
|
|
|
|
public void OnStop() {
|
|
SzafaEngine.Shared.GetService<InputService>().DisableControls();
|
|
SzafaEngine.Shared.GetService<InputService>().UnlockCursor();
|
|
Object.Destroy(_player);
|
|
}
|
|
|
|
public void OnTick() {
|
|
_player.FPPCamera.Sensitivity = _sensitivityCVar.FloatValue;
|
|
|
|
Vector2 lookInput = _config.LookActionReference.action.ReadValue<Vector2>();
|
|
_player.FPPCamera.Rotate(lookInput.x, lookInput.y);
|
|
|
|
Vector2 moveInput = _config.MoveActionReference.action.ReadValue<Vector2>();
|
|
_player.MoveRight(moveInput.x);
|
|
_player.MoveForward(moveInput.y);
|
|
|
|
if (_config.JumpActionReference.action.WasPerformedThisFrame()) {
|
|
_player.Locomotion.Jump();
|
|
}
|
|
|
|
_player.Dragger.TargetWorldPosition = _player.FPPCamera.Camera.transform.position + _player.FPPCamera.Camera.transform.forward * _player.DragObjectDistanceFromCamera;
|
|
|
|
if (_config.DragObjectActionReference.action.WasPressedThisFrame() && !_player.Dragger.IsDragging) {
|
|
GameObject pickedGameObject = _player.FPPCamera.Pick();
|
|
if (pickedGameObject != null && pickedGameObject.TryGetComponent(out Rigidbody rigidbody)) {
|
|
_player.Dragger.Grab(rigidbody);
|
|
}
|
|
}
|
|
|
|
if (_config.DragObjectActionReference.action.WasReleasedThisFrame()) {
|
|
_player.Dragger.Drop();
|
|
}
|
|
}
|
|
}
|
|
|
|
[CreateAssetMenu(menuName = SzafaConsts.AssetMenu + "Defaults/Player Controller")]
|
|
public class ScriptableDefaultPlayerController : ScriptableController {
|
|
[SerializeField]
|
|
private DefaultPlayerController.Config _config;
|
|
|
|
public override IController Create() {
|
|
return new DefaultPlayerController(_config);
|
|
}
|
|
}
|
|
} |