示例#1
0
        static void Main(string[] args)
        {
            var address = IPAddress.Parse(args[0]);
            var port    = int.Parse(args[1]);

            var tickRate = TimeSpan.FromMilliseconds(100); // 100 ticks per second

            var world = new World();

            _ = new World.Entity.TimeComponent(new World.Entity(world))
            {
                deltaTime = tickRate.TotalSeconds
            };
            _ = new World.Entity.ConsoleBufferComponent(new World.Entity(world))
            {
                consoleBuffer = new byte[120, 32]
            };

            var deltasByTick = new SortedList <int, (float, float)> {
                { 0, (0f, 0f) }
            };
            var client = new Client <(float, float), (World, int)>(
                initialRemoteState: (world, 0),
                localStatesByTick: deltasByTick,
                localStateDiffer: new Vector2Differ(),
                remoteStateDiffer: new World.Differ(),
                localEndPoint: new IPEndPoint(IPAddress.Any, 0),
                remoteEndPoint: new IPEndPoint(address, port),
                sendInterval: TimeSpan.FromSeconds(0.1));

            var keyPressStopwatches = new ConcurrentDictionary <ConsoleKey, Stopwatch>();
            var keyPressThread      = new Thread(() => {
                while (true)
                {
                    var key = Console.ReadKey(true).Key;
                    keyPressStopwatches.AddOrUpdate(
                        key,
                        key => {
                        var stopwatch = new Stopwatch();
                        stopwatch.Start();
                        return(stopwatch);
                    },
                        (key, stopwatch) => {
                        stopwatch.Restart();
                        return(stopwatch);
                    });
                }
            });

            keyPressThread.Start();

            FixedTimer(tick => {
                float deltaX = (
                    keyPressStopwatches.TryGetValue(ConsoleKey.LeftArrow, out Stopwatch leftArrowStopwatch) &&
                    leftArrowStopwatch.ElapsedMilliseconds < 100 ?
                    -1f : 0f) + (
                    keyPressStopwatches.TryGetValue(ConsoleKey.RightArrow, out Stopwatch rightArrowStopwatch) &&
                    rightArrowStopwatch.ElapsedMilliseconds < 100 ?
                    1f : 0f);
                float deltaY = (
                    keyPressStopwatches.TryGetValue(ConsoleKey.DownArrow, out Stopwatch downArrowStopwatch) &&
                    downArrowStopwatch.ElapsedMilliseconds < 100 ?
                    1f : 0f) + (
                    keyPressStopwatches.TryGetValue(ConsoleKey.UpArrow, out Stopwatch upArrowStopwatch) &&
                    upArrowStopwatch.ElapsedMilliseconds < 100 ?
                    -1f : 0f);

                deltasByTick.Add(tick, (deltaX, deltaY));
                client.LocalTick = tick;

                var world = (IClientWorld)client.RemoteStatesByTick[client.AckingRemoteTick].Item1;
                lock (world) {
                    world.Tick(client.AckingRemoteTick + 1);
                }
            }, tickRate, new CancellationToken());
        }
示例#2
0
        static void Main(string[] args)
        {
            int portStart = int.Parse(args[0]);
            int capacity  = int.Parse(args[1]);

            var tickRate = TimeSpan.FromMilliseconds(10); // 100 ticks per second

            var world         = new World();
            var timeComponent = new World.Entity.TimeComponent(new World.Entity(world))
            {
                deltaTime = tickRate.TotalSeconds
            };

            _ = new World.Entity.ConsoleBufferComponent(new World.Entity(world))
            {
                consoleBuffer = new byte[120, 32]
            };

            var server = new Server <(World, int), (float, float)>(
                initialClientState: (0f, 0f),
                serverStatesByTick: world,
                serverStateDiffer: new World.Differ(),
                clientStateDiffer: new Vector2Differ(),
                portStart: portStart,
                capacity: capacity,
                sendInterval: TimeSpan.FromSeconds(0.1));

            var velocityComponents = new IVelocityComponent[capacity];
            var positionComponents = new IPositionComponent[capacity];
            var viewComponents     = new IViewComponent[capacity];

            FixedTimer(tick => {
                lock (world) {
                    for (int i = 0; i < capacity; ++i)
                    {
                        int port = portStart + i;

                        if (server.GetClientConnected(port))
                        {
                            if (velocityComponents[i] == null)
                            {
                                var entity            = new World.Entity(world);
                                velocityComponents[i] = new World.Entity.VelocityComponent(entity)
                                {
                                    speed = 10f
                                };
                                positionComponents[i] = new World.Entity.PositionComponent(entity)
                                {
                                    x = Console.WindowWidth / 2,
                                    y = Console.WindowHeight / 2
                                };
                                viewComponents[i] = new World.Entity.ViewComponent(entity)
                                {
                                    avatar = new[] { '@', '#', '$', '%' }[i]
                                };
                            }

                            var input = server.GetClientState(port);
                            velocityComponents[i].deltaX = input.Item1;
                            velocityComponents[i].deltaY = input.Item2;

                            Console.SetCursorPosition(0, i * 2);
                            Console.Write($@"velocity: ({velocityComponents[i].deltaX}, {velocityComponents[i].deltaY})
position: ({positionComponents[i].x}, {positionComponents[i].y})");
                        }
                        else
                        {
                            if (velocityComponents[i] != null)
                            {
                                velocityComponents[i].Dispose();
                                velocityComponents[i] = null;
                            }

                            if (positionComponents[i] != null)
                            {
                                positionComponents[i].Dispose();
                                positionComponents[i] = null;
                            }

                            if (viewComponents[i] != null)
                            {
                                viewComponents[i].Dispose();
                                viewComponents[i] = null;
                            }
                        }
                    }

                    ((IServerWorld)world).Tick(tick);
                    server.serverTick = tick;
                }
            }, tickRate, new CancellationToken());
        }