Skip to content

Commit

Permalink
Add SetHeadRotation and SetRotation (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides authored Nov 30, 2023
1 parent 0b0f47b commit 6e89baa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
3 changes: 3 additions & 0 deletions Obsidian.API/_Interfaces/IEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public interface IEntity

public VectorF GetLookDirection();

public void SetHeadRotation(Angle headYaw);
public void SetRotation(Angle yaw, Angle pitch, bool onGround = true);

public bool HasAttribute(string attributeResourceName);
public bool TryAddAttribute(string attributeResourceName, float value);
public bool TryUpdateAttribute(string attributeResourceName, float newValue);
Expand Down
17 changes: 2 additions & 15 deletions Obsidian/Commands/MainCommandModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,22 +349,9 @@ public async Task DerpAsync(CommandContext ctx, string entityType)
{
while (true)
{
var rotato = new SetHeadRotationPacket()
{
EntityId = frogge.EntityId,
HeadYaw = new Angle((byte)(new Random().Next(1, 255)))
};
frogge.SetHeadRotation(new Angle((byte)(Random.Shared.Next(1, 255))));
frogge.SetRotation(new Angle((byte)(Random.Shared.Next(1, 255))), new Angle((byte)(Random.Shared.Next(1, 255))), false);

var rotato2 = new UpdateEntityRotationPacket()
{
EntityId = frogge.EntityId,
Pitch = new Angle((byte)(new Random().Next(1, 255))),
Yaw = new Angle((byte)(new Random().Next(1, 255))),
OnGround = false
};

//server.BroadcastPacket(rotato);
//server.BroadcastPacket(rotato2);
await Task.Delay(15);
}
});
Expand Down
44 changes: 24 additions & 20 deletions Obsidian/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ internal virtual async Task UpdateAsync(VectorF position, Angle yaw, Angle pitch
OnGround = onGround
}, EntityId);

this.PacketBroadcaster.BroadcastToWorld(this.World, new SetHeadRotationPacket
{
EntityId = EntityId,
HeadYaw = yaw
}, EntityId);
this.SetHeadRotation(yaw);
}
else
{
Expand All @@ -140,25 +136,33 @@ internal virtual Task UpdateAsync(Angle yaw, Angle pitch, bool onGround)

if (isNewRotation)
{
this.PacketBroadcaster.BroadcastToWorld(this.World, new UpdateEntityRotationPacket
{
EntityId = EntityId,
OnGround = onGround,
Yaw = yaw,
Pitch = pitch
}, EntityId);

this.PacketBroadcaster.BroadcastToWorld(this.World, new SetHeadRotationPacket
{
EntityId = EntityId,
HeadYaw = yaw
}, EntityId);
UpdatePosition(yaw, pitch, onGround);
this.SetRotation(yaw, pitch, onGround);
this.SetHeadRotation(yaw);
}

return Task.CompletedTask;
}

public void SetHeadRotation(Angle headYaw) =>
this.PacketBroadcaster.BroadcastToWorld(this.World, new SetHeadRotationPacket
{
EntityId = EntityId,
HeadYaw = headYaw
}, EntityId);

public void SetRotation(Angle yaw, Angle pitch, bool onGround = true)
{
this.PacketBroadcaster.BroadcastToWorld(this.World, new UpdateEntityRotationPacket
{
EntityId = EntityId,
OnGround = onGround,
Yaw = yaw,
Pitch = pitch
}, EntityId);

this.UpdatePosition(yaw, pitch, onGround);
}

public async Task UpdatePositionAsync(VectorF pos, bool onGround = true)
{
var (x, z) = pos.ToChunkCoord();
Expand Down Expand Up @@ -415,7 +419,7 @@ public async virtual Task TeleportAsync(VectorF pos)
});
}

public bool TryAddAttribute(string attributeResourceName, float value) =>
public bool TryAddAttribute(string attributeResourceName, float value) =>
Attributes.TryAdd(attributeResourceName, value);

public bool TryUpdateAttribute(string attributeResourceName, float newValue)
Expand Down

0 comments on commit 6e89baa

Please sign in to comment.