Commit 7158e3ee by alsunj

Move inputreader from playercontroller to playermanager

parent 3a5a4e15
using System;
using UnityEngine;
using UnityEngine.Serialization;
public class PlayerManager : MonoBehaviour
{
public InputReader inputReader;
public PlayerEvents playerEvents;
public void Initialize()
{
inputReader.InitializeInput();
playerEvents = new PlayerEvents();
}
}
\ No newline at end of file
......@@ -6,7 +6,6 @@ public abstract class Pickupable : NetworkBehaviour, IEquatable<Pickupable>, IPi
{
private NetworkVariable<bool> isObjectPickedup = new NetworkVariable<bool>();
private FollowTransform _followTransform;
private Quaternion _startingRotation;
private void Start()
{
......@@ -15,8 +14,6 @@ public abstract class Pickupable : NetworkBehaviour, IEquatable<Pickupable>, IPi
{
Debug.LogError("FollowTransform component not found on the GameObject.");
}
_startingRotation = transform.rotation;
}
public void RequestPutDownObject(NetworkObjectReference pickupingTarget)
......@@ -30,10 +27,10 @@ public abstract class Pickupable : NetworkBehaviour, IEquatable<Pickupable>, IPi
if (isObjectPickedup.Value)
{
isObjectPickedup.Value = false;
Vector3 newPosition = Vector3.zero;
Vector3 putDownPosition = Vector3.zero;
if (pickupingTarget.TryGet(out NetworkObject target))
{
newPosition = target.transform.position + target.transform.forward;
putDownPosition = target.transform.position + target.transform.forward;
PlayerPlacements playerPlacements = target.GetComponent<PlayerPlacements>();
if (playerPlacements != null)
{
......@@ -42,23 +39,27 @@ public abstract class Pickupable : NetworkBehaviour, IEquatable<Pickupable>, IPi
}
// Notify all clients to update their visuals
PutDownObjectClientRpc(newPosition);
PutDownObjectClientRpc(putDownPosition);
}
}
[ClientRpc]
private void PutDownObjectClientRpc(Vector3 newPosition)
private void PutDownObjectClientRpc(Vector3 putDownPosition)
{
_followTransform.SetTargetTransform(null);
transform.position = newPosition;
transform.rotation = _startingRotation;
// Update the visuals on the client
_followTransform.RemoveTargetTransform(putDownPosition);
//do not null it _followTransform.SetTargetPlayerControls(null);
PutDown();
}
public void RequestPickupObject(NetworkObjectReference pickupingTarget)
public bool RequestPickupObject(NetworkObjectReference pickupingTarget)
{
if (isObjectPickedup.Value)
{
return false;
}
RequestPickupObjectServerRpc(pickupingTarget);
return true;
}
[ServerRpc(RequireOwnership = false)]
......@@ -74,7 +75,30 @@ public abstract class Pickupable : NetworkBehaviour, IEquatable<Pickupable>, IPi
if (playerPlacements != null)
{
playerPlacements.SetPlayerRightHandItem(new NetworkObjectReference(this.NetworkObject));
_followTransform.SetTargetTransform(playerPlacements.playerRightHand.transform);
//_followTransform.SetTargetTransform(playerPlacements.playerRightHand.transform);
}
else
{
Debug.LogError("Cannot assign the object to the player's hand.");
}
PlayerManager switchPlayerMap = target.GetComponent<PlayerManager>();
if (switchPlayerMap != null)
{
ISwitchPlayerMap switchPlayerMapInterface = switchPlayerMap.inputReader;
if (switchPlayerMapInterface != null)
{
_followTransform.SetTargetPlayerControls(switchPlayerMapInterface);
}
else
{
Debug.LogError("Cannot get ISwitchPlayerMap from inputReader.");
}
}
else
{
Debug.LogError("target is" + target.name);
Debug.LogError("Cannot get target player controls map");
}
}
......
using System;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Serialization;
public class FollowTransform : MonoBehaviour
{
private ISwitchPlayerMap _targetPlayerControls;
private Transform targetTransform;
private Quaternion _startingRotation;
[SerializeField] private FollowTransformSettings followTransformSettings;
private void Start()
{
_startingRotation = transform.rotation;
}
public void SetTargetPlayerControls(ISwitchPlayerMap targetPlayerControls)
{
this._targetPlayerControls = targetPlayerControls;
}
public void SetTargetTransform(Transform targetTransform)
{
this.targetTransform = targetTransform;
if (targetTransform != null)
{
_targetPlayerControls.TurnOffPlayerControls();
transform.DOMove(targetTransform.position, followTransformSettings.pickupDuration).OnComplete(() =>
{
this.targetTransform = targetTransform;
_targetPlayerControls.TurnOnPlayerControls();
});
}
}
public void RemoveTargetTransform(Vector3 putDownPosition)
{
_targetPlayerControls.TurnOffPlayerControls();
transform.DOMove(putDownPosition, followTransformSettings.putDownDuration).OnComplete(() =>
{
transform.rotation = _startingRotation;
targetTransform = null;
_targetPlayerControls.TurnOnPlayerControls();
});
SetTargetPlayerControls(null);
}
private void LateUpdate()
......
......@@ -61,7 +61,7 @@ public class PlayerAnimator : NetworkBehaviour
private void SetPlayerInteract()
{
_animator.SetTrigger(IS_INTERACTING);
_animator.CrossFade(IS_INTERACTING, 0.1f, -1, 0, 1f);
}
private void SetPlayerAttack()
......
......@@ -9,8 +9,6 @@ public class PlayerController : NetworkBehaviour
[SerializeField] private PlayerInteractionSettings playerInteractionSettings;
[SerializeField] private float speed = 2f;
[SerializeField] private InputReader inputReader;
private PlayerPlacements _playerPlacements;
private PlayerManager _playerManager;
private PlayerAnimator _playerAnimator;
......@@ -78,40 +76,37 @@ public class PlayerController : NetworkBehaviour
#endregion
private void OnEnable()
private void OnDisable()
{
if (inputReader != null)
if (_playerManager.inputReader != null)
{
inputReader.MoveEvent += OnMove;
inputReader.InteractEvent += OnInteract;
inputReader.SprintEvent += OnSprint;
inputReader.AttackEvent += OnAttack;
_playerManager.inputReader.MoveEvent -= OnMove;
_playerManager.inputReader.InteractEvent -= OnInteract;
_playerManager.inputReader.SprintEvent -= OnSprint;
_playerManager.inputReader.AttackEvent -= OnAttack;
}
}
private void OnDisable()
private void InitializeMovements()
{
if (inputReader != null)
{
inputReader.MoveEvent -= OnMove;
inputReader.InteractEvent -= OnInteract;
inputReader.SprintEvent -= OnSprint;
inputReader.AttackEvent -= OnAttack;
}
_playerManager.inputReader.MoveEvent += OnMove;
_playerManager.inputReader.InteractEvent += OnInteract;
_playerManager.inputReader.SprintEvent += OnSprint;
_playerManager.inputReader.AttackEvent += OnAttack;
}
private void Start()
{
_sprintRemaining = sprintDuration;
_playerManager = GetComponent<PlayerManager>();
if (_playerManager == null)
if (_playerManager.inputReader != null)
{
Debug.LogError("PlayerManager is null");
_playerManager.Initialize();
InitializeMovements();
}
else
{
_playerManager.Initialize();
Debug.LogError("InputReader is null");
}
_playerAnimator = GetComponentInChildren<PlayerAnimator>();
......@@ -136,7 +131,8 @@ public class PlayerController : NetworkBehaviour
}
inputReader.InitializeInput();
//_playerManager.inputReader.InitializeInput();
_rb = GetComponent<Rigidbody>();
if (_rb == null)
{
......@@ -351,11 +347,7 @@ public class PlayerController : NetworkBehaviour
if (interactable != null)
{
RotatePlayerTowardsTarget(closestCollider);
if (interactable.Interact())
{
_playerManager.playerEvents.PlayerInteract();
return true;
}
PlayPlayerInteract(interactable.Interact());
}
}
......@@ -373,26 +365,39 @@ public class PlayerController : NetworkBehaviour
switch (closestCollider.GetComponent<Pickupable>())
{
case Key key:
PickupObject(key);
RotatePlayerTowardsTarget(closestCollider);
PlayPlayerInteract(PickupObject(key));
break;
case Explosive explosive:
PickupObject(explosive);
RotatePlayerTowardsTarget(closestCollider);
PlayPlayerInteract(PickupObject(explosive));
break;
}
}
}
private void PickupObject(Pickupable pickupable)
private void PlayPlayerInteract(bool state)
{
if (state)
{
_playerManager.playerEvents.PlayerInteract();
}
}
private bool PickupObject(Pickupable pickupable)
{
//you can always put down the pickupable, but you can only pick it up
//if your right hand is empty and it's not held by anyone else.
if (!_playerPlacements.IsRightHandFull())
{
pickupable.RequestPickupObject(
return pickupable.RequestPickupObject(
new NetworkObjectReference(gameObject.GetComponent<NetworkObject>()));
}
else
{
pickupable.RequestPutDownObject(
new NetworkObjectReference(gameObject.GetComponent<NetworkObject>()));
return true;
}
}
......
......@@ -6,4 +6,6 @@ public class FollowTransformSettings : ScriptableObject
{
public Vector3 targetRotationOffset = new Vector3(180, 0, 0);
public Vector3 targetPositionOffset = new Vector3(0f, 0, 0);
public float pickupDuration = 1f;
public float putDownDuration = 0.5f;
}
\ No newline at end of file
public interface ISwitchPlayerMap
{
public void TurnOffPlayerControls();
public void TurnOnPlayerControls();
}
\ No newline at end of file
fileFormatVersion: 2
guid: 95d3d82036385714ba0b7f877f34d9e3
\ No newline at end of file
......@@ -3,7 +3,7 @@ using UnityEngine;
using UnityEngine.InputSystem;
[CreateAssetMenu(fileName = "InputReader", menuName = "Scriptable Objects/InputReader")]
public class InputReader : ScriptableObject, InputSystem_Actions.IPlayerActions, IInputHandler
public class InputReader : ScriptableObject, InputSystem_Actions.IPlayerActions, IInputHandler, ISwitchPlayerMap
{
private InputSystem_Actions inputActions;
public event Action<Vector2> MoveEvent;
......@@ -121,4 +121,14 @@ public class InputReader : ScriptableObject, InputSystem_Actions.IPlayerActions,
{
throw new NotImplementedException();
}
public void TurnOffPlayerControls()
{
inputActions.Player.Disable();
}
public void TurnOnPlayerControls()
{
inputActions.Player.Enable();
}
}
\ No newline at end of file
......@@ -230,7 +230,6 @@ MonoBehaviour:
ShowTopMostFoldoutHeaderGroup: 1
playerInteractionSettings: {fileID: 11400000, guid: 1bc75bcaab451a44d8a71f0189dc90f8, type: 2}
speed: 2
inputReader: {fileID: 11400000, guid: fae963ae99ce6d14d8bbcd54becb58d7, type: 2}
offset: {x: 0, y: 7.4, z: -6.4}
enableSprint: 1
unlimitedSprint: 0
......@@ -334,6 +333,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c5e28a848c1e15846a459b682cc64164, type: 3}
m_Name:
m_EditorClassIdentifier:
inputReader: {fileID: 11400000, guid: fae963ae99ce6d14d8bbcd54becb58d7, type: 2}
--- !u!114 &911307299827299822
MonoBehaviour:
m_ObjectHideFlags: 0
......@@ -359,6 +359,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5dee285952d3d7f43936f433ec54e262, type: 3}
m_Name:
m_EditorClassIdentifier:
ShowTopMostFoldoutHeaderGroup: 1
playerRightHand: {fileID: 0}
playerLeftHand: {fileID: 0}
--- !u!1 &8839871738369281743
GameObject:
m_ObjectHideFlags: 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment