Ejemplo n.º 1
0
        private void OnTargetDestroy(Unit unit)
        {
            Unit.onDestroyedE -= OnTargetDestroy;

            if (unit == target)
            {
                StopAllCoroutines();
                CancelInvoke();
                ObjectPoolManager.Unspawn(shootEffect);
            }
            else if (unit == attInstance.srcUnit)
            {
                StopAllCoroutines();
                CancelInvoke();
                if (effectSetting)
                {
                    effectSetting.Deactivate();
                }
                ObjectPoolManager.Unspawn(shootEffect);
                if (target)
                {
                    target.PurifyEffect();
                }
            }
        }
Ejemplo n.º 2
0
        IEnumerator FPSProjectileRoutine()
        {
            float timeShot = Time.time;

            while (true)
            {
                RaycastHit raycastHit;
                Vector3    dir         = thisT.TransformDirection(new Vector3(0, 0, 1));
                float      travelDist  = speed * Time.fixedDeltaTime;
                bool       hitCollider = Physics.SphereCast(thisT.position, hitRadius, dir, out raycastHit, travelDist);
                if (hitCollider)
                {
                    travelDist = raycastHit.distance + hitRadius;
                }

                thisT.Translate(Vector3.forward * travelDist);
                if (Time.time - timeShot > 5)
                {
                    break;
                }
                yield return(new WaitForSeconds(Time.fixedDeltaTime));
            }

            ObjectPoolManager.Unspawn(thisObj);
            yield return(null);
        }
Ejemplo n.º 3
0
        public string UpgradeToNextTower(int ID = 0)
        {
            UnitTower nextLevelTower = nextLevelTowerList[Mathf.Clamp(ID, 0, nextLevelTowerList.Count)];

            List <int> cost     = GetCost();
            int        suffCost = ResourceManager.HasSufficientResource(cost);

            if (suffCost == -1)
            {
                ResourceManager.SpendResource(cost);

                GameObject towerObj      = (GameObject)ObjectPoolManager.Spawn(nextLevelTower.gameObject, thisT.position, thisT.rotation);
                UnitTower  towerInstance = towerObj.GetComponent <UnitTower>();
                towerInstance.InitTower(instanceID);
                towerInstance.SetPlatform(occupiedPlatform, occupiedNode);
                towerInstance.AddValue(value);
                towerInstance.SetLevel(level + 1);
                towerInstance.Build();
                GameControl.SelectTower(towerInstance);

                if (onUpgradedE != null)
                {
                    onUpgradedE(towerInstance);
                }

                ObjectPoolManager.Unspawn(thisObj);

                return("");
            }
            return("Insufficient Resource");
        }
Ejemplo n.º 4
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.º 5
0
        private void Hit()
        {
            hit = true;

            if (hitEffect != null)
            {
                print("show hit effect  "); ObjectPoolManager.Spawn(hitEffect, targetPos, Quaternion.identity);
            }

            if (spell.effects.Count > 0)
            {
                Debug.Log("Cast spell effects");
                CastSpell(spell, targetPos, target);
            }
            ObjectPoolManager.Unspawn(gameObject);
        }
Ejemplo n.º 6
0
        IEnumerator FPSEffectRoutine()
        {
            yield return(new WaitForSeconds(0.05f));

            RaycastHit raycastHit;
            Vector3    dir = thisT.TransformDirection(new Vector3(0, 0, 1));

            if (Physics.SphereCast(thisT.position, hitRadius / 2, dir, out raycastHit))
            {
                Unit unit = raycastHit.transform.GetComponent <Unit>();
                FPSHit(unit, raycastHit.point);

                if (hitEffect != null)
                {
                    ObjectPoolManager.Spawn(hitEffect, raycastHit.point, Quaternion.identity);
                }
            }

            yield return(new WaitForSeconds(0.1f));

            ObjectPoolManager.Unspawn(thisObj);
        }
