Esempio n. 1
0
        public GameForm(GameSession session, GameSettings settings)
            : base()
        {
            _graphics = new GraphicsDeviceManager(this);

            _engine = new Engine(session.FieldWidth, session.FieldWidth);
            _engine.Players = _players = new List<Player>();

            var cords = _engine.GetRandomPosition();
            while (_engine.HasPlayer((int)cords.X, (int)cords.Y))
            {
                cords = _engine.GetRandomPosition();
            }

            _player = new Player(settings.PlayerName, settings.Team, cords);
            _player.HandleKeyboard = true;
            _player.OnKeyPressed += (s, e) => { _engine.MovePlayer(_player); };
            _player.ViewPortWidth = _engine.Width;
            _player.ViewPortHeight = _engine.Height;
            _players.Add(_player);
            _engine.AddPlayer(_player);

            _engine.CurrentPlayer = _player;

            _cellSize = 50;
            _graphics.PreferredBackBufferWidth = _engine.Width;
            _graphics.PreferredBackBufferHeight = _engine.Height;

            Content.RootDirectory = "Content";

            _session = session;

            _packet = new GameMulticastPacket()
            {
                PlayerName = settings.PlayerName,
                Team = settings.Team
            };

            Multicaster.MulticastGameSession<GameMulticastPacket>(_session.IP, () =>
            {
                if (_player.IsDead)
                {
                    Multicaster.Send = false;
                    System.Diagnostics.Debug.WriteLine("Send dead");
                }

                _packet.Move = _player.Move;
                _packet.IsDead = _player.IsDead;
                _packet.Score = _player.Score;
                _packet.PosX = _player.Postion.X;
                _packet.PosY = _player.Postion.Y;

                //System.Diagnostics.Debug.WriteLine("Send " + _player.Name + "-" + _player.Move);

                return _packet;
            });

            Multicaster.ListenToGameSession<GameMulticastPacket>(_session.IP,
                (p, sender) =>
                {
                    if (_textureHunterPlayerSheet != null && _textureHuntedPlayerSheet != null && !_player.Name.Equals(p.PlayerName))
                    {
                        var player = _players.FirstOrDefault(op => op.Name.Equals(p.PlayerName));
                        if (player == null)
                        {
                            player = new Player(p.PlayerName, p.Team, new Vector2(p.PosX, p.PosY));
                            player.Texture = p.Team == Team.Hunter ? _textureHunterPlayerSheet : _textureHuntedPlayerSheet;
                            player.ViewPortWidth = _engine.Width;
                            player.ViewPortHeight = _engine.Height;
                            _players.Add(player);
                            _engine.AddPlayer(player);
                        }

                        lock (_synchPlayer)
                        {
                            player.Move = p.Move;
                            player.IsDead = p.IsDead;
                            player.Team = p.Team;

                            //if (player.Postion.X != p.PosX && player.Postion.Y != p.PosY)
                            //{
                            //    player.Postion = new Vector2(p.PosX, p.PosY);
                            //}
                            player.Postion = new Vector2(p.PosX, p.PosY);

                            if (p.IsDead)
                            {
                                System.Diagnostics.Debug.WriteLine("Receive dead");
                                _engine.RemovePlayer(player);
                            }
                            else
                            {
                                _engine.MovePlayer(player);
                            }
                        }

                        //System.Diagnostics.Debug.WriteLine("Received " + p.PlayerName + "-" + p.Move);
                    }
                });
        }
Esempio n. 2
0
        private GameSession AddLobbyGameSession(LobbyBroadcastPacket packet, IPEndPoint ip = null)
        {
            var gameSession = _gameSessions.FirstOrDefault(p => p.Id == packet.Id);

            if (gameSession == null)
            {
                gameSession = new GameSession()
                {
                    Id = packet.Id,
                    SessionName = packet.SessionName,
                    SessionCreator = packet.SessionCreator,
                    JoinedPlayersNumber = packet.JoinedPlayersNumber,
                    FieldHeight = packet.FieldHeight,
                    FieldWidth = packet.FieldWidth,
                    IP = packet.Ip,
                    BroadcastReceivedTimeStamp = DateTime.Now
                };
                lock (_synch) { _gameSessions.Add(gameSession); }
                listBoxLobby.Items.Add(packet.ToString());
            }
            else
            {
                gameSession.JoinedPlayersNumber = packet.JoinedPlayersNumber;
                gameSession.BroadcastReceivedTimeStamp = DateTime.Now;

                for (int i = 0; i < listBoxLobby.Items.Count; i++)
                {
                    var item = listBoxLobby.Items[i];
                    var lobbyItem = ((string)item);
                    if (lobbyItem.Contains(gameSession.GetKey()))
                    {
                        var index = listBoxLobby.Items.IndexOf(item);
                        if (index >= 0)
                        {
                            listBoxLobby.Items.RemoveAt(index);
                            listBoxLobby.Items.Insert(index, packet.ToString());
                        }
                    }
                }
            }

            return gameSession;
        }