major update
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* 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
|
||||
{
|
||||
public sealed class RagonEntity
|
||||
{
|
||||
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
|
||||
private RagonClient _client;
|
||||
|
||||
public ushort Id { get; private set; }
|
||||
public ushort Type { get; private set; }
|
||||
public bool Replication { get; private set; }
|
||||
|
||||
public RagonAuthority Authority { get; private set; }
|
||||
public RagonPlayer Owner { get; private set; }
|
||||
public RagonEntityState State { get; private set; }
|
||||
|
||||
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;
|
||||
|
||||
private ushort _sceneId;
|
||||
private bool _propertiesChanged;
|
||||
|
||||
private RagonPayload _spawnPayload;
|
||||
private RagonPayload _destroyPayload;
|
||||
|
||||
private Dictionary<int, OnEventDelegate> _events = new();
|
||||
private Dictionary<int, Action<RagonPlayer, IRagonEvent>> _localEvents = new();
|
||||
|
||||
public RagonEntity(ushort type = 0, ushort sceneId = 0)
|
||||
{
|
||||
State = new RagonEntityState(this);
|
||||
Type = type;
|
||||
|
||||
_sceneId = sceneId;
|
||||
}
|
||||
|
||||
internal void Attach(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer owner, RagonPayload payload)
|
||||
{
|
||||
Type = entityType;
|
||||
Id = entityId;
|
||||
Owner = owner;
|
||||
IsAttached = true;
|
||||
Replication = true;
|
||||
HasAuthority = hasAuthority;
|
||||
|
||||
_client = client;
|
||||
_spawnPayload = payload;
|
||||
|
||||
Attached?.Invoke(this);
|
||||
}
|
||||
|
||||
internal void Detach(RagonPayload payload)
|
||||
{
|
||||
_destroyPayload = payload;
|
||||
|
||||
Detached?.Invoke();
|
||||
}
|
||||
|
||||
internal T GetPayload<T>(RagonPayload data) where T : IRagonPayload, new()
|
||||
{
|
||||
var buffer = new RagonBuffer();
|
||||
data.Write(buffer);
|
||||
|
||||
var payload = new T();
|
||||
payload.Deserialize(buffer);
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
public T GetSpawnPayload<T>() where T : IRagonPayload, new()
|
||||
{
|
||||
return GetPayload<T>(_spawnPayload);
|
||||
}
|
||||
|
||||
public T GetDestroyPayload<T>() where T : IRagonPayload, new()
|
||||
{
|
||||
return GetPayload<T>(_destroyPayload);
|
||||
}
|
||||
|
||||
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
|
||||
where TEvent : IRagonEvent, new()
|
||||
{
|
||||
var evntId = _client.Event.GetEventCode(evnt);
|
||||
var buffer = _client.Buffer;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
buffer.WriteUShort(Id);
|
||||
buffer.WriteUShort(evntId);
|
||||
buffer.WriteByte((byte) replicationMode);
|
||||
buffer.WriteByte((byte) RagonTarget.Player);
|
||||
buffer.WriteUShort((ushort) target.PeerId);
|
||||
|
||||
evnt.Serialize(buffer);
|
||||
|
||||
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()
|
||||
{
|
||||
if (target != RagonTarget.ExceptOwner)
|
||||
{
|
||||
if (replicationMode == RagonReplicationMode.Local)
|
||||
{
|
||||
var eventCode = _client.Event.GetEventCode(evnt);
|
||||
_localEvents[eventCode].Invoke(_client.Room.Local, evnt);
|
||||
return;
|
||||
}
|
||||
|
||||
if (replicationMode == RagonReplicationMode.LocalAndServer)
|
||||
{
|
||||
var eventCode = _client.Event.GetEventCode(evnt);
|
||||
_localEvents[eventCode].Invoke(_client.Room.Local, evnt);
|
||||
}
|
||||
}
|
||||
|
||||
var evntId = _client.Event.GetEventCode(evnt);
|
||||
var buffer = _client.Buffer;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
buffer.WriteUShort(Id);
|
||||
buffer.WriteUShort(evntId);
|
||||
buffer.WriteByte((byte) replicationMode);
|
||||
buffer.WriteByte((byte) target);
|
||||
|
||||
evnt.Serialize(buffer);
|
||||
|
||||
var sendData = buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
|
||||
{
|
||||
var t = new TEvent();
|
||||
var eventCode = _client.Event.GetEventCode(t);
|
||||
|
||||
if (_events.ContainsKey(eventCode))
|
||||
{
|
||||
RagonLog.Warn($"Event already {eventCode} subscribed");
|
||||
return;
|
||||
}
|
||||
|
||||
_localEvents.Add(eventCode, (player, eventData) => { callback.Invoke(player, (TEvent) eventData); });
|
||||
_events.Add(eventCode, (player, serializer) =>
|
||||
{
|
||||
t.Deserialize(serializer);
|
||||
callback.Invoke(player, t);
|
||||
});
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (_events.ContainsKey(eventCode))
|
||||
_events[eventCode]?.Invoke(caller, buffer);
|
||||
}
|
||||
|
||||
internal void TrackChangedProperty(RagonProperty property)
|
||||
{
|
||||
_propertiesChanged = true;
|
||||
}
|
||||
|
||||
public void OnOwnershipChanged(RagonPlayer player)
|
||||
{
|
||||
var prevOwner = Owner;
|
||||
|
||||
Owner = player;
|
||||
HasAuthority = player.PeerId == _client.Room.Local.PeerId;
|
||||
|
||||
OwnershipChanged?.Invoke(prevOwner, player);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
public sealed class RagonEntityState
|
||||
{
|
||||
private List<RagonProperty> _properties;
|
||||
private RagonEntity _entity;
|
||||
|
||||
public RagonEntityState(RagonEntity entity)
|
||||
{
|
||||
_entity = entity;
|
||||
_properties = new List<RagonProperty>(6);
|
||||
}
|
||||
|
||||
public void AddProperty(RagonProperty property)
|
||||
{
|
||||
_properties.Add(property);
|
||||
|
||||
property.AssignEntity(_entity);
|
||||
}
|
||||
|
||||
internal void WriteInfo(RagonBuffer buffer)
|
||||
{
|
||||
buffer.WriteUShort((ushort)_properties.Count);
|
||||
foreach (var property in _properties)
|
||||
{
|
||||
buffer.WriteBool(property.IsFixed);
|
||||
buffer.WriteUShort((ushort)property.Size);
|
||||
}
|
||||
}
|
||||
|
||||
internal void ReadState(RagonBuffer buffer)
|
||||
{
|
||||
foreach (var property in _properties)
|
||||
{
|
||||
var changed = buffer.ReadBool();
|
||||
if (changed)
|
||||
property.Deserialize(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
internal void WriteState(RagonBuffer buffer)
|
||||
{
|
||||
foreach (var prop in _properties)
|
||||
{
|
||||
if (prop.IsDirty)
|
||||
{
|
||||
buffer.WriteBool(true);
|
||||
prop.Write(buffer);
|
||||
prop.Flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
prop.AddTick();
|
||||
buffer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
public struct RagonPayload
|
||||
{
|
||||
private uint[] _data = new uint[128];
|
||||
private int _size = 0;
|
||||
|
||||
public RagonPayload(int capacity)
|
||||
{
|
||||
_size = capacity;
|
||||
}
|
||||
public int Size => _size;
|
||||
|
||||
public void Read(RagonBuffer buffer)
|
||||
{
|
||||
var readOnlySpan = _data.AsSpan();
|
||||
|
||||
buffer.ReadSpan(ref readOnlySpan, _size);
|
||||
}
|
||||
|
||||
public void Write(RagonBuffer buffer)
|
||||
{
|
||||
ReadOnlySpan<uint> readOnlySpan = _data.AsSpan();
|
||||
|
||||
buffer.WriteSpan(ref readOnlySpan, _size);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Payload Size: {_size}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* 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
|
||||
{
|
||||
[Serializable]
|
||||
public class RagonProperty
|
||||
{
|
||||
public string Name => _name;
|
||||
public RagonEntity Entity => _entity;
|
||||
|
||||
public event Action Changed;
|
||||
public bool IsDirty => _dirty && _ticks >= _priority;
|
||||
public bool IsFixed => _fixed;
|
||||
public int Size => _size;
|
||||
|
||||
private bool _fixed;
|
||||
private string _name;
|
||||
protected bool _invokeLocal;
|
||||
|
||||
private RagonEntity _entity;
|
||||
private bool _dirty;
|
||||
private int _size;
|
||||
private int _ticks;
|
||||
private int _priority;
|
||||
|
||||
protected RagonProperty(int priority, bool invokeLocal)
|
||||
{
|
||||
_size = 0;
|
||||
_priority = priority;
|
||||
_fixed = false;
|
||||
_invokeLocal = invokeLocal;
|
||||
}
|
||||
|
||||
public void SetName(string name)
|
||||
{
|
||||
_name = name;
|
||||
}
|
||||
|
||||
protected void SetFixedSize(int size)
|
||||
{
|
||||
_size = size;
|
||||
_fixed = true;
|
||||
}
|
||||
|
||||
protected void InvokeChanged()
|
||||
{
|
||||
if (!_invokeLocal)
|
||||
return;
|
||||
|
||||
Changed?.Invoke();
|
||||
}
|
||||
|
||||
protected void MarkAsChanged()
|
||||
{
|
||||
InvokeChanged();
|
||||
|
||||
if (_dirty) return;
|
||||
_dirty = true;
|
||||
|
||||
_entity?.TrackChangedProperty(this);
|
||||
}
|
||||
|
||||
internal void Flush()
|
||||
{
|
||||
_dirty = false;
|
||||
_ticks = 0;
|
||||
}
|
||||
|
||||
internal void AddTick()
|
||||
{
|
||||
_ticks++;
|
||||
}
|
||||
|
||||
internal void AssignEntity(RagonEntity obj)
|
||||
{
|
||||
_entity = obj;
|
||||
|
||||
Changed?.Invoke();
|
||||
}
|
||||
|
||||
internal void Write(RagonBuffer buffer)
|
||||
{
|
||||
if (_fixed)
|
||||
{
|
||||
Serialize(buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
var sizeOffset = buffer.WriteOffset;
|
||||
buffer.Write(0, 16);
|
||||
var propOffset = buffer.WriteOffset;
|
||||
|
||||
Serialize(buffer);
|
||||
|
||||
var propSize = (uint) (buffer.WriteOffset - propOffset);
|
||||
buffer.Write(propSize, 16, sizeOffset);
|
||||
}
|
||||
|
||||
public virtual void Serialize(RagonBuffer buffer)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Deserialize(RagonBuffer buffer)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user