Remote thing

This commit is contained in:
Pascal Serrarens 2025-06-11 14:22:52 +02:00
parent 765231588f
commit ba81b9f71d
3 changed files with 22 additions and 3 deletions

View File

@ -22,12 +22,14 @@ namespace RoboidControl.Unity {
/// If this is not available, a default representation is created.
public static DifferentialDrive Create(RoboidControl.DifferentialDrive coreDrive) {
DifferentialDrive differentialDrive;
Rigidbody rb = null;
GameObject prefab = (GameObject)Resources.Load("DifferentialDrive");
if (prefab != null) {
// Use resource prefab when available
GameObject gameObj = Instantiate(prefab);
differentialDrive = gameObj.GetComponent<DifferentialDrive>();
differentialDrive.Init(coreDrive);
rb = gameObj.GetComponent<Rigidbody>();
}
else {
// Default implementation
@ -35,10 +37,14 @@ namespace RoboidControl.Unity {
differentialDrive = gameObj.AddComponent<DifferentialDrive>();
differentialDrive.Init(coreDrive);
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
rb = gameObj.AddComponent<Rigidbody>();
rb.isKinematic = false;
rb.mass = 0.1f;
}
if (coreDrive.isRemote) {
if (rb != null)
rb.isKinematic = true;
}
return differentialDrive;
}

View File

@ -463,8 +463,18 @@ namespace RoboidControl {
this.updateQueue.Enqueue(e);
Console.WriteLine("Added Participant");
}
Thing thing = owner.Get(msg.networkId, msg.thingId);
thing ??= ProcessNewThing(owner, msg);
if (sender.networkId != owner.networkId) {
// Sender forwarded this thing msg
// So this is a remote thing
thing ??= ProcessNewThing(owner, msg, true);
}
else {
// Sender is the owner of the thing
// So this is a simulated thing
thing ??= ProcessNewThing(owner, msg, false);
}
if (msg.parentId != 0) {
thing.parent = owner.Get(msg.networkId, msg.parentId);
@ -479,7 +489,7 @@ namespace RoboidControl {
ForwardMessage(sender, msg);
}
protected virtual Thing ProcessNewThing(Participant owner, ThingMsg msg) {
protected virtual Thing ProcessNewThing(Participant owner, ThingMsg msg, bool isRemote) {
Console.WriteLine("--- New Thing");
Thing newThing = msg.thingType switch {
Thing.Type.TouchSensor => new TouchSensor(owner.root),
@ -488,6 +498,7 @@ namespace RoboidControl {
};
newThing.id = msg.thingId;
newThing.type = msg.thingType;
newThing.isRemote = isRemote;
return newThing;
}

View File

@ -136,6 +136,8 @@ namespace RoboidControl {
/// This can be either a \ref RoboidControl::Thing::Type "Thing.Type" or a byte value for custom types.
public byte type = Type.Undetermined;
public bool isRemote = false;
/// <summary>
/// The participant owning this thing
/// </summary>