Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
RestInArea.cs
Go to the documentation of this file.
1 using System.Collections;
8 using UnityEngine;
9 
10 namespace droid.Runtime.Prototyping.Evaluation {
14  [AddComponentMenu(EvaluationComponentMenuPath._ComponentMenuPath
15  + "RestInArea"
16  + EvaluationComponentMenuPath._Postfix)]
17  public class RestInArea : ObjectiveFunction {
18  [SerializeField] Actor _actor = null;
19 
20  [SerializeField] Collider _area = null;
21  [SerializeField] bool _is_resting = false;
22 
23  [SerializeField] Obstruction[] _obstructions = null;
24 
25  //Used for.. if outside playable area then reset
26  [SerializeField] ActorOverlapping _overlapping = ActorOverlapping.Outside_area_;
27 
28  [SerializeField] BoundingBox _playable_area = null;
29  [SerializeField] float _resting_time = 3f;
30  [SerializeField] bool _sparse = false;
31  [SerializeField] Coroutine _wait_for_resting = null;
32  WaitForSeconds _wait_for_seconds = new WaitForSeconds(3f);
33 
38  public override float InternalEvaluate() {
39  var signal = 0f;
40 
41  if (this._overlapping == ActorOverlapping.Inside_area_ && this._is_resting) {
42  if (this._actor is KillableActor) {
43  if (((KillableActor)this._actor).IsAlive) {
44  this.ParentEnvironment.Terminate("Inside goal area");
45  return 1f;
46  }
47  } else {
48  this.ParentEnvironment.Terminate("Inside goal area");
49  return 1f;
50  }
51  }
52 
53  if (!this._sparse) {
54  signal += 1 / Vector3.Distance(this._actor.transform.position, this._area.transform.position);
55  }
56 
57  if (this._playable_area && this._actor) {
58  if (!this._playable_area.Bounds.Intersects(this._actor.GetComponent<Collider>().bounds)) {
59  this.ParentEnvironment.Terminate("Actor is outside playable area");
60  }
61  }
62 
63  return signal;
64  }
65 
69  public override void InternalReset() {
70  if (this._wait_for_resting != null) {
71  this.StopCoroutine(this._wait_for_resting);
72  }
73 
74  this._is_resting = false;
75  }
76 
77  IEnumerator WaitForResting() {
78  yield return this._wait_for_seconds;
79 
80  this._is_resting = true;
81  }
82 
83  protected override void PostSetup() {
84  if (!this._area) {
85  this._area = FindObjectOfType<Sensor>().gameObject.GetComponent<Collider>();
86  }
87 
88  if (!this._actor) {
89  this._actor = FindObjectOfType<Actor>();
90  }
91 
92  if (this._obstructions.Length <= 0) {
93  this._obstructions = FindObjectsOfType<Obstruction>();
94  }
95 
96  if (!this._playable_area) {
97  this._playable_area = FindObjectOfType<BoundingBox>();
98  }
99 
100  NeodroidUtilities
101  .RegisterCollisionTriggerCallbacksOnChildren<ChildCollider3DSensor, Collider, Collision>(this,
102  this
103  ._area
104  .transform,
105  null,
106  this
107  .OnTriggerEnterChild,
108  null,
109  this
110  .OnTriggerExitChild,
111  null,
112  this
113  .OnTriggerStayChild);
114 
115  NeodroidUtilities
116  .RegisterCollisionTriggerCallbacksOnChildren<ChildCollider3DSensor, Collider, Collision>(this,
117  this
118  ._actor
119  .transform,
120  null,
121  this
122  .OnTriggerEnterChild,
123  null,
124  this
125  .OnTriggerExitChild,
126  null,
127  this
128  .OnTriggerStayChild);
129  this._wait_for_seconds = new WaitForSeconds(this._resting_time);
130  }
131 
132  void OnTriggerEnterChild(GameObject child_game_object, Collider other_game_object) {
133  if (this._actor) {
134  if (child_game_object == this._area.gameObject
135  && other_game_object.gameObject == this._actor.gameObject) {
136  #if NEODROID_DEBUG
137  if (this.Debugging) {
138  Debug.Log("Actor is inside area");
139  }
140  #endif
141 
142  this._overlapping = ActorOverlapping.Inside_area_;
143  if (this._wait_for_resting != null) {
144  this.StopCoroutine(this._wait_for_resting);
145  }
146 
147  this._wait_for_resting = this.StartCoroutine(this.WaitForResting());
148  }
149  }
150  }
151 
152  void OnTriggerStayChild(GameObject child_game_object, Collider other_game_object) {
153  if (this._actor) {
154  if (child_game_object == this._area.gameObject
155  && other_game_object.gameObject == this._actor.gameObject) {
156  #if NEODROID_DEBUG
157  if (this.Debugging) {
158  Debug.Log("Actor is inside area");
159  }
160  #endif
161 
162  this._overlapping = ActorOverlapping.Inside_area_;
163  }
164  }
165  }
166 
167  void OnTriggerExitChild(GameObject child_game_object, Collider other_game_object) {
168  if (this._actor) {
169  if (child_game_object == this._area.gameObject
170  && other_game_object.gameObject == this._actor.gameObject) {
171  #if NEODROID_DEBUG
172  if (this.Debugging) {
173  Debug.Log("Actor is outside area");
174  }
175  #endif
176 
177  this._overlapping = ActorOverlapping.Outside_area_;
178  if (this._wait_for_resting != null) {
179  this.StopCoroutine(this._wait_for_resting);
180  }
181  }
182  }
183  }
184  }
185 }