75 lines
2.0 KiB
C#
75 lines
2.0 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Northbound
|
|
{
|
|
public class PlayerActionSystem : InputActionManager
|
|
{
|
|
[Header("Actions")]
|
|
public List<MonoBehaviour> actionComponents = new List<MonoBehaviour>();
|
|
|
|
[Header("Animation")]
|
|
public bool playAnimations = true;
|
|
|
|
private Dictionary<string, IAction> _actions = new Dictionary<string, IAction>();
|
|
private Animator _animator;
|
|
|
|
private void Awake()
|
|
{
|
|
_animator = GetComponent<Animator>();
|
|
}
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
base.OnNetworkSpawn();
|
|
|
|
if (!IsOwner) return;
|
|
|
|
foreach (var component in actionComponents)
|
|
{
|
|
if (component is IAction action)
|
|
{
|
|
_actions[action.GetActionName()] = action;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void BindInputActions()
|
|
{
|
|
_inputActions.Player.Attack.performed += OnAttack;
|
|
}
|
|
|
|
protected override void UnbindInputActions()
|
|
{
|
|
_inputActions.Player.Attack.performed -= OnAttack;
|
|
}
|
|
|
|
private void OnAttack(InputAction.CallbackContext context)
|
|
{
|
|
ExecuteAction("Attack");
|
|
}
|
|
|
|
public void ExecuteAction(string actionName)
|
|
{
|
|
if (_actions.TryGetValue(actionName, out IAction action))
|
|
{
|
|
if (action.CanExecute(OwnerClientId))
|
|
{
|
|
if (playAnimations && _animator != null)
|
|
{
|
|
string animTrigger = action.GetActionAnimation();
|
|
if (!string.IsNullOrEmpty(animTrigger))
|
|
{
|
|
_animator.SetTrigger(animTrigger);
|
|
}
|
|
}
|
|
|
|
action.Execute(OwnerClientId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|