59 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| 
 | |
| namespace RoboidControl {
 | |
| 
 | |
|     /// <summary>
 | |
|     /// A sensor which can detect touches
 | |
|     /// </summary>
 | |
|     public class TouchSensor : Thing {
 | |
| 
 | |
|         public Participant thisParticipant;
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Value which is true when the sensor is touching something, false otherwise
 | |
|         /// </summary>
 | |
|         //public bool touchedSomething = false;
 | |
|         private bool _touchedSomething = false;
 | |
|         public bool touchedSomething {
 | |
|             get { return _touchedSomething; }
 | |
|             set {
 | |
|                 _touchedSomething = value;
 | |
|                 if (thisParticipant != null && this.owner != thisParticipant ) {
 | |
|                     BinaryMsg msg = new(networkId, this);
 | |
|                     foreach (RemoteParticipant remoteParticipant in thisParticipant.owners)
 | |
|                         thisParticipant.Send(remoteParticipant, msg);
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Create a touch sensor
 | |
|         /// </summary>
 | |
|         /// <param name="owner">The participant for with the sensor is needed</param>
 | |
|         /// <param name="invokeEvent">True when the creation should trigger an event</param>
 | |
|         public TouchSensor(RemoteParticipant owner, bool invokeEvent = true) : base(owner, invokeEvent) {
 | |
|             //touchedSomething = false;
 | |
|             //thisParticipant = owner;
 | |
|         }
 | |
| 
 | |
|         public TouchSensor(RemoteParticipant owner, byte networkId, byte thingId) : base(owner, networkId, thingId) {
 | |
|             Console.Write("TouchSensor constructor");
 | |
|             //touchedSomething = false;
 | |
|             //thisParticipant = participant;
 | |
|         }
 | |
| 
 | |
| #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
 | |
|         public override byte[] GenerateBinary() {
 | |
|             byte[] buffer = new byte[1];
 | |
|             buffer[0] = (byte)(touchedSomething ? 1 : 0);
 | |
|             return buffer;
 | |
|         }
 | |
|     }
 | |
| } | 
