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;

namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        G2D_ScrollingBG bg; // 宣告一個背景物件
        wukangcontrol gc; // 宣告一個 PC 物件

        blood myExplore;
        lacoste myFallingSprite;
        SpriteFont Font1; // 文字

        G2D_Bar bar2D; // 宣告一個長條圖物件

        flyingaxe bullet; // 宣告一個物件
        Texture2D myBullet; // 宣告一個2D紋理圖
        KeyboardState oldKey;
        List<flyingaxe> bulletList = new List<flyingaxe>(); // 紀錄子彈的動態陣列

       /* G2D_Sprite_Explore2 myExplore2;*/

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.IsMouseVisible = true;

            this.IsMouseVisible = true; // 滑鼠游標
            this.Window.AllowUserResizing = true; // 視窗寬高 可調整
            this.Window.Title = "橫向捲動的射擊遊戲"; // 視窗抬頭

            // 設定視窗寬高
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            bg = new G2D_ScrollingBG(this, Content.Load<Texture2D>("water"));
            bg.X_Speed = 6;
            bg.Y_Speed = 0;
            Components.Add(bg);

            gc = new wukangcontrol(this, this.Content.Load<Texture2D>("wukang")); // 新增這個物件
            int x = this.Window.ClientBounds.Width / 2;
            int y = this.Window.ClientBounds.Height / 2;

            gc.SetPos(new Vector2(x, y));     // 設定2D貼圖的座標 (預設值為 0, 0)
            gc.KeyBoardEnabled = true;        // 允許 鍵盤上下左右鍵 操控位置 (預設值為不允許)
            //Components.Add(gc);               // 變成 Game1 的遊戲元件

            myFallingSprite = new lacoste(this, this.Content.Load<Texture2D>("lacoste"), 4, 3);
            myFallingSprite.scale = 0.5f;
            Components.Add(myFallingSprite);

            myExplore = new blood(this, this.Content.Load<Texture2D>("blood"), 4, 3);
            Components.Add(myExplore);

            Font1 = Content.Load<SpriteFont>("Courier New");

            bar2D = new G2D_Bar(this, Font1);
            bar2D.Pos = new Vector2(10, 10);  // 擺放的位置
            bar2D.score_Max = 100;  // 設定最高的分數值
            bar2D.score = 50;  // 設定目前的分數值
            Components.Add(bar2D);  // 變成 Game1 的遊戲元件

            myBullet = this.Content.Load<Texture2D>("axe");
            
            /*myExplore2 = new G2D_Sprite_Explore2(this, this.Content.Load<Texture2D>("Earth1"), 5, 2);*/
           /* Components.Add(myExplore2);  // 變成 Game1 的遊戲元件*/
            Components.Add(gc);
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Services.AddService(typeof(SpriteBatch), spriteBatch);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            // 檢查 掉落物 和 玩家角色 的碰撞
            Vector2 posExplosion = new Vector2(); // 發生 爆炸 的位置
            if (myFallingSprite.CheckCollision(gc.pos, out posExplosion, 100))
            {
                myExplore.AddPos(posExplosion); // 增加一個 爆炸物件
                gc.hit = true; // 玩家角色 抖動
                bar2D.score--; // 目前的分數值 加一
            }
            myFallingSprite.AddMore(); // 增加 掉落的精靈圖

            // 檢查 子彈 和 掉落物 的碰撞
            for (int i = bulletList.Count - 1; i >= 0; i--)
            {
                if (bulletList[i].alive == false) // 子彈 已經 爆炸
                    bulletList.RemoveAt(i);
                else
                {
                    if (myFallingSprite.CheckCollision(bulletList[i].pos, out posExplosion, 40))
                    {
                        myExplore.AddPos(posExplosion); // 增加一個 爆炸物件
                        bar2D.score++; // 目前的分數值 加一

                        bulletList[i].alive = false; // 子彈 應該消失
                    }
                }
            }

            // 發射 子彈
            KeyboardState newKey = Keyboard.GetState();
            if (newKey.IsKeyDown(Keys.Space) && oldKey.IsKeyUp(Keys.Space))
            {
                bullet = new flyingaxe(this, myBullet, 4, 3,
                               new Vector2(gc.pos.X - 30, gc.pos.Y), new Vector2(0, gc.pos.Y));
                bullet.scale = 0.5;
                bullet.aliveTime = 1000;  // 壽命
                bulletList.Add(bullet);   // 加入 動態陣列中
                Components.Add(bullet);   // 變成 Game1 的遊戲元件
            }

            // 檢查 掉落物 和 大爆炸 的碰撞
            /*if (myExplore2.alive)
            {
                if (myFallingSprite.CheckCollision(gc.pos, out posExplosion, 500))
                {
                    myExplore.AddPos(posExplosion); // 增加一個 爆炸物件
                    gc.hit = true; // 玩家角色 抖動
                    bar2D.score++; // 目前的分數值 加一
                }
            }*/

            // 啟動 大爆炸
            /*if (newKey.IsKeyDown(Keys.Enter) && oldKey.IsKeyUp(Keys.Enter))
                myExplore2.AddPos(gc.pos);*/

            oldKey = newKey;
            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)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
          
            base.Draw(gameTime);
        }
    }
}
