Exemple #1
0
        protected override EnemyShip CreateFrom(GameObject g)
        {
            var a = g.AddComponent <EnemyShip>();

            a.VisibilityRadius = 100;
            a.VisibilityAngle  = 15;
            a.DistanceToKeep   = 3;
            a.RotationSpeed    = 5;

            g.AddComponent <Scorable>().Score = 10;

            var c = g.AddComponent <Collider>();

            c.Transform.Scale = new Vector2(1, 1);
            c.CollisionLayer  = Layer.EnemyShip;
            // c.Collision += g.DestroyObject;

            a.Transform.Scale = new Vector2(0.5f, 1);
            a.Speed           = 1f;

            var p = g.AddComponent <Gun>();

            p.Force = 6;

            var am = Game.Create <Pool <Ammo> >();

            am.Parent    = g;
            am.Factory   = BulletFactory;
            am.BaseSize  = 1;
            am.FixedSize = true;

            p.AddAmmoBox(am);

            a.Commands = new List <Command>()
            {
                new Command(() => Asteroids.Physics.AnyOverlaps(a.Transform.Position, a.VisibilityRadius, Layer.Player, out Vector2 hit), () =>
                {
                    Asteroids.Physics.AnyOverlaps(a.Transform.Position, a.VisibilityRadius, Layer.Player, out Vector2 hit);
                    float signedAngle = Vector2.SignedAngle(a.Transform.Direction, hit - a.Transform.Position);

                    if (Math.Abs(signedAngle) >= a.VisibilityAngle)
                    {
                        a.Parent.Rotate(a.RotationSpeed * Mathf.Sign(signedAngle));
                    }
                    else
                    {
                        a.GetComponent <Gun>().TryShootWithType(0);
                    }

                    var dir = hit - a.Transform.Position;
                    if (dir.magnitude > a.DistanceToKeep)
                    {
                        var dirNorm = dir.normalized * a.Speed;
                        a.Parent.Move(dirNorm * a.Speed * Asteroids.Time.DeltaTime);
                    }
                }),
            };

            return(a);
        }
        public T AddComponent <T>() where T : Component
        {
            var created = Game.Create <T>();

            components.Add(typeof(T), created);
            // created.Destroy += RemoveComponent<T>;
            created.Parent = this;
            return(created);
        }
Exemple #3
0
        protected override Ship CreateFrom(GameObject g)
        {
            var a = g.AddComponent <Ship>();

            a.RotationSpeed = 5;

            var t = g.AddComponent <Thruster>();

            t.LinearDrag = 0.9f;

            g.AddComponent <Player>();

            var c = g.AddComponent <Collider>();

            c.Transform.Scale = new Vector2(1, 1);
            c.CollisionLayer  = Layer.Player;
            // c.Collision += g.DestroyObject;

            a.Transform.Scale = new Vector2(0.5f, 1);
            a.Speed           = 0.02f;

            var p = g.AddComponent <Gun>();

            var am = Game.Create <Pool <Ammo> >();

            am.BaseSize = 6;
            am.Factory  = BulletFactory;
            am.Parent   = g;

            var lm = Game.Create <Pool <Ammo> >();

            lm.BaseSize  = 2;
            lm.FixedSize = true;
            lm.Factory   = LaserAmmoFactory;
            lm.Parent    = g;

            p.AddAmmoBox(am);
            p.AddAmmoBox(lm);

            p.Force = 10;

            a.Commands = new List <Command>()
            {
                new Command(() => Input.GetAxisRaw("Vertical") > 0, () => a.GetComponent <Thruster>().AddForce(a.Transform.Direction.normalized * a.Speed)),
                new Command(() => Input.GetAxisRaw("Horizontal") > 0, () => a.Parent.Rotate(-a.RotationSpeed)),
                new Command(() => Input.GetAxisRaw("Horizontal") < 0, () => a.Parent.Rotate(a.RotationSpeed)),
                new Command(() => Input.GetMouseButtonDown(0), () => a.GetComponent <Gun>().TryShootWithType(0)),
                new Command(() => Input.GetMouseButtonDown(1), () => a.GetComponent <Gun>().TryShootWithType(1)),
            };

            return(a);
        }
Exemple #4
0
        public T Create(Vector2 pos)
        {
            var go = Game.Create <GameObject>();

            var transform = go.AddComponent <Transform>();

            transform.Position  = pos;
            transform.Scale     = new Vector2(1, 1);
            transform.Direction = new Vector2(0, 1);

            var created = CreateFrom(go);

            if (Spawned != null)
            {
                Spawned.Raise(created);
            }

            return(created);
        }