Пример #1
0
    public Terrain(Scene scene)
    {
        _scene = scene;

        // We load the materials, the ones in this example only use built in techniques, we use diffuse for the ground for performance
        _surfaceMaterial = Cache.Get <Material>("Materials/UnlitAlpha.xml");
        _surfaceMaterial.SetTexture(0, Cache.Get <Texture2D>("Scenarios/grasslands/surface.png"));
        _chunkMaterial = Cache.Get <Material>("Materials/Unlit.xml");
        _chunkMaterial.SetTexture(0, Cache.Get <Texture2D>("Scenarios/grasslands/ground.png"));

        // We create and populate a library with the decorations
        Func <string, Sprite2D> GetSprite = file => Cache.Get <Sprite2D>($"Scenarios/grasslands/Object/{file}");

        _decorationLib = new DecorationLibrary <Sprite2D>(
            new [] { GetSprite("Bush (1).png"), GetSprite("Bush (2).png"), GetSprite("Bush (3).png"), GetSprite("Bush (4).png") },
            new [] { GetSprite("Mushroom_1.png"), GetSprite("Mushroom_2.png"), GetSprite("Stone.png"), GetSprite("Sign_2.png") },
            new [] { GetSprite("Tree_1.png"), GetSprite("Tree_2.png"), GetSprite("Tree_3.png") }
            );

        // We setup the hotstop/origin of each sprite to be next to its bottom
        foreach (Sprite2D sprite in _decorationLib.GetAllResources())
        {
            sprite.SetHotSpot(new Vector2(0.5f, 0.1f));
        }

        // We generate the chunks and add some boxes randomly
        Sprite2D crateSprite = Cache.Get <Sprite2D>("Scenarios/grasslands/Object/Crate.png");

        for (int i = 0; i < Chunksize * chunksToGenerate; i += Chunksize)
        {
            GenerateChunk(i);

            // Crates
            if (_rng.Next(100) < PercentageOfChunksWithCrates)
            {
                Node crateNode = AtomicMain.CreateSpriteNode(crateSprite, 3);
                crateNode.SetPosition(new Vector3(i + _rng.Next(8), 20, -5));
                CollisionBox2D crateCollider = crateNode.CreateComponent <CollisionBox2D>();
                crateCollider.SetSize(0.76f, 0.76f);
                crateCollider.SetDensity(1.0f);
                crateCollider.SetRestitution(0.6f);
                crateCollider.SetFriction(0.4f);
            }
        }
    }
Пример #2
0
        void CreateScene()
        {
            scene = new Scene();
            scene.CreateComponent <Octree>();
            scene.CreateComponent <DebugRenderer>();
            // Create camera node
            CameraNode = scene.CreateChild("Camera");
            // Set camera's position
            CameraNode.Position = new Vector3(0.0f, 5.0f, -10.0f);

            Camera camera = CameraNode.CreateComponent <Camera>();

            camera.Orthographic = true;

            var graphics = Graphics;

            camera.OrthoSize = graphics.Height * 0.05f;
            camera.Zoom      = 1.5f * Math.Min(graphics.Width / 1280.0f, graphics.Height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.5) is set for full visibility at 1280x800 resolution)

            // Create 2D physics world component
            PhysicsWorld2D physicsWorld = scene.CreateComponent <PhysicsWorld2D>();

            physicsWorld.DrawJoint = (true);

            // Create ground
            Node groundNode = scene.CreateChild("Ground");
            // Create 2D rigid body for gound
            RigidBody2D groundBody = groundNode.CreateComponent <RigidBody2D>();
            // Create edge collider for ground
            CollisionEdge2D groundShape = groundNode.CreateComponent <CollisionEdge2D>();

            groundShape.SetVertices(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            const float y        = 15.0f;
            RigidBody2D prevBody = groundBody;

            for (uint i = 0; i < NumObjects; ++i)
            {
                Node node = scene.CreateChild("RigidBody");

                // Create rigid body
                RigidBody2D body = node.CreateComponent <RigidBody2D>();
                body.BodyType = BodyType2D.Dynamic;

                // Create box
                CollisionBox2D box = node.CreateComponent <CollisionBox2D>();
                // Set friction
                box.Friction = 0.2f;
                // Set mask bits.
                box.MaskBits = 0xFFFF & ~0x0002;

                if (i == NumObjects - 1)
                {
                    node.Position       = new Vector3(1.0f * i, y, 0.0f);
                    body.AngularDamping = 0.4f;
                    box.SetSize(3.0f, 3.0f);
                    box.Density      = 100.0f;
                    box.CategoryBits = 0x0002;
                }
                else
                {
                    node.Position = new Vector3(0.5f + 1.0f * i, y, 0.0f);
                    box.SetSize(1.0f, 0.25f);
                    box.Density      = 20.0f;
                    box.CategoryBits = 0x0001;
                }

                ConstraintRevolute2D joint = node.CreateComponent <ConstraintRevolute2D>();
                joint.OtherBody        = prevBody;
                joint.Anchor           = new Vector2(i, y);
                joint.CollideConnected = false;

                prevBody = body;
            }

            ConstraintRope2D constraintRope = groundNode.CreateComponent <ConstraintRope2D>();

            constraintRope.OtherBody       = prevBody;
            constraintRope.OwnerBodyAnchor = new Vector2(0.0f, y);
            constraintRope.MaxLength       = NumObjects - 1.0f + 0.01f;
        }