Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
Actor.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
7 using UnityEditor;
8 using UnityEngine;
9 
10 namespace droid.Runtime.Prototyping.Actors {
14  [AddComponentMenu(ActorComponentMenuPath._ComponentMenuPath + "Base" + ActorComponentMenuPath._Postfix)]
15  [ExecuteInEditMode]
16  public class Actor : PrototypingGameObject,
17  IActor
18  //IResetable
19  {
22  [SerializeField]
23  Bounds _bounds;
24 
27  [SerializeField]
28  bool _draw_bounds = false;
29 
32  public Bounds ActorBounds {
33  get {
34  var col = this.GetComponent<BoxCollider>();
35  this._bounds = new Bounds(this.transform.position, Vector3.zero); // position and size
36 
37  if (col) {
38  this._bounds.Encapsulate(col.bounds);
39  }
40 
41  foreach (var child_col in this.GetComponentsInChildren<Collider>()) {
42  if (child_col != col) {
43  this._bounds.Encapsulate(child_col.bounds);
44  }
45  }
46 
47  return this._bounds;
48  }
49  }
50 
51  Dictionary<string, IActuator> IActor.Actuators { get { return this._Actuators; } }
52 
53  public Transform Transform { get { return this.transform; } }
54 
58  public virtual void ApplyMotion(IMotion motion) {
59  #if NEODROID_DEBUG
60  if (this.Debugging) {
61  Debug.Log($"Applying {motion} To {this.name}'s Actuators");
62  }
63  #endif
64 
65  var motion_actuator_name = motion.ActuatorName;
66  if (this._Actuators.ContainsKey(motion_actuator_name)
67  && this._Actuators[motion_actuator_name] != null) {
68  this._Actuators[motion_actuator_name].ApplyMotion(motion);
69  } else {
70  #if NEODROID_DEBUG
71  if (this.Debugging) {
72  Debug.Log($"Could find not Actuator with the specified name: {motion_actuator_name} on actor {this.name}");
73  }
74  #endif
75  }
76  }
77 
81  public virtual void EnvironmentReset() {
82  if (this._Actuators != null) {
83  foreach (var actuator in this._Actuators.Values) {
84  actuator?.EnvironmentReset();
85  }
86  }
87  }
88 
93  public void UnRegister(IActuator actuator, string identifier) {
94  if (this._Actuators != null) {
95  if (this._Actuators.ContainsKey(identifier)) {
96  #if NEODROID_DEBUG
97  if (this.Debugging) {
98  Debug.Log($"Actor {this.name} unregistered Actuator {identifier}");
99  }
100  #endif
101 
102  this._Actuators.Remove(identifier);
103  }
104  }
105  }
106 
110  public void UnRegister(IActuator actuator) { this.UnRegister(actuator, actuator.Identifier); }
111 
115  protected override void Setup() {
116  #if UNITY_EDITOR
117  if (!Application.isPlaying) {
118  var manager_script = MonoScript.FromMonoBehaviour(this);
119  if (MonoImporter.GetExecutionOrder(manager_script) != _script_execution_order) {
120  MonoImporter.SetExecutionOrder(manager_script,
121  _script_execution_order); // Ensures that PreStep is called first, before all other scripts.
122  Debug.LogWarning("Execution Order changed, you will need to press play again to make everything function correctly!");
123  EditorApplication.isPlaying = false;
124  //TODO: UnityEngine.Experimental.LowLevel.PlayerLoop.SetPlayerLoop(new UnityEngine.Experimental.LowLevel.PlayerLoopSystem());
125  }
126  }
127  #endif
128  }
129 
133  protected override void Clear() { this._Actuators.Clear(); }
134 
138  protected override void RegisterComponent() {
139  this.ParentEnvironment =
140  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, this);
141  }
142 
146  protected override void UnRegisterComponent() { this._environment?.UnRegister(this); }
147 
150  void Update() {
151  if (this._draw_bounds) {
152  var corners =
153  Corners.ExtractCorners(this.ActorBounds.center, this.ActorBounds.extents, this.transform);
154 
155  Corners.DrawBox(corners[0],
156  corners[1],
157  corners[2],
158  corners[3],
159  corners[4],
160  corners[5],
161  corners[6],
162  corners[7],
163  Color.gray);
164  }
165  }
166 
171  public void RegisterActuator(IActuator actuator, string identifier) {
172  #if NEODROID_DEBUG
173  if (this.Debugging) {
174  Debug.Log("Actor " + this.name + " has Actuator " + identifier);
175  }
176  #endif
177 
178  if (!this._Actuators.ContainsKey(identifier)) {
179  this._Actuators.Add(identifier, actuator);
180  } else {
181  #if NEODROID_DEBUG
182  if (this.Debugging) {
183  Debug.Log($"A Actuator with the identifier {identifier} is already registered");
184  }
185  #endif
186  }
187  }
188 
189  #region Fields
190 
193  [Header("References", order = 99)]
194  [SerializeField]
195  ActorisedPrototypingEnvironment _environment;
196 
199  [Header("General", order = 101)]
200  [SerializeField]
201  protected Dictionary<string, IActuator> _Actuators = new Dictionary<string, IActuator>();
202 
203  #if UNITY_EDITOR
204  const int _script_execution_order = -10;
205  #endif
206 
207  #endregion
208 
209  #region Getters
210 
214  public override string PrototypingTypeName { get { return "Actor"; } }
215 
219  public void Register(IActuator actuator) { this.RegisterActuator(actuator, actuator.Identifier); }
220 
226  public void Register(IActuator actuator, string identifier) {
227  this.RegisterActuator(actuator, identifier);
228  }
229 
232  public Dictionary<string, IActuator> Actuators { get { return this._Actuators; } }
233 
236  public ActorisedPrototypingEnvironment ParentEnvironment {
237  get { return this._environment; }
238  set { this._environment = value; }
239  }
240 
241  #endregion
242  }
243 }
Environment to be used with the prototyping components.
virtual void ApplyMotion(IMotion motion)
Definition: Actor.cs:58
void Register(IActuator actuator)
Definition: Actor.cs:219
void UnRegister(IActuator actuator, string identifier)
Definition: Actor.cs:93
void RegisterActuator(IActuator actuator, string identifier)
Definition: Actor.cs:171
Dictionary< string, IActuator > Actuators
Definition: IActor.cs:7
void UnRegister(IActuator actuator)
Definition: Actor.cs:110
override void UnRegisterComponent()
Definition: Actor.cs:146
void Register(IActuator actuator, string identifier)
Definition: Actor.cs:226