/*
    // Use this for initialization
    void Start () {

    }
    */
    /*
    // Update is called once per frame
    void Update () {

    }
    */
    public void Smash(Vector2 dir)
    {
        dir.Normalize();
        dir *= 100f;

        for(int i=0; i<5; ++i)
        {
            GameObject temp = OT.CreateObject("particle");
            OTSprite particle = temp.GetComponent<OTSprite>();

            particle.transform.parent = ParticlesContainer.transform;

            particle.position = GetComponent<OTSprite>().position;
            Vector2 dirTemp = CreatureManager.RotateDeg(dir, Random.Range(-30f, 30f));
            particle.rigidbody.AddForce(dirTemp.x, dirTemp.y, 0, ForceMode.Impulse);

            OTTween tween = new OTTween(particle, 2f, OTEasing.Linear);
            tween.TweenAdd("rotation", Random.Range(100f, 1000f));

            OT.Destroy(particle, 2f);
        }

        OTSprite destructible = GetComponent<OTSprite>();
        OTTween tween2 = new OTTween(destructible, 0.5f, OTEasing.Linear);
        tween2.TweenAdd("alpha", -1f);

        destructible.collidable = false;

        OT.Destroy(destructible, 0.5f);
    }
    public void AddEffects(List<Effect> effects)
    {
        foreach(Effect ef in effects)
        {
            // prevent effects stack?
            if(! _effects.Contains(ef))
            {
                _effects.Add(ef);

                // visual effects?
                switch(ef.EfType)
                {
                case EffectType.Stun:
                    GameObject newVisualEffect = OT.CreateObject("visualEffect");
                    newVisualEffect.GetComponent<OTSprite>().frameName = "stars";
                    newVisualEffect.transform.parent = GetComponent<OTSprite>().transform;
                    newVisualEffect.GetComponent<OTSprite>().depth = -3;
                    newVisualEffect.GetComponent<OTSprite>().position = new Vector2(0f,0f);
                    OTTween tween = new OTTween(newVisualEffect.GetComponent<OTSprite>(),
                        ef.EfDuration, OTEasing.Linear);
                    tween.TweenAdd("rotation", 360f);
                    OT.Destroy(newVisualEffect, ef.EfDuration);
                    break;

                }
            }
        }
    }
 public void Parry()
 {
     if(!_tween.isRunning)
     {
         _state = HandheldItemState.Parry;
         _tween = new OTTween(GetComponent<OTSprite>(), CONSTANTS.BaseAttackAnimDuration, OTEasing.Linear);
         _tween.TweenAdd("rotation", -90f);
         _tween.onTweenFinish = OnTweenFinish;
     }
 }
 public void Block()
 {
     if(!_tween.isRunning)
     {
         _state = HandheldItemState.Block;
         _tween = new OTTween(GetComponent<OTSprite>(), CONSTANTS.BaseAttackAnimDuration);
         _tween.TweenAdd("position", new Vector2(0, 0.5f),
             OTEasing.ExpoOut);
         _tween.onTweenFinish = OnTweenFinish;
     }
 }
 public void Recoil(float duration)
 {
     if(!_tween.isRunning)
     {
         _timer = duration;
         _state = HandheldItemState.Recoil;
         _tween = new OTTween(GetComponent<OTSprite>(), duration/2f);
         _tween.TweenAdd("position", new Vector2(0,-0.2f),
             OTEasing.ExpoOut);
         _tween.onTweenFinish = OnTweenFinish;
     }
 }
 public void Stab(float duration)
 {
     if(!_tween.isRunning)
     {
         _timer = duration;
         _state = HandheldItemState.Stab;
         _tween = new OTTween(GetComponent<OTSprite>(), duration/2f);
         _tween.TweenAdd("position", new Vector2(0,0.3f),
             OTEasing.BounceIn);
         _tween.onTweenFinish = OnTweenFinish;
     }
 }
 public void Slash(float duration)
 {
     if(!_tween.isRunning)
     {
         _timer = duration;
         _state = HandheldItemState.Slash;
         _tween = new OTTween(GetComponent<OTSprite>(), duration/2f);
         _tween.TweenAdd("position", new Vector2(-0.6f,0.3f),
             OTEasing.ElasticIn);
         GetComponent<OTSprite>().rotation =
             GetComponent<OTSprite>().transform.parent.GetComponent<OTObject>().rotation-90f;
         _tween.TweenAdd("rotation", 120f,
             OTEasing.Linear);
         _tween.onTweenFinish = OnTweenFinish;
     }
 }
    private void OnTweenFinish(OTTween tween)
    {
        switch(_state)
        {
        case HandheldItemState.Twirl:
            GetComponent<OTSprite>().rotation =
                GetComponent<OTSprite>().transform.parent.GetComponent<OTObject>().rotation;
            break;
        case HandheldItemState.Stab:
            _tween = new OTTween(GetComponent<OTSprite>(), _timer/2f);
            _tween.TweenAdd("position", new Vector2(0,-0.3f),
                OTEasing.ExpoOut);
            break;
        case HandheldItemState.Slash:
            GetComponent<OTSprite>().rotation =
                GetComponent<OTSprite>().transform.parent.GetComponent<OTObject>().rotation;
            _tween = new OTTween(GetComponent<OTSprite>(), _timer/2f);
            _tween.TweenAdd("position", new Vector2(0.6f,-0.3f),
                OTEasing.ExpoOut);
            break;
        case HandheldItemState.Recoil:
            _tween = new OTTween(GetComponent<OTSprite>(), _timer/2f);
            _tween.TweenAdd("position", new Vector2(0,0.2f),
                OTEasing.ExpoIn);
            break;
        case HandheldItemState.Block:
            _tween = new OTTween(GetComponent<OTSprite>(), CONSTANTS.BaseAttackAnimDuration);
            _tween.TweenAdd("position", new Vector2(0, -0.5f),
                OTEasing.ExpoIn);
            break;
        case HandheldItemState.Parry:
            GetComponent<OTSprite>().rotation =
                GetComponent<OTSprite>().transform.parent.GetComponent<OTObject>().rotation;
            break;

        case HandheldItemState.StrikeTarget:
            GetComponent<OTSprite>().position = new Vector2(0.3f, 0.3f);
            // do a reset at hero
            break;
        }
        _state = HandheldItemState.Idle;
    }
 public void Twirl(float duration)
 {
     if(!_tween.isRunning)
     {
         _timer = duration;
         _state = HandheldItemState.Twirl;
         _tween = new OTTween(GetComponent<OTSprite>(), duration, OTEasing.Linear);
         _tween.TweenAdd("rotation", 360f);
         _tween.onTweenFinish = OnTweenFinish;
     }
 }
 public void StrikeTarget(OTObject target, float duration)
 {
     if(!_tween.isRunning)
     {
         _state = HandheldItemState.StrikeTarget;
         _tween = new OTTween(GetComponent<OTSprite>(), duration);
         _tween.TweenAdd("position", target.position - GetComponent<OTSprite>().position,
             OTEasing.BounceIn);
         _tween.onTweenFinish = OnTweenFinish;
     }
 }
    private void LevelUP()
    {
        // effects
        GameObject temp = OT.CreateObject("visualEffect");
        _levelUpEffect = temp.GetComponent<OTSprite>();
        _levelUpEffect.frameName = "circle";
        _levelUpEffect.size *= 100;
        _levelUpEffect.transform.parent = self.transform;
        _levelUpEffect.depth = -10;
        _levelUpEffect.tintColor = Color.yellow;
        _levelUpEffect.alpha = 0f;
        OTTween tween = new OTTween(_levelUpEffect, 2f, OTEasing.Linear);
        tween.TweenAdd("alpha", 0.5f);
        tween.onTweenFinish = LevelUpTweenOnFinish;

        // stats
        _xp -= CONSTANTS.BaseXPPerLevel*_level;
        ++_level;

        switch(_heroClass)
        {
        case 0:
            ++_strength;
            break;
        case 1:
            ++_agility;
            break;
        case 2:
            ++_magic;
            break;
        }
        RecalcStats();
        _currentHP = _maxHP;
    }
    private void SkillTeleportHandler()
    {
        // Shoot ray in dir of mouse
        RaycastHit losRay;

        Vector3 startPoint = new Vector3(self.transform.position.x,
            self.transform.position.y, 40);

        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 1;
        Vector3 targetxy = Camera.main.ScreenToWorldPoint(mousePos);

        Vector3 endPoint = new Vector3(targetxy.x, targetxy.y, 40);
        Vector3 dir = endPoint - startPoint;
        float distance = Vector3.Distance(startPoint, endPoint);

        float range = 0f;

        switch(_skillNames[_quickSkills[3]])
        {
        case "Teleport":
            range = CONSTANTS.SkillTeleportRange;
            break;
        case "Leap":
            range = CONSTANTS.SkillLeapRange;
            break;
        case "Slink":
            range = CONSTANTS.SkillSlinkRange;
            break;
        }

        distance = Mathf.Min(distance, range);
        dir.Normalize();
        dir *= distance;

        Physics.Raycast(startPoint, dir, out losRay, distance);

        Vector3 targetSpot;

        // no obstructions, can teleport to mouse
        if(losRay.rigidbody == null)
        {
            targetSpot = self.transform.position + new Vector3(dir.x,
                dir.y, 0);
        }
        // obstruction, teleport in front of it
        else
        {
            Vector3 dir2 = dir;
            dir2.Normalize();

            Vector2 selfxy = new Vector2(self.collider.bounds.extents.x,
                self.collider.bounds.extents.y);

            Vector3 target = losRay.point - dir2*selfxy.magnitude;

            targetSpot =
                new Vector3(target.x, target.y, self.transform.position.z);
        }

        // effects
        // Leap
        if(_skillNames[_quickSkills[3]] == "Leap")
        {
            OTTween leap = new OTTween(self.transform, 0.5f, OTEasing.Linear);
            leap.TweenAdd("position", targetSpot-self.transform.position, OTEasing.ExpoInOut);
            leap.onTweenFinish = TweenLeapOnFinish;

            _usingSkill = true;
        }
        // Slink
        else if(_skillNames[_quickSkills[3]] == "Slink")
        {
            OTTween slink = new OTTween(self.transform, 0.5f, OTEasing.Linear);
            slink.TweenAdd("position", targetSpot-self.transform.position, OTEasing.Linear);
            slink.onTweenFinish = TweenSlinkOnFinish;

            self.alpha = 0.5f;

            _usingSkill = true;
        }
        // Teleport
        else if(_skillNames[_quickSkills[3]] == "Teleport")
        {
            self.transform.position = targetSpot;

            Effects.frameName = "circle";
            Effects.visible = true;
            OTTween teleport = new OTTween(Effects, 0.5f, OTEasing.Linear);
            teleport.TweenAdd("alpha", -1f, OTEasing.Linear);
            teleport.onTweenFinish = TweenTeleport1OnFinish;

            _usingSkill = true;
        }

        _quickSkillsTimers[3] = _quickSkillsCooldowns[3];
    }
    private void SkillAOEHandler()
    {
        Vector2 facedir = OT.view.mouseWorldPosition - self.position;
        facedir.Normalize();

        // Flurry
        if(_skillNames[_quickSkills[4]] == "Flurry")
        {
            // weapon sync check
            if(! ItemSkillSyncCheck(true))
            {
                return;
            }

            OTTween flurry = new OTTween(self, 0.5f, OTEasing.Linear);
            flurry.TweenAdd("rotation", 360f, OTEasing.Linear);
            flurry.onTweenFinish = TweenFlurryOnFinish;

            _usingSkill = true;
            Handheld1.position += new Vector2(0, 0.2f);

            GameObject AOE = OT.CreateObject("AOE");
            AOE.GetComponent<OTAnimatingSprite>().position = self.position;
            AOE.GetComponent<OTAnimatingSprite>().visible = false;
            AOE.GetComponent<AOEManager>().
                SetParemeters(self, _attack*CONSTANTS.SkillFlurryDamageMP);
        }
        // Multishot
        else if(_skillNames[_quickSkills[4]] == "Multishot")
        {
            // weapon sync check
            if(! ItemSkillSyncCheck(false))
            {
                return;
            }

            facedir = CreatureManager.RotateDeg(facedir, -CONSTANTS.SkillMultishotHalfarcDeg);

            float angle = 2*CONSTANTS.SkillMultishotHalfarcDeg/(CONSTANTS.SkillMultishotArrows-1);

            for(int i=0; i<CONSTANTS.SkillMultishotArrows; ++i)
            {
                GameObject newarrow = OT.CreateObject("arrow");
                newarrow.GetComponent<arrowManager>().SetParameters(self,
                    CONSTANTS.BaseRangedProjectileSpeed*facedir,
                    CONSTANTS.SkillMultishotRange, _attack*CONSTANTS.SkillMultishotDamageMP);
                newarrow.GetComponent<OTSprite>().rotation =
                    CreatureManager.FindAngleFacing(facedir);

                facedir = CreatureManager.RotateDeg(facedir, angle);
            }
        }
        // Fireball
        else if(_skillNames[_quickSkills[4]] == "Fireball")
        {

            GameObject newarrow = OT.CreateObject("arrow");
            newarrow.GetComponent<OTSprite>().frameName = "fireballProj";
            newarrow.GetComponent<OTSprite>().rotation
                = CreatureManager.FindAngleFacing(facedir);
            newarrow.GetComponent<arrowManager>().SetParameters(self,
                CONSTANTS.SkillFireballSpeedMP*CONSTANTS.BaseMagicProjectileSpeed*facedir,
                CONSTANTS.SkillFireballRange, _attack*CONSTANTS.SkillFireballDamageMP, true);
        }

        _quickSkillsTimers[4] = _quickSkillsCooldowns[4];
    }
    private void EmitBloodParticles(int count)
    {
        GameObject temp;
        OTSprite particle;

        Vector2 dir = GetComponent<OTSprite>().position -
            GetComponent<MonsterManager>().Hero.position ;
        dir.Normalize();
        dir *=70f;

        for(int i=0; i< count; ++i)
        {
            temp = OT.CreateObject("particle");
            particle = temp.GetComponent<OTSprite>();
            particle.frameName = "BloodParticle";

            particle.transform.parent = ParticlesContainer.transform;

            particle.collidable = false;

            particle.position = GetComponent<OTSprite>().position;
            Vector2 dirTemp = CreatureManager.RotateDeg(dir, Random.Range(-30f, 30f));
            particle.rigidbody.AddForce(dirTemp.x, dirTemp.y, 0, ForceMode.Impulse);

            OTTween tween = new OTTween(particle, 1f, OTEasing.Linear);
            tween.TweenAdd("rotation", Random.Range(100f, 1000f));

            OT.Destroy(particle, 1f);
        }
    }