Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
PlayerReactions.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace droid.Runtime.InternalReactions {
11  [ExecuteInEditMode]
15  [SerializeField]
16  bool _auto_reset = true;
17 
18  List<ActuatorMotion> _motions = new List<ActuatorMotion>();
19 
22  [SerializeField]
23  PlayerMotions _player_motions = null;
24 
27  EnvironmentState[] _states;
28 
31  void Start() {
32  this._Manager = FindObjectOfType<AbstractNeodroidManager>();
33  if (Application.isPlaying) {
34  var reset_reaction = new ReactionParameters(reset : true);
35  this._states = this._Manager.ReactAndCollectStates(new Reaction(reset_reaction, "all"));
36  }
37  }
38 
39  void Update() {
40  if (Application.isPlaying) {
41  if (this._states == null) {
42  var reset_reaction_parameters = new ReactionParameters(reset : true);
43  this._states = this._Manager.ReactAndCollectStates(new Reaction(reset_reaction_parameters, "all"));
44  }
45 
46  var reset = false;
47  if (this._player_motions != null) {
48  this._motions.Clear();
49  if (this._player_motions._Motions != null) {
50  foreach (var player_motion in this._player_motions._Motions) {
51  if (Input.GetKey(player_motion._Key)) {
52  #if NEODROID_DEBUG
53  if (this.Debugging) {
54  Debug.Log($"{player_motion._Actor} {player_motion._Actuator} {player_motion._Strength}");
55  }
56  #endif
57 
58  if (player_motion._Actuator == "Reset") {
59  reset = true;
60  break;
61  }
62 
63  var motion = new ActuatorMotion(player_motion._Actor,
64  player_motion._Actuator,
65  player_motion._Strength);
66  this._motions.Add(motion);
67  }
68  }
69  }
70 
71  if (reset) {
72  var reset_reaction_parameters = new ReactionParameters(reset : true);
73  this._states =
74  this._Manager.ReactAndCollectStates(new Reaction(reset_reaction_parameters, "all"));
75  } else {
76  var step = this._motions.Count > 0;
77  if (step) {
78  foreach (var state in this._states) {
79  if (this._auto_reset && state.Terminated) {
80  var reset_reaction = new ReactionParameters(reset : true) {IsExternal = false};
81  this._Manager.ReactAndCollectStates(new Reaction(reset_reaction, state.EnvironmentName));
82  }
83  }
84 
85  var parameters = new ReactionParameters(true, true, episode_count : true) {IsExternal = false};
86  var reaction = new Reaction(parameters, this._motions.ToArray(), null, null, null, "");
87  this._states = this._Manager.ReactAndCollectStates(reaction);
88  }
89  }
90  } else {
91  #if NEODROID_DEBUG
92  if (this.Debugging) {
93  Debug.Log("No PlayerMotions ScriptableObject assigned");
94  }
95  #endif
96  }
97  }
98  }
99  }
100 }
Has a possible direction given by the sign of the float in strength
Definition: MotorMotion.cs:9