Esempio n. 1
0
    public override void Start()
    {
        // We setup our scene, main camera and viewport
        _viewport = GetSubsystem <Renderer>().GetViewport(0);
        _scene    = new Scene();
        _scene.CreateComponent <Octree>().SetSize(new BoundingBox(1, 100), 3);
        _viewport.Scene       = _scene;
        _camera               = _scene.CreateChild("Camera").CreateComponent <Camera>();
        _camera.Node.Position = new Vector3(50, 10, -1);
        _camera.Orthographic  = true;
        _camera.OrthoSize     = 26;
        _viewport.Camera      = _camera;

        // We create a sound source for the music and the music
        SoundSource musicSource = _scene.CreateComponent <SoundSource>();
        Sound       music       = Cache.Get <Sound>("music/Happy_Bee.ogg");

        music.SetLooped(true);
        musicSource.Play(music);
        musicSource.SetSoundType("Music");

        // We don't need a sound listener for the above, but we add one for the sounds and adjust the music gain
        Audio audioSystem = GetSubsystem <Audio>();

        audioSystem.SetListener(_camera.Node.CreateComponent <SoundListener>());
        audioSystem.SetMasterGain("Music", 0.3f);

        // We create a background node which is a child of the camera so it won't move relative to it
        Node           bg    = _camera.Node.CreateChild("Background");
        StaticSprite2D bgspr = bg.CreateComponent <StaticSprite2D>();

        bgspr.SetSprite(Cache.Get <Sprite2D>("scenarios/grasslands/bg.png"));
        bg.SetPosition(new Vector3(0, 0, 100));
        bg.SetScale2D(Vector2.One * 5);

        // We add a physics world so we can simulate physics, and enable CCD
        PhysicsWorld2D pw = _scene.CreateComponent <PhysicsWorld2D>();

        pw.SetContinuousPhysics(true);

        // We create a terrain, vehicle and cloud system
        _terrain = new Terrain(_scene);
        _vehicle = CreateVehicle(new Vector2(50, 10));
        _clouds  = new Clouds(50, 5, 40, 16, 40);

        // We subscribe to the PostUpdateEvent
        SubscribeToEvent <PostUpdateEvent>(PostUpdate);

        // If we're building a debug release, we draw debug data
        #if DEBUG
        DebugRenderer dbr = _scene.CreateComponent <DebugRenderer>();
        pw.SetDrawCenterOfMass(true); pw.SetDrawJoint(true); pw.SetDrawPair(true); pw.SetDrawShape(true);
        SubscribeToEvent <PostRenderUpdateEvent>(e => { _scene.GetComponent <PhysicsWorld2D>().DrawDebugGeometry(dbr, false); });
        #endif
    }
    public override void Start()
    {
        cache = GetSubsystem <ResourceCache>();
        input = GetSubsystem <Input>();
        Renderer renderer = GetSubsystem <Renderer>();

        graphics = GetSubsystem <Graphics>();
        viewport = renderer.GetViewport(0);

        scene = new Scene();
        scene.CreateComponent <Octree>().SetSize(new BoundingBox(1, 100), 3);
        viewport.Scene = scene;

        camera = scene.CreateChild("Camera").CreateComponent <Camera>();
        camera.Node.Position = new Vector3(15, 0, -1);
        camera.Orthographic  = true;
        camera.OrthoSize     = 26;
        viewport.Camera      = camera;


        Node bg    = camera.Node.CreateChild("bg");
        var  bgspr = bg.CreateComponent <StaticSprite2D>();

        bgspr.SetSprite(cache.GetResource <Sprite2D>("scenarios/grasslands/bg.png"));
        bg.SetPosition(new Vector3(0, 0, 100));
        bg.SetScale2D(Vector2.One * 5);


        chunkmat = cache.GetResource <Material>("_DBG/Unlit.xml");
        chunkmat.SetTexture(0, cache.GetResource <Texture2D>("scenarios/grasslands/ground.png"));

        surfMat = cache.GetResource <Material>("_DBG/UnlitAlpha.xml");
        surfMat.SetTexture(0, cache.GetResource <Texture2D>("scenarios/grasslands/surface.png"));
        //surfMat.SetTexture(0, cache.GetResource<Texture2D>("_DBG/dbg.png"));

        PhysicsWorld2D pw = scene.CreateComponent <PhysicsWorld2D>();

        pw.SetContinuousPhysics(true);


#if DEBUG
        scene.CreateComponent <DebugRenderer>();
        SubscribeToEvent <PostRenderUpdateEvent>(e => RenderDebug());
#endif

        SubscribeToEvent <UpdateEvent>(e => Update());



        // decors quick crappy hack
        decors.Add(GetSprite("scenarios/grasslands/Object/Bush (1).png"));
        decors.Add(GetSprite("scenarios/grasslands/Object/Bush (2).png"));
        decors.Add(GetSprite("scenarios/grasslands/Object/Bush (3).png"));
        decors.Add(GetSprite("scenarios/grasslands/Object/Bush (4).png"));

        decors.Add(GetSprite("scenarios/grasslands/Object/Mushroom_1.png"));
        decors.Add(GetSprite("scenarios/grasslands/Object/Mushroom_2.png"));
        decors.Add(GetSprite("scenarios/grasslands/Object/Stone.png"));
        decors.Add(GetSprite("scenarios/grasslands/Object/Sign_2.png"));

        decors.Add(GetSprite("scenarios/grasslands/Object/Tree_1.png"));
        decors.Add(GetSprite("scenarios/grasslands/Object/Tree_2.png"));
        decors.Add(GetSprite("scenarios/grasslands/Object/Tree_3.png"));

        foreach (Sprite2D d in decors)
        {
            d.SetHotSpot(new Vector2(0.5f, 0.1f));
        }


        Sprite2D crateSprite = GetSprite("scenarios/grasslands/Object/Crate.png");

        Sprite2D[] cloudSprites =
        {
            GetSprite("scenarios/cloud1.png"),
            GetSprite("scenarios/cloud2.png"),
            GetSprite("scenarios/cloud3.png"),
        };
        Random rng = new Random();
        for (int i = 0; i < chunksize * 200; i += chunksize)
        {
            GenerateChunk(i);


            //innefficient clouds :P
            var n = CreateSpriteNode(scene, cloudSprites[rng.Next(3)], 5, false);
            n.SetPosition(new Vector3(i + rng.Next(8), rng.Next(15) + 15, 15));
            clouds.Add(n);

            //boxes
            if (rng.Next(10) == 0)
            {
                var c = CreateSpriteNode(scene, crateSprite, 2, true);
                c.SetPosition(new Vector3(i + rng.Next(8), 20, -5));
                var box = c.CreateComponent <CollisionBox2D>();
                box.SetSize(.8f, .8f);
                box.SetDensity(0.1f);
                box.SetRestitution(0.2f);
                box.SetFriction(0.05f);
            }
        }



        //for (float i = 0; i < chunksize*200; i+=0.2f)
        //{
        //    CreateDecor(i);
        //}

        //foreach (CollisionChain2D col in chunks)
        //{
        //    col.SetEnabled(false);
        //}

        currentVehicle = CreateTruck(new Vector2(2, 3));
    }