Beispiel #1
0
    private IEnumerator ControlCoroutine()
    {
        Disposable = new CompositeDisposable();

        var source = Owner.transform.position;

        Shots = new EnemyShot[BulletNum];
        var span   = 360.0f / BulletNum;
        var radius = (BulletNum * 3) + 8;

        for (int i = 0; i < BulletNum; i++)
        {
            int j      = i;
            var offset = Vector2Extensions.FromAngleLength(i * span, radius);
            var shot   = Owner.Api.Shot(source + offset.ToVector3(), 0, 0);
            Shots[i] = shot;
            // TODO: ここで全要素にnullが代入されるラムダ式ができているのかも・・・
            shot.DestroyEvent.Subscribe(u => Shots[j] = null)
            .AddTo(Disposable);
        }

        float angle = 0;

        while (true)
        {
            var origin = Owner.transform.position;
            for (int i = 0; i < BulletNum; i++)
            {
                if (Shots[i] != null)
                {
                    var offset = Vector2Extensions.FromAngleLength(angle + i * span, radius);
                    Shots[i].transform.position = origin + offset.ToVector3();
                }
            }
            for (int i = 0; i < Shots.Length; i++)
            {
                Debug.DrawLine(origin, Shots[i].transform.position, new Color(0, 0, 1));
            }
            angle += RotateSpeed;
            yield return(null);
        }
    }
    private TShot InitializeShot <TShot>(GameObject shot, EnemyShotBehavior behavior, List <TShot> list, Vector3 source, float angle, float speed, EnemyApi api)
        where TShot : EnemyShot
    {
        shot.transform.position = source;
        shot.transform.SetEulerAnglesZ(0);
        shot.transform.parent = SpriteStudioManager.I.ManagerDraw.transform;

        var velocity  = Vector2Extensions.FromAngleLength(angle, speed);
        var rigidbody = shot.GetComponent <Rigidbody2D>();

        rigidbody.velocity = velocity;

        var script = shot.GetComponent <TShot>();

        script.InitializeBullet(behavior, api);
        script.DestroyEvent.Subscribe(u => list.Remove(script));
        list.Add(script);

        shot.GetComponent <Collider2D>().enabled = true;
        return(script);
    }
Beispiel #3
0
    private IEnumerator PrepareCoroutine()
    {
        int   bodyCount = asset.BodyCount;
        float initialPositionAngleRange = asset.InitialPositionAngleRange;

        float initialPositionAngleSpan = initialPositionAngleRange / (bodyCount - 1);

        var locations = Enumerable.Range(0, bodyCount)
                        .Select(i =>
        {
            var angle = i * initialPositionAngleSpan - initialPositionAngleRange / 2;
            var pos   = Vector2Extensions.FromAngleLength(angle, asset.InitialPositionDisplacement);
            return(pos + new Vector2(0, -10));
        })
                        .ToArray();

        int locationIndex = (int)(UnityEngine.Random.value * bodyCount) % bodyCount;

        for (int i = 0; i < bodyCount; i++)
        {
            if (i != locationIndex)
            {
                var behavior = new ShootingEnemyShotBehavior()
                {
                    Way          = asset.CopiesShotWay,
                    AngleSpan    = asset.CopiesShotAngleSpan,
                    ShotSpeed    = asset.CopiesShotSpeed,
                    ShotTimeSpan = asset.CopiesShotTimeSpan
                };
                var mob = Api.ShotMob(Api.Enemy.transform.position, 180, 0, behavior);
                behavior.InitializeComponent();
                behavior.Move(locations[i], 20);
            }
        }
        Api.MoveIt(locations[locationIndex], 20);
        yield return(new WaitForSeconds(0.5f));
    }