44
55namespace Kekser . Sensors
66{
7- public abstract class Sensor < T > : MonoBehaviour where T : Component
7+ public abstract class Sensor : MonoBehaviour
88 {
9- private const int MIN_RAYCASTS = 1 ;
10- private const int MAX_RAYCASTS = 10 ;
11-
12- private List < GameObject > _detectedObjects = new List < GameObject > ( ) ;
9+ protected List < GameObject > _detectedObjects = new List < GameObject > ( ) ;
1310
1411 [ Header ( "Base Sensor Settings" ) ]
1512 [ SerializeField ]
1613 protected bool _autoUpdateSensor = false ;
1714 [ SerializeField ]
1815 protected List < GameObject > _ignore ;
19- [ SerializeField ]
20- protected bool _checkVisibility = false ;
21- [ SerializeField ]
22- protected LayerMask _scanLayer = 0 ;
23- [ SerializeField ]
24- protected LayerMask _obstructionLayer = 0 ;
25- [ SerializeField , Range ( MIN_RAYCASTS , MAX_RAYCASTS ) ]
26- protected int _scanRays = 1 ;
27- [ SerializeField , Range ( 0f , 1f ) ]
28- protected float _visibility = 0.5f ;
2916 [ Header ( "Events" ) ]
3017 [ SerializeField ]
3118 public SensorEvent OnEnter ;
@@ -40,6 +27,47 @@ public bool AutoUpdateSensor
4027 set => _autoUpdateSensor = value ;
4128 }
4229
30+ public void AddIgnore ( GameObject ignoreObject )
31+ {
32+ if ( ! _ignore . Contains ( ignoreObject ) )
33+ _ignore . Add ( ignoreObject ) ;
34+ }
35+
36+ public void RemoveIgnore ( GameObject ignoreObject )
37+ {
38+ if ( _ignore . Contains ( ignoreObject ) )
39+ _ignore . Remove ( ignoreObject ) ;
40+ }
41+
42+ public GameObject [ ] DetectedObjects => _detectedObjects . ToArray ( ) ;
43+
44+ public abstract void SensorUpdate ( ) ;
45+
46+ protected virtual void FixedUpdate ( )
47+ {
48+ if ( ! _autoUpdateSensor )
49+ return ;
50+ SensorUpdate ( ) ;
51+ }
52+ }
53+
54+ public abstract class Sensor < T > : Sensor where T : Component
55+ {
56+ private const int MIN_RAYCASTS = 1 ;
57+ private const int MAX_RAYCASTS = 10 ;
58+
59+ [ Header ( "Visibility Settings" ) ]
60+ [ SerializeField ]
61+ protected bool _checkVisibility = false ;
62+ [ SerializeField ]
63+ protected LayerMask _scanLayer = 0 ;
64+ [ SerializeField ]
65+ protected LayerMask _obstructionLayer = 0 ;
66+ [ SerializeField , Range ( MIN_RAYCASTS , MAX_RAYCASTS ) ]
67+ protected int _scanRays = 1 ;
68+ [ SerializeField , Range ( 0f , 1f ) ]
69+ protected float _visibility = 0.5f ;
70+
4371 public bool CheckVisibility
4472 {
4573 get => _checkVisibility ;
@@ -69,25 +97,11 @@ public float Visibility
6997 get => _visibility ;
7098 set => _visibility = Mathf . Clamp01 ( value ) ;
7199 }
72-
73- public void AddIgnore ( GameObject ignoreObject )
74- {
75- if ( ! _ignore . Contains ( ignoreObject ) )
76- _ignore . Add ( ignoreObject ) ;
77- }
78-
79- public void RemoveIgnore ( GameObject ignoreObject )
80- {
81- if ( _ignore . Contains ( ignoreObject ) )
82- _ignore . Remove ( ignoreObject ) ;
83- }
84-
85- public GameObject [ ] DetectedObjects => _detectedObjects . ToArray ( ) ;
86100
87101 protected abstract T [ ] GetComponentsInSensor ( ) ;
88102 protected abstract float CheckForVisibility ( T checkObject ) ;
89103
90- public void SensorUpdate ( )
104+ public override void SensorUpdate ( )
91105 {
92106 List < GameObject > oldObjects = new List < GameObject > ( _detectedObjects ) ;
93107 T [ ] checkObjects = gameObject . activeInHierarchy && enabled ? GetComponentsInSensor ( ) : Array . Empty < T > ( ) ;
@@ -117,12 +131,5 @@ public void SensorUpdate()
117131 OnExit . Invoke ( oldObjects [ i ] ) ;
118132 }
119133 }
120-
121- protected virtual void FixedUpdate ( )
122- {
123- if ( ! _autoUpdateSensor )
124- return ;
125- SensorUpdate ( ) ;
126- }
127134 }
128135}
0 commit comments