コード例 #1
0
    public void AutoAim()
    {
        Vector3 pos    = this.gameObject.transform.position;
        float   thresh = 0.7f;

        Collider2D[] overlaps = Physics2D.OverlapCircleAll(pos, AutoAimRange);

        float      min_dist   = Mathf.Infinity;
        GameObject old_target = target;

        target = null;
        foreach (Collider2D overlap in overlaps)
        {
            if (overlap.gameObject.CompareTag("Enemy"))
            {
                Vector2 hit_pos = new Vector2(overlap.gameObject.transform.position.x, overlap.gameObject.transform.position.y);
                Vector2 sub     = new Vector2(pos.x - hit_pos.x, pos.y - hit_pos.y);
                if (min_dist > sub.magnitude + thresh)
                {
                    target   = overlap.gameObject;
                    min_dist = sub.magnitude;
                }
            }
        }

        if (target != null)
        {
            // Vector3 target_center = target.GetComponent<Collider2D>().bounds.center;
            Vector3 target_center = target.transform.position;
            Vector2 dir           = (target_center - gameObject.transform.position).normalized;
            //Vector2 dir = (target_center - m_FirePoint.transform.position).normalized;
            m_MovementComponent.FlipSprite(dir.x);
            Quaternion rot = GameplayStatics.GetRotationFromDir(dir);
            m_PlayerHand.GetComponent <SpriteRenderer>().transform.rotation = rot;

            target.GetComponentInChildren <TargetHighlightComponent>().ToggleHighlight(true);
            if (old_target)
            {
                if (old_target != target)
                {
                    var shadow = old_target.GetComponentInChildren <TargetHighlightComponent>();
                    if (shadow)
                    {
                        shadow.ToggleHighlight(false);
                    }
                }
            }
        }

        m_SpellComponent.SetTarget(target);
    }
コード例 #2
0
ファイル: LightningBolt.cs プロジェクト: IvanNSBS/Geeko-Game
    public override GameObject CastSpell(GameObject owner, GameObject target = null, Vector3?spawn_pos = null, Quaternion?spawn_rot = null)
    {
        if (m_Prefab && owner)
        {
            Vector3    dir     = ((Quaternion)spawn_rot * Vector3.right).normalized;
            Vector3    rot_dir = ((Quaternion)spawn_rot * Vector3.up).normalized;
            Quaternion rot     = GameplayStatics.GetRotationFromDir(rot_dir);

            Vector3 target_pos     = owner.transform.position + dir * 8.5f;
            Vector3 spawn_position = spawn_pos != null ? (Vector3)spawn_pos : owner.transform.position;
            float   distance       = (spawn_position - target_pos).magnitude;

            Debug.DrawRay(spawn_position, dir * distance, Color.green, 1.0f);
            RaycastHit2D[] hit = Physics2D.CircleCastAll(spawn_position, 2.0f, dir, distance, layerMask: GameplayStatics.LayerEnemy);
            foreach (RaycastHit2D enemy in hit)
            {
                GameplayStatics.ApplyDamage(enemy.collider.gameObject, m_Damage);
            }
            hit = Physics2D.CircleCastAll(spawn_position, 2.0f, dir, distance, layerMask: LayerMask.GetMask("FlyingEnemy"));;
            foreach (RaycastHit2D enemy in hit)
            {
                GameplayStatics.ApplyDamage(enemy.collider.gameObject, m_Damage);
            }

            GameObject obj = Instantiate(m_Prefab);
            obj.GetComponent <SpellPrefabManager>().SetOwner(owner);

            obj.tag = "SpellUninteractive";
            Destroy(obj.GetComponent <BoxCollider2D>());
            obj.transform.position   = spawn_position + dir * distance / 2;
            obj.transform.rotation   = rot;
            obj.transform.localScale = new Vector2(1.5f, distance);

            // GameplayStatics.AddQuad(obj, m_Material);

            if (m_Material)
            {
                obj.GetComponent <MeshRenderer>().material = m_Material;

                obj.GetComponent <SpellPrefabManager>().AddOnUpdate((objt) => {
                    SpellPrefabManager spm = objt.GetComponent <SpellPrefabManager>();
                    objt.GetComponent <MeshRenderer>().material.SetFloat("_Strength", spm.m_TimeToLive - spm.m_TimeAlive);
                });
            }

            obj.GetComponent <SpellPrefabManager>().m_TimeToLive = m_SpellDuration;
            obj.GetComponent <SpellPrefabManager>().AddTriggerTick(this.SpellTriggerTick);
            obj.GetComponent <SpellPrefabManager>().AddOnUpdate(this.OnTick);
        }
        return(null);
    }