Ejemplo n.º 7
0
        IEnumerator FPSBeamRoutine(Transform sp)
        {
            thisT.parent = sp;
            float duration = 0;

            while (duration < beamDuration)
            {
                RaycastHit raycastHit;
                Vector3    dir         = thisT.TransformDirection(new Vector3(0, 0, 1));
                bool       hitCollider = Physics.SphereCast(thisT.position, hitRadius, dir, out raycastHit);
                if (hitCollider)
                {
                    if (!hit)
                    {
                        hit = true;
                        Unit unit = raycastHit.transform.GetComponent <Unit>();
                        FPSHit(unit, raycastHit.point);

                        if (hitEffect != null)
                        {
                            ObjectPoolManager.Spawn(hitEffect, raycastHit.point, Quaternion.identity);
                        }
                    }
                }

                float lineDist = raycastHit.distance == 0 ? 9999 : raycastHit.distance;
                for (int i = 0; i < lineList.Count; i++)
                {
                    lineList[i].SetPosition(1, new Vector3(0, 0, lineDist));
                }

                duration += Time.fixedDeltaTime;
                yield return(new WaitForSeconds(Time.fixedDeltaTime));
            }

            thisT.parent = null;
            ObjectPoolManager.Unspawn(thisObj);
        }
Ejemplo n.º 8
0
        void Hit()
        {
            hit = true;

            if (isArrow) //leave an arrow in the ground effect
            {
                targetPos = new Vector3(targetPos.x + Random.value - .5f, 0.05f, targetPos.z + Random.value - .5f);
            }
            if (hitEffect != null)
            {
                ObjectPoolManager.Spawn(hitEffect, targetPos, isArrow ? Quaternion.Euler(-180, Random.Range(0, 30), Random.Range(0, 30)) : Quaternion.identity);
            }

            //apply ability
            if (Ab_End_Holder)
            {
                AbilityManager.instance.ActivateAbility(Ab_End_Holder.ability, attInstance.srcUnit, targetPos, target);
            }
            thisT.position          = targetPos;
            attInstance.impactPoint = thisT.position;
            LayerMask mask = attInstance.srcUnit.TargetMask;

            if (attInstance.srcUnit.GetAOERadius() > 0)
            {
                Collider[] cols = Physics.OverlapSphere(thisT.position, attInstance.srcUnit.GetAOERadius(), mask);
                AttackTargets(cols);
                print("Splash attack kill more " + cols.Length);
            }
            else
            {
                if (target != null)
                {
                    target.ApplyEffect(attInstance);
                    if (target.alarmWhenGotAttacked && !target.isAlarming && target.GetAttackRange() < 2 && attInstance.damage > 0)
                    {
                        target.isAlarming = true;
                        target.attacker   = attInstance.srcUnit;
                        target.StartCoroutine(SetAlarmOff(target, 10));
                    }

                    if (penetratable && penetration_distance > 0) //penetratable attack
                    {
                        Vector3      position  = shootPoint.position;
                        Vector3      direction = target.GetTargetT().position - position;
                        RaycastHit[] hits      = Physics.RaycastAll(position, direction.normalized, penetration_distance);
                        AttackTargets(hits.GetColliders());
                        print("Penetrate attack kill more " + hits.Length);
                    }
                }
            }
            //DestroyImmediate(thisObj);
            if (!bouncing)
            {
                ObjectPoolManager.Unspawn(thisObj);
            }
            else
            {
                Collider[] cols = Physics.OverlapSphere(thisT.position, bouncing_radius, mask);
                if (cols.Length > 0)
                {
                    print("Bonce");
                    List <Unit> tgtList = new List <Unit>();
                    for (int i = 0; i < cols.Length; i++)
                    {
                        Unit unit = cols[i].gameObject.GetComponent <Unit>();
                        if (unit && !unit.dead && unit != attInstance.tgtUnit)
                        {
                            tgtList.Add(unit);
                        }
                    }
                    if (tgtList.Count > 0 && bouncing_max_targets >= 0)
                    {
                        bouncing_max_targets--;
                        attInstance.tgtUnit = tgtList[0];
                        Shoot(attInstance, thisT);
                    }
                }
                else
                {
                    ObjectPoolManager.Unspawn(thisObj);
                }
            }
        }
