Example #1
0
        public void Shoot()
        {
            var creationPosition = Position + Forward * 2;
            var scale            = Vector2.One * 0.2f;
            //bulletTexture, bulletTemplate, creationPosition, scale

            PhysicsObjectTemplate template = new PhysicsObjectTemplate
            {
                Position      = creationPosition,
                BodyTemplate  = bulletTemplate,
                Texture       = bulletTexture,
                PhysicsObject = new Laser(),
                Scale         = scale
            };

            var bulletRef = template.Create(scene).Item1.Convert <Laser>();

            bulletRef.Get(out var bullet);
            bullet.Scene  = scene;
            bullet.Source = this;
            bullet.Body.LinearVelocity = Body.LinearVelocity + Forward * 10;
            bullet.Body.IsBullet       = true;
            bullet.Body.OnCollision   += bullet.OnCollision;
            bullet.Damage        = 10;
            bullet.Body.Rotation = Rotation;
        }
        public void LoadContent(ContentManager content)
        {
            spriteBatchEffect = new BasicEffect(graphics);
            spriteBatchEffect.TextureEnabled = true;

            liveContent.ReadMetadata("Content.yaml");

            Texture2D shipTexture      = liveContent.GetTexture("Hulls/ship.png");
            Texture2D squareTexture    = liveContent.GetTexture("square.png");
            Texture2D asteroid1Texture = liveContent.GetTexture("Sprites/Asteroids/asteroid1.png");
            Texture2D asteroid2Texture = liveContent.GetTexture("Sprites/Asteroids/asteroid2.png");
            Texture2D asteroid3Texture = liveContent.GetTexture("Sprites/Asteroids/asteroid3.png");
            Texture2D asteroid4Texture = liveContent.GetTexture("Sprites/Asteroids/asteroid4.png");

            Texture2D[] asteroidTextures = { asteroid1Texture, asteroid2Texture, asteroid3Texture, asteroid4Texture };

            // Body templates
            bodyTemplates = PhysicsShapeLoader.LoadBodies(File.ReadAllText("./content/Bodies.xml"));

            // Scene setup

            var enemyShipRef = CreateShip(shipTexture, bodyTemplates["ship"], new Vector2(-5, 0), shipTexture, bodyTemplates["square"], 40);

            shipRef = CreateShip(shipTexture, bodyTemplates["ship"], new Vector2(0, 0), shipTexture, bodyTemplates["square"], 20);

            PhysicsObjectTemplate[] asteroidTemplates = new PhysicsObjectTemplate[4];
            for (int j = 0; j < 4; j++)
            {
                asteroidTemplates[j] = new PhysicsObjectTemplate
                {
                    BodyTemplate = bodyTemplates[$"asteroid{j+1}"],
                    Texture      = asteroidTextures[j]
                };
            }
            for (int i = 0; i < 200; i++)
            {
                var type     = (int)(Mathf.Bias(Mathf.Random(0, 1), 0.3f) * 4);
                var template = asteroidTemplates[type];

                float radius = 100;
                do
                {
                    template.Position = new Vector2(Mathf.Random(-radius, radius), Mathf.Random(-radius, radius));
                } while (template.Position.LengthSquared() < 100);

                var po = template.Create(this).Item2;
                po.Body.Rotation        = Mathf.Random(0, Mathf.TAU);
                po.Body.AngularVelocity = Mathf.Random(-1, 1) / po.Body.Mass;
                po.Body.LinearVelocity  = Mathf.Random(0, 0.1f) * Mathf.RandomUnit() / po.Body.Mass;
            }


            polygonEffect = new BasicEffect(graphics);
            polygonEffect.VertexColorEnabled = true;
        }
        CastRef <Ship> CreateShip(Texture2D shipTexture, BodyTemplate bodyTemplate, Vector2 position, Texture2D bulletTexture, BodyTemplate bulletTemplate, float health)
        {
            Ship ship = new Ship();

            ship.SetAssets(this, bulletTexture, bulletTemplate);
            ship.HealthAmount = health;

            PhysicsObjectTemplate template = new PhysicsObjectTemplate
            {
                Position      = position,
                BodyTemplate  = bodyTemplate,
                Texture       = shipTexture,
                PhysicsObject = ship
            };

            return(template.Create(this).Item1.Convert <Ship>());
        }