コード例 #3
0
    public override GameObject CastSpell(GameObject owner, GameObject target = null, Vector3?spawn_pos = null, Quaternion?spawn_rot = null)
    {
        if (m_Prefab && owner)
        {
            Vector3    dir   = target == null ? ((Quaternion)spawn_rot * Vector3.right).normalized : (target.transform.position - owner.transform.position).normalized;
            Quaternion rot   = GameplayStatics.GetRotationFromDir(dir);
            Vector2    speed = new Vector2(m_ProjectileSpeed * dir.x, m_ProjectileSpeed * dir.y);

            GameObject obj = SpellUtilities.InstantiateSpell(m_Prefab, owner, target, this, (Vector3)spawn_pos, rot, spell_velocity: speed, tag: "SpellUninteractive");
            return(obj);
        }

        return(null);
    }
コード例 #4
0
    public override GameObject CastSpell(GameObject owner, GameObject target = null, Vector3?spawn_pos = null, Quaternion?spawn_rot = null)
    {
        if (m_Prefab && owner)
        {
            Vector3    dir   = target == null ? ((Quaternion)spawn_rot * Vector3.right).normalized : (target.transform.position - owner.transform.position).normalized;
            Quaternion rot   = GameplayStatics.GetRotationFromDir(dir);
            Vector2    speed = new Vector2(0, 0);

            GameObject obj = SpellUtilities.InstantiateSpell(m_Prefab, owner, target, this, (Vector3)spawn_pos, Quaternion.identity, spell_velocity: speed, tick_delay: m_TickDealay, tag: "SpellUninteractive");

            var sprite = obj.GetComponent <SpriteRenderer>();
            if (sprite)
            {
                sprite.enabled = false;
            }

            var manager = obj.GetComponent <SpellPrefabManager>();
            if (m_TickBehaviors.Count > 0 && m_TickBehaviors[0] != null)
            {
                manager.m_TickDelay = m_TickBehaviors[0].m_Delay;
            }

            foreach (var behaviour in m_TickBehaviors)
            {
                if (behaviour.m_SCData.m_RemoveOnModified)
                {
                    UnityAction <float, GameplayStatics.DamageType> action = (dmg, type) => Destroy(obj);

                    manager.GetOwner().GetComponent <StatusComponent>().AddOnTakeDamage((dmg, type) => {
                        action(dmg, type);
                        // status.RemoveOnTakeDamage(action);
                    });
                }
            }

            //obj.GetComponent<SpellPrefabManager>().SetFollowerOwner(FollowWho.Player);
            //var status = obj.GetComponent<SpellPrefabManager>().GetOwner().GetComponent<StatusComponent>();

            //UnityAction<float, GameplayStatics.DamageType> action = (dmg, type) => Destroy(obj);

            //status.AddOnTakeDamage( (dmg, type) => {
            //    action(dmg, type);
            //    status.RemoveOnTakeDamage(action);
            //});

            return(obj);
        }
        return(null);
    }
コード例 #5
0
    public void GenericStream(
        TargetingManager tm,
        int numberOfShots,
        float timeBetweenShots,
        Func <float> bulletSpeed,
        Action <Bullet> callback = null
        )
    {
        var shotsFired   = 0;
        var lastShotTime = Time.time - timeBetweenShots;

        shootingFuncs.Add((() =>
        {
            while (Time.time > lastShotTime + timeBetweenShots && shotsFired < numberOfShots)
            {
                lastShotTime += timeBetweenShots;
                ++shotsFired;
                var dir = tm.Target().normalized;
                var obj = Instantiate(bulletPrefab, firePoint.position, GameplayStatics.GetRotationFromDir(dir));
                var bullet = obj.GetComponent <Bullet>();
                var bs = bulletSpeed();
                bullet.rb.velocity = dir * bs;
                bullet.targetTag = targetTag;
                bullet.maxSpeed = bs;
                bullet.SetInstantiator(owner);
                if (_homingRotational)
                {
                    bullet.HomeRotational(_homingTarget, _homingDegreesPerSecond);
                }
                else if (_homingDirectional)
                {
                    bullet.HomeDirectional(_homingTarget, _homingAcceleration);
                }

                if (_willDisappear)
                {
                    bullet.DisappearIn = _disappearIn;
                }

                callback?.Invoke(bullet);
            }

            return(shotsFired >= numberOfShots);
        }));
    }