Ejemplo n.º 9
0
        public void Shoot(AttackInstance attInst = null, Transform sp = null)
        {
            if (attInst.tgtUnit == null || attInst.tgtUnit.GetTargetT() == null)
            {
                ObjectPoolManager.Unspawn(thisObj);
                return;
            }

            attInstance = attInst;
            target      = attInstance.tgtUnit;
            //print(attInstance.srcUnit.unitName + " attacks " + attInstance.tgtUnit.unitName + " with " + attInstance.damage + " damage");

            targetPos = target.GetTargetT().position;
#if Game_2D
            hitThreshold = Mathf.Max(.01f, target.hitThreshold);
#else
            hitThreshold = Mathf.Max(.1f, target.hitThreshold);
#endif
            shootPoint = sp;

            if (shootPoint != null)
            {
                thisT.rotation = shootPoint.rotation;
            }
            if (shootEffect != null && !target.dead)
            {
                GameObject shootEffectInstance = ObjectPoolManager.Spawn(shootEffect, thisT.position, thisT.rotation);
                if (shootEffectInstance)
                {
                    effectSetting = shootEffectInstance.GetComponent <EffectSettings>();
                    if (effectSetting == null)
                    {
                        effectSetting = shootEffectInstance.GetComponentInChildren <EffectSettings>();
                    }
                    if (effectSetting)
                    {
                        if (effectSetting.EffectType == EffectSettings.EffectTypeEnum.Other)
                        {
                            Vector3 direction = (targetPos - thisT.position).normalized;
                            shootEffectInstance.transform.rotation = Quaternion.LookRotation(direction);
                        }

                        effectSetting.Target = target.GetTargetT().gameObject;
                        SelfDeactivator selfDeactivator = shootEffectInstance.GetComponent <SelfDeactivator>();
                        if (selfDeactivator == null)
                        {
                            selfDeactivator = shootEffectInstance.AddComponent <SelfDeactivator>();
                        }
                        selfDeactivator.duration = attInstance.srcUnit.CurrentStat.cooldown;
                    }
                }
                Unit.onDestroyedE += OnTargetDestroy;
            }
            hit = false;


            if (Ab_Start_Holder)
            {
                AbilityManager.instance.ActivateAbility(Ab_Start_Holder.ability, attInstance.srcUnit, targetPos, target);
            }

            if (type == _ShootObjectType.Projectile)
            {
                StartCoroutine(ProjectileRoutine());
            }
            else if (type == _ShootObjectType.Beam)
            {
                StartCoroutine(BeamRoutine());
            }
            else if (type == _ShootObjectType.Missile)
            {
                StartCoroutine(MissileRoutine());
            }
            else if (type == _ShootObjectType.Effect && EffectDelay > 0)
            {
                StartCoroutine(EffectRoutine());
            }
            else
            {
                Hit();
            }
        }
Ejemplo n.º 10
0
        IEnumerator Building(float duration, bool reverse = false)
        {       //reverse flag is set to true when selling (thus unbuilding) the tower
            construction = !reverse ? _Construction.Constructing : _Construction.Deconstructing;

            builtDuration = 0;
            buildDuration = duration;

            if (onConstructionStartE != null)
            {
                onConstructionStartE(this);
            }

            yield return(null);

            if (!reverse && playConstructAnimation != null)
            {
                playConstructAnimation();
            }
            else if (reverse && playDeconstructAnimation != null)
            {
                playDeconstructAnimation();
            }

            if (SpawnEffect)
            {
                SpawnEffect = ObjectPoolManager.Spawn(SpawnEffect, thisT.position, thisT.rotation);
                foreach (Renderer render in renderParent)
                {
                    render.enabled = false;
                }
            }
            while (true)
            {
                yield return(null);

                builtDuration += Time.deltaTime;
                if (builtDuration > buildDuration)
                {
                    break;
                }
            }
            if (SpawnEffect)
            {
                ObjectPoolManager.Unspawn(SpawnEffect);
                foreach (Renderer render in renderParent)
                {
                    render.enabled = true;
                }
            }
            construction = _Construction.None;

            if (!reverse && onConstructionCompleteE != null)
            {
                onConstructionCompleteE(this);
            }

            if (reverse)
            {
                if (onSoldE != null)
                {
                    onSoldE(this);
                }

                if (occupiedPlatform != null)
                {
                    occupiedPlatform.UnbuildTower(occupiedNode);
                }
                ResourceManager.GainResource(GetValue());
                Dead();
            }
        }
Ejemplo n.º 11
0
        IEnumerator _ReachDestination(float duration)
        {
            yield return(new WaitForSeconds(duration));

            ObjectPoolManager.Unspawn(thisObj);
        }