Esempio n. 1
0
        private Spawner BuildBulletSpawner(CollisionService collisionService)
        {
            var spriteSheet = _assetsResolver.Get <SpriteSheet>("assets/sheet.json");

            var spawner = new Spawner(() =>
            {
                var bullet = new GameObject();
                bullet.Components.Add <TransformComponent>();

                var bulletSpriteRenderer        = bullet.Components.Add <SpriteRenderComponent>();
                bulletSpriteRenderer.Sprite     = spriteSheet.Get("fire01.png");
                bulletSpriteRenderer.LayerIndex = (int)RenderLayers.Items;

                var bulletBBox = bullet.Components.Add <BoundingBoxComponent>();
                bulletBBox.SetSize(bulletSpriteRenderer.Sprite.Bounds.Size);

                var speed = 7000f;

                var bulletRigidBody      = bullet.Components.Add <MovingBody>();
                bulletRigidBody.MaxSpeed = speed;

                var brain     = bullet.Components.Add <BulletBrain>();
                brain.Speed   = speed;
                brain.Display = this.Game.Display;

                collisionService.Add(bulletBBox);

                return(bullet);
            }, bullet =>
            {
                bullet.Components.Get <MovingBody>().Reset();

                bullet.Components.Get <TransformComponent>().Local.Reset();
                bullet.Components.Get <TransformComponent>().World.Reset();
            });

            spawner.Components.Add <TransformComponent>();

            return(spawner);
        }
Esempio n. 2
0
        private Spawner BuildAsteroidsSpawner(CollisionService collisionService)
        {
            var spriteNames = new[]
            {
                "meteorBrown_big1.png",
                "meteorBrown_big2.png",
                "meteorBrown_big3.png",
                "meteorBrown_big4.png",
                "meteorGrey_big1.png",
                "meteorGrey_big2.png",
                "meteorGrey_big3.png",
                "meteorGrey_big4.png",
            };
            int spriteIndex = 0;

            var spriteSheet = _assetsResolver.Get <SpriteSheet>("assets/sheet.json");

            var spawner = new Spawner(() =>
            {
                var asteroid = new GameObject();

                var transform = asteroid.Components.Add <TransformComponent>();

                var spriteRenderer        = asteroid.Components.Add <SpriteRenderComponent>();
                var sprite                = spriteSheet.Get(spriteNames[spriteIndex]);
                spriteIndex               = spriteIndex + 1 % spriteNames.Length;
                spriteRenderer.Sprite     = sprite;
                spriteRenderer.LayerIndex = (int)RenderLayers.Enemies;

                var bbox = asteroid.Components.Add <BoundingBoxComponent>();
                bbox.SetSize(sprite.Bounds.Size);
                collisionService.Add(bbox);

                var brain      = asteroid.Components.Add <AsteroidBrain>();
                brain.Display  = this.Game.Display;
                brain.OnDeath += o =>
                {
                    _killedAsteroids++;
                    _gameStats.IncreaseScore();

                    var explosion          = _explosionsSpawner.Spawn();
                    var explosionTransform = explosion.Components.Get <TransformComponent>();
                    explosionTransform.Local.Clone(transform.Local);
                    explosionTransform.World.Clone(transform.Local);
                };

                return(asteroid);
            }, asteroid =>
            {
                var transform = asteroid.Components.Get <TransformComponent>();

                transform.World.Reset();
                transform.Local.Reset();

                transform.Local.Position.X = MathUtils.Random.NextBool() ? 0 : this.Game.Display.Size.Width;
                transform.Local.Position.Y = MathUtils.Random.NextBool() ? 0 : this.Game.Display.Size.Height;

                var brain       = asteroid.Components.Get <AsteroidBrain>();
                var dir         = _player.Components.Get <TransformComponent>().Local.Position - transform.Local.Position;
                brain.Direction = Vector2.Normalize(dir);
                brain.Speed     = (float)MathUtils.Random.NextDouble(0.15, 0.5);

                //var w = (double)this.Game.Display.Size.Width;
                //var rx = MathUtils.Random.NextDouble(0, .4, .6, 1);
                //var tx = MathUtils.Normalize(rx, 0, 1, -1, 1);
                //transform.Local.Position.X = (float)(tx * w / 2.5 + w / 2);

                //var h = (double)this.Game.Display.Size.Height;
                //var ry = MathUtils.Random.NextDouble(0, .35, .65, 1);
                //var ty = MathUtils.Normalize(ry, 0, 1, -1, 1);
                //transform.Local.Position.Y = (float)(ty * h / 2.5 + h / 2);
            });

            spawner.Components.Add <TransformComponent>();

            return(spawner);
        }