コード例 #6
0
    public override void OnTick(GameObject obj)
    {
        GameObject owner = obj.GetComponent <SpellPrefabManager>().GetOwner();
        Vector3    target = (Vector3)obj.GetComponent <SpellPrefabManager>().m_TargetInitialPos;
        Vector2    point, derivative;


        Vector3 dir = (obj.GetComponent <SpellPrefabManager>().m_SpawnRot *Vector3.right).normalized;

        Vector3 up_down = obj.GetComponent <SpellPrefabManager>().m_Toggle? new Vector3(1.8f, -2.4f) : new Vector3(1.8f, 2.4f);

        Vector2 p0 = owner.transform.position;
        Vector2 p1 = v3_to_v2((GameplayStatics.GetRotationFromDir(dir) * up_down) + (owner.transform.position));
        Vector2 p2 = target;

        eval_curve(obj.GetComponent <SpellPrefabManager>().m_TimeAlive *m_SpeedMult, p0, p1, p2, out point, out derivative);
        dir = (point - v3_to_v2(obj.transform.position)).normalized;
        obj.transform.position = point;
        obj.transform.rotation = GameplayStatics.GetRotationFromDir(dir);
    }
コード例 #7
0
    public override GameObject CastSpell(GameObject owner, GameObject target = null, Vector3?spawn_pos = null, Quaternion?spawn_rot = null)
    {
        if (m_Prefab && owner)
        {
            Vector3    dir   = target == null ? ((Quaternion)spawn_rot * Vector3.right).normalized : (target.transform.position - owner.transform.position).normalized;
            Quaternion rot   = GameplayStatics.GetRotationFromDir(dir);
            Vector2    speed = new Vector2(0, 0);

            GameObject obj  = SpellUtilities.InstantiateSpell(m_Prefab, owner, target, this, (Vector3)spawn_pos, rot, spell_velocity: speed, tag: "SpellUninteractive");
            GameObject obj2 = SpellUtilities.InstantiateSpell(m_Prefab, owner, target, this, (Vector3)spawn_pos, rot, spell_velocity: speed, tag: "SpellUninteractive");
            obj2.GetComponent <SpellPrefabManager>().m_Toggle = true;

            obj.GetComponent <SpellPrefabManager>().m_TargetInitialPos = target == null?v3_to_v2(owner.transform.position + 8 *dir) : v3_to_v2(target.transform.position);

            obj2.GetComponent <SpellPrefabManager>().m_TargetInitialPos = target == null?v3_to_v2(owner.transform.position + 8 *dir) : v3_to_v2(target.transform.position);

            AudioSource.PlayClipAtPoint(m_SpellSound, owner.transform.position);

            return(obj);
        }
        return(null);
    }
コード例 #8
0
    public void Shoot()
    {
        var vel    = DetermineVelocity();
        var obj    = Instantiate(bulletPrefab, firePoint.position, GameplayStatics.GetRotationFromDir(vel));
        var bullet = obj.GetComponent <Bullet>();

        bullet.targetTag   = this.targetTag;
        bullet.rb.velocity = vel;
        bullet.maxSpeed    = vel.magnitude;
        bullet.SetInstantiator(owner);
        if (_homingRotational)
        {
            bullet.HomeRotational(_homingTarget, _homingDegreesPerSecond);
        }
        else if (_homingDirectional)
        {
            bullet.HomeDirectional(_homingTarget, _homingAcceleration);
        }

        if (_willDisappear)
        {
            bullet.DisappearIn = _disappearIn;
        }
    }