Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
GridPositionSensor.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
4 using UnityEngine;
5 
6 namespace droid.Runtime.Prototyping.Sensors.Grid {
10  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
11  + "GridPosition"
12  + SensorComponentMenuPath._Postfix)]
13  public class GridPositionSensor : Sensor,
14  IHasSingle {
17  int[,] _grid = null;
18 
21  [SerializeField]
22  int _height = 0;
23 
24  [Header("Observation", order = 103)]
25  [SerializeField]
26  float _observation_value;
27 
28  [SerializeField] Space1 _observation_value_space;
29  [SerializeField] int _width = 0;
30 
33  public override string PrototypingTypeName { get { return "Value"; } }
34 
37  public float ObservationValue {
38  get { return this._observation_value; }
39  set {
40  this._observation_value = this.SingleSpace.IsNormalised
41  ? this._observation_value_space.ClipNormaliseRound(value)
42  : value;
43  }
44  }
45 
46  public Space1 SingleSpace { get { return this._observation_value_space; } }
47 
48  protected override void PreSetup() {
49  this._grid = new int[this._width, this._height];
50 
51  var k = 0;
52  for (var i = 0; i < this._width; i++) {
53  for (var j = 0; j < this._height; j++) {
54  this._grid[i, j] = k++;
55  }
56  }
57  }
58 
59  public override IEnumerable<float> FloatEnumerable { get { return new[] {this.ObservationValue}; } }
60 
61  public override void UpdateObservation() {
62  var position = this.transform.position;
63  var x = position.x + this._width;
64  var z = position.z + this._height;
65 
66  this.ObservationValue = this._grid[(int)x, (int)z];
67  }
68  }
69 }