简体   繁体   中英

Darken Libgdx Tiled Map

I am new to Libgdx and am doing a game prototype with a certain features. One of which I am unable to achieve at the moment is to darken the map such that my character can "glow" in the dark map.

Before Blending Output:变暗前

Currently, I load my map using TiledMap and TiledMapRenderer .

Load my map:

TiledMap map = new TmxMapLoader().load("map/ascent/ascent.tmx");
float unitScale = 2f;
OrthogonalTiledMapRenderer tiledMapRenderer = new OrthogonalTiledMapRenderer(map, unitScale);

In my render() method:

@Override
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Run the playMovement Method
        player1Movement(playerHashMap.get(p1), delta);

        // Draw the Map
        tiledMapRenderer.setView(camera);
        tiledMapRenderer.render();

        // Set the brightness and blend function
        float lightness = .1f;
        Gdx.gl.glClearColor(lightness,lightness,lightness,1f);
        tiledMapRenderer.getBatch().enableBlending();
        tiledMapRenderer.getBatch().setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE);

        // Begin Batch drawing
        tiledMapRenderer.getBatch().begin();

        // Draw the light
        tiledMapRenderer.getBatch().draw(light, p1.box.x - 25f, p1.box.y - 25f, 80, 80);

        // Draw the player
        playerHashMap.forEach((player, input) -> player.draw(tiledMapRenderer.getBatch()));

        tiledMapRenderer.getBatch().end();
    }

My attempt at blending:我的尝试

I tried blending and is unable to darken my surroundings map.

Qn: Is there any way to darken a TiledMap?

I would like to thank you all in advance for your advice for your help !

You can get the batch that renders the map and apply a tint before drawing. The tint would darken by reducing all the RGB values (or lightening etc).

final float darken =0.5f;
tiledMapRenderer.getBatch().setColor(darken,darken,darken,1f);

and then you don't need to set the clear color and blend. Yes, setColor should be called setTint !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM