Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
PunishmentFunction.cs
Go to the documentation of this file.
2 using UnityEngine;
3 
4 namespace droid.Runtime.Prototyping.Evaluation {
8  [AddComponentMenu(EvaluationComponentMenuPath._ComponentMenuPath
9  + "PunishmentFunction"
10  + EvaluationComponentMenuPath._Postfix)]
11  [RequireComponent(typeof(Rigidbody))]
13  [SerializeField] string _avoid_tag = "balls";
14  [SerializeField] int _hits = 0;
15 
16  //[SerializeField] LayerMask _layer_mask;
17 
18  [SerializeField] GameObject _player = null;
19 
20  // Use this for initialization
24  protected override void PostSetup() {
25  this.ResetHits();
26 
27  var tagged_gos = GameObject.FindGameObjectsWithTag(this._avoid_tag);
28 
29  foreach (var ball in tagged_gos) {
30  if (ball) {
31  var publisher = ball.GetComponent<ChildCollider3DSensor>();
32  if (!publisher || publisher.Caller != this) {
33  publisher = ball.AddComponent<ChildCollider3DSensor>();
34  }
35 
36  publisher.Caller = this;
37  publisher.OnCollisionEnterDelegate = this.OnChildCollision;
38  }
39  }
40  }
41 
42  void OnChildCollision(GameObject child_sensor_game_object, Collision collision) {
43  if (collision.collider.name == this._player.name) {
44  this._hits += 1;
45  }
46 
47  #if NEODROID_DEBUG
48  if (this.Debugging) {
49  Debug.Log(this._hits);
50  }
51  #endif
52  }
53 
54  void ResetHits() { this._hits = 0; }
55 
59  public override void InternalReset() { this.ResetHits(); }
60 
65  public override float InternalEvaluate() { return this._hits * -1f; }
66  }
67 }