Ejemplo n.º 1
0
        public void CastSpell(Spell spell, Vector3 pos = default(Vector3), Unit2D target = null)
        {
            if (spell.effectRange > 0)
            {
                print("AOE effect!!!");
                Collider2D[] cols = Physics2D.OverlapCircleAll(pos, spell.effectRange, spell.customMask.value);
                if (cols.Length > 0)
                {
                    for (int i = 0; i < cols.Length; i++)
                    {
                        Unit2D unit = cols[i].gameObject.GetComponent <Unit2D>();
                        if (unit && unit.isAlive)
                        {
                            foreach (var effect in spell.effects)
                            {
                                print(unit.unitName + " takes effect " + spell.spellName);

                                StartCoroutine(ApplySpellEffect(effect, unit));
                            }
                        }
                    }
                }
            }
            else //single unit is affected
            {
                foreach (var effect in spell.effects)
                {
                    StartCoroutine(ApplySpellEffect(effect, target));
                }
            }
        }
Ejemplo n.º 2
0
        public void Shoot(AttackInstance2D attInst = null, Transform sp = null)
        {
            if (attInst.tgtUnit == null || attInst.tgtUnit.GetTargetTransform() == null)
            {
                ObjectPoolManager.Unspawn(gameObject);
                return;
            }

            attInstance  = attInst;
            target       = attInstance.tgtUnit;
            targetPos    = target.GetTargetTransform().position;
            hitThreshold = Mathf.Max(.1f, target.hitThreshold);
            shootPoint   = sp;

            if (type != _ShootObjectType.Beam)
            {
                if (shootPoint.position.x > targetPos.x)
                {
                    renderer.flipX = true;
                }
                else
                {
                    renderer.flipX = false;
                }
            }

            //if (shootPoint != null) transform.rotation = shootPoint.rotation;
            if (shootEffect != null && target.isAlive)
            {
                ObjectPoolManager.Spawn(shootEffect, transform.position, transform.rotation);
            }
            hit = false;

            print("Shoot");
            if (type == _ShootObjectType.Projectile)
            {
                StartCoroutine(ProjectileRoutine());
            }
            else if (type == _ShootObjectType.Beam)
            {
                StartCoroutine(BeamRoutine());
            }
            else if (type == _ShootObjectType.Bullet)
            {
                StartCoroutine(BulletRoutine());
            }
            else if (type == _ShootObjectType.Missile)
            {
                StartCoroutine(MissileRoutine());
            }
            else if (type == _ShootObjectType.Effect && effectDelay > 0)
            {
                StartCoroutine(EffectRoutine());
            }
            else
            {
                Hit();
            }
        }
Ejemplo n.º 3
0
 private IEnumerator ApplySpellEffect(SpellEffect effect, Unit2D target)
 {
     target.TakeDamage(effect.damage);
     if (effect.slow.slowMultiplier > 0f && effect.slow.duration > 0f)
     {
         target.TakeEffect(effect.slow);
     }
     yield return(null);
 }
Ejemplo n.º 4
0
 protected virtual void Fire()
 {
     target.TakeDamage(damage, () =>
     {
         target = null;
         time   = attackSpeed * .5f;
     });
     if (!target || target.isDead)
     {
         isAttacking = false;
         target      = null;
     }
 }
Ejemplo n.º 5
0
        // Use this for initialization
        void Start()
        {
            DOVirtual.DelayedCall(.5f, () =>
            {
                int offsetY       = quatity / 2;
                Vector2 offsetPos = new Vector2(20, screenHeight * offsetY);
                for (int i = 0; i < quatity; i++)
                {
                    Unit2D unit = Instantiate(prefab);
                    unit.transform.ScreenPlacement(ScreenPosition.Left, offsetPos);
                    unit.transform.parent = transform;
                    offsetPos            -= new Vector2(0, screenHeight);
                    units.Add(unit);
                    if (OnUnit2DSpawned != null)
                    {
                        OnUnit2DSpawned(unit);
                    }
                }
            });

            //StartCoroutine(CheckRenderSortingOrder());
        }
Ejemplo n.º 6
0
        protected virtual void Update()
        {
            if (isDead)
            {
                return;
            }

            velocity = Vector2.zero;
            if (target != null && target.isAlive)
            {
                direct           = target.transform.position - transform.position;
                distanceToTarget = direct.magnitude;
                if (direct.x < 0)
                {
                    transform.localScale = new Vector3(-1, 1, 1);
                }
                else
                {
                    transform.localScale = new Vector3(1, 1, 1);
                }
                rigidBody2D.velocity = Vector2.zero;
                if (distanceToTarget > attackRange)
                {
                    velocity             = new Vector2(direct.normalized.x * MoveSpeed, direct.normalized.y * MoveSpeed) * Time.deltaTime;
                    rigidBody2D.velocity = velocity;
                }
                else
                {
                    float distanceY = (transform.position.y - target.transform.position.y);
                    PlayAttackAnimation(distanceY);
                    time += Time.deltaTime;
                    if (time > attackSpeed)
                    {
                        time = -attackSpeed;
                        Fire();
                    }
                }
                renderer.sortingOrder = SortingLayer;
            }
            else
            {
                Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, detectiveRange, enemyMask.value);
                if (colliders.Length > 0)
                {
                    Debug.Log("tg: " + colliders.Length);
                    float minDis = float.MaxValue;
                    for (int i = 0; i < colliders.Length; i++)
                    {
                        var enemy = colliders[i].GetComponent <Unit2D>();
                        if (enemy && enemy.isAlive)
                        {
                            float dis = Vector2.Distance(enemy.GetTargetTransform().position, GetTargetTransform().position);
                            if (dis < minDis)
                            {
                                minDis = dis;
                                target = enemy;
                            }
                        }
                    }
                }
                else
                {
                    StopMoving();
                    PlayAnimation("Idle");
                }
            }
        }