using System; namespace RoboidControl { /// /// A sensor which can detect touches /// public class TouchSensor : Thing { /* /// /// Create a touch sensor without communication abilities /// /// Invoke a OnNewThing event when the thing has been created public TouchSensor(bool invokeEvent = true) : base(Type.TouchSensor, invokeEvent) { } /// /// Create a touch sensor for a participant /// /// The owning participant /// The ID of the thing, leave out or set to zero to generate an ID /// Invoke a OnNewThing event when the thing has been created public TouchSensor(Participant owner, byte thingId = 0, bool invokeEvent = true) : base(owner, Type.TouchSensor, thingId, invokeEvent) { } */ /// /// Create a new child touch sensor /// /// The parent thing public TouchSensor(Thing parent) : base(parent) { this.type = Type.TouchSensor; this.name = "TouchSensor"; } /// /// Value which is true when the sensor is touching something, false otherwise /// private bool _touchedSomething = false; public bool touchedSomething { get { return _touchedSomething; } set { if (_touchedSomething != value) { _touchedSomething = value; owner.Send(new BinaryMsg(this)); } touchUpdated = true; } } private bool touchUpdated = false; #if UNITY_5_3_OR_NEWER /// @copydoc Passer::RoboidControl::Thing::CreateComponent public override void CreateComponent() { // System.Console.Write("Create touch sensor component"); this.component = Unity.TouchSensor.Create(this); this.component.core = this; } #endif /// /// Function used to generate binary data for this touch sensor /// /// A byte array with the binary data /// The byte array will be empty when the touch status has not changed public override byte[] GenerateBinary() { #if UNITY_5_3_OR_NEWER // We use the unity component because we only want to send the state of what is generated in the simulation Unity.TouchSensor touchComponent = this.component as Unity.TouchSensor; if (touchComponent == null || !touchUpdated) return Array.Empty(); byte[] bytes = new byte[1]; bytes[0] = (byte)(touchComponent.touchedSomething ? 1 : 0); touchUpdated = false; return bytes; #else return Array.Empty(); #endif } private bool externalTouch = false; /// /// Function used to process binary data received for this touch sensor /// /// The binary data to process public override void ProcessBinary(byte[] bytes) { //this.touchedSomething |= (bytes[0] == 1); this.externalTouch = (bytes[0] == 1); } } }