Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ReachGoal.cs
Go to the documentation of this file.
5 using UnityEngine;
6 
7 namespace droid.Runtime.Prototyping.Evaluation {
11  [AddComponentMenu(EvaluationComponentMenuPath._ComponentMenuPath
12  + "ReachGoal"
13  + EvaluationComponentMenuPath._Postfix)]
14  public class ReachGoal : ObjectiveFunction {
15  [SerializeField] Actor _actor = null;
16 
17  [SerializeField] bool _based_on_tags = false;
18 
19  [SerializeField] EmptyCell _goal = null;
20 
21  //Used for.. if outside playable area then reset
22  [SerializeField] ActorOverlapping _overlapping = ActorOverlapping.Outside_area_;
23 
28  public override float InternalEvaluate() {
29  var distance =
30  Mathf.Abs(Vector3.Distance(this._goal.transform.position, this._actor.transform.position));
31 
32  if (this._overlapping == ActorOverlapping.Inside_area_ || distance < 0.5f) {
33  this.ParentEnvironment.Terminate("Inside goal area");
34  return 1f;
35  }
36 
37  return 0f;
38  }
39 
43  public override void InternalReset() {
44  this.Setup();
45  this._overlapping = ActorOverlapping.Outside_area_;
46  }
47 
52  public void SetGoal(EmptyCell goal) {
53  this._goal = goal;
54  this.InternalReset();
55  }
56 
60  protected override void PostSetup() {
61  if (!this._goal) {
62  this._goal = FindObjectOfType<EmptyCell>();
63  }
64 
65  if (!this._actor) {
66  this._actor = FindObjectOfType<Actor>();
67  }
68 
69  if (this._goal) {
70  NeodroidUtilities
71  .RegisterCollisionTriggerCallbacksOnChildren<ChildCollider3DSensor, Collider, Collision>(this,
72  this
73  ._goal
74  .transform,
75  null,
76  this
77  .OnTriggerEnterChild);
78  }
79 
80  if (this._actor) {
81  NeodroidUtilities
82  .RegisterCollisionTriggerCallbacksOnChildren<ChildCollider3DSensor, Collider, Collision>(this,
83  this
84  ._actor
85  .transform,
86  null,
87  this
88  .OnTriggerEnterChild);
89  }
90  }
91 
92  void OnTriggerEnterChild(GameObject child_game_object, Collider other_game_object) {
93  Debug.Log("triggered");
94  if (this._actor) {
95  if (this._based_on_tags) {
96  if (other_game_object.CompareTag(this._actor.tag)) {
97  #if NEODROID_DEBUG
98  if (this.Debugging) {
99  Debug.Log("Actor is inside area");
100  }
101  #endif
102 
103  this._overlapping = ActorOverlapping.Inside_area_;
104  }
105  } else {
106  if (child_game_object == this._goal.gameObject
107  && other_game_object.gameObject == this._actor.gameObject) {
108  #if NEODROID_DEBUG
109  if (this.Debugging) {
110  Debug.Log("Actor is inside area");
111  }
112  #endif
113  this._overlapping = ActorOverlapping.Inside_area_;
114  }
115  }
116  }
117  }
118 
121  enum ActorOverlapping {
124  Inside_area_,
125 
129  }
130  }
131 }