Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
Actuator.cs
Go to the documentation of this file.
1 using System;
8 using UnityEngine;
9 
10 namespace droid.Runtime.Prototyping.Actuators {
14  [ExecuteInEditMode]
15  [Serializable]
16  public abstract class Actuator : PrototypingGameObject,
17  //IResetable,
18  IActuator {
21  public IHasRegister<Actuator> Parent { get { return this._parent; } set { this._parent = value; } }
22 
25  public float EnergySpendSinceReset {
26  get { return this._energy_spend_since_reset; }
27  set { this._energy_spend_since_reset = value; }
28  }
29 
32  public float EnergyCost { get { return this._energy_cost; } set { this._energy_cost = value; } }
33 
36  public override String PrototypingTypeName { get { return "Actuator"; } }
37 
40  public Space1 MotionSpace {
41  get { return this._motion_value_space; }
42  set { this._motion_value_space = value; }
43  }
44 
48  public void ApplyMotion(IMotion motion) {
49  #if NEODROID_DEBUG
50  if (this.Debugging) {
51  Debug.Log("Applying " + motion + " To " + this.name);
52  }
53  #endif
54 
55  if (motion.Strength < this.MotionSpace._Min_Value || motion.Strength > this.MotionSpace._Max_Value) {
56  Debug.LogWarning($"It does not accept input {motion.Strength}, outside the allowed range from {this.MotionSpace._Min_Value} to {this.MotionSpace._Max_Value}");
57  return; // Do nothing
58  }
59 
60  motion.Strength = this._motion_value_space.Round(motion.Strength);
61 
62  this.InnerApplyMotion(motion);
63  this.EnergySpendSinceReset += Mathf.Abs(this.EnergyCost * motion.Strength);
64  }
65 
69  public virtual float GetEnergySpend() { return this._energy_spend_since_reset; }
70 
73  public void EnvironmentReset() { this._energy_spend_since_reset = 0; }
74 
79  public virtual float Sample() { return this.MotionSpace.Sample(); }
80 
84  protected override void RegisterComponent() {
85  this.Parent = NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.Parent, this, true);
86  }
87 
91  protected override void UnRegisterComponent() { this.Parent?.UnRegister(this); }
92 
96  protected abstract void InnerApplyMotion(IMotion motion);
97 
102  public override string ToString() { return this.Identifier; }
103 
104  #region Fields
105 
106  [Header("References", order = 99)]
107  [SerializeField]
108  IHasRegister<Actuator> _parent;
109 
110  [Header("General", order = 101)]
111  [SerializeField]
112  Space1 _motion_value_space = Space1.MinusOneOne;
113 
114  [SerializeField] float _energy_spend_since_reset;
115 
116  [SerializeField] float _energy_cost;
117 
118  #endregion
119  }
120 }