Пример #1
0
 public override void Use(int playerId)
 {
     if (GameLevel.CreateEntity(new Mine(0, 0, 0), playerId))
     {
         base.Use();
     }
 }
        private void _analyzeUDPCommand(byte[] data, IPEndPoint ipep)
        {
            for (int i = 0; i < _connections.Count; i++)
            {
                if (_connections[i].Address.Address == ipep.Address.Address && _connections[i].Port == ipep.Port)
                {
                    _requestTime[i] = DateTime.Now;
                }
            }

            int pos = 0;
            int id  = BinaryHelper.ReadInt32(data, ref pos);

            //check if id is id and not a server command
            if (id >= 0)
            {
                //if id is id
                //load current pos and direction of player
                float      x   = BinaryHelper.ReadFloat(data, ref pos);
                float      y   = BinaryHelper.ReadFloat(data, ref pos);
                Directions dir = (Directions)BinaryHelper.ReadInt32(data, ref pos);

                //if player shooted
                bool shooting = BinaryHelper.ReadBool(data, ref pos);
                int  team     = BinaryHelper.ReadInt32(data, ref pos);

                //selected inv item
                byte    curInvItem = BinaryHelper.ReadByte(data, ref pos);
                InvType invType    = (InvType)BinaryHelper.ReadInt32(data, ref pos);

                //check if bonus was removed
                int removedBonus = BinaryHelper.ReadInt32(data, ref pos);
                if (removedBonus != -1)
                {
                    if (OnBonusRemoved != null)
                    {
                        OnBonusRemoved(removedBonus);
                    }
                }

                //load statistics information
                int Deaths = BinaryHelper.ReadInt32(data, ref pos);
                int Kills  = BinaryHelper.ReadInt32(data, ref pos);

                //check if invItem was used
                lock (Players[id])
                {
                    Players[id].CurrentInvItemAKey = curInvItem;
                    Players[id].X         = x;
                    Players[id].Y         = y;
                    Players[id].Direction = dir;
                    Players[id].SetTeam((Teams)team);
                    Players[id].Kills  = Kills;
                    Players[id].Deaths = Deaths;

                    if (shooting)
                    {
                        Players[id].Shoot(true);
                    }
                    if (invType != InvType.Unknown && invType != InvType.Cannon)
                    {
                        GameLevel.CreateEntity(_createInvItem(invType), id);
                    }
                }

                //check if new bonus was created
                if (pos != data.Length)
                {
                    int       bId  = BinaryHelper.ReadInt32(data, ref pos);
                    int       bx   = (int)BinaryHelper.ReadFloat(data, ref pos);
                    int       by   = (int)BinaryHelper.ReadFloat(data, ref pos);
                    BonusType type = (BonusType)BinaryHelper.ReadByte(data, ref pos);
                    Bonus     b    = _createBonus(bId, type, bx, by);
                    GameLevel.AddBonus(b);
                }
            }
            else
            {
                //if id is a server command
                UDPCommands command = (UDPCommands)id;
                switch (command)
                {
                case UDPCommands.AddPlayer:
                {
                    Player t = new Player(Players.Count, 0, 0);
                    t.Id      = BinaryHelper.ReadInt32(data, ref pos);
                    t.Removed = true;
                    if (t.Id == Players.Count)
                    {
                        Players.Add(t);
                        _connections.Add(ipep);
                        _requestTime.Add(DateTime.Now);
                    }
                    else
                    {
                        Players[t.Id]      = t;
                        _connections[t.Id] = ipep;
                        _requestTime[t.Id] = DateTime.Now;
                    }
                    break;
                }

                case UDPCommands.AddPlayerComplete:
                {
                    id = BinaryHelper.ReadInt32(data, ref pos);
                    lock (Players[id])
                    {
                        Players[id].Removed = false;
                    }
                    break;
                }

                case UDPCommands.ClientDisconnected:
                {
                    id = BinaryHelper.ReadInt32(data, ref pos);
                    lock (Players[id])
                    {
                        Players[id]      = null;
                        _connections[id] = null;
                        _requestTime[id] = new DateTime();
                    }
                    //MessageBox.Show("Client " + id + " disconnected");
                    break;
                }

                case UDPCommands.ConnectToGame:
                {
                    _connectToGame(ipep);
                    break;
                }

                case UDPCommands.ServerChanged:
                {
                    long ip = BinaryHelper.ReadLong(data, ref pos);
                    if (ip == CIp.Address)
                    {
                        _isMain = true;
                    }
                    GameLevel.BonusId = BinaryHelper.ReadInt32(data, ref pos);
                    break;
                }
                }
            }
        }