| 
									
										
										
										
											2021-01-27 11:27:56 +00:00
										 |  |  | using System; | 
					
						
							|  |  |  | using System.Collections; | 
					
						
							|  |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  | using UnityEngine; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public struct BufferedInput | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public BufferedInput( float bufferTime ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         _bufferTime = bufferTime; | 
					
						
							|  |  |  |         _value = false; | 
					
						
							| 
									
										
										
										
											2021-02-15 18:09:32 +00:00
										 |  |  |         _timeSincePress = Mathf.Infinity; | 
					
						
							|  |  |  |         _timeSinceRelease = Mathf.Infinity; | 
					
						
							| 
									
										
										
										
											2021-01-27 11:27:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     private float _bufferTime; | 
					
						
							|  |  |  |     private bool _value; | 
					
						
							|  |  |  |     private float _timeSinceRelease; | 
					
						
							|  |  |  |     private float _timeSincePress; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public void Set( bool newValue ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ( newValue && !_value ) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             _timeSincePress = 0f; | 
					
						
							| 
									
										
										
										
											2021-02-15 18:09:32 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if ( !newValue && _value ) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-01-27 11:27:56 +00:00
										 |  |  |             _timeSinceRelease = 0f; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         _value = newValue; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public void Update( float deltaTime ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         _timeSincePress += deltaTime; | 
					
						
							| 
									
										
										
										
											2021-02-15 18:09:32 +00:00
										 |  |  |         _timeSinceRelease += deltaTime; | 
					
						
							| 
									
										
										
										
											2021-01-27 11:27:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public bool GetValue() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return _value || _timeSinceRelease <= _bufferTime; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 18:09:32 +00:00
										 |  |  |     public bool GetRawValue() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return _value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public bool GetDown( bool consume = true ) | 
					
						
							| 
									
										
										
										
											2021-01-27 11:27:56 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-15 18:09:32 +00:00
										 |  |  |         bool value = _timeSincePress <= _bufferTime; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         if ( consume && value ) | 
					
						
							|  |  |  |             _timeSincePress = Mathf.Infinity; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         return value; | 
					
						
							| 
									
										
										
										
											2021-01-27 11:27:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 18:09:32 +00:00
										 |  |  |     public bool GetRelease( bool consume = true ) | 
					
						
							| 
									
										
										
										
											2021-01-27 11:27:56 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-15 18:09:32 +00:00
										 |  |  |         bool value = _timeSinceRelease <= _bufferTime; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         if ( consume && value ) | 
					
						
							|  |  |  |             _timeSinceRelease = Mathf.Infinity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return value; | 
					
						
							| 
									
										
										
										
											2021-01-27 11:27:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } |