/* 玩家角色的2D貼圖遊戲元件  鄞永傳老師 2009.04.15
 
  GC_2D_Basic_PC gc; // 宣告一個物件
  
  gc = new GC_2D_Basic_PC(this, this.Content.Load<Texture2D>("png-0397")); // 新增這個物件
  gc.SetPos(new Vector2(400, 100)); // 設定2D貼圖的座標 (預設值為 0, 0)
  gc.KeyBoardEnabled = true;        // 允許 鍵盤上下左右鍵 操控位置 (預設值為不允許)
  Components.Add(gc);               // 變成 Game1 的遊戲元件
  
  Services.AddService(typeof(SpriteBatch), spriteBatch); // Game1 要將 spriteBatch 放到服務區
*/
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace WindowsGame1
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class wukangcontrol : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D my;
        public int T0 = 2;        // 未受到撞擊時的 抖動程度
        public int T = 10;        // 受到撞擊時的 抖動程度
        public byte alpha = 255;  // 不透明度
        public bool KeyBoardEnabled = false; //是否允許 鍵盤上下左右鍵 操控位置

        public Vector2 pos = new Vector2(0, 0); // 位置
        Random rd = new Random();
        
        Game game;
        public bool hit = false; // 是否受到撞擊
        double ElapsedTime = 0; // 抖動的時間 累進值

        // 設定座標
        public void SetPos(Vector2 pos)
        {
            this.pos = pos;
        }

        public wukangcontrol(Game game, Texture2D my)
            : base(game)
        {
            // TODO: Construct any child components here
            this.game = game;
            this.my = my;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            //是否允許 鍵盤上下左右鍵 操控位置
            if (KeyBoardEnabled)
            { 
                // 上下左右按鍵 連發
                Rectangle boundary = new Rectangle(my.Width / 2, my.Height / 2,
                    game.Window.ClientBounds.Width - my.Width / 2,
                    game.Window.ClientBounds.Height - my.Height / 2);

                KeyboardState newKey = Keyboard.GetState();
                if (newKey.IsKeyDown(Keys.Up))
                {
                    pos.Y -= 10;
                    if (pos.Y < boundary.Y) pos.Y = boundary.Y;
                }
                if (newKey.IsKeyDown(Keys.Down))
                {
                    pos.Y += 10;
                    if (pos.Y > boundary.Height) pos.Y = boundary.Height;
                }
                if (newKey.IsKeyDown(Keys.Left))
                {
                    pos.X -= 10;
                    if (pos.X < boundary.X) pos.X = boundary.X;
                }
                if (newKey.IsKeyDown(Keys.Right))
                {
                    pos.X += 10;
                    if (pos.X > boundary.Width) pos.X = boundary.Width;
                }
            }

            if (hit)
            {
                ElapsedTime += gameTime.ElapsedGameTime.TotalMilliseconds; // 兩個 Frame 間隔的千分秒
                if (ElapsedTime > 500)
                {
                    ElapsedTime = 0;
                    hit = false; // 抖動結束
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch =
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            if (spriteBatch == null) return;

            spriteBatch.Begin();

            Rectangle rect;
            rect.Width = my.Width;
            rect.Height = my.Height;

            if (hit) // 因撞擊 而抖動
            {
                rect.X = (int)pos.X + (rd.Next(T) - T / 2) - rect.Width / 2;
                rect.Y = (int)pos.Y + (rd.Next(T) - T / 2) - rect.Height / 2;
            }
            else // 不抖動
            {
                rect.X = (int)pos.X + (rd.Next(T0) - T0 / 2) - rect.Width / 2;
                rect.Y = (int)pos.Y + (rd.Next(T0) - T0 / 2) - rect.Height / 2;
            }

            // 不透明度
            spriteBatch.Draw(my, rect, new Color(255, 255, 255, alpha));

            spriteBatch.End();

            base.Draw(gameTime);
        }

    }
}