using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net; 
using Microsoft.Xna.Framework.Storage; 

namespace WindowsGame1
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D mySpriteTexture,start,point;
        Texture2D D123, player1,player2,player3,player4,heart,over;
        Rectangle pRect;
        int Offset_Y = 0;  // Y 軸 偏移値(4.5.10)
        int X = 480 ,Y = 300;
        Boolean right = false;
        Rectangle []dRect=new Rectangle[5];
        int dx;
        int[] array = new int[40] { 1, 3, 6, 4, 2, 1, 3, 5, 1, 4, 6, 5, 3, 6, 1, 1, 4, 2, 3, 4, 1, 3, 6, 4, 2, 1, 3, 5, 1, 4, 6, 5, 3, 6, 1, 1, 4, 2, 3, 4 };
        int[] array1 = new int[40] { 1, 4, 10, 14, 16, 17, 20, 25, 26, 30, 36, 41, 44, 50, 51, 52, 56, 58, 61, 65, 1, 4, 10, 14, 16, 17, 20, 25, 26, 30, 36, 41, 44, 50, 51, 52, 56, 58, 61, 65 };
        int[] array2 = new int[200];
        int[] soud = new int[200];
        MyAudio audio;
        SpriteFont Font1; // 文字
        Vector2 textpos;
        int score = 0;
        int enter = 0;
        int life=3;
        KeyboardState oldState;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.Window.Title = "<電腦遊戲設計 第四組 娜美尋寶記>";//視窗名稱
            this.IsMouseVisible = true;  // 呈現滑鼠游標
            this.Window.AllowUserResizing = false; // 允許視窗縮放
            graphics.PreferredBackBufferWidth = 1000;  //視窗的預設寬
            graphics.PreferredBackBufferHeight = 700; //視窗的預設高
            for (int i = 0; i < 5; i++)
                dRect[i] = new Rectangle(i * 85, 0, 80, 85);
            audio = new MyAudio(); // 建立audio
            audio.PlayBgm();
        }
       
        protected override void Initialize()
       
         {   base.Initialize();  }
      
      
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            mySpriteTexture = Content.Load<Texture2D>("background");//背景圖
            start = Content.Load<Texture2D>("cover");//
            point = Content.Load<Texture2D>("intro");
            player1 = Content.Load<Texture2D>("nami-big");//原本的圖
            player2 = Content.Load<Texture2D>("nami-middle");//生命值-1圖
            player3 = Content.Load<Texture2D>("nami-small");//生命值-2圖
            player4 = Content.Load<Texture2D>("gameover-pic");//gameover圖
            over = Content.Load<Texture2D>("game over");
            D123 = Content.Load<Texture2D>("treasure");
            Font1 = Content.Load<SpriteFont>("SpriteFont1");
            heart=Content.Load<Texture2D>("life");//生命值圖
        }


        protected override void UnloadContent()
        { }
        
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            KeyboardState newState;  // 宣告一個KeyboardState 結構的變數
            newState = Keyboard.GetState();  //得到目前鍵盤每一個按鍵的狀況
           
            
            if (newState.IsKeyDown(Keys.Right) && X<=850)  //不可超出最右邊
                X += 2;  //判斷Right鍵是否已經被按下


            if (newState.IsKeyDown(Keys.Up)&&Y<=480)
            {
                Y += 3;
            }

            if (newState.IsKeyDown(Keys.Down) && Y >= 0)
            {
                Y-=3;
            }

            if (X > 0 && !newState.IsKeyDown(Keys.Right))
            {
                X -=5;
            }

            if (newState.IsKeyDown(Keys.Escape)) this.Exit();
            base.Update(gameTime);
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
       
        
        protected override void Draw(GameTime gameTime)
        {
            int W = graphics.GraphicsDevice.Viewport.Width;
            int H = graphics.GraphicsDevice.Viewport.Height;
            int n = 0;  
            textpos = new Vector2(800,50);
            
            string output = "Grade: "+score.ToString(); // 顯示出來的文字
            Vector2 FontOrigin = Font1.MeasureString(output) / 2; // Find the center of the string
            spriteBatch.Begin();
            KeyboardState newState;  // 宣告一個KeyboardState 結構的變數
            newState = Keyboard.GetState();//得到目前鍵盤每一個按鍵的狀況
            Vector2 s = new Vector2(0,0);
            if(enter==0)
                spriteBatch.Draw(start, s, Color.White);                   //印出開始圖片

            if (newState.IsKeyDown(Keys.Enter) && oldState.IsKeyUp(Keys.Enter) && enter==0)
            {
                enter = 1;
                oldState = newState;
            } 
            if (enter == 1)
            {
                
                spriteBatch.Draw(point, s, Color.White);           //印出POINT第二頁說明
                if (newState.IsKeyDown(Keys.Enter) && oldState.IsKeyUp(Keys.Enter))
                {
                    enter = 2;
                    oldState = newState;
                }
            }
            if (enter == 2)
            {

             
               
                Offset_Y -= 4;
                for (int i = -1; i <= W / mySpriteTexture.Height + 1; i++)        // i 是 X 方向 要貼幾次
               {
                    for (int a = -1; a <= H / mySpriteTexture.Width + 1; a++)  // j 是 Y 方向 要貼幾次
                    {
                        Vector2 position = new Vector2(
                                      i * mySpriteTexture.Width + (Offset_Y % mySpriteTexture.Width),
                                       a * mySpriteTexture.Height); // 算出要 貼上的位置
                        
                        spriteBatch.Draw(mySpriteTexture, position, Color.White);
                       
                   }
                    if (life != 0)
                    {
                        for (int k = 0; k < array.Length; k++)//產生D123
                        {
                            if (k > 0)
                            {
                                n = array1[k - 1] * 2;
                                dx = 1000 + array1[k - 1] * 2 * 80;
                            }
                            else
                                dx = 1000;
                            for (int j = 0; j < array[k]; j++)
                            {
                                Rectangle dRect1 = new Rectangle(dx + 80 * j + Offset_Y, this.graphics.PreferredBackBufferWidth - 85 - 70 - 85 * j,
                                     80, 85);//出怪1
                                if (pRect.Intersects(dRect1))//碰撞
                                {
                                    array2[j + n] = 1;
                                    if (array2[j + n] == 1 && soud[j + n] == 0)
                                    {
                                        audio.PlaySound("chimes");
                                        soud[j + n] = 1;
                                        if ((j + n) % 5 == 4)
                                            life--;
                                        else
                                            score += 10;
                                    }
                                }
                                if (array2[j + n] == 0)
                                    spriteBatch.Draw(D123, dRect1, dRect[(j + n) % 5], Color.White);
                            
                            }
                            if (k > 0)
                            {
                                n = array1[k - 1] * 2 + array[k];
                                dx = 1000 + array1[k - 1] * 80 * 2 + array[k] * 80;
                            }
                            else
                            {
                                n = array[k];
                                dx = 1000 + array[k] * 80;
                            }

                            for (int j = array[k] - 1; j >= 0; j--)
                            {
                                Rectangle dRect1 = new Rectangle(dx + 80 * j + Offset_Y, this.graphics.PreferredBackBufferHeight - 70 - 85 * (array[k] - j),
                                     80, 85);//出怪2
                                if (pRect.Intersects(dRect1))
                                {
                                    array2[j + n] = 1;
                                    if (array2[j + n] == 1 && soud[j + n] == 0)
                                    {
                                       audio.PlaySound("chimes");
                                        soud[j + n] = 1;
                                        if ((j + k) % 5 == 4)
                                            life--;
                                        else
                                            score += 10;
                                    }
                                }
                                if (array2[j + n] == 0)
                                    spriteBatch.Draw(D123, dRect1, dRect[(j+k) % 5], Color.White);
                            }

                            if (k == array.Length ) {
                                life = 1;
                            }
                           
                        }
                        if (life == 3) {
                            if (newState.IsKeyDown(Keys.Left))
                         {
                             Vector2 pos = new Vector2(X, this.graphics.PreferredBackBufferHeight - player1.Height - 30 - Y);
                                spriteBatch.Draw(player1, pos, Color.White);
                                pRect = new Rectangle((int)pos.X, (int)pos.Y, player1.Width, player1.Height);
                        }
                        else if (newState.IsKeyDown(Keys.Right))
                        {
                            Vector2 pos = new Vector2(X, this.graphics.PreferredBackBufferHeight - player1.Height - 30 - Y);
                            spriteBatch.Draw(player1, pos, Color.White);
                            pRect = new Rectangle((int)pos.X, (int)pos.Y, player1.Width, player1.Height);
                        }
                        else
                        {
                            Vector2 pos = new Vector2(X, this.graphics.PreferredBackBufferHeight - player1.Height - 30 - Y);
                            spriteBatch.Draw(player1, pos, Color.White);
                            pRect = new Rectangle((int)pos.X, (int)pos.Y, player1.Width, player1.Height);
                        }
                    
                    }

                        else  if (life == 2)//當生命只剩二時
                        {
                            if (newState.IsKeyDown(Keys.Left))
                            {
                                Vector2 pos1 = new Vector2(X, this.graphics.PreferredBackBufferHeight - player2.Height - 30 - Y);
                                spriteBatch.Draw(player2, pos1, Color.White);
                                pRect = new Rectangle((int)pos1.X, (int)pos1.Y, player2.Width, player2.Height);
                            }
                            else if (newState.IsKeyDown(Keys.Right))
                            {
                                Vector2 pos1 = new Vector2(X, this.graphics.PreferredBackBufferHeight - player2.Height - 30 - Y);
                                spriteBatch.Draw(player2, pos1, Color.White);
                                pRect = new Rectangle((int)pos1.X, (int)pos1.Y, player2.Width, player2.Height);
                            }
                            else
                            {
                                Vector2 pos1 = new Vector2(X, this.graphics.PreferredBackBufferHeight - player2.Height - 30 - Y);
                                spriteBatch.Draw(player2, pos1, Color.White);
                                pRect = new Rectangle((int)pos1.X, (int)pos1.Y, player2.Width, player2.Height);
                            }

                        }
                        else if (life <= 1)//當生命只有一時
                        {
                            if (newState.IsKeyDown(Keys.Left))
                            {
                                Vector2 pos2 = new Vector2(X, this.graphics.PreferredBackBufferHeight - player3.Height - 30 - Y);
                                spriteBatch.Draw(player3, pos2, Color.White);
                                pRect = new Rectangle((int)pos2.X, (int)pos2.Y, player3.Width, player3.Height);
                            }
                            else if (newState.IsKeyDown(Keys.Right))
                            {
                                Vector2 pos2 = new Vector2(X, this.graphics.PreferredBackBufferHeight - player3.Height - 30 - Y);
                                spriteBatch.Draw(player3, pos2, Color.White);
                                pRect = new Rectangle((int)pos2.X, (int)pos2.Y, player3.Width, player3.Height);
                            }
                            else
                            {
                                Vector2 pos2 = new Vector2(X, this.graphics.PreferredBackBufferHeight - player3.Height - 30 - Y);
                                spriteBatch.Draw(player3, pos2, Color.White);
                                pRect = new Rectangle((int)pos2.X, (int)pos2.Y, player3.Width, player3.Height);
                            }

                        }

                    
}
                }
                spriteBatch.DrawString(Font1, output,textpos,Color.Black, 0, FontOrigin, 1.0f, SpriteEffects.None, 0);
                if (newState.IsKeyDown(Keys.Enter) && oldState.IsKeyUp(Keys.Enter))
                {
                    enter = 0;
                    oldState = newState;
                    n = 0;
                    score = 0;
                    life = 3;
                    Offset_Y = 0;
                    for (int i = 0; i < 200; i++)
                    {
                        array2[i] = 0;
                        soud[i] = 0;
                    }
                }
                for (int g = 0; g < life; g++)                             //draw 顯示生命(愛心)
                {
                    Vector2 Heart = new Vector2(50 * (g+1)+g, 30);
                    spriteBatch.Draw(heart, Heart, Color.White);
                }
            }
            
            oldState = newState;
            if (life == 0)
            {   Vector2 overpos1=new Vector2(270,0);                        //GAME OVER 時會出現的畫面
                Vector2 overpos2 = new Vector2(180, 400);
                spriteBatch.Draw(player4,overpos1,Color.White);
                spriteBatch.Draw(over, overpos2, Color.White);
              
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}