コード例 #1
0
ファイル: GameForm.cs プロジェクト: Lewis945/DS-HW3
        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);
                    }
                });
        }
コード例 #2
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);
                }
            });
        }