first
This commit is contained in:
29
Runtime/FPPKit/Code/FPPActor.cs
Normal file
29
Runtime/FPPKit/Code/FPPActor.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using RebootKit.Engine.Services.Simulation;
|
||||
using RebootKit.Engine.Services.Simulation.Characters;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RebootKit.FPPKit {
|
||||
public class FPPActor : Actor {
|
||||
[field: SerializeField]
|
||||
public CharacterLocomotion Locomotion { get; private set; }
|
||||
|
||||
[field: SerializeField]
|
||||
public FirstPersonCamera FPPCamera { get; private set; }
|
||||
|
||||
[field: SerializeField]
|
||||
public PhysicsObjectDragger Dragger { get; private set; }
|
||||
|
||||
[field: SerializeField]
|
||||
public float DragObjectDistanceFromCamera { get; private set; } = 2.0f;
|
||||
|
||||
public void MoveRight(float input) {
|
||||
Vector3 direction = Quaternion.AngleAxis(FPPCamera.Yaw, Vector3.up) * Vector3.right;
|
||||
Locomotion.AddMovementInput(direction, input);
|
||||
}
|
||||
|
||||
public void MoveForward(float input) {
|
||||
Vector3 direction = Quaternion.AngleAxis(FPPCamera.Yaw, Vector3.up) * Vector3.forward;
|
||||
Locomotion.AddMovementInput(direction, input);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Runtime/FPPKit/Code/FPPActor.cs.meta
Normal file
2
Runtime/FPPKit/Code/FPPActor.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aaa2199caa5cc4d4caf0677e0fe43a1b
|
||||
58
Runtime/FPPKit/Code/FirstPersonCamera.cs
Normal file
58
Runtime/FPPKit/Code/FirstPersonCamera.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Cinemachine;
|
||||
using RebootKit.Engine.Services.Simulation.Sensors;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RebootKit.FPPKit {
|
||||
public class FirstPersonCamera : MonoBehaviour {
|
||||
[SerializeField]
|
||||
private float _pickDistance = 5.0f;
|
||||
|
||||
[SerializeField]
|
||||
private LayerMask _pickLayer;
|
||||
|
||||
[field: SerializeField]
|
||||
public float Sensitivity { get; set; }
|
||||
|
||||
[SerializeField]
|
||||
private float _pitchMin = -80f;
|
||||
|
||||
[SerializeField]
|
||||
private float _pitchMax = 80f;
|
||||
|
||||
[field: SerializeField]
|
||||
public CinemachineVirtualCamera Camera { get; private set; }
|
||||
|
||||
public float Pitch { get; private set; }
|
||||
public float Yaw { get; private set; }
|
||||
|
||||
private readonly RaycastSensor _raycastSensor = new();
|
||||
|
||||
public ISensor Sensor {
|
||||
get {
|
||||
_raycastSensor.Ray = new Ray(Camera.transform.position, Camera.transform.forward);
|
||||
_raycastSensor.MaxDistance = _pickDistance;
|
||||
_raycastSensor.LayerMask = _pickLayer;
|
||||
return _raycastSensor;
|
||||
}
|
||||
}
|
||||
|
||||
private void LateUpdate() {
|
||||
Camera.transform.localRotation = Quaternion.Euler(Pitch, 0f, 0f);
|
||||
transform.rotation = Quaternion.Euler(0f, Yaw, 0f);
|
||||
}
|
||||
|
||||
public void Rotate(float x, float y) {
|
||||
float sens = Sensitivity;
|
||||
|
||||
Pitch -= y * sens;
|
||||
Pitch = Mathf.Clamp(Pitch, _pitchMin, _pitchMax);
|
||||
|
||||
Yaw += x * sens;
|
||||
}
|
||||
|
||||
public void SetLookDirection(Vector3 forward) {
|
||||
Pitch = Mathf.Asin(-forward.y) * Mathf.Rad2Deg;
|
||||
Yaw = Mathf.Atan2(forward.x, forward.z) * Mathf.Rad2Deg;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Runtime/FPPKit/Code/FirstPersonCamera.cs.meta
Normal file
2
Runtime/FPPKit/Code/FirstPersonCamera.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b82691de261d95842bde9bca81db0893
|
||||
88
Runtime/FPPKit/Code/PlayerController.cs
Normal file
88
Runtime/FPPKit/Code/PlayerController.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using RebootKit.Engine;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Services.Console;
|
||||
using RebootKit.Engine.Services.Input;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace RebootKit.FPPKit {
|
||||
public class PlayerController : IController {
|
||||
[System.Serializable]
|
||||
public class Config {
|
||||
public AssetReferenceT<GameObject> FPPSetupAsset;
|
||||
|
||||
public InputActionReference MoveActionReference;
|
||||
public InputActionReference LookActionReference;
|
||||
public InputActionReference JumpActionReference;
|
||||
public InputActionReference DragObjectActionReference;
|
||||
public InputActionReference PrimaryActionReference;
|
||||
public InputActionReference SecondaryActionReference;
|
||||
}
|
||||
|
||||
private Config _config;
|
||||
|
||||
private FPPActor _player;
|
||||
|
||||
private CVar _sensitivityCVar;
|
||||
private CVar _movementSpeedCVar;
|
||||
|
||||
public PlayerController(Config config) {
|
||||
_config = config;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
}
|
||||
|
||||
public async UniTask OnStart(CancellationToken cancellationToken) {
|
||||
RR.Service<InputService>().LockCursor();
|
||||
RR.Service<InputService>().EnableControls();
|
||||
|
||||
_sensitivityCVar = RR.CVarNumber("fpp_cam_look_sens", 0.25f);
|
||||
_movementSpeedCVar = RR.CVarNumber("p_move_speed", 4.0f);
|
||||
|
||||
_player = await RR.World().SpawnActor<FPPActor>(_config.FPPSetupAsset, cancellationToken);
|
||||
|
||||
await Awaitable.NextFrameAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public void OnStop() {
|
||||
RR.Service<InputService>().DisableControls();
|
||||
RR.Service<InputService>().UnlockCursor();
|
||||
Object.Destroy(_player);
|
||||
|
||||
RR.World().KillActor(_player);
|
||||
}
|
||||
|
||||
public void OnTick() {
|
||||
_player.FPPCamera.Sensitivity = _sensitivityCVar.FloatValue;
|
||||
_player.Locomotion.MaxMovementSpeed = _movementSpeedCVar.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.Sensor.Sense();
|
||||
if (pickedGameObject != null && pickedGameObject.TryGetComponent(out Rigidbody rigidbody)) {
|
||||
_player.Dragger.Grab(rigidbody);
|
||||
}
|
||||
}
|
||||
|
||||
if (_config.DragObjectActionReference.action.WasReleasedThisFrame()) {
|
||||
_player.Dragger.Drop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Runtime/FPPKit/Code/PlayerController.cs.meta
Normal file
2
Runtime/FPPKit/Code/PlayerController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5e9f327e597f7d8489ee899941f7255a
|
||||
15
Runtime/FPPKit/Code/PlayerControllerAsset.cs
Normal file
15
Runtime/FPPKit/Code/PlayerControllerAsset.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using RebootKit.Engine;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RebootKit.FPPKit {
|
||||
[CreateAssetMenu(menuName = RConsts.AssetMenu + "FPP/Player Controller")]
|
||||
public class PlayerControllerAsset : ControllerAsset {
|
||||
[SerializeField]
|
||||
private PlayerController.Config _config;
|
||||
|
||||
public override IController Create() {
|
||||
return new PlayerController(_config);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Runtime/FPPKit/Code/PlayerControllerAsset.cs.meta
Normal file
2
Runtime/FPPKit/Code/PlayerControllerAsset.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 43f38c326fd565e46a29f30e4f20031b
|
||||
Reference in New Issue
Block a user