Files
Ragon/Ragon.Client/Sources/Entity/RagonEntity.cs
T

285 lines
8.0 KiB
C#
Raw Normal View History

2023-03-06 10:06:43 +04:00
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Ragon.Protocol;
namespace Ragon.Client
{
2023-10-09 09:17:43 +03:00
public sealed class RagonEntity: IDisposable
2023-03-06 10:06:43 +04:00
{
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
private RagonClient _client;
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
public ushort Id { get; private set; }
public ushort Type { get; private set; }
public bool Replication { get; private set; }
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
public RagonAuthority Authority { get; private set; }
public RagonPlayer Owner { get; private set; }
public RagonEntityState State { get; private set; }
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
public bool IsAttached { get; private set; }
public bool HasAuthority { get; private set; }
public event Action<RagonEntity> Attached;
public event Action Detached;
public event Action<RagonPlayer, RagonPlayer> OwnershipChanged;
internal bool PropertiesChanged => _propertiesChanged;
internal ushort SceneId => _sceneId;
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
private ushort _sceneId;
private bool _propertiesChanged;
private RagonPayload _spawnPayload;
private RagonPayload _destroyPayload;
2023-07-30 21:46:42 +03:00
private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
2023-10-07 20:20:02 +03:00
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
2023-03-06 10:06:43 +04:00
public RagonEntity(ushort type = 0, ushort sceneId = 0)
{
State = new RagonEntityState(this);
Type = type;
2023-07-30 16:56:11 +03:00
_spawnPayload = new RagonPayload(0);
_destroyPayload = new RagonPayload(0);
2023-03-06 10:06:43 +04:00
_sceneId = sceneId;
}
2023-07-29 10:58:06 +03:00
internal void Attach(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer owner)
2023-03-06 10:06:43 +04:00
{
Type = entityType;
Id = entityId;
Owner = owner;
IsAttached = true;
Replication = true;
HasAuthority = hasAuthority;
2023-07-09 07:40:06 +03:00
_client = client;
2023-03-06 10:06:43 +04:00
Attached?.Invoke(this);
}
2023-10-12 11:18:04 +03:00
internal void SetReplication(bool enabled)
2023-10-11 19:37:50 +03:00
{
2023-10-12 11:18:04 +03:00
Replication = enabled;
2023-10-11 19:37:50 +03:00
}
2023-03-06 10:06:43 +04:00
internal void Detach(RagonPayload payload)
{
_destroyPayload = payload;
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
Detached?.Invoke();
}
internal T GetPayload<T>(RagonPayload data) where T : IRagonPayload, new()
{
2023-07-23 15:56:08 +03:00
var payload = new T();
if (data.Size <= 0) return payload;
2023-03-06 10:06:43 +04:00
var buffer = new RagonBuffer();
2023-07-23 15:56:08 +03:00
2023-03-06 10:06:43 +04:00
data.Write(buffer);
2023-07-23 15:56:08 +03:00
2023-03-06 10:06:43 +04:00
payload.Deserialize(buffer);
2023-07-23 15:56:08 +03:00
2023-03-06 10:06:43 +04:00
return payload;
}
2023-07-09 07:40:06 +03:00
2023-07-29 10:58:06 +03:00
public void AttachPayload(RagonPayload payload)
2023-07-09 07:40:06 +03:00
{
2023-07-29 10:58:06 +03:00
_spawnPayload = payload;
2023-07-09 07:40:06 +03:00
}
2023-03-06 10:06:43 +04:00
2023-05-07 18:16:46 +03:00
public T GetAttachPayload<T>() where T : IRagonPayload, new()
2023-03-06 10:06:43 +04:00
{
return GetPayload<T>(_spawnPayload);
}
2023-05-07 18:16:46 +03:00
public T GetDetachPayload<T>() where T : IRagonPayload, new()
2023-03-06 10:06:43 +04:00
{
return GetPayload<T>(_destroyPayload);
}
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
where TEvent : IRagonEvent, new()
{
2023-05-07 12:46:39 +03:00
if (!IsAttached)
{
RagonLog.Error("Entity not attached");
return;
}
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
var evntId = _client.Event.GetEventCode(evnt);
var buffer = _client.Buffer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(Id);
buffer.WriteUShort(evntId);
2023-07-09 07:40:06 +03:00
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)RagonTarget.Player);
2023-05-07 12:46:39 +03:00
buffer.WriteUShort(target.PeerId);
2023-03-06 10:06:43 +04:00
evnt.Serialize(buffer);
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
public void ReplicateEvent<TEvent>(
TEvent evnt,
RagonTarget target = RagonTarget.All,
RagonReplicationMode replicationMode = RagonReplicationMode.Server)
where TEvent : IRagonEvent, new()
{
2023-05-07 12:46:39 +03:00
if (!IsAttached)
{
RagonLog.Error("Entity not attached");
return;
}
2023-07-09 07:40:06 +03:00
2023-07-30 21:46:42 +03:00
var eventCode = _client.Event.GetEventCode(evnt);
2023-03-06 10:06:43 +04:00
if (target != RagonTarget.ExceptOwner)
{
if (replicationMode == RagonReplicationMode.Local)
{
2023-10-07 20:20:02 +03:00
var localListeners = _localListeners[eventCode];
foreach (var listener in localListeners)
listener.Invoke(_client.Room.Local, evnt);
2023-03-06 10:06:43 +04:00
return;
}
if (replicationMode == RagonReplicationMode.LocalAndServer)
{
2023-10-07 20:20:02 +03:00
var localListeners = _localListeners[eventCode];
foreach (var listener in localListeners)
listener.Invoke(_client.Room.Local, evnt);
2023-03-06 10:06:43 +04:00
}
}
var buffer = _client.Buffer;
2023-07-30 21:46:42 +03:00
2023-03-06 10:06:43 +04:00
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
buffer.WriteUShort(Id);
2023-07-30 21:46:42 +03:00
buffer.WriteUShort(eventCode);
2023-07-09 07:40:06 +03:00
buffer.WriteByte((byte)replicationMode);
buffer.WriteByte((byte)target);
2023-03-06 10:06:43 +04:00
evnt.Serialize(buffer);
var sendData = buffer.ToArray();
_client.Reliable.Send(sendData);
}
2023-10-07 20:20:02 +03:00
public Action<RagonPlayer, IRagonEvent> OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
2023-03-06 10:06:43 +04:00
{
var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t);
2023-10-11 19:37:50 +03:00
2023-10-07 20:20:02 +03:00
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
2023-10-11 19:37:50 +03:00
if (!_listeners.TryGetValue(eventCode, out var callbacks))
2023-03-06 10:06:43 +04:00
{
2023-10-07 20:20:02 +03:00
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
_listeners.Add(eventCode, callbacks);
}
2023-10-11 19:37:50 +03:00
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
2023-10-07 20:20:02 +03:00
{
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
2023-10-11 19:37:50 +03:00
_localListeners.Add(eventCode, localCallbacks);
2023-03-06 10:06:43 +04:00
}
2023-07-09 07:40:06 +03:00
2023-10-07 20:20:02 +03:00
callbacks.Add(action);
localCallbacks.Add(action);
2023-10-11 19:37:50 +03:00
if (!_events.ContainsKey(eventCode))
2023-03-06 10:06:43 +04:00
{
2023-10-11 19:37:50 +03:00
_events.Add(eventCode, (player, serializer) =>
{
t.Deserialize(serializer);
2023-10-07 20:20:02 +03:00
2023-10-11 19:37:50 +03:00
foreach (var callbackListener in callbacks)
callbackListener.Invoke(player, t);
});
}
2023-10-07 20:20:02 +03:00
return action;
}
public void OffEvent<TEvent>(Action<RagonPlayer, IRagonEvent> callback) where TEvent : IRagonEvent, new()
{
var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t);
2023-10-11 19:37:50 +03:00
if (_listeners.TryGetValue(eventCode, out var callbacks))
callbacks.Remove(callback);
if (_localListeners.TryGetValue(eventCode, out var localCallbacks))
localCallbacks.Remove(callback);
2023-03-06 10:06:43 +04:00
}
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
internal void Write(RagonBuffer buffer)
{
buffer.WriteUShort(Id);
State.WriteState(buffer);
_propertiesChanged = false;
}
internal void Read(RagonBuffer buffer)
{
State.ReadState(buffer);
}
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
{
2023-05-07 12:46:39 +03:00
if (_events.TryGetValue(eventCode, out var evnt))
evnt?.Invoke(caller, buffer);
2023-07-09 07:40:06 +03:00
else
2023-05-07 12:46:39 +03:00
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
2023-03-06 10:06:43 +04:00
}
internal void TrackChangedProperty(RagonProperty property)
{
_propertiesChanged = true;
}
public void OnOwnershipChanged(RagonPlayer player)
{
var prevOwner = Owner;
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
Owner = player;
2023-07-01 07:47:57 +03:00
HasAuthority = player.IsLocal;
2023-07-09 07:40:06 +03:00
2023-03-06 10:06:43 +04:00
OwnershipChanged?.Invoke(prevOwner, player);
}
2023-10-09 09:17:43 +03:00
public void Dispose()
{
_events.Clear();
_listeners.Clear();
_localListeners.Clear();
}
2023-03-06 10:06:43 +04:00
}
}