Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Events;
- using UnityEngine;
- namespace Gameplay.Controller.Input
- {
- public interface IEventHandler<in TGameEvent> where TGameEvent : GameEvent
- {
- void PerformAction(TGameEvent gameEvent);
- }
- public class EventBinder<TGameEvent> : IEventHandler<TGameEvent> where TGameEvent : GameEvent
- {
- private readonly GameObject _agent;
- private readonly IEventControllable<TGameEvent> _controllable;
- public EventBinder(GameObject agent, IEventControllable<TGameEvent> controllable)
- {
- _agent = agent;
- _controllable = controllable;
- }
- public virtual void PerformAction(TGameEvent gameEvent)
- {
- _controllable.PerformAction(gameEvent, _agent);
- }
- }
- public class EventBinder<TGameEvent, TDataFormat> : IEventHandler<TGameEvent> where TGameEvent : GameEvent where TDataFormat : struct
- {
- private readonly GameObject _agent;
- private readonly IEventControllable<TGameEvent, TDataFormat> _controllable;
- private readonly IDataSource<TDataFormat> _dataSource;
- public EventBinder(GameObject agent, IEventControllable<TGameEvent, TDataFormat> controllable, IDataSource<TDataFormat> dataSource)
- {
- _agent = agent;
- _controllable = controllable;
- _dataSource = dataSource;
- }
- public virtual void PerformAction(TGameEvent gameEvent)
- {
- _controllable.PerformAction(gameEvent, _agent, _dataSource);
- }
- }
- public interface IDataSource<out T> where T : struct
- {
- T Data {get;}
- }
- public interface IEventControllable<in TGameEvent, in TDataFormat> where TGameEvent : GameEvent where TDataFormat : struct
- {
- void PerformAction(TGameEvent gameEvent, GameObject agent, IDataSource<TDataFormat> dataSource);
- }
- public interface IEventControllable<in T> where T : GameEvent
- {
- void PerformAction(T gameEvent, GameObject agent);
- }
- public class CommonDirectionSource : IDataSource<float>
- {
- private readonly Func<float> _getDirection;
- public CommonDirectionSource(Func<float> getDirection)
- {
- _getDirection = getDirection;
- }
- public float Data => _getDirection.Invoke();
- }
- public class CommonVectorSource : CommonDirectionSource, IDataSource<Vector2>
- {
- private readonly Func<Vector2> _getVector;
- public CommonVectorSource(Func<Vector2> getVector) : base(() =>
- {
- var vector = getVector();
- return Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
- })
- {
- _getVector = getVector;
- }
- Vector2 IDataSource<Vector2>.Data => _getVector();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment