BITKit/Packages/Runtime~/Unity/Extensions/Entity/EntityInput.cs

60 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System;
using System.Linq;
using BITKit;
namespace BITKit.Entities
{
public class EntityInput : EntityInputComponent
{
public UniversalInputSystem.BITController controller;
EntityInputComponent[] inputComponents;
public override void OnSpawn()
{
if (isLocalPlayer)
{
var list = GetComponents<EntityInputComponent>().ToList();
list.Remove(this);
inputComponents = list.ToArray();
controller = new();
controller.Enable();
Init();
}
}
public override void OnDespawn()
{
if (isLocalPlayer)
{
controller.Disable();
controller.Dispose();
}
}
void Init()
{
var gamePlayType = typeof(UniversalInputSystem.BITController.GameplayActions);
gamePlayType
.GetProperties()
.Where(x => x.PropertyType == typeof(InputAction))
.ForEach(property =>
{
var action = property.GetValue(controller.Gameplay) as InputAction;
object[] pars = new object[1];
action.started += Callback;
action.performed += Callback;
action.canceled += Callback;
void Callback(InputAction.CallbackContext context)
{
pars[0] = context;
foreach (var component in inputComponents)
{
typeof(UniversalInputSystem.BITController.IGameplayActions)
.GetMethod($"On{property.Name}")
.Invoke(component, pars);
}
}
});
}
}
}