示例#1
0
 public void OnPlayerJoinedGame(Player player)
 {
     if (PlayerJoinedGame != null)
     {
         PlayerJoinedGame(null, player);
     }
 }
示例#2
0
 public PlayerCell(int mass, int x, int y, int cellID, Player owner)
 {
     SetMass(mass);
     SetXY(x, y);
     this.obj = MovableObject.Create(Speed, x, y);
     this._owner = owner;
     this.CID = cellID;
 }
示例#3
0
        private void HandlePacket(Packet p)
        {
            Debug.WriteLine("Received packet: " + p.ToString(), "IN");
            p.Seek(1);
            if (p.Header == "")
            {
                return;
            }
            if (p.Header == "INIT")
            {
                int uid = p.ReadInt();
                string username = p.ReadString();
                int x = p.ReadInt(), y = p.ReadInt();
                int mass = p.ReadInt();

            }
            if (p.Header == "2") // set map area
            {
                int wid = p.ReadInt(), hei = p.ReadInt();
                if (Program.GameManager.Screen == null)
                {
                    Program.GameManager.Screen = new UI.Screen(new Size(wid, hei), Program.GForm.PanelSize);
                }
                else
                {
                    Program.GameManager.Screen.SetPlatformArea(wid, hei);
                }
                Program.GameManager.OnMapAreaModified();
            }
            if (p.Header == "A") // add something
            {
                int x, y;
                int id;
                string subhead = p.ReadString();
                switch (subhead)
                {
                    case "P":
                        string username = p.ReadString();
                        id = p.ReadInt();
                        int cells = p.ReadInt();
                        byte[] color = new[] { p.ReadByte(), p.ReadByte(), p.ReadByte() };

                        Color c = Color.FromArgb(color[0], color[1], color[2]);

                        Player player = new Player(id, username, c, cells);
                        if (!Program.GameManager.Players.ContainsKey(id))
                        {
                            Program.GameManager.Players.Add(id, player);
                        }
                        else
                        {
                            Program.GameManager.Players[id] = player;
                        }
                        Program.GameManager.OnPlayerJoinedGame(player);
                        break;

                    case "p":
                        int ownerID = p.ReadInt();
                        int cellID = p.ReadInt();
                        int mass = p.ReadInt();
                        x = p.ReadInt();
                        y = p.ReadInt();
                        lock (Program.GameManager.Players)
                        {
                            if (Program.GameManager.Players.ContainsKey(ownerID))
                            {
                                lock (Program.GameManager.PlayerCells)
                                {
                                    Player owner = Program.GameManager.Players[ownerID];
                                    if (!Program.GameManager.PlayerCells.ContainsKey(cellID))
                                    {
                                        owner.AddCell(new PlayerCell(mass, x, y, cellID, owner));
                                    }
                                    else
                                    {
                                        var cell = Program.GameManager.PlayerCells[cellID];
                                        cell.SetMass(mass);
                                        cell.SetXY(x, y);
                                        if (ownerID != cell.Owner.PID) // did this cell get eaten?
                                        {
                                            cell.Owner.RemoveCell(cell);
                                            owner.AddCell(cell);
                                        }
                                    }
                                }
                            }
                        }

                        break;

                    case "f":
                        x = p.ReadInt();
                        y = p.ReadInt();
                        id = p.ReadInt();

                        FoodCell food = new FoodCell(id, x, y);
                        food.SetMass(p.ReadInt());
                        lock (Program.GameManager.OtherCells)
                        {
                            if (Program.GameManager.OtherCells.ContainsKey(id))
                            {
                                Program.GameManager.OtherCells[id] = food;
                            }
                            else Program.GameManager.OtherCells.Add(id, food);
                        }

                        break;

                    case "v":
                        throw new NotImplementedException();
                    default:
                        break;
                }
            }
            if (p.Header == "R") // remove
            {
                string subhead = p.ReadString();
                switch (subhead)
                {
                    case "F": // remove eaten food
                        int count = p.ReadInt();
                        lock (Program.GameManager.OtherCells)
                        {
                            while (--count >= 0)
                            {
                                Program.GameManager.OtherCells.Remove(p.ReadInt());
                            }
                        }

                        break;
                    default:
                        break;
                }
            }
        }