From c9fa7356c2db8daacbeb7cf38ba4870965341bd7 Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 16 Sep 2021 12:40:35 -0400 Subject: [PATCH] Asteroid background improvements --- .../sprites/blocks/environment/empty.png | Bin 0 -> 116 bytes core/src/mindustry/content/Blocks.java | 8 +++- core/src/mindustry/core/Renderer.java | 40 +++++++++++++++++- core/src/mindustry/core/World.java | 13 +++--- .../src/mindustry/entities/comp/UnitComp.java | 1 + core/src/mindustry/game/Rules.java | 10 +++++ .../maps/planet/AsteroidGenerator.java | 14 +++--- core/src/mindustry/type/UnitType.java | 9 +++- .../world/blocks/environment/AirBlock.java | 1 + .../world/blocks/environment/EmptyFloor.java | 24 +++++++++++ .../world/blocks/environment/Floor.java | 2 + 11 files changed, 107 insertions(+), 15 deletions(-) create mode 100644 core/assets-raw/sprites/blocks/environment/empty.png create mode 100644 core/src/mindustry/world/blocks/environment/EmptyFloor.java diff --git a/core/assets-raw/sprites/blocks/environment/empty.png b/core/assets-raw/sprites/blocks/environment/empty.png new file mode 100644 index 0000000000000000000000000000000000000000..b76b9c46a9885cf93860fb48e9abb89255d98af1 GIT binary patch literal 116 zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANL}jKx9jP7LeL$-HD>V9@h) zaSVxQeS6N3k->nc!654YJafs~CPt&HuQ%+S!_0sJLTVL-811iiKBxx?d%F6$taD0e F0s!>X9F+h7 literal 0 HcmV?d00001 diff --git a/core/src/mindustry/content/Blocks.java b/core/src/mindustry/content/Blocks.java index 06155deab7..4340e5edaa 100644 --- a/core/src/mindustry/content/Blocks.java +++ b/core/src/mindustry/content/Blocks.java @@ -35,7 +35,7 @@ public class Blocks implements ContentList{ public static Block //environment - air, spawn, cliff, deepwater, water, taintedWater, deepTaintedWater, tar, slag, stone, craters, charr, sand, darksand, dirt, mud, ice, snow, darksandTaintedWater, space, + air, spawn, cliff, deepwater, water, taintedWater, deepTaintedWater, tar, slag, stone, craters, charr, sand, darksand, dirt, mud, ice, snow, darksandTaintedWater, space, empty, dacite, rhyolite, rhyoliteCrater, regolith, yellowStone, redIce, redmat, bluemat, stoneWall, dirtWall, sporeWall, iceWall, daciteWall, sporePine, snowPine, pine, shrubs, whiteTree, whiteTreeDead, sporeCluster, @@ -231,6 +231,12 @@ public class Blocks implements ContentList{ placeableOn = false; solid = true; variants = 0; + canShadow = false; + }}; + + empty = new EmptyFloor("empty"){{ + placeableOn = false; + solid = true; }}; stone = new Floor("stone"); diff --git a/core/src/mindustry/core/Renderer.java b/core/src/mindustry/core/Renderer.java index d4368ddf15..6e7eb14cbd 100644 --- a/core/src/mindustry/core/Renderer.java +++ b/core/src/mindustry/core/Renderer.java @@ -1,6 +1,7 @@ package mindustry.core; import arc.*; +import arc.assets.loaders.TextureLoader.*; import arc.files.*; import arc.graphics.*; import arc.graphics.Texture.*; @@ -310,8 +311,43 @@ public class Renderer implements ApplicationListener{ Events.fire(Trigger.postDraw); } - private void drawBackground(){ - //nothing to draw currently + protected void drawBackground(){ + if(state.rules.backgroundTexture != null){ + if(!assets.isLoaded(state.rules.backgroundTexture, Texture.class)){ + var file = assets.getFileHandleResolver().resolve(state.rules.backgroundTexture); + + //don't draw invalid/non-existent backgrounds. + if(!file.exists() || !file.extEquals("png")){ + return; + } + + var desc = assets.load(state.rules.backgroundTexture, Texture.class, new TextureParameter(){{ + wrapU = TextureWrap.mirroredRepeat; + wrapV = TextureWrap.mirroredRepeat; + magFilter = TextureFilter.linear; + minFilter = TextureFilter.linear; + }}); + + assets.finishLoadingAsset(desc); + } + + Texture tex = assets.get(state.rules.backgroundTexture, Texture.class); + Tmp.tr1.set(tex); + Tmp.tr1.u = 0f; + Tmp.tr1.v = 0f; + + float ratio = camera.width / camera.height; + float size = state.rules.backgroundScl; + + Tmp.tr1.u2 = size; + Tmp.tr1.v2 = size / ratio; + + float sx = (camera.position.x) / state.rules.backgroundSpeed, sy = (camera.position.y) / state.rules.backgroundSpeed; + + Tmp.tr1.scroll(sx + state.rules.backgroundOffsetX, -sy + state.rules.backgroundOffsetY); + + Draw.rect(Tmp.tr1, camera.position.x, camera.position.y, camera.width, camera.height); + } } void updateLandParticles(){ diff --git a/core/src/mindustry/core/World.java b/core/src/mindustry/core/World.java index 2cf042b10a..47d4c05475 100644 --- a/core/src/mindustry/core/World.java +++ b/core/src/mindustry/core/World.java @@ -8,6 +8,7 @@ import arc.math.geom.Geometry.*; import arc.struct.*; import arc.util.*; import arc.util.noise.*; +import mindustry.*; import mindustry.content.*; import mindustry.core.GameState.*; import mindustry.ctype.*; @@ -479,12 +480,14 @@ public class World{ //TODO optimize; this is very slow and called too often! public float getDarkness(int x, int y){ - int edgeBlend = 2; - float dark = 0; - int edgeDst = Math.min(x, Math.min(y, Math.min(Math.abs(x - (tiles.width - 1)), Math.abs(y - (tiles.height - 1))))); - if(edgeDst <= edgeBlend){ - dark = Math.max((edgeBlend - edgeDst) * (4f / edgeBlend), dark); + + if(Vars.state.rules.borderDarkness){ + int edgeBlend = 2; + int edgeDst = Math.min(x, Math.min(y, Math.min(Math.abs(x - (tiles.width - 1)), Math.abs(y - (tiles.height - 1))))); + if(edgeDst <= edgeBlend){ + dark = Math.max((edgeBlend - edgeDst) * (4f / edgeBlend), dark); + } } if(state.hasSector() && state.getSector().preset == null){ diff --git a/core/src/mindustry/entities/comp/UnitComp.java b/core/src/mindustry/entities/comp/UnitComp.java index 36207abd35..5a29d1d3d4 100644 --- a/core/src/mindustry/entities/comp/UnitComp.java +++ b/core/src/mindustry/entities/comp/UnitComp.java @@ -44,6 +44,7 @@ abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, I boolean spawnedByCore; double flag; + transient float shadowAlpha = -1f; transient Seq abilities = new Seq<>(0); transient float healTime; private transient float resupplyTime = Mathf.random(10f); diff --git a/core/src/mindustry/game/Rules.java b/core/src/mindustry/game/Rules.java index 51d9b86f72..ab67b3d0e3 100644 --- a/core/src/mindustry/game/Rules.java +++ b/core/src/mindustry/game/Rules.java @@ -117,8 +117,18 @@ public class Rules{ public @Nullable String modeName; /** Whether cores incinerate items when full, just like in the campaign. */ public boolean coreIncinerates = false; + /** If false, borders fade out into darkness. Only use with custom backgrounds!*/ + public boolean borderDarkness = true; /** special tags for additional info. */ public StringMap tags = new StringMap(); + /** path to background texture with extension (e.g. "sprites/space.png")*/ + public @Nullable String backgroundTexture; + /** background texture move speed scaling - bigger numbers mean slower movement */ + public float backgroundSpeed = 27000f; + /** background texture scaling factor */ + public float backgroundScl = 1f; + /** background UV offsets */ + public float backgroundOffsetX = 0.1f, backgroundOffsetY = 0.1f; /** Copies this ruleset exactly. Not efficient at all, do not use often. */ public Rules copy(){ diff --git a/core/src/mindustry/maps/planet/AsteroidGenerator.java b/core/src/mindustry/maps/planet/AsteroidGenerator.java index 714f619b93..fb912ee368 100644 --- a/core/src/mindustry/maps/planet/AsteroidGenerator.java +++ b/core/src/mindustry/maps/planet/AsteroidGenerator.java @@ -46,9 +46,9 @@ public class AsteroidGenerator extends BlankPlanetGenerator{ int sx = width/2, sy = height/2; rand = new Rand(seed); - pass((x, y) -> { - floor = Blocks.space; - }); + Floor background = Blocks.empty.asFloor(); + + tiles.eachTile(t -> t.setFloor(background)); //spawn asteroids asteroid(sx, sy, rand.random(30, 50)); @@ -70,7 +70,7 @@ public class AsteroidGenerator extends BlankPlanetGenerator{ //random noise stone pass((x, y) -> { - if(floor != Blocks.space){ + if(floor != background){ if(Ridged.noise2d(seed, x, y, foct, fper, 1f / fscl) - Ridged.noise2d(seed, x, y, 1, 1f, 5f)/2.7f > fmag){ floor = Blocks.stone; } @@ -79,12 +79,12 @@ public class AsteroidGenerator extends BlankPlanetGenerator{ //walls at insides pass((x, y) -> { - if(floor == Blocks.space || Ridged.noise2d(seed + 1, x, y, 4, 0.7f, 1f / 60f) > 0.45f || Mathf.within(x, y, sx, sy, 20 + Ridged.noise2d(seed, x, y, 3, 0.5f, 1f / 30f) * 6f)) return; + if(floor == background || Ridged.noise2d(seed + 1, x, y, 4, 0.7f, 1f / 60f) > 0.45f || Mathf.within(x, y, sx, sy, 20 + Ridged.noise2d(seed, x, y, 3, 0.5f, 1f / 30f) * 6f)) return; int radius = 6; for(int dx = x - radius; dx <= x + radius; dx++){ for(int dy = y - radius; dy <= y + radius; dy++){ - if(Mathf.within(dx, dy, x, y, radius + 0.0001f) && tiles.in(dx, dy) && tiles.getn(dx, dy).floor() == Blocks.space){ + if(Mathf.within(dx, dy, x, y, radius + 0.0001f) && tiles.in(dx, dy) && tiles.getn(dx, dy).floor() == background){ return; } } @@ -132,6 +132,8 @@ public class AsteroidGenerator extends BlankPlanetGenerator{ Schematics.placeLaunchLoadout(sx, sy); + state.rules.backgroundTexture = "sprites/space.png"; + state.rules.borderDarkness = false; state.rules.environment = Env.space; } diff --git a/core/src/mindustry/type/UnitType.java b/core/src/mindustry/type/UnitType.java index 4d74597c7c..4031332720 100644 --- a/core/src/mindustry/type/UnitType.java +++ b/core/src/mindustry/type/UnitType.java @@ -698,8 +698,15 @@ public class UnitType extends UnlockableContent{ } public void drawShadow(Unit unit){ - Draw.color(Pal.shadow); float e = Math.max(unit.elevation, visualElevation) * (1f - unit.drownTime); + float x = unit.x + shadowTX * e, y = unit.y + shadowTY * e; + Floor floor = world.floorWorld(x, y); + + float dest = floor.canShadow ? 1f : 0f; + //yes, this updates state in draw()... which isn't a problem, because I don't want it to be obvious anyway + unit.shadowAlpha = unit.shadowAlpha < 0 ? dest : Mathf.approachDelta(unit.shadowAlpha, dest, 0.11f); + Draw.color(Pal.shadow, Pal.shadow.a * unit.shadowAlpha); + Draw.rect(shadowRegion, unit.x + shadowTX * e, unit.y + shadowTY * e, unit.rotation - 90); Draw.color(); } diff --git a/core/src/mindustry/world/blocks/environment/AirBlock.java b/core/src/mindustry/world/blocks/environment/AirBlock.java index b100bbc4ef..e0eef67635 100644 --- a/core/src/mindustry/world/blocks/environment/AirBlock.java +++ b/core/src/mindustry/world/blocks/environment/AirBlock.java @@ -14,6 +14,7 @@ public class AirBlock extends Floor{ useColor = false; wall = this; needsSurface = false; + canShadow = false; } @Override diff --git a/core/src/mindustry/world/blocks/environment/EmptyFloor.java b/core/src/mindustry/world/blocks/environment/EmptyFloor.java new file mode 100644 index 0000000000..3b1aba9e82 --- /dev/null +++ b/core/src/mindustry/world/blocks/environment/EmptyFloor.java @@ -0,0 +1,24 @@ +package mindustry.world.blocks.environment; + +import mindustry.content.*; +import mindustry.world.*; + +public class EmptyFloor extends Floor{ + + public EmptyFloor(String name){ + super(name); + variants = 0; + canShadow = false; + } + + @Override + public void drawBase(Tile tile){ + //draws only edges, never itself + drawEdges(tile); + + Floor floor = tile.overlay(); + if(floor != Blocks.air && floor != this){ + floor.drawBase(tile); + } + } +} diff --git a/core/src/mindustry/world/blocks/environment/Floor.java b/core/src/mindustry/world/blocks/environment/Floor.java index b40b7e9667..660ea77070 100644 --- a/core/src/mindustry/world/blocks/environment/Floor.java +++ b/core/src/mindustry/world/blocks/environment/Floor.java @@ -59,6 +59,8 @@ public class Floor extends Block{ public Block wall = Blocks.air; /** Decoration block. Usually a rock. May be air. */ public Block decoration = Blocks.air; + /** Whether units can draw shadows over this. */ + public boolean canShadow = true; /** Whether this overlay needs a surface to be on. False for floating blocks, like spawns. */ public boolean needsSurface = true;