1010
1111namespace ECS_MLAgents_v0 . Example . SpaceWars . Scripts
1212{
13-
14- public class SmartShipSystem : AgentSystem < ShipSensor , Steering > { }
15- public class PlayerShipSystem : AgentSystem < ShipSensor , Steering > { }
13+
14+ public class SmartShipSystem : AgentSystem < ShipSensor , Steering > { }
15+ public class PlayerShipSystem : AgentSystem < ShipSensor , Steering > { }
1616
1717
1818 public class Manager : MonoBehaviour
1919 {
20-
21-
2220 public float TargetAngle ;
2321 public GameObject target ;
2422 public GameObject Camera ;
2523
24+ public enum DecisionSelector { NeuralNetwork , ExternalDecision } ;
25+ public DecisionSelector shipDecisionSelector ;
26+ public DecisionSelector playerDecisionSelector ;
27+
2628 private EntityManager manager ;
2729 public GameObject prefab ;
28-
29-
30+
31+
3032 private SmartShipSystem _shipSystemA ;
3133 private PlayerShipSystem _playerSystem ;
3234
@@ -39,43 +41,36 @@ public class Manager : MonoBehaviour
3941
4042 void Start ( )
4143 {
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
5644 Time . captureFramerate = 60 ;
5745 QualitySettings . vSyncCount = 0 ;
5846 Application . targetFrameRate = - 1 ;
59-
60-
61-
6247 manager = World . Active . GetOrCreateManager < EntityManager > ( ) ;
63-
6448 _sensorSystem = World . Active . GetOrCreateManager < SensorPopulate > ( ) ;
6549 _impactSystem = World . Active . GetOrCreateManager < ImpactSystem > ( ) ;
6650 _impactSystem . Radius = 20 ;
6751
6852 _shipSystemA = World . Active . GetExistingManager < SmartShipSystem > ( ) ;
69- // _shipSystemA.Decision = new NNDecision<ShipSensor, Steering>(model);
70- _shipSystemA . Decision = new NNDecision < ShipSensor , Steering > ( model ) ;
71- // _shipSystemA.Decision = new ExternalDecision();
53+ if ( shipDecisionSelector == DecisionSelector . ExternalDecision )
54+ {
55+ _shipSystemA . Decision = new ExternalDecision < ShipSensor , Steering > ( ) ;
56+ }
57+ else
58+ {
59+ _shipSystemA . Decision = new NNDecision < ShipSensor , Steering > ( model ) ;
60+ }
7261 _playerSystem = World . Active . GetExistingManager < PlayerShipSystem > ( ) ;
73- // _playerSystem.Decision = new NNDecision<ShipSensor, Steering>(model);
74- _playerSystem . Decision = new HumanDecision < ShipSensor > ( ) ;
62+ if ( playerDecisionSelector == DecisionSelector . ExternalDecision )
63+ {
64+ _playerSystem . Decision = new HumanDecision < ShipSensor > ( ) ;
65+ }
66+ else
67+ {
68+ _playerSystem . Decision = new NNDecision < ShipSensor , Steering > ( model ) ;
69+ }
7570 _playerSystem . SetNewComponentGroup ( typeof ( PlayerFlag ) ) ;
7671 _shipSystemA . DecisionRequester = new FixedTimeRequester ( 0.1f ) ;
7772
78- _playerEntity = manager . Instantiate ( prefab ) ;
73+ _playerEntity = manager . Instantiate ( prefab ) ;
7974 MakeSpaceShip ( _playerEntity ) ;
8075 manager . AddComponentData ( _playerEntity , new PlayerFlag ( ) ) ;
8176 manager . SetComponentData ( _playerEntity , new Ship
@@ -85,36 +80,37 @@ void Start()
8580 MaxReloadTime = 1f
8681 } ) ;
8782
88-
83+
8984 Spawn ( 10 ) ;
90-
91- // Debug.Log(typeof(ShipSensor).GetCustomAttributes(typeof(SerializableAttribute), true)[0]);
92- AttributeUtility . GetSensorMetaData ( typeof ( ShipSensor ) ) ;
85+
86+ // Debug.Log(typeof(ShipSensor).GetCustomAttributes(typeof(SerializableAttribute), true)[0]);
87+ AttributeUtility . GetSensorMetaData ( typeof ( ShipSensor ) ) ;
9388 }
9489
9590
96- void FixedUpdate ( ) {
91+ void FixedUpdate ( )
92+ {
9793 // World.Active.GetOrCreateManager<SimulationSystemGroup>();
9894 }
9995
10096 void Update ( )
10197 {
10298
103-
99+
104100
105101 // for (var i = 0; i < 10; i++){
106102 // foreach(var behavior in World.Active.BehaviourManagers)
107103 // {
108104 // behavior.Update();
109105 // }
110106 // }
111- // Debug.Log(Application.targetFrameRate);
107+ // Debug.Log(Application.targetFrameRate);
112108 float3 targetPos = 100 * new float3 ( math . cos ( TargetAngle ) , 0 , math . sin ( TargetAngle ) ) ;
113109 _sensorSystem . Center = targetPos ;
114110 _impactSystem . Center = targetPos ;
115111 target . transform . position = targetPos ;
116112
117- TargetAngle += Time . deltaTime / 20f ;
113+ TargetAngle += Time . deltaTime / 20f ;
118114 if ( Input . GetKeyDown ( KeyCode . A ) )
119115 {
120116 Spawn ( 1 ) ;
@@ -133,8 +129,8 @@ void Update()
133129 var camPosition = manager . GetComponentData < Position > ( _playerEntity ) . Value ;
134130 var camRotation = manager . GetComponentData < Rotation > ( _playerEntity ) . Value ;
135131 camPosition += math . mul ( camRotation , new float3 ( 0 , 0 , 5 ) ) ;
136- Camera . transform . position = Vector3 . Lerp ( Camera . transform . position , camPosition , 0.1f ) ;
137- Camera . transform . rotation = Quaternion . Lerp ( Camera . transform . rotation , camRotation , 0.1f ) ;
132+ Camera . transform . position = Vector3 . Lerp ( Camera . transform . position , camPosition , 0.1f ) ;
133+ Camera . transform . rotation = Quaternion . Lerp ( Camera . transform . rotation , camRotation , 0.1f ) ;
138134
139135 }
140136
@@ -146,18 +142,19 @@ void Spawn(int amount)
146142 for ( int i = 0 ; i < amount ; i ++ )
147143 {
148144 MakeSpaceShip ( entities [ i ] ) ;
149-
145+
150146 }
151147 entities . Dispose ( ) ;
152148 }
153-
154- private void MakeSpaceShip ( Entity ent ) {
149+
150+ private void MakeSpaceShip ( Entity ent )
151+ {
155152 float valX = Random . Range ( - 1f , 1f ) ;
156153 float valY = Random . Range ( - 1f , 1f ) ;
157154 float valZ = Random . Range ( - 1f , 1f ) ;
158155 float valD = Random . Range ( 0f , 1f ) ;
159-
160-
156+
157+
161158
162159 float3 SpawnOffset = valD *
163160 Globals . SPAWN_DISTANCE *
@@ -167,15 +164,15 @@ private void MakeSpaceShip(Entity ent){
167164 manager . SetComponentData ( ent ,
168165 new Position
169166 {
170- Value = SpawnOffset
167+ Value = SpawnOffset
171168 } ) ;
172169 manager . SetComponentData ( ent ,
173170 new Rotation
174171 {
175172 Value = quaternion . EulerXYZ (
176173 math . normalize ( new float3 (
177- Random . Range ( - 1f , 1f ) ,
178- Random . Range ( - 1f , 1f ) ,
174+ Random . Range ( - 1f , 1f ) ,
175+ Random . Range ( - 1f , 1f ) ,
179176 Random . Range ( - 1f , 1f ) ) )
180177 )
181178 } ) ;
@@ -198,7 +195,7 @@ private void MakeSpaceShip(Entity ent){
198195 }
199196
200197 }
201-
198+
202199
203200}
204201
0 commit comments