124 lines
4.4 KiB
C#
Executable File
124 lines
4.4 KiB
C#
Executable File
using System;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using RebootKit.Engine;
|
|
using RebootKit.Engine.Foundation;
|
|
using RebootKit.Engine.Main;
|
|
using RebootKit.Engine.Services.Console;
|
|
using RebootKit.Engine.Services.Input;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.InputSystem;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace RebootKit.FPPKit {
|
|
public static class FPPConfig {
|
|
[ConfigVar("fpp.mouse.sens", 0.25f, "Mouse look sensitivity")] public static ConfigVar s_MouseSensitivity;
|
|
[ConfigVar("fpp.mouse.invert.y", 0, "Invert mouse look")] public static ConfigVar s_MouseInvertY;
|
|
[ConfigVar("fpp.gamepad.sens", 4.0f, "Gamepad look sensitivity")] public static ConfigVar s_GamepadSensitivity;
|
|
}
|
|
|
|
public static class SensitivityReader {
|
|
public static float2 Read(InputAction action) {
|
|
if (action == null || action.activeControl == null) {
|
|
return float2.zero;
|
|
}
|
|
|
|
float2 sensitivity = action.ReadValue<Vector2>();
|
|
|
|
bool isGamepad = action.activeControl.device is Gamepad;
|
|
|
|
if (isGamepad) {
|
|
sensitivity *= FPPConfig.s_GamepadSensitivity.FloatValue;
|
|
} else {
|
|
sensitivity *= FPPConfig.s_MouseSensitivity.FloatValue;
|
|
|
|
if (FPPConfig.s_MouseInvertY.IndexValue == 1) {
|
|
sensitivity.y *= -1;
|
|
}
|
|
}
|
|
|
|
return sensitivity;
|
|
}
|
|
}
|
|
|
|
public class FPPPlayerController : IController {
|
|
readonly Config m_Config;
|
|
FPPActor m_FPPActor;
|
|
|
|
[Inject] InputService m_InputService;
|
|
|
|
public FPPPlayerController(Config config) {
|
|
m_Config = config;
|
|
}
|
|
|
|
public void Dispose() {
|
|
}
|
|
|
|
public async UniTask OnStart(CancellationToken cancellationToken) {
|
|
m_InputService.LockCursor();
|
|
m_InputService.EnableControls();
|
|
|
|
m_FPPActor = await RR.World.SpawnActor<FPPActor>(m_Config.fppActorPrefab, cancellationToken);
|
|
|
|
await Awaitable.NextFrameAsync(cancellationToken);
|
|
}
|
|
|
|
public void OnStop() {
|
|
m_InputService.DisableControls();
|
|
m_InputService.UnlockCursor();
|
|
Object.Destroy(m_FPPActor);
|
|
|
|
RR.World.KillActor(m_FPPActor);
|
|
}
|
|
|
|
public void OnTick() {
|
|
if (m_FPPActor == null) {
|
|
return;
|
|
}
|
|
|
|
if (!Application.isFocused) {
|
|
return;
|
|
}
|
|
|
|
// m_FPPActor.Locomotion.maxMovementSpeed = m_MovementSpeedCVar.FloatValue;
|
|
|
|
Vector2 lookInput = m_Config.lookActionReference.action.ReadValue<Vector2>();
|
|
m_FPPActor.Look(lookInput * FPPConfig.s_MouseSensitivity.FloatValue);
|
|
|
|
Vector2 moveInput = m_Config.moveActionReference.action.ReadValue<Vector2>();
|
|
m_FPPActor.MoveRight(moveInput.x);
|
|
m_FPPActor.MoveForward(moveInput.y);
|
|
|
|
if (m_Config.jumpActionReference.action.WasPerformedThisFrame()) {
|
|
m_FPPActor.Jump();
|
|
}
|
|
|
|
// m_FPPActor.Dragger.TargetWorldPosition = m_FPPActor.FPPCamera.Camera.transform.position + m_FPPActor.FPPCamera.Camera.transform.forward * m_FPPActor.DragObjectDistanceFromCamera;
|
|
//
|
|
// if (m_Config.dragObjectActionReference.action.WasPressedThisFrame() && !m_FPPActor.Dragger.IsDragging) {
|
|
// GameObject pickedGameObject = m_FPPActor.FPPCamera.Sensor.Sense();
|
|
// if (pickedGameObject != null && pickedGameObject.TryGetComponent(out Rigidbody rigidbody)) {
|
|
// m_FPPActor.Dragger.Grab(rigidbody);
|
|
// }
|
|
// }
|
|
//
|
|
// if (m_Config.dragObjectActionReference.action.WasReleasedThisFrame()) {
|
|
// m_FPPActor.Dragger.Drop();
|
|
// }
|
|
}
|
|
|
|
[Serializable]
|
|
public class Config {
|
|
public AssetReferenceT<GameObject> fppActorPrefab;
|
|
|
|
public InputActionReference moveActionReference;
|
|
public InputActionReference lookActionReference;
|
|
public InputActionReference jumpActionReference;
|
|
public InputActionReference dragObjectActionReference;
|
|
public InputActionReference primaryActionReference;
|
|
public InputActionReference secondaryActionReference;
|
|
}
|
|
}
|
|
} |