Added terrible fog-of-war

This commit is contained in:
Anuken
2018-06-24 00:09:00 -04:00
parent 0267ad574f
commit 77a63a39ad
9 changed files with 123 additions and 23 deletions

View File

@@ -0,0 +1,81 @@
package io.anuke.mindustry.graphics;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.utils.Disposable;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.game.EventType.WorldLoadGraphicsEvent;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Events;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.graphics.Fill;
import static io.anuke.mindustry.Vars.*;
/**Used for rendering fog of war. A framebuffer is used for this.*/
public class FogRenderer implements Disposable{
private TextureRegion region = new TextureRegion();
private FrameBuffer buffer;
public FogRenderer(){
Events.on(WorldLoadGraphicsEvent.class, () -> {
dispose();
buffer = new FrameBuffer(Format.RGBA8888, world.width(), world.height(), false);
//clear buffer to black
buffer.begin();
Graphics.clear(Color.BLACK);
buffer.end();
});
}
public void draw(){
if(buffer == null) return;
float vw = Core.camera.viewportWidth * Core.camera.zoom;
float vh = Core.camera.viewportHeight * Core.camera.zoom;
float px = Core.camera.position.x -= vw/2f;
float py = Core.camera.position.y -= vh/2f;
float u = px / tilesize / world.width();
float v = py / tilesize / world.height();
float u2 = (px + vw)/ tilesize / world.width();
float v2 = (py + vh)/ tilesize / world.height();
Core.batch.getProjectionMatrix().setToOrtho2D(0, 0, world.width() * tilesize, world.height() * tilesize);
Draw.color(Color.WHITE);
buffer.begin();
Graphics.begin();
for(Player player : playerGroup.all()){
Fill.circle(player.x, player.y, 60f);
}
Graphics.end();
buffer.end();
region.setTexture(buffer.getColorBufferTexture());
//region.setRegion(0, 0, 1, 1);
region.setRegion(u, v2, u2, v);
Core.batch.setProjectionMatrix(Core.camera.combined);
Graphics.shader(Shaders.fog);
Graphics.begin();
// Core.batch.draw(buffer.getColorBufferTexture(), px + 50, py, 200, 200 * world.height()/(float)world.width());
Core.batch.draw(region, px, py, vw, vh);
Graphics.end();
Graphics.shader();
}
@Override
public void dispose() {
if(buffer != null) buffer.dispose();
}
}

View File

@@ -24,6 +24,7 @@ public class Shaders{
public static UnitBuild build;
public static MixShader mix;
public static Shader fullMix;
public static FogShader fog;
public static void init(){
outline = new Outline();
@@ -36,9 +37,16 @@ public class Shaders{
space = new Space();
build = new UnitBuild();
mix = new MixShader();
fog = new FogShader();
fullMix = new Shader("fullmix", "default");
}
public static class FogShader extends Shader{
public FogShader(){
super("fog", "default");
}
}
public static class MixShader extends Shader{
public Color color = new Color(Color.WHITE);