87 lines
3.4 KiB
C#
87 lines
3.4 KiB
C#
using System;
|
|
|
|
namespace RoboidControl {
|
|
|
|
/// <summary>
|
|
/// A sensor which can detect touches
|
|
/// </summary>
|
|
public class TouchSensor : Thing {
|
|
/*
|
|
/// <summary>
|
|
/// Create a touch sensor without communication abilities
|
|
/// </summary>
|
|
/// <param name="invokeEvent">Invoke a OnNewThing event when the thing has been created</param>
|
|
public TouchSensor(bool invokeEvent = true) : base(Type.TouchSensor, invokeEvent) { }
|
|
/// <summary>
|
|
/// Create a touch sensor for a participant
|
|
/// </summary>
|
|
/// <param name="owner">The owning participant</param>
|
|
/// <param name="thingId">The ID of the thing, leave out or set to zero to generate an ID</param>
|
|
/// <param name="invokeEvent">Invoke a OnNewThing event when the thing has been created</param>
|
|
public TouchSensor(Participant owner, byte thingId = 0, bool invokeEvent = true) : base(owner, Type.TouchSensor, thingId, invokeEvent) { }
|
|
*/
|
|
/// <summary>
|
|
/// Create a new child touch sensor
|
|
/// </summary>
|
|
/// <param name="parent">The parent thing</param>
|
|
public TouchSensor(Thing parent) : base(parent) {
|
|
this.type = Type.TouchSensor;
|
|
this.name = "TouchSensor";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Value which is true when the sensor is touching something, false otherwise
|
|
/// </summary>
|
|
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
|
|
/// <summary>
|
|
/// Function used to generate binary data for this touch sensor
|
|
/// </summary>
|
|
/// <returns>A byte array with the binary data</returns>
|
|
/// <remark>The byte array will be empty when the touch status has not changed</remark>
|
|
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>();
|
|
|
|
byte[] bytes = new byte[1];
|
|
bytes[0] = (byte)(touchComponent.touchedSomething ? 1 : 0);
|
|
touchUpdated = false;
|
|
return bytes;
|
|
#else
|
|
return Array.Empty<byte>();
|
|
#endif
|
|
}
|
|
|
|
private bool externalTouch = false;
|
|
/// <summary>
|
|
/// Function used to process binary data received for this touch sensor
|
|
/// </summary>
|
|
/// <param name="bytes">The binary data to process</param>
|
|
public override void ProcessBinary(byte[] bytes) {
|
|
//this.touchedSomething |= (bytes[0] == 1);
|
|
this.externalTouch = (bytes[0] == 1);
|
|
}
|
|
}
|
|
} |