Skip to content

Commit

Permalink
Keyboard: support players other than owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebual committed Jun 13, 2021
1 parent 2ea4c7c commit abafb42
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 38 deletions.
39 changes: 11 additions & 28 deletions code/entities/KeyboardEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ public partial class KeyboardEntity : Prop, IUse, WireOutputEntity
["Slot9"] = InputButton.Slot9,
};

public void UpdateEntity( Entity owner )
{
if ( IsServer && owner is SandboxPlayer playerOwner ) {
playerOwner.OnSimulate -= OnSimulatePlayer;
playerOwner.OnSimulate += OnSimulatePlayer;
}
}

public bool IsUsable( Entity user )
{
return user is SandboxPlayer;
Expand All @@ -54,33 +46,22 @@ public bool OnUse( Entity user )
{
if ( user is SandboxPlayer player ) {
if ( player.Controller.GetType() != typeof( LockedPositionController ) ) {
player.OnSimulate -= OnSimulatePlayer;
player.OnSimulate += OnSimulatePlayer;
player.EnableSolidCollisions = false;
player.PhysicsBody.CollisionEnabled = false;
PhysicsBody targetPhysicsBody;
if ( Parent is ModelEntity modelParent ) {
targetPhysicsBody = modelParent.PhysicsBody;
}
else if ( Parent is Entity entityParent ) {
targetPhysicsBody = entityParent.PhysicsGroup.GetBody( 0 );
}
else {
targetPhysicsBody = this.PhysicsBody;
}
for ( int shapeIndex = 0; shapeIndex < player.PhysicsGroup.BodyCount; ++shapeIndex ) {
//player.PhysicsGroup.GetBody( shapeIndex ).Parent = targetPhysicsBody;
}
player.Controller = new LockedPositionController();

this.WireTriggerOutput( "Active", true );
}
else {
player.OnSimulate -= OnSimulatePlayer;
player.EnableSolidCollisions = true;
player.PhysicsBody.CollisionEnabled = true;
player.Controller = new WalkController();

for ( int shapeIndex = 0; shapeIndex < player.PhysicsGroup.BodyCount; ++shapeIndex ) {
//player.PhysicsGroup.GetBody( shapeIndex ).Parent = null;
}
this.WireTriggerOutput( "Active", false );
}
player.Controller = player.Controller.GetType() != typeof( LockedPositionController )
? new LockedPositionController()
: new WalkController();
}
return false;
}
Expand All @@ -97,6 +78,8 @@ private void OnSimulatePlayer( SandboxPlayer player )
}
public string[] WireGetOutputs()
{
return inputButtons.Keys.ToArray();
return inputButtons.Keys
.Concat(new string[]{"Active"})
.ToArray();
}
}
11 changes: 1 addition & 10 deletions code/tools/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,10 @@ protected override string GetModel()
}
protected override ModelEntity SpawnEntity( TraceResult tr )
{
var ent = new KeyboardEntity {
return new KeyboardEntity {
Position = tr.EndPos,
Rotation = Rotation.LookAt( tr.Normal, tr.Direction ) * Rotation.From( new Angles( 90, 0, 0 ) ),
};

ent.UpdateEntity( (SandboxPlayer)Owner );

return ent;
}

protected override void UpdateEntity( Entity ent )
{
((KeyboardEntity)ent).UpdateEntity( Owner );
}
}
}

0 comments on commit abafb42

Please sign in to comment.