Animated water

This commit is contained in:
Anuken
2018-03-13 22:34:52 -04:00
parent b822a02d41
commit 3056e9e79c
12 changed files with 230 additions and 41 deletions

View File

@@ -44,7 +44,7 @@ import static io.anuke.ucore.core.Core.camera;
public class Renderer extends RendererModule{
private final static float shieldHitDuration = 18f;
public Surface shadowSurface, shieldSurface, indicatorSurface;
public Surface shadowSurface, shieldSurface, indicatorSurface, waterSurface;
private int targetscale = baseCameraScale;
private Texture background = new Texture("sprites/background.png");
@@ -83,6 +83,7 @@ public class Renderer extends RendererModule{
shieldSurface = Graphics.createSurface(scale);
indicatorSurface = Graphics.createSurface(scale);
pixelSurface = Graphics.createSurface(scale);
waterSurface = Graphics.createSurface(scale);
}
public void setPixelate(boolean pixelate){

View File

@@ -114,7 +114,7 @@ public class BlockRenderer{
Draw.color();
Graphics.end();
drawCache(1, crangex, crangey);
drawCache(DrawLayer.walls, crangex, crangey);
Graphics.begin();
Arrays.sort(requests.items, 0, requestidx);
@@ -165,12 +165,13 @@ public class BlockRenderer{
//render the entire map
if(cache == null || cache.length != chunksx || cache[0].length != chunksy){
cache = new int[chunksx][chunksy][2];
cache = new int[chunksx][chunksy][DrawLayer.values().length];
for(int x = 0; x < chunksx; x++){
for(int y = 0; y < chunksy; y++){
cacheChunk(x, y, true);
cacheChunk(x, y, false);
for(DrawLayer layer : DrawLayer.values()){
for(int x = 0; x < chunksx; x++){
for(int y = 0; y < chunksy; y++){
cacheChunk(x, y, layer);
}
}
}
}
@@ -182,7 +183,11 @@ public class BlockRenderer{
int crangex = (int)(camera.viewportWidth * camera.zoom / (chunksize * tilesize))+1;
int crangey = (int)(camera.viewportHeight * camera.zoom / (chunksize * tilesize))+1;
drawCache(0, crangex, crangey);
DrawLayer[] layers = DrawLayer.values();
for(int i = 0; i < layers.length - 1; i ++) {
drawCache(layers[i], crangex, crangey);
}
Graphics.begin();
@@ -224,11 +229,11 @@ public class BlockRenderer{
}
void drawCache(int layer, int crangex, int crangey){
void drawCache(DrawLayer layer, int crangex, int crangey){
Gdx.gl.glEnable(GL20.GL_BLEND);
cbatch.setProjectionMatrix(Core.camera.combined);
cbatch.beginDraw();
layer.begin(cbatch);
for(int x = -crangex; x <= crangex; x++){
for(int y = -crangey; y <= crangey; y++){
int worldx = Mathf.scl(camera.position.x, chunksize * tilesize) + x;
@@ -237,14 +242,14 @@ public class BlockRenderer{
if(!Mathf.inBounds(worldx, worldy, cache))
continue;
cbatch.drawCache(cache[worldx][worldy][layer]);
cbatch.drawCache(cache[worldx][worldy][layer.ordinal()]);
}
}
cbatch.endDraw();
layer.end(cbatch);
}
void cacheChunk(int cx, int cy, boolean floor){
void cacheChunk(int cx, int cy, DrawLayer layer){
if(cbatch == null){
createBatch();
}
@@ -256,18 +261,21 @@ public class BlockRenderer{
for(int tiley = cy * chunksize; tiley < (cy + 1) * chunksize; tiley++){
Tile tile = world.tile(tilex, tiley);
if(tile == null) continue;
if(floor){
if(!(tile.block() instanceof StaticBlock)){
tile.floor().draw(tile);
}
}else if(tile.block() instanceof StaticBlock){
if(tile.floor().drawLayer == layer && tile.block().drawLayer != DrawLayer.walls){
tile.floor().draw(tile);
}else if(tile.floor().drawLayer.ordinal() < layer.ordinal() && tile.block().drawLayer != DrawLayer.walls){
tile.floor().drawNonLayer(tile);
}
if(tile.block().drawLayer == layer && layer == DrawLayer.walls){
tile.block().draw(tile);
}
}
}
Graphics.popBatch();
cbatch.end();
cache[cx][cy][floor ? 0 : 1] = cbatch.getLastCache();
cache[cx][cy][layer.ordinal()] = cbatch.getLastCache();
}
public void clearTiles(){

View File

@@ -0,0 +1,51 @@
package io.anuke.mindustry.graphics;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.graphics.CacheBatch;
import io.anuke.ucore.graphics.Draw;
import static io.anuke.mindustry.Vars.renderer;
public enum DrawLayer {
water{
@Override
public void begin(CacheBatch batch){
batch.setProjectionMatrix(Core.camera.combined);
Graphics.useBatch(batch.drawBatch());
Graphics.begin();
Graphics.surface(renderer.waterSurface);
}
@Override
public void end(CacheBatch batch){
Graphics.surface();
Graphics.end();
Graphics.popBatch();
Graphics.shader(Shaders.water);
Graphics.begin();
Draw.rect(renderer.waterSurface.texture(), Core.camera.position.x, Core.camera.position.y,
Core.camera.viewportWidth * Core.camera.zoom, -Core.camera.viewportHeight * Core.camera.zoom);
Graphics.end();
Graphics.shader();
}
},
normal,
walls;
public void begin(CacheBatch batch){
batch.setProjectionMatrix(Core.camera.combined);
Graphics.useBatch(batch.drawBatch());
Graphics.begin();
}
public void end(CacheBatch batch){
Graphics.end();
Graphics.popBatch();
}
}

View File

@@ -1,5 +1,6 @@
package io.anuke.mindustry.graphics;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.FloatArray;
@@ -12,9 +13,25 @@ import io.anuke.ucore.scene.ui.layout.Unit;
public class Shaders{
public static final Outline outline = new Outline();
public static final Shield shield = new Shield();
public static final Water water = new Water();
private static final Vector2 vec = new Vector2();
public static class Water extends Shader{
public Water(){
super("water", "default");
}
@Override
public void apply(){
shader.setUniformf("camerapos", Core.camera.position.x + Core.camera.zoom, Core.camera.position.y);
shader.setUniformf("screensize", Gdx.graphics.getWidth() / Core.cameraScale * Core.camera.zoom,
Gdx.graphics.getHeight() / Core.cameraScale * Core.camera.zoom);
shader.setUniformf("time", Timers.time());
}
}
public static class Outline extends Shader{
public Color color = new Color();
public float lighten = 0f;

View File

@@ -7,6 +7,7 @@ import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.graphics.DrawLayer;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.NetEvents;
@@ -79,6 +80,8 @@ public class Block extends BaseBlock {
public boolean expanded = false;
/**Max of timers used.*/
public int timers = 0;
/**Draw layer. Only used for 'cached' rendering.*/
public DrawLayer drawLayer = DrawLayer.normal;
/**Layer to draw extra stuff on.*/
public Layer layer = null;
/**Extra layer to draw extra extra stuff on.*/
@@ -219,6 +222,8 @@ public class Block extends BaseBlock {
tile.entity.update();
}
}
public void drawNonLayer(Tile tile){}
public void drawShadow(Tile tile){

View File

@@ -1,5 +1,6 @@
package io.anuke.mindustry.world.blocks;
import io.anuke.mindustry.graphics.DrawLayer;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.resource.ItemStack;
@@ -31,6 +32,7 @@ public class Blocks{
solid = true;
liquidDrop = Liquid.water;
liquid = true;
drawLayer = DrawLayer.water;
}},
water = new Floor("water"){{
@@ -38,6 +40,7 @@ public class Blocks{
solid = true;
liquidDrop = Liquid.water;
liquid = true;
drawLayer = DrawLayer.water;
}},
lava = new Floor("lava"){

View File

@@ -67,8 +67,8 @@ public class ProductionBlocks{
health = 200;
inputLiquid = Liquid.water;
outputLiquid = Liquid.cryofluid;
inputItem = Item.titanium;
liquidPerItem = 70f;
inputItem = Item.quartz;
liquidPerItem = 50f;
itemCapacity = 50;
powerUse = 0.1f;
size = 2;

View File

@@ -19,41 +19,53 @@ public class Floor extends Block{
super(name);
variants = 3;
}
@Override
public void drawNonLayer(Tile tile){
MathUtils.random.setSeed(tile.id());
drawEdges(tile, true);
}
@Override
public void draw(Tile tile){
MathUtils.random.setSeed(tile.id());
Draw.rect(variants > 0 ? (name() + MathUtils.random(1, variants)) : name(), tile.worldx(), tile.worldy());
if(blend)
drawEdges(tile, false);
}
private void drawEdges(Tile tile, boolean sameLayer){
if(!blend) return;
for(int dx = -1; dx <= 1; dx ++){
for(int dy = -1; dy <= 1; dy ++){
if(dx == 0 && dy == 0) continue;
Tile other = world.tile(tile.x+dx, tile.y+dy);
if(other == null) continue;
Block floor = other.floor();
if(floor.id <= this.id || !blends.test(floor)) continue;
if(floor.id <= this.id || !blends.test(floor) || (floor.drawLayer.ordinal() > this.drawLayer.ordinal() && !sameLayer) ||
(sameLayer && floor.drawLayer == this.drawLayer)) continue;
TextureRegion region = Draw.hasRegion(floor.name() + "edge") ? Draw.region(floor.name() + "edge") :
Draw.region(floor.edge + "edge");
Draw.region(floor.edge + "edge");
int sx = -dx*8+2, sy = -dy*8+2;
int x = Mathf.clamp(sx, 0, 12);
int y = Mathf.clamp(sy, 0, 12);
int w = Mathf.clamp(sx+8, 0, 12) - x, h = Mathf.clamp(sy+8, 0, 12) - y;
float rx = Mathf.clamp(dx*8, 0, 8-w);
float ry = Mathf.clamp(dy*8, 0, 8-h);
tempRegion.setTexture(region.getTexture());
tempRegion.setRegion(region.getRegionX()+x, region.getRegionY()+y+h, w, -h);
Draw.crect(tempRegion, tile.worldx()-4 + rx, tile.worldy()-4 + ry, w, h);
}
}

View File

@@ -1,11 +1,13 @@
package io.anuke.mindustry.world.blocks.types;
import io.anuke.mindustry.graphics.DrawLayer;
import io.anuke.mindustry.world.Block;
public class StaticBlock extends Block{
public StaticBlock(String name) {
super(name);
drawLayer = DrawLayer.walls;
}
}