Skip to content

Commit

Permalink
Add wired movement events & handle user/item state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
b7c committed Feb 3, 2024
1 parent b60ae7b commit f97d3f1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Xabbo.Core/Events/WiredMovementsEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

using System;
using System.Collections.Generic;

namespace Xabbo.Core.Events;

public class WiredMovementsEventArgs : EventArgs
{
public IReadOnlyCollection<WiredMovement> Movements { get; }

public WiredMovementsEventArgs(IEnumerable<WiredMovement> movements)
{
Movements = new List<WiredMovement>(movements).AsReadOnly();
}
}
39 changes: 39 additions & 0 deletions src/Xabbo.Core/Game/Room/RoomManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ protected virtual void OnFloorItemSlide(IFloorItem item, Tile previousTile, long
FloorItemSlide?.Invoke(this, new FloorItemSlideEventArgs(item, previousTile, rollerId));
}

/// <summary>
/// Invoked when users or furni are moved by wired.
/// </summary>
public event EventHandler<WiredMovementsEventArgs>? WiredMovements;
protected virtual void OnWiredMovements(IEnumerable<WiredMovement> movements)
{
WiredMovements?.Invoke(this, new WiredMovementsEventArgs(movements));
}

/// <summary>
/// Invoked when a floor item is removed from the room.
/// </summary>
Expand Down Expand Up @@ -1604,6 +1613,36 @@ private void HandleQueueMoveUpdate(InterceptArgs e)
}
}
}

[InterceptIn("WiredMovements")]
private void HandleWiredMovements(InterceptArgs e)
{
var room = _currentRoom;

int n = e.Packet.ReadInt();
var movements = new WiredMovement[n];
for (int i = 0; i < n; i++)
{
var movement = movements[i] = WiredMovement.Parse(e.Packet);
if (room is null) continue;
switch (movement) {
case UserWiredMovement m:
if (room.Entities.TryGetValue(m.UserIndex, out Entity? entity))
entity.Location = m.Destination;
break;
case FloorItemWiredMovement m:
if (room.FloorItems.TryGetValue(m.FurniId, out FloorItem? item))
item.Location = m.Destination;
break;
case WallItemWiredMovement m:
if (room.WallItems.TryGetValue(m.ItemId, out WallItem? wallItem))
wallItem.Location = m.Destination;
break;
}
}

OnWiredMovements(movements);
}

[InterceptIn(nameof(Incoming.UpdateAvatar))]
private void HandleUpdateAvatar(InterceptArgs e)
Expand Down

0 comments on commit f97d3f1

Please sign in to comment.