major update
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
|
||||
|
||||
namespace Ragon.Client.Simulation;
|
||||
|
||||
public class Game : IRagonListener
|
||||
{
|
||||
private RagonFloat _health;
|
||||
private RagonInt _points;
|
||||
private RagonString _name;
|
||||
private RagonEntity _entity;
|
||||
private RagonClient _client;
|
||||
|
||||
public Game(RagonClient client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public void OnConnected(RagonClient client)
|
||||
{
|
||||
RagonLog.Trace("Connected");
|
||||
_client.Session.AuthorizeWithKey("defaultkey", "Player Eduard", Array.Empty<byte>());
|
||||
}
|
||||
|
||||
public void OnAuthorizationSuccess(RagonClient client, string playerId, string playerName)
|
||||
{
|
||||
RagonLog.Trace("Authorized");
|
||||
client.Session.CreateOrJoin("Example", 1, 20);
|
||||
}
|
||||
|
||||
public void OnAuthorizationFailed(RagonClient client, string message)
|
||||
{
|
||||
Console.WriteLine($"Authorization failed: {message}");
|
||||
}
|
||||
|
||||
public void OnJoined(RagonClient client)
|
||||
{
|
||||
RagonLog.Trace("Joined");
|
||||
|
||||
_health = new RagonFloat(100.0f, false, 0);
|
||||
_health.Changed += () => Console.WriteLine($"[Ragon Property] Health: {_health.Value}");
|
||||
|
||||
_points = new RagonInt(0, -1000, 1000, false, 0);
|
||||
_points.Changed += () => Console.WriteLine($"[Ragon Property] Points: {_points.Value}");
|
||||
|
||||
_name = new RagonString("Edmand 000", false);
|
||||
_name.Changed += () => Console.WriteLine($"[Ragon Property] Name: {_name.Value}");
|
||||
|
||||
_entity = new RagonEntity(12, 0);
|
||||
_entity.State.AddProperty(_health);
|
||||
_entity.State.AddProperty(_points);
|
||||
_entity.State.AddProperty(_name);
|
||||
|
||||
client.Room.CreateEntity(_entity);
|
||||
}
|
||||
|
||||
public void OnFailed(RagonClient client, string message)
|
||||
{
|
||||
RagonLog.Trace("Failed to join");
|
||||
}
|
||||
|
||||
public void OnLeft(RagonClient client)
|
||||
{
|
||||
RagonLog.Trace("Left");
|
||||
}
|
||||
|
||||
public void OnDisconnected(RagonClient client)
|
||||
{
|
||||
RagonLog.Trace("Disconnected");
|
||||
}
|
||||
|
||||
public void OnPlayerJoined(RagonClient client, RagonPlayer player)
|
||||
{
|
||||
RagonLog.Trace("Player joined");
|
||||
}
|
||||
|
||||
public void OnPlayerLeft(RagonClient client, RagonPlayer player)
|
||||
{
|
||||
RagonLog.Trace("Player left");
|
||||
}
|
||||
|
||||
public void OnOwnershipChanged(RagonClient client, RagonPlayer player)
|
||||
{
|
||||
RagonLog.Trace("Owner ship changed");
|
||||
}
|
||||
|
||||
public void OnLevel(RagonClient client, string sceneName)
|
||||
{
|
||||
RagonLog.Trace($"New level: {sceneName}");
|
||||
|
||||
client.Room.SceneLoaded();
|
||||
}
|
||||
|
||||
private float _timer = 0;
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (_client.Status != RagonStatus.ROOM)
|
||||
return;
|
||||
|
||||
_timer += 1 / 60.0f;
|
||||
if (_timer > 1)
|
||||
{
|
||||
_health.Value += 20.0f;
|
||||
_points.Value += 10;
|
||||
_name.Value = $"Edmand 00{_client.Room.Local.PeerId}";
|
||||
Console.WriteLine($"{_health.Value} {_points.Value} {_name.Value}");
|
||||
_timer = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* 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 ENet;
|
||||
using Event = ENet.Event;
|
||||
using EventType = ENet.EventType;
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public class RagonENetConnection : INetworkConnection
|
||||
{
|
||||
public ushort Id { get; }
|
||||
|
||||
public NetworkStatistics Statistics { get; private set; }
|
||||
public INetworkChannel Reliable { get; private set; }
|
||||
public INetworkChannel Unreliable { get; private set; }
|
||||
|
||||
public Action<byte[]> OnData { get; set; }
|
||||
public Action OnConnected { get; set; }
|
||||
public Action<DisconnectReason> OnDisconnected { get; set; }
|
||||
public ulong BytesSent { get; }
|
||||
public ulong BytesReceived { get; }
|
||||
public int Ping { get; }
|
||||
|
||||
private static bool _libraryLoaded = false;
|
||||
private Host _host;
|
||||
private Peer _peer;
|
||||
private Event _netEvent;
|
||||
|
||||
public RagonENetConnection()
|
||||
{
|
||||
_host = new Host();
|
||||
_host.Create();
|
||||
}
|
||||
|
||||
|
||||
public void Prepare()
|
||||
{
|
||||
if (!_libraryLoaded)
|
||||
{
|
||||
Library.Initialize();
|
||||
_libraryLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
if (_peer.IsSet)
|
||||
_peer.DisconnectNow(0);
|
||||
}
|
||||
|
||||
public void Connect(string server, ushort port, uint protocol)
|
||||
{
|
||||
Address address = new Address();
|
||||
address.SetHost(server);
|
||||
address.Port = port;
|
||||
|
||||
_peer = _host.Connect(address, 2, protocol);
|
||||
_peer.Timeout(32, 5000, 5000);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
bool polled = false;
|
||||
while (!polled)
|
||||
{
|
||||
if (_host.CheckEvents(out _netEvent) <= 0)
|
||||
{
|
||||
if (_host.Service(0, out _netEvent) <= 0)
|
||||
break;
|
||||
|
||||
polled = true;
|
||||
}
|
||||
|
||||
switch (_netEvent.Type)
|
||||
{
|
||||
case EventType.None:
|
||||
break;
|
||||
case EventType.Connect:
|
||||
Statistics = new NetworkStatistics();
|
||||
Reliable = new ENetReliableChannel(_netEvent.Peer, 0);
|
||||
Unreliable = new ENetUnreliableChannel(_netEvent.Peer, 1);
|
||||
|
||||
OnConnected?.Invoke();
|
||||
break;
|
||||
case EventType.Disconnect:
|
||||
OnDisconnected?.Invoke(DisconnectReason.MANUAL);
|
||||
break;
|
||||
case EventType.Timeout:
|
||||
OnDisconnected?.Invoke(DisconnectReason.TIMEOUT);
|
||||
break;
|
||||
case EventType.Receive:
|
||||
var data = new byte[_netEvent.Packet.Length];
|
||||
|
||||
_netEvent.Packet.CopyTo(data);
|
||||
_netEvent.Packet.Dispose();
|
||||
|
||||
OnData?.Invoke(data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_host.IsSet)
|
||||
{
|
||||
_host?.Flush();
|
||||
_host?.Dispose();
|
||||
}
|
||||
|
||||
if (_libraryLoaded)
|
||||
Library.Deinitialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 ENet;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public sealed class ENetReliableChannel : INetworkChannel
|
||||
{
|
||||
private Peer _peer;
|
||||
private byte _channelId;
|
||||
|
||||
public ENetReliableChannel(Peer peer, int channelId)
|
||||
{
|
||||
_peer = peer;
|
||||
_channelId = (byte) channelId;
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
var newPacket = new Packet();
|
||||
newPacket.Create(data, data.Length, PacketFlags.Reliable);
|
||||
|
||||
_peer.Send(_channelId, ref newPacket);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 ENet;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public sealed class ENetUnreliableChannel : INetworkChannel
|
||||
{
|
||||
private Peer _peer;
|
||||
private byte _channelId;
|
||||
|
||||
public ENetUnreliableChannel(Peer peer, int channelId)
|
||||
{
|
||||
_peer = peer;
|
||||
_channelId = (byte) channelId;
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
var newPacket = new Packet();
|
||||
newPacket.Create(data, data.Length, PacketFlags.None);
|
||||
|
||||
_peer.Send(_channelId, ref newPacket);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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 ENet;
|
||||
using Event = ENet.Event;
|
||||
using EventType = ENet.EventType;
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public class RagonNullConnection : INetworkConnection
|
||||
{
|
||||
public ushort Id { get; }
|
||||
|
||||
public NetworkStatistics Statistics { get; private set; }
|
||||
public INetworkChannel Reliable { get; private set; }
|
||||
public INetworkChannel Unreliable { get; private set; }
|
||||
|
||||
public Action<byte[]> OnData { get; set; }
|
||||
public Action OnConnected { get; set; }
|
||||
public Action<DisconnectReason> OnDisconnected { get; set; }
|
||||
public ulong BytesSent { get; }
|
||||
public ulong BytesReceived { get; }
|
||||
public int Ping { get; }
|
||||
|
||||
private static bool _libraryLoaded = false;
|
||||
private Host _host;
|
||||
private Peer _peer;
|
||||
private Event _netEvent;
|
||||
|
||||
public RagonNullConnection()
|
||||
{
|
||||
_host = new Host();
|
||||
_host.Create();
|
||||
}
|
||||
|
||||
|
||||
public void Prepare()
|
||||
{
|
||||
if (!_libraryLoaded)
|
||||
{
|
||||
Library.Initialize();
|
||||
_libraryLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
if (_peer.IsSet)
|
||||
_peer.DisconnectNow(0);
|
||||
}
|
||||
|
||||
public void Connect(string server, ushort port, uint protocol)
|
||||
{
|
||||
Address address = new Address();
|
||||
address.SetHost(server);
|
||||
address.Port = port;
|
||||
|
||||
_peer = _host.Connect(address, 2, protocol);
|
||||
_peer.Timeout(32, 5000, 5000);
|
||||
|
||||
Statistics = new NetworkStatistics();
|
||||
Reliable = new NullReliableChannel(_netEvent.Peer, 0);
|
||||
Unreliable = new NullUnreliableChannel(_netEvent.Peer, 1);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
bool polled = false;
|
||||
while (!polled)
|
||||
{
|
||||
if (_host.CheckEvents(out _netEvent) <= 0)
|
||||
{
|
||||
if (_host.Service(0, out _netEvent) <= 0)
|
||||
break;
|
||||
|
||||
polled = true;
|
||||
}
|
||||
|
||||
switch (_netEvent.Type)
|
||||
{
|
||||
case EventType.None:
|
||||
break;
|
||||
case EventType.Connect:
|
||||
|
||||
OnConnected?.Invoke();
|
||||
break;
|
||||
case EventType.Disconnect:
|
||||
OnDisconnected?.Invoke(DisconnectReason.MANUAL);
|
||||
break;
|
||||
case EventType.Timeout:
|
||||
OnDisconnected?.Invoke(DisconnectReason.TIMEOUT);
|
||||
break;
|
||||
case EventType.Receive:
|
||||
var data = new byte[_netEvent.Packet.Length];
|
||||
|
||||
_netEvent.Packet.CopyTo(data);
|
||||
_netEvent.Packet.Dispose();
|
||||
|
||||
OnData?.Invoke(data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_host.IsSet)
|
||||
{
|
||||
_host?.Flush();
|
||||
_host?.Dispose();
|
||||
}
|
||||
|
||||
if (_libraryLoaded)
|
||||
Library.Deinitialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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 ENet;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public sealed class NullReliableChannel : INetworkChannel
|
||||
{
|
||||
private Peer _peer;
|
||||
private byte _channelId;
|
||||
|
||||
public NullReliableChannel(Peer peer, int channelId)
|
||||
{
|
||||
_peer = peer;
|
||||
_channelId = (byte) channelId;
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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 ENet;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public sealed class NullUnreliableChannel : INetworkChannel
|
||||
{
|
||||
private Peer _peer;
|
||||
private byte _channelId;
|
||||
|
||||
public NullUnreliableChannel(Peer peer, int channelId)
|
||||
{
|
||||
_peer = peer;
|
||||
_channelId = (byte) channelId;
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.Simulation.Sources;
|
||||
|
||||
public class PlayerPayload : IRagonPayload
|
||||
{
|
||||
public uint Name { get; set; }
|
||||
|
||||
public void Serialize(RagonBuffer buffer)
|
||||
{
|
||||
buffer.Write(Name, 16);
|
||||
}
|
||||
|
||||
public void Deserialize(RagonBuffer buffer)
|
||||
{
|
||||
Name = buffer.Read(16);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
namespace Ragon.Client.Simulation;
|
||||
|
||||
public class EntityListener : IRagonEntityListener
|
||||
{
|
||||
public void OnEntityCreated(RagonEntity entity)
|
||||
{
|
||||
var health = new RagonFloat(100.0f, false, 0);
|
||||
health.Value = 50;
|
||||
health.Changed += () => Console.WriteLine($"[Ragon Property] Another Health: {health.Value}");
|
||||
|
||||
var points = new RagonInt(0, -1000, 1000, false, 0);
|
||||
points.Changed += () => Console.WriteLine($"[Ragon Property] Anther Points: {points.Value}");
|
||||
|
||||
var name = new RagonString("Eduard", false);
|
||||
name.Changed += () => Console.WriteLine($"[Ragon Property] Another Name: {name.Value}");
|
||||
|
||||
entity.State.AddProperty(health);
|
||||
entity.State.AddProperty(points);
|
||||
entity.State.AddProperty(name);
|
||||
}
|
||||
}
|
||||
|
||||
public class Simulation
|
||||
{
|
||||
public void Start()
|
||||
{
|
||||
// INetworkConnection protocol = debug ? new RagonNullConnection() : new RagonENetConnection();
|
||||
// var network = new RagonClient(protocol, new EntityListener(), 30);
|
||||
// var game = new Game(network);
|
||||
// network.AddListener(game);
|
||||
// network.Connect("127.0.0.1", 5001, "1.0.0");
|
||||
// var dt = 1000 / 60.0f;
|
||||
// while (true)
|
||||
// {
|
||||
// game.Update();
|
||||
// network.Update(dt);
|
||||
// Thread.Sleep((int) dt);
|
||||
// }
|
||||
//
|
||||
// network.Disconnect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user