Eresor

Untitled

Mar 19th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using Events;
  3. using UnityEngine;
  4.  
  5. namespace Gameplay.Controller.Input
  6. {
  7.     public interface IEventHandler<in TGameEvent> where TGameEvent : GameEvent
  8.     {
  9.         void PerformAction(TGameEvent gameEvent);
  10.     }
  11.    
  12.     public class EventBinder<TGameEvent> : IEventHandler<TGameEvent> where TGameEvent : GameEvent
  13.     {
  14.         private readonly GameObject _agent;
  15.         private readonly IEventControllable<TGameEvent> _controllable;
  16.  
  17.         public EventBinder(GameObject agent, IEventControllable<TGameEvent> controllable)
  18.         {
  19.             _agent = agent;
  20.             _controllable = controllable;
  21.         }
  22.        
  23.         public virtual void PerformAction(TGameEvent gameEvent)
  24.         {
  25.             _controllable.PerformAction(gameEvent, _agent);    
  26.         }
  27.     }
  28.    
  29.     public class EventBinder<TGameEvent, TDataFormat> : IEventHandler<TGameEvent> where TGameEvent : GameEvent where TDataFormat : struct
  30.     {
  31.         private readonly GameObject _agent;
  32.         private readonly IEventControllable<TGameEvent, TDataFormat> _controllable;
  33.         private readonly IDataSource<TDataFormat> _dataSource;
  34.        
  35.         public EventBinder(GameObject agent, IEventControllable<TGameEvent, TDataFormat> controllable, IDataSource<TDataFormat> dataSource)
  36.         {
  37.             _agent = agent;
  38.             _controllable = controllable;
  39.             _dataSource = dataSource;
  40.         }
  41.        
  42.         public virtual void PerformAction(TGameEvent gameEvent)
  43.         {
  44.             _controllable.PerformAction(gameEvent, _agent, _dataSource);    
  45.         }
  46.     }
  47.    
  48.     public interface IDataSource<out T> where T : struct
  49.     {
  50.         T Data {get;}
  51.     }
  52.  
  53.     public interface IEventControllable<in TGameEvent, in TDataFormat> where TGameEvent : GameEvent where TDataFormat : struct
  54.     {
  55.         void PerformAction(TGameEvent gameEvent, GameObject agent, IDataSource<TDataFormat> dataSource);
  56.     }
  57.    
  58.     public interface IEventControllable<in T> where T : GameEvent
  59.     {
  60.         void PerformAction(T gameEvent, GameObject agent);
  61.     }
  62.  
  63.    
  64.     public class CommonDirectionSource : IDataSource<float>
  65.     {
  66.         private readonly Func<float> _getDirection;
  67.  
  68.         public CommonDirectionSource(Func<float> getDirection)
  69.         {
  70.             _getDirection = getDirection;
  71.         }
  72.        
  73.         public float Data => _getDirection.Invoke();
  74.     }
  75.    
  76.     public class CommonVectorSource : CommonDirectionSource, IDataSource<Vector2>
  77.     {
  78.         private readonly Func<Vector2> _getVector;
  79.  
  80.         public CommonVectorSource(Func<Vector2> getVector) : base(() =>
  81.         {
  82.             var vector = getVector();
  83.             return Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
  84.         })
  85.         {
  86.             _getVector = getVector;
  87.         }
  88.  
  89.         Vector2 IDataSource<Vector2>.Data => _getVector();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment