using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pcd_g1
{
    class TornadoBombStageObject:BombStageObject
    {
        private static Random rand = new Random();

        private double angle=0.0f;

        private bool isLastBlow;//前回、主人公を吹き飛ばしたか

        public TornadoBombStageObject(int x, int y, Stage stg, IStageObject character,int TTL)
            : base(x, y, stg, character)
        {
            KilledByBlock = false;		//trueにするとブロックで消滅
            KilledByCharacter = false;	//trueにするとキャラを突き抜けない
            Damage = 15;					//敵に与えるダメージ
            Visibility = true;			//trueにすると非表示に
            this.TTL = TTL;					//存在時間(fps)
            sound = "";

            //サイズ
            Width = 32;
            Height = 32;

            //当たり判定
            Range = new FreeRectangleCollideRange(this);
            ((FreeRectangleCollideRange)Range).Rect = new System.Drawing.RectangleF(0f, 0f, 32f, 32f);

            //移動速度
            SpeedX = 0;
            SpeedY = 0;

            //表示する画像
            picture = ImageManager.Search("871");
        }

        public override void Update(int keyStatus, out int vx, out int vy)
        {
            base.Update(keyStatus, out vx, out vy);

            angle+=Math.PI/16;

            int speed = rand.Next(5);
            SpeedX += (int)Math.Cos(angle)*speed;
            SpeedY += (int)Math.Sin(angle)*speed;

            vx = (int)SpeedX; vy = (int)SpeedY;
        }

        public override bool EventProc(EventType type, IStageObject sender, CollideDirection direction)
        {
            bool blow = false;

            switch (type)
            {
                case EventType.Collided:
                case EventType.BeCollided:
                    if (KilledByBlock)
                    {
                        Die();
                    }
                    break;
                case EventType.Touched:
                case EventType.BeTouched:
                    if (!(sender == ownerCharacter) && sender is CharacterStageObject)
                    {
                        if ((ownerCharacter is MainCharacterStageObject && !(sender is MainCharacterStageObject)) ||
                            (!(ownerCharacter is MainCharacterStageObject) && sender is MainCharacterStageObject))
                        {
                            if(isLastBlow==false)
                                ((CharacterStageObject)sender).SignalProc(SignalType.SIGPUSH, rand.Next(-10,10), rand.Next(-10,10));
                            blow = true;
                        }
                    }
                    if (KilledByCharacter && sender != ownerCharacter)
                    {
                        Die();
                    }
                    break;
            }

            isLastBlow = blow;
            
            return true;
        }
    }
}
