void Awake() { if (instance == null) { instance = this; DontDestroyOnLoad(this.gameObject); } }
// Start is called before the first frame update void Start() { var udpSarlInterface = FindObjectsOfType(typeof(UdpSarlInterface)) as UdpSarlInterface[]; if (udpSarlInterface.Length != 0) { udpSarlInterface[0].PhysicalInfluenceReceived += physicalInfluence => { var id = physicalInfluence.Id; UnityMainThreadDispatcher.Instance().Enqueue(() => { // Instantiate the body if it doesn't exist (later we will remove this part) if (!BodiesRepository.Instance().Bodies.ContainsKey(id)) { var bodyGameObject = BodiesRepository.Instance().Spawn(id); // TODO Position is randomized (the concept of spawners must be introduced) bodyGameObject.transform.position = new Vector3(Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f)); } // TODO In a first time we just apply the force of the influence (PoC) BodiesRepository.Instance().Bodies[id].GetComponent <Rigidbody>().AddRelativeForce(physicalInfluence.Force, ForceMode.Force); //bodies[id].GetComponent<Rigidbody>().AddForceAtPosition(physicalInfluence.Force, physicalInfluence.ForceLocation); //bodies[id].GetComponent<Rigidbody>().AddTorque(physicalInfluence.AngularForce); }); //influences.Add(id, physicalInfluence); }; udpSarlInterface[0].SimulationControlReceived += simulationControl => { if (simulationControl.Type == SimulationControl.ActionType.CreateBody) { UnityMainThreadDispatcher.Instance().Enqueue(() => { var id = simulationControl.Data; Debug.Log("Creating body: " + id); if (!BodiesRepository.Instance().Bodies.ContainsKey(id)) { var bodyGameObject = BodiesRepository.Instance().Spawn(id); // TODO Position is randomized (the concept of spawners must be introduced) bodyGameObject.transform.position = new Vector3(Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f)); } }); } }; } }
/// <summary> /// Trigger a simulation step /// </summary> void Step() { UnityMainThreadDispatcher.Instance().Enqueue(() => { // Trigger the phyics simulation Physics.Simulate(deltaTime); // Compute the perceptions for all bodies if they have a perceiver component foreach (var agentBody in BodiesRepository.Instance().Bodies) { var perceiver = agentBody.Value.GetComponent(typeof(Perceiver)) as Perceiver; if (perceiver != null) { var perceptions = perceiver.PerceivedObjets; this.sarlInterface.EmitPerceptions(new PerceptionList(agentBody.Key, perceptions)); } } }); }
void OnDestroy() { instance = null; }