Пример #1
0
        private void OnCreateRemotableActor(ref CreateRemotableActor p)
        {
            int index = p.Index;

            Player         player;
            RemotableActor remotableActor = new RemotableActor {
                Index       = index,
                EventType   = p.EventType,
                EventParams = p.EventParams,
                Pos         = p.Pos
            };

            lock (sync) {
                if (!players.TryGetValue(p.SenderConnection, out player))
                {
                    return;
                }

                if (player.Index != (index & 0xff))
                {
                    return;
                }

                remotableActor.Owner = player;

                remotableActors[index] = remotableActor;

                remotableActor.AABB = new AABB(remotableActor.Pos.Xy, 30, 30);
                collisions.AddProxy(remotableActor);
            }

            Send(new CreateRemoteObject {
                Index       = remotableActor.Index,
                EventType   = remotableActor.EventType,
                EventParams = remotableActor.EventParams,
                Pos         = remotableActor.Pos,
            }, 13, playerConnections, NetDeliveryMethod.ReliableUnordered, PacketChannels.Main);
        }
Пример #2
0
        private void OnGameLoop()
        {
            const int TargetFps = 30;

            Stopwatch sw = new Stopwatch();

            while (threadGame != null)
            {
                sw.Restart();

                // Update time
                Time.FrameTick(false, false);

                lock (sync) {
                    // Update objects
                    foreach (KeyValuePair <NetConnection, Player> pair in players)
                    {
                        if (pair.Value.State == PlayerState.Dead)
                        {
                            if (playerSpawningEnabled)
                            {
                                RespawnPlayer(pair.Value);
                            }
                            continue;
                        }
                        else if (pair.Value.State != PlayerState.Spawned)
                        {
                            continue;
                        }
                    }

                    // Update all players
                    if (playerConnections.Count > 0)
                    {
                        int playerCount = players.Count;
                        int actorCount  = remotableActors.Count;

                        NetOutgoingMessage m = server.CreateMessage(14 + 21 * playerCount + 24 * actorCount);
                        m.Write(PacketTypes.UpdateAll);
                        m.Write((long)(NetTime.Now * 1000));

                        m.Write((byte)playerCount);
                        foreach (KeyValuePair <NetConnection, Player> pair in players)
                        {
                            Player player = pair.Value;
                            m.Write((byte)player.Index); // Player Index

                            if (player.State != PlayerState.Spawned)
                            {
                                m.Write((byte)0); // Flags - None
                                continue;
                            }

                            m.Write((byte)1); // Flags - Spawned

                            m.Write((ushort)player.Pos.X);
                            m.Write((ushort)player.Pos.Y);
                            m.Write((ushort)player.Pos.Z);

                            m.Write((uint)player.AnimState);
                            m.Write((float)player.AnimTime);
                            m.Write((bool)player.IsFacingLeft);

                            //AABB aabb = new AABB(player.Pos.Xy, 20, 30);
                            //collisions.MoveProxy(player, ref aabb, player.Speed);
                            //player.AABB = aabb;
                        }

                        m.Write((int)actorCount);
                        foreach (KeyValuePair <int, RemotableActor> pair in remotableActors)
                        {
                            RemotableActor actor = pair.Value;
                            m.Write((int)actor.Index); // Object Index

                            m.Write((byte)0);          // Flags - None

                            m.Write((ushort)actor.Pos.X);
                            m.Write((ushort)actor.Pos.Y);
                            m.Write((ushort)actor.Pos.Z);

                            m.Write((short)(actor.Speed.X * 500f));
                            m.Write((short)(actor.Speed.Y * 500f));

                            m.Write((uint)actor.AnimState);
                            m.Write((float)actor.AnimTime);
                            m.Write((bool)actor.IsFacingLeft);

                            //AABB aabb = new AABB(actor.Pos.Xy, 8, 8);
                            //collisions.MoveProxy(actor, ref aabb, actor.Speed);
                            //actor.AABB = aabb;
                        }

                        server.Send(m, playerConnections, NetDeliveryMethod.Unreliable, PacketChannels.Main);

                        ResolveCollisions();
                    }
                }

                sw.Stop();

                lastGameLoadMs = (int)sw.ElapsedMilliseconds;
                int sleepMs = (1000 / TargetFps) - lastGameLoadMs;
                if (sleepMs > 0)
                {
                    Thread.Sleep(sleepMs);
                }
            }
        }