示例#1
0
        public (CastRef <PhysicsObject>, PhysicsObject) Create(IScene scene)
        {
            // Scale vertices of body template
            BodyTemplate bodyTemplateCopy = BodyTemplate.Scale(Scale);

            PhysicsObject physicsObject = PhysicsObject ?? new PhysicsObject();

            physicsObject.Texture = Texture;
            physicsObject.Scale   = Scale;

            CastRef <PhysicsObject> poref = scene.SetupPhysicsObject(physicsObject, bodyTemplateCopy);

            physicsObject.Body.Position = Position;
            physicsObject.Body.Rotation = Rotation;
            physicsObject.Depth         = Depth;

            physicsObject.Body.Tag         = physicsObject;
            physicsObject.Body.LocalCenter = Vector2.Zero;

            CalculateVertices(physicsObject);

            if (!Offset.HasValue)
            {
                physicsObject.Offset = new Vector2(Texture.Width * 0.5f, Texture.Height * 0.5f);
            }

            return(poref, physicsObject);
        }
        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;
        }
        public CastRef <PhysicsObject> SetupPhysicsObject(PhysicsObject physicsObject, BodyTemplate bodyTemplate)
        {
            var body = bodyTemplate.Create(physicsWorld);

            body.BodyType        = BodyType.Dynamic;
            body.SleepingAllowed = false;

            physicsObject.Body = body;


            var entityRef = SetupGameObject(physicsObject);
            var poRef     = new CastRef <PhysicsObject>(entityRef);

            poRef.EntityRef.RegisterRemovedCallback(PhysicsObjectDestroyed);

            physicsObjects.Add(poRef);

            return(poRef);
        }
 public void Destroy <T>(CastRef <T> castRef) where T : GameObject
 {
     gameObjects.Remove(castRef.EntityRef);
 }