118 lines
3.9 KiB
C#
Executable File
118 lines
3.9 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.Input;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.Serialization;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace RealityReboot.jelycho.Player {
|
|
public class PlayerController : IController {
|
|
readonly Config m_Config;
|
|
PlayerActor m_FPPActor;
|
|
|
|
public PlayerController(Config config) {
|
|
m_Config = config;
|
|
}
|
|
|
|
public void Dispose() {
|
|
}
|
|
|
|
public async UniTask OnStart(CancellationToken cancellationToken) {
|
|
RR.Input.LockCursor();
|
|
RR.Input.EnableControls();
|
|
|
|
m_FPPActor = await RR.World.SpawnActor<PlayerActor>(m_Config.playerActorPrefab, cancellationToken);
|
|
m_FPPActor.Simulate = true;
|
|
|
|
await Awaitable.NextFrameAsync(cancellationToken);
|
|
}
|
|
|
|
public void OnStop() {
|
|
RR.Input.DisableControls();
|
|
RR.Input.UnlockCursor();
|
|
Object.Destroy(m_FPPActor);
|
|
|
|
RR.World.KillActor(m_FPPActor);
|
|
}
|
|
|
|
public void OnTick() {
|
|
if (m_FPPActor == null) {
|
|
return;
|
|
}
|
|
|
|
if (!Application.isFocused) {
|
|
return;
|
|
}
|
|
|
|
Vector2 lookInput = SensitivityReader.Read(m_Config.lookActionReference.action);
|
|
m_FPPActor.Look(lookInput * FPPConfig.MouseSensitivity.FloatValue);
|
|
|
|
Vector2 moveInput = m_Config.moveActionReference.action.ReadValue<Vector2>();
|
|
m_FPPActor.SetMoveInput(moveInput);
|
|
|
|
m_FPPActor.SetSprint(m_Config.sprintActionReference.action.IsPressed());
|
|
|
|
if (m_Config.jumpActionReference.action.WasPerformedThisFrame()) {
|
|
m_FPPActor.Jump();
|
|
}
|
|
|
|
if (m_Config.dragObjectActionReference.action.WasPressedThisFrame()) {
|
|
m_FPPActor.StartDrag();
|
|
}
|
|
|
|
if (m_Config.dragObjectActionReference.action.WasReleasedThisFrame()) {
|
|
m_FPPActor.StopDrag();
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class Config {
|
|
public AssetReferenceT<GameObject> playerActorPrefab;
|
|
|
|
public InputActionReference moveActionReference;
|
|
public InputActionReference lookActionReference;
|
|
public InputActionReference jumpActionReference;
|
|
public InputActionReference sprintActionReference;
|
|
public InputActionReference dragObjectActionReference;
|
|
public InputActionReference primaryActionReference;
|
|
public InputActionReference secondaryActionReference;
|
|
}
|
|
}
|
|
|
|
public static class FPPConfig {
|
|
[ConfigVar("fpp.mouse.sens", 0.25f, "Mouse look sensitivity")] public static ConfigVar MouseSensitivity;
|
|
[ConfigVar("fpp.mouse.invert.y", 0, "Invert mouse look")] public static ConfigVar MouseInvertY;
|
|
[ConfigVar("fpp.gamepad.sens", 4.0f, "Gamepad look sensitivity")] public static ConfigVar 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.GamepadSensitivity.FloatValue;
|
|
} else {
|
|
sensitivity *= FPPConfig.MouseSensitivity.FloatValue;
|
|
|
|
if (FPPConfig.MouseInvertY.IndexValue == 1) {
|
|
sensitivity.y *= -1;
|
|
}
|
|
}
|
|
|
|
return sensitivity;
|
|
}
|
|
}
|
|
} |