using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Client;

namespace WindowsGame2
{
    class Arena
    {
        public Map map;
        public Article article; 
        public Player[] playerArray = new Player[4]; 

        public static int Width = 15;
        public static int Hight = 12;
        /////////////////////////////////
        //需要从服务器得到现在的玩家数量
        private int currentPlayerNum;  

        int theArticle = 0;
        bool mapUpdate = false;
         
        public Arena()
        {
            map = new Map();
            article = new Article();
          //----------------------------------------------------
          //  侧使用 
            for (int i = 0; i < Connect.currentPlayerNum; i++)
                playerArray[i] = new Player(this, (byte)i);
          //----------------------------------------------------
            currentPlayerNum = Connect.currentPlayerNum;
        }
         
        public void addPlayer(byte playerNum)
        {
            playerArray[playerNum] = new Player(this,playerNum);
            currentPlayerNum++;
        }

        public void setMap(int x, int y)
        {
            //我吃到了东西 x,y

            //如果是金币
            if (article.article[x, y] > 50)
                article.article[x, y] = 0;
            //如果是道具
            else if (article.article[x, y] > 0 && MyInfo.length<10)
            {
                theArticle = article.article[x, y];
                article.article[x, y] = 0;
            }
        }

        
        public void check(int x,int y,int number)
        { 
          
            //如果是金币
            if (article.article[x, y] > 50)
            {  
                if (number == Connect.getMyPlayerID())
                {
                    //MyInfo.addYellow();
                    mapUpdate = true;
                    Connect.cToSCommand[9] = (byte)(Connect.cToSCommand[9] & (255 - ParseCommand.eatingMask));
                    Connect.cToSCommand[9] += 1;
                }
                
            }
            
            //如果是道具
            else if (article.article[x, y] > 0 && MyInfo.length<10)
            {
                //theArticle = article.article[x, y];
                if (number == Connect.getMyPlayerID())
                {
                    MyInfo.addArticle(theArticle);
                    mapUpdate = true;
                    Connect.cToSCommand[9] = (byte)(Connect.cToSCommand[9] & (255 - ParseCommand.eatingMask));
                    Connect.cToSCommand[9] += 1;
                }
            }
            //更新cToSCommand[10]表示的是被吃掉道具的格子x,y
            if (mapUpdate)
            {
                
                Connect.cToSCommand[10] = (byte)(x << 4);
                Connect.cToSCommand[10] += (byte)y;

                Connect.setKeyUpdating();
                mapUpdate = false;
                
            }
        }
        
       
        public void update()
        {
            //map update
            map.update();
            //article update
            article.update();

            for (int i = 0; i < 2; i++)
                playerArray[i].update();  
             
        }

        public void draw()
        {
            Game1.foreGround.Begin();

            Vector2 mapPos = new Vector2(0, 0);
            Vector2 pos = new Vector2(0, 0);
            Rectangle mapDataRect = new Rectangle(0, 0, 40, 80);
           

            for (int i = 0; i < Arena.Hight; i++)
            {
                pos.Y = i * 40+30;

                for (int j = 0; j < Arena.Width; j++)
                {
                    pos.X = j * 40+30;

                    //draw the yellow
                    if (article.article[i, j] > 50)
                        article.draw(i, j, true); 

                    //draw the article
                    else if (article.article[i, j] > 0)
                        article.draw(i, j,false);
                    
                    //draw the map
                    if (map.map[i, j] > 0)
                    {
                        mapDataRect.X = (map.map[i, j] % 10 - 1) * 40;
                        mapDataRect.Y = ((map.map[i, j] - map.map[i, j] % 10) / 10 - 1) * 80;
                        mapPos.X = pos.X;
                        mapPos.Y = pos.Y - 40;
                        Game1.foreGround.Draw(Map.mapSprite, mapPos, mapDataRect, Color.White);
                    } 
                   
                    //draw the player
                    for (int n = 0; n < Connect.currentPlayers; n++)
                    {
                        if (playerArray[n].lineAndRow.X == i && playerArray[n].lineAndRow.Y == j)
                            playerArray[n].draw();
                        
                    }
                }

            }  

            Game1.foreGround.End();
        }

        public Article getArticleRef()
        {
            return article;
        }

        public void removeArticle(int x, int y)
        {
            article.article[x, y] = 0;
        }

         
    }
}
