Files
Mindustry/core/src/mindustry/graphics/BlockRenderer.java

311 lines
11 KiB
Java
Raw Normal View History

2019-12-25 01:39:38 -05:00
package mindustry.graphics;
2019-12-25 01:39:38 -05:00
import arc.*;
import arc.graphics.*;
import arc.graphics.Texture.*;
import arc.graphics.g2d.*;
import arc.graphics.gl.*;
import arc.math.*;
2020-02-18 19:34:07 -05:00
import arc.struct.*;
2019-12-25 01:39:38 -05:00
import arc.util.*;
import mindustry.content.*;
import mindustry.game.EventType.*;
import mindustry.game.Teams.*;
2020-03-06 15:22:13 -05:00
import mindustry.gen.*;
2019-12-25 01:39:38 -05:00
import mindustry.ui.*;
import mindustry.world.*;
2020-04-22 17:23:30 -04:00
import mindustry.world.blocks.power.*;
2019-12-25 01:39:38 -05:00
import static arc.Core.camera;
import static mindustry.Vars.*;
2019-04-23 11:59:07 -04:00
public class BlockRenderer implements Disposable{
2018-07-12 20:37:14 -04:00
private final static int initialRequests = 32 * 32;
2019-03-02 23:06:14 -05:00
private final static int expandr = 9;
private final static Color shadowColor = new Color(0, 0, 0, 0.71f);
2018-07-12 20:37:14 -04:00
2018-12-28 16:05:49 -05:00
public final FloorRenderer floor = new FloorRenderer();
2018-07-12 20:37:14 -04:00
2020-04-22 15:19:39 -04:00
private Array<Tile> requests = new Array<>(initialRequests);
2020-04-22 12:47:29 -04:00
private int lastCamX, lastCamY, lastRangeX, lastRangeY;
private float brokenFade = 0f;
2020-04-22 16:06:26 -04:00
private FrameBuffer shadows = new FrameBuffer();
private FrameBuffer fog = new FrameBuffer();
2020-03-06 15:22:13 -05:00
private Array<Tilec> outArray2 = new Array<>();
private Array<Tile> shadowEvents = new Array<>();
2020-03-24 12:02:26 -04:00
private boolean displayStatus = false;
2018-07-12 20:37:14 -04:00
public BlockRenderer(){
2018-12-26 13:22:31 -05:00
Events.on(WorldLoadEvent.class, event -> {
shadowEvents.clear();
2018-07-20 13:59:00 -04:00
lastCamY = lastCamX = -99; //invalidate camera position so blocks get updated
shadows.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
shadows.resize(world.width(), world.height());
shadows.begin();
Core.graphics.clear(Color.white);
Draw.proj().setOrtho(0, 0, shadows.getWidth(), shadows.getHeight());
Draw.color(shadowColor);
2020-03-03 19:29:41 -05:00
for(Tile tile : world.tiles){
if(tile.block().hasShadow){
Fill.rect(tile.x + 0.5f, tile.y + 0.5f, 1, 1);
}
}
Draw.flush();
Draw.color();
shadows.end();
fog.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
fog.resize(world.width(), world.height());
fog.begin();
Core.graphics.clear(Color.white);
Draw.proj().setOrtho(0, 0, fog.getWidth(), fog.getHeight());
2020-03-03 19:29:41 -05:00
for(Tile tile : world.tiles){
float darkness = world.getDarkness(tile.x, tile.y);
2020-02-18 19:34:07 -05:00
2020-03-03 19:29:41 -05:00
if(darkness > 0){
Draw.color(0f, 0f, 0f, Math.min((darkness + 0.5f) / 4f, 1f));
Fill.rect(tile.x + 0.5f, tile.y + 0.5f, 1, 1);
}
}
Draw.flush();
Draw.color();
fog.end();
2018-07-20 13:59:00 -04:00
});
Events.on(TileChangeEvent.class, event -> {
shadowEvents.add(event.tile);
2018-12-26 13:22:31 -05:00
int avgx = (int)(camera.position.x / tilesize);
2019-04-08 09:03:18 -04:00
int avgy = (int)(camera.position.y / tilesize);
int rangex = (int)(camera.width / tilesize / 2) + 2;
int rangey = (int)(camera.height / tilesize / 2) + 2;
2018-12-26 13:22:31 -05:00
if(Math.abs(avgx - event.tile.x) <= rangex && Math.abs(avgy - event.tile.y) <= rangey){
lastCamY = lastCamX = -99; //invalidate camera position so blocks get updated
}
2018-07-20 13:59:00 -04:00
});
2018-07-12 20:37:14 -04:00
}
public void drawFog(){
float ww = world.width() * tilesize, wh = world.height() * tilesize;
2019-04-08 09:03:18 -04:00
float x = camera.position.x + tilesize / 2f, y = camera.position.y + tilesize / 2f;
float u = (x - camera.width / 2f) / ww,
v = (y - camera.height / 2f) / wh,
u2 = (x + camera.width / 2f) / ww,
v2 = (y + camera.height / 2f) / wh;
Tmp.tr1.set(fog.getTexture());
Tmp.tr1.set(u, v2, u2, v);
Draw.shader(Shaders.fog);
Draw.rect(Tmp.tr1, camera.position.x, camera.position.y, camera.width, camera.height);
Draw.shader();
}
2019-11-04 23:47:51 -05:00
public void drawDestroyed(){
if(!Core.settings.getBool("destroyedblocks")) return;
if(control.input.isPlacing() || control.input.isBreaking()){
brokenFade = Mathf.lerpDelta(brokenFade, 1f, 0.1f);
}else{
brokenFade = Mathf.lerpDelta(brokenFade, 0f, 0.1f);
}
if(brokenFade > 0.001f){
for(BrokenBlock block : state.teams.get(player.team()).brokenBlocks){
2019-09-30 22:12:02 -04:00
Block b = content.block(block.block);
if(!camera.bounds(Tmp.r1).grow(tilesize * 2f).overlaps(Tmp.r2.setSize(b.size * tilesize).setCenter(block.x * tilesize + b.offset(), block.y * tilesize + b.offset()))) continue;
2019-12-11 15:13:45 -05:00
Draw.alpha(0.33f * brokenFade);
2019-10-07 17:17:01 -04:00
Draw.mixcol(Color.white, 0.2f + Mathf.absin(Time.globalTime(), 6f, 0.2f));
2019-10-01 23:46:40 -04:00
Draw.rect(b.icon(Cicon.full), block.x * tilesize + b.offset(), block.y * tilesize + b.offset(), b.rotate ? block.rotation * 90 : 0f);
2019-09-30 22:12:02 -04:00
}
Draw.reset();
}
}
public void drawShadows(){
if(!shadowEvents.isEmpty()){
Draw.flush();
2019-04-02 17:08:22 -04:00
shadows.begin();
Draw.proj().setOrtho(0, 0, shadows.getWidth(), shadows.getHeight());
2019-01-27 23:26:07 -05:00
for(Tile tile : shadowEvents){
//clear it first
Draw.color(Color.white);
Fill.rect(tile.x + 0.5f, tile.y + 0.5f, 1, 1);
//then draw the shadow
Draw.color(!tile.block().hasShadow ? Color.white : shadowColor);
Fill.rect(tile.x + 0.5f, tile.y + 0.5f, 1, 1);
}
2019-01-27 18:09:59 -05:00
Draw.flush();
Draw.color();
shadows.end();
shadowEvents.clear();
2019-01-18 09:19:42 -05:00
2020-04-14 20:29:59 -04:00
Draw.proj(camera);
2019-01-18 09:19:42 -05:00
}
float ww = world.width() * tilesize, wh = world.height() * tilesize;
2019-04-08 09:03:18 -04:00
float x = camera.position.x + tilesize / 2f, y = camera.position.y + tilesize / 2f;
float u = (x - camera.width / 2f) / ww,
v = (y - camera.height / 2f) / wh,
u2 = (x + camera.width / 2f) / ww,
v2 = (y + camera.height / 2f) / wh;
Tmp.tr1.set(shadows.getTexture());
Tmp.tr1.set(u, v2, u2, v);
Draw.shader(Shaders.fog);
Draw.rect(Tmp.tr1, camera.position.x, camera.position.y, camera.width, camera.height);
2018-12-29 13:06:17 -05:00
Draw.shader();
}
2019-04-08 09:03:18 -04:00
/** Process all blocks to draw. */
2018-07-12 20:37:14 -04:00
public void processBlocks(){
2020-03-24 12:02:26 -04:00
displayStatus = Core.settings.getBool("blockstatus");
2018-07-12 20:37:14 -04:00
2018-12-26 13:22:31 -05:00
int avgx = (int)(camera.position.x / tilesize);
int avgy = (int)(camera.position.y / tilesize);
2018-07-20 13:59:00 -04:00
2019-04-08 09:03:18 -04:00
int rangex = (int)(camera.width / tilesize / 2) + 3;
int rangey = (int)(camera.height / tilesize / 2) + 3;
if(avgx == lastCamX && avgy == lastCamY && lastRangeX == rangex && lastRangeY == rangey){
2018-07-20 13:59:00 -04:00
return;
}
2018-07-12 20:37:14 -04:00
int minx = Math.max(avgx - rangex - expandr, 0);
int miny = Math.max(avgy - rangey - expandr, 0);
int maxx = Math.min(world.width() - 1, avgx + rangex + expandr);
int maxy = Math.min(world.height() - 1, avgy + rangey + expandr);
for(int x = minx; x <= maxx; x++){
for(int y = miny; y <= maxy; y++){
boolean expanded = (Math.abs(x - avgx) > rangex || Math.abs(y - avgy) > rangey);
Tile tile = world.rawTile(x, y);
2019-01-15 21:21:28 -05:00
Block block = tile.block();
2018-07-12 20:37:14 -04:00
2020-03-05 18:05:59 -05:00
if(block != Blocks.air && tile.isCenter() && block.cacheLayer == CacheLayer.normal){
2019-01-15 21:21:28 -05:00
if(block.expanded || !expanded){
2020-04-22 15:19:39 -04:00
requests.add(tile);
}
2019-01-27 18:09:59 -05:00
2020-04-22 15:19:39 -04:00
if(tile.entity != null && tile.entity.power() != null && tile.entity.power().links.size > 0){
for(Tilec other : tile.entity.getPowerConnections(outArray2)){
2020-04-22 17:23:30 -04:00
if(other.block() instanceof PowerNode){ //TODO need a generic way to render connections!
2020-04-22 15:19:39 -04:00
requests.add(other.tile());
}
}
2018-07-12 20:37:14 -04:00
}
}
}
}
2018-07-20 13:59:00 -04:00
lastCamX = avgx;
lastCamY = avgy;
lastRangeX = rangex;
lastRangeY = rangey;
2018-07-12 20:37:14 -04:00
}
2020-04-22 15:19:39 -04:00
public void drawBlocks(){
drawDestroyed();
for(int i = 0; i < requests.size; i++){
Tile tile = requests.items[i];
Block block = tile.block();
Tilec entity = tile.entity;
block.drawBase(tile);
if(entity != null){
if(entity.damaged()){
entity.drawCracks();
}
if(entity.team() != player.team()){
entity.drawTeam();
}
entity.drawLight();
if(displayStatus && block.consumes.any()){
entity.drawStatus();
}
}
}
}
/*
2018-07-12 20:37:14 -04:00
public void drawBlocks(Layer stopAt){
int startIdx = iterateidx;
2018-07-12 20:37:14 -04:00
for(; iterateidx < requestidx; iterateidx++){
BlockRequest request = requests.get(iterateidx);
2018-07-12 20:37:14 -04:00
if(request.layer.ordinal() > stopAt.ordinal()){
2018-07-12 20:37:14 -04:00
break;
}
if(request.layer == Layer.power){
if(iterateidx - startIdx > 0 && request.tile.pos() == requests.get(iterateidx - 1).tile.pos()){
continue;
}
}
2018-07-12 20:37:14 -04:00
Block block = request.tile.block();
2020-03-24 12:02:26 -04:00
boolean isEnd = (request.layer == Layer.block && block.layer == null) || request.layer == block.layer;
if(request.layer == Layer.block){
2020-03-06 15:22:13 -05:00
block.drawBase(request.tile);
if(request.tile.entity != null && request.tile.entity.damaged()){
2020-03-06 15:22:13 -05:00
request.tile.entity.drawCracks();
2019-04-16 12:15:06 -04:00
}
2020-03-08 12:12:06 -04:00
if(block.synthetic() && request.tile.entity != null && request.tile.team() != player.team()){
2020-03-06 15:22:13 -05:00
request.tile.entity.drawTeam();
}
2019-11-10 16:18:47 -05:00
2020-03-08 12:12:06 -04:00
}else if(request.layer == Layer.lights && request.tile.entity != null){
2020-03-06 15:22:13 -05:00
request.tile.entity.drawLight();
}else if(request.layer == block.layer){
block.drawLayer(request.tile);
}else if(request.layer == block.layer2){
block.drawLayer2(request.tile);
2018-07-12 20:37:14 -04:00
}
2020-03-24 12:02:26 -04:00
if(isEnd && request.tile.entity != null && displayStatus && block.consumes.any()){
request.tile.entity.drawStatus();
}
2018-07-12 20:37:14 -04:00
}
}
private void addRequest(Tile tile, Layer layer){
if(requestidx >= requests.size){
requests.add(new BlockRequest());
}
BlockRequest r = requests.get(requestidx);
if(r == null){
requests.set(requestidx, r = new BlockRequest());
}
r.tile = tile;
r.layer = layer;
requestidx++;
2020-04-22 15:19:39 -04:00
}*/
2018-07-12 20:37:14 -04:00
2019-04-23 11:59:07 -04:00
@Override
public void dispose(){
shadows.dispose();
fog.dispose();
shadows = fog = null;
floor.dispose();
}
}