From b15b396935e7b349077bdb7c90534403aa42b786 Mon Sep 17 00:00:00 2001 From: josua Date: Tue, 10 Sep 2019 12:37:31 +1000 Subject: [PATCH] Started adding RGB light sources, cleaned up some code for faded tiles. --- src/shootergame/display/DisplayRender.java | 5 + .../display/lighting/LightingManager.java | 159 ++++++++++++++++++ .../transparent/TransparentObjects.java | 1 - src/shootergame/entity/Entity.java | 6 + src/shootergame/entity/EntityBullet.java | 5 +- src/shootergame/entity/EntityParticle.java | 1 - src/shootergame/entity/EntityVertical.java | 8 + .../entity/particle/ParticleBlood.java | 10 +- .../entity/particle/ParticleBreak.java | 7 + .../entity/particle/ParticleSmoke.java | 7 +- .../entity/particle/ParticleSpark.java | 1 + .../entity/particle/ParticleWater.java | 5 +- .../entity/player/EntityPlayer.java | 14 +- src/shootergame/input/KeyCallback.java | 17 +- src/shootergame/tiles/Tile.java | 11 ++ src/shootergame/tiles/TileDirt.java | 13 +- src/shootergame/tiles/TileFlat.java | 12 +- src/shootergame/tiles/TileFlatFaded.java | 22 +++ src/shootergame/tiles/TileStone.java | 13 +- src/shootergame/tiles/TileTree.java | 10 ++ src/shootergame/tiles/TileVertical.java | 2 + src/shootergame/tiles/TileWall.java | 5 +- src/shootergame/tiles/TileWater.java | 1 + src/shootergame/util/math/TileState.java | 8 +- src/shootergame/util/math/map/Map2D.java | 31 ++++ src/shootergame/util/math/vec/Vec2i.java | 7 +- src/shootergame/util/math/vec/Vec3i.java | 4 + src/shootergame/world/World.java | 1 - src/shootergame/world/chunk/Chunk.java | 57 ++++++- src/shootergame/world/chunk/ChunkEmpty.java | 46 +++++ src/shootergame/world/layer/Layer.java | 19 +++ .../world/layer/layergen/LayerGen.java | 3 +- .../world/layer/layergen/LayerGenCaves.java | 19 ++- .../world/layer/layergen/LayerGenEarth.java | 15 +- .../layer/layergen/LayerGenLavaCaves.java | 37 ++++ 35 files changed, 517 insertions(+), 65 deletions(-) create mode 100644 src/shootergame/display/lighting/LightingManager.java create mode 100644 src/shootergame/tiles/TileFlatFaded.java create mode 100644 src/shootergame/world/layer/layergen/LayerGenLavaCaves.java diff --git a/src/shootergame/display/DisplayRender.java b/src/shootergame/display/DisplayRender.java index 621311f..2a39ede 100644 --- a/src/shootergame/display/DisplayRender.java +++ b/src/shootergame/display/DisplayRender.java @@ -16,6 +16,7 @@ import org.lwjgl.opengl.GL; import org.lwjgl.system.MemoryStack; import shootergame.Main; +import shootergame.display.lighting.LightingManager; import shootergame.display.transparent.TransparentObjects; import shootergame.entity.player.EntityPlayer; import shootergame.init.Textures; @@ -82,8 +83,12 @@ public class DisplayRender GlHelpers.rotate(camera.angle.x, 0, 0, 1); GlHelpers.translate(-camera.pos.x, -camera.pos.y, -camera.pos.z); + // Process all the light sources + LightingManager.update(); + // Render the world and the player Main.world.render(camera); + player.chunk = Main.world.getLayer().getChunk(player.pos); player.doRender(player.pos, camera); // Render the sorted transparent objects diff --git a/src/shootergame/display/lighting/LightingManager.java b/src/shootergame/display/lighting/LightingManager.java new file mode 100644 index 0000000..84a1ce1 --- /dev/null +++ b/src/shootergame/display/lighting/LightingManager.java @@ -0,0 +1,159 @@ +package shootergame.display.lighting; + +import shootergame.Main; +import shootergame.display.Camera; +import shootergame.entity.Entity; +import shootergame.entity.player.EntityPlayer; +import shootergame.util.math.MathHelpers; +import shootergame.util.math.TileState; +import shootergame.util.math.map.Map2DElement; +import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; +import shootergame.world.chunk.Chunk; +import shootergame.world.layer.Layer; + +public class LightingManager +{ + @Deprecated + public static void markDirty() { + } + + public static void update() + { + // Do nothing if nothing has changed + //if(!isDirty) return; + //isDirty = false; + + int r = Camera.camera.renderDistance; + Layer layer = Main.world.getLayer(); + Vec3d light_clear = layer.layergen.getLightLevel(); + EntityPlayer player = Main.player; + + // Loop over all the loaded blocks and reset the light level + for(int x=-r*16;x mce : layer.chunks) + { + if(mce.pos.squareDistance(new Vec2i((int)(player.pos.x/16), (int)(player.pos.y/16))) < r) + { + // Loop over all the entities + Chunk chunk = mce.o; + for(Entity e : chunk.entities) + { + // Does this entity emit light + if(e.emitsLight) + { + // Create some light + addLightToTiles(layer, new Vec2i( + MathHelpers.floor(e.pos.x), + MathHelpers.floor(e.pos.y)), + e.getLightLevel()); + } + } + + // Loop over all the tiles + for(int x=0;x<16;x++) { + for(int y=0;y<16;y++) + { + // Get the tile position + Vec2i tpos = new Vec2i(x + mce.pos.x * 16, y + mce.pos.y * 16); + int tid = tpos.getId(Chunk.CHUNK_SIZE); + + // Get the front and back tiles + TileState fts = chunk.getFrontTile(tid); + TileState bts = chunk.getBackTile(tid); + + // Do any of these emit light + if(fts.tile.emitsLight || bts.tile.emitsLight) + { + // Calculate the light given off by the tile + Vec3d light_tile = chunk.getLightLevel(tid); + Vec3d light_tile2 = light_tile.copy(); + Vec3d ftsl = fts.tile.getLightLevel(fts); + Vec3d btsl = fts.tile.getLightLevel(bts); + if(ftsl.x > light_tile.x) light_tile.x = ftsl.x; + if(ftsl.y > light_tile.y) light_tile.y = ftsl.y; + if(ftsl.z > light_tile.z) light_tile.z = ftsl.z; + if(btsl.x > light_tile.x) light_tile.x = btsl.x; + if(btsl.y > light_tile.y) light_tile.y = btsl.y; + if(btsl.z > light_tile.z) light_tile.z = btsl.z; + + // Has the light level changed; add light to this tile + if(!light_tile.equal(light_tile2)) { + addLightToTiles(layer, tpos, light_tile); + } + } + } + } + } + } + + // Does the player emit light + if(player.emitsLight) { + addLightToTiles(layer, new Vec2i( + MathHelpers.floor(player.pos.x), + MathHelpers.floor(player.pos.y)), + player.getLightLevel()); + } + } + + private static void addLightToTiles(Layer layer, Vec2i lpos, Vec3d light) + { + // Get the light pos id + int lid = lpos.getId(Chunk.CHUNK_SIZE); + + // Get the light dissipation amount + Chunk chunk = layer.getChunk(lpos); + TileState bt = chunk.getBackTile(lid); + TileState ft = chunk.getFrontTile(lid); + Vec3d light_dissipation = bt.tile.getLightDissipation(bt); + Vec3d light_dissipation2 = ft.tile.getLightDissipation(ft); + if(light_dissipation2.x > light_dissipation.x) light_dissipation.x = light_dissipation2.x; + if(light_dissipation2.y > light_dissipation.y) light_dissipation.y = light_dissipation2.y; + if(light_dissipation2.z > light_dissipation.z) light_dissipation.z = light_dissipation2.z; + + // Set the light dissipation + light = light.subtract(light_dissipation); + if(light.x < 0) light.x = 0; + if(light.y < 0) light.y = 0; + if(light.z < 0) light.z = 0; + + if(light.x == 0 && light.y == 0 && light.z == 0) { + return; + } + + // Calculate the light level + Vec3d light_tile = chunk.getLightLevel(lid); + if( + light.x <= light_tile.x && + light.y <= light_tile.y && + light.z <= light_tile.z) { + return; + } + + // Merge the light and the light tile values + if(light.x > light_tile.x) light_tile.x = light.x; + if(light.y > light_tile.y) light_tile.y = light.y; + if(light.z > light_tile.z) light_tile.z = light.z; + chunk.setLightLevel(light_tile, lid); + + // Get all the adjacent positions of the light tiles to flow onto + Vec2i positions[] = { + new Vec2i(lpos.x+1, lpos.y), + new Vec2i(lpos.x-1, lpos.y), + new Vec2i(lpos.x, lpos.y+1), + new Vec2i(lpos.x, lpos.y-1) + }; + + // Add the light to all the adjacent positions + for(Vec2i position : positions) { + addLightToTiles(layer, position, light); + } + } +} diff --git a/src/shootergame/display/transparent/TransparentObjects.java b/src/shootergame/display/transparent/TransparentObjects.java index e8ea311..c4a9e88 100644 --- a/src/shootergame/display/transparent/TransparentObjects.java +++ b/src/shootergame/display/transparent/TransparentObjects.java @@ -1,7 +1,6 @@ package shootergame.display.transparent; import shootergame.display.Camera; -import shootergame.tiles.Tile; import shootergame.util.math.TileState; import shootergame.util.math.vec.Vec2d; diff --git a/src/shootergame/entity/Entity.java b/src/shootergame/entity/Entity.java index 28cc89f..6a721c8 100644 --- a/src/shootergame/entity/Entity.java +++ b/src/shootergame/entity/Entity.java @@ -12,6 +12,7 @@ import shootergame.util.math.MathHelpers; import shootergame.util.math.TileState; import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -29,6 +30,7 @@ public class Entity implements ITransparentObject public boolean crossUnWalkable = true; public boolean goThroughSolid = true; protected static final Random rand = new Random(); + public boolean emitsLight = false; public Entity(Vec2d pos, double angle) { @@ -214,4 +216,8 @@ public class Entity implements ITransparentObject public Vec2d getRenderOffset(TileState state) { return new Vec2d(0, 0); } + + public Vec3d getLightLevel() { + return new Vec3d(0, 0, 0); + } } diff --git a/src/shootergame/entity/EntityBullet.java b/src/shootergame/entity/EntityBullet.java index dfde378..ec5dabe 100644 --- a/src/shootergame/entity/EntityBullet.java +++ b/src/shootergame/entity/EntityBullet.java @@ -116,9 +116,12 @@ public class EntityBullet extends EntityParticle public void render(Vec2d pos, Camera camera) { // Set the colour - GlHelpers.color3(1, 0.8, 0.3); + Vec3d light = chunk.getLightLevel(new Vec2i( + MathHelpers.floor(pos.x), MathHelpers.floor(pos.y))); + GlHelpers.color3(1 * light.x, 0.8 * light.y, 0.3 * light.z); // Call super super.render(pos, camera); + GlHelpers.color3(1, 1, 1); } } diff --git a/src/shootergame/entity/EntityParticle.java b/src/shootergame/entity/EntityParticle.java index 4b63c9e..4af6f5c 100644 --- a/src/shootergame/entity/EntityParticle.java +++ b/src/shootergame/entity/EntityParticle.java @@ -63,7 +63,6 @@ public class EntityParticle extends Entity // Pop the matrix, remove the colour, and enable textures GlHelpers.enableTexture2d(); - GlHelpers.color3(1, 1, 1); GlHelpers.popMatrix(); } } diff --git a/src/shootergame/entity/EntityVertical.java b/src/shootergame/entity/EntityVertical.java index 0113795..569610d 100644 --- a/src/shootergame/entity/EntityVertical.java +++ b/src/shootergame/entity/EntityVertical.java @@ -1,9 +1,13 @@ package shootergame.entity; import shootergame.display.Camera; +import shootergame.util.gl.GlHelpers; import shootergame.util.gl.VerticalRender; import shootergame.util.gl.texture.TextureReference; +import shootergame.util.math.MathHelpers; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; public class EntityVertical extends Entity { @@ -22,6 +26,10 @@ public class EntityVertical extends Entity @Override public void render(Vec2d pos, Camera camera) { + Vec3d light = chunk.getLightLevel(new Vec2i( + MathHelpers.floor(pos.x), MathHelpers.floor(pos.y))); + GlHelpers.color3(light.x, light.y, light.z); this.render(pos, camera, tex, size); + GlHelpers.color3(1, 1, 1); } } diff --git a/src/shootergame/entity/particle/ParticleBlood.java b/src/shootergame/entity/particle/ParticleBlood.java index 9fb49a7..7309fc6 100644 --- a/src/shootergame/entity/particle/ParticleBlood.java +++ b/src/shootergame/entity/particle/ParticleBlood.java @@ -5,8 +5,11 @@ import java.util.Random; import shootergame.display.Camera; import shootergame.entity.EntityParticle; import shootergame.util.gl.GlHelpers; +import shootergame.util.math.MathHelpers; import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -57,13 +60,18 @@ public class ParticleBlood extends EntityParticle @Override public void render(Vec2d pos, Camera camera) { + // Get the light level + Vec3d light = chunk.getLightLevel(new Vec2i( + MathHelpers.floor(pos.x), MathHelpers.floor(pos.y))); + // Set some settings GlHelpers.pushMatrix(); - GlHelpers.color3(r_color, 0, 0); + GlHelpers.color3(r_color * light.x, 0, 0); GlHelpers.translate(0, 0, height); // Call super super.render(pos, camera); + GlHelpers.color3(1, 1, 1); GlHelpers.popMatrix(); } } diff --git a/src/shootergame/entity/particle/ParticleBreak.java b/src/shootergame/entity/particle/ParticleBreak.java index fbc69e0..1640618 100644 --- a/src/shootergame/entity/particle/ParticleBreak.java +++ b/src/shootergame/entity/particle/ParticleBreak.java @@ -6,9 +6,12 @@ import shootergame.entity.EntityVertical; import shootergame.util.gl.GlHelpers; import shootergame.util.gl.texture.IHasTexture; import shootergame.util.gl.texture.TextureReference; +import shootergame.util.math.MathHelpers; import shootergame.util.math.TileState; import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -66,9 +69,13 @@ public class ParticleBreak extends EntityVertical @Override public void render(Vec2d pos, Camera camera, TextureReference tex, Vec2d size) { + Vec3d light = chunk.getLightLevel(new Vec2i( + MathHelpers.floor(pos.x), MathHelpers.floor(pos.y))); + GlHelpers.color3(light.x, light.y, light.z); GlHelpers.pushMatrix(); GlHelpers.translate(0, 0, height); super.render(pos, camera, tex, size); + GlHelpers.color3(1, 1, 1); GlHelpers.popMatrix(); } } diff --git a/src/shootergame/entity/particle/ParticleSmoke.java b/src/shootergame/entity/particle/ParticleSmoke.java index edcf4fd..7da3bb9 100644 --- a/src/shootergame/entity/particle/ParticleSmoke.java +++ b/src/shootergame/entity/particle/ParticleSmoke.java @@ -5,8 +5,11 @@ import shootergame.display.Camera; import shootergame.entity.EntityVertical; import shootergame.init.Textures; import shootergame.util.gl.GlHelpers; +import shootergame.util.math.MathHelpers; import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -33,7 +36,9 @@ public class ParticleSmoke extends EntityVertical if(opacity <= 0) return; GlHelpers.pushMatrix(); GlHelpers.translate(0, 0, height); - GlHelpers.color4(1, 1, 1, opacity); + Vec3d light = chunk.getLightLevel(new Vec2i( + MathHelpers.floor(pos.x), MathHelpers.floor(pos.y))); + GlHelpers.color4(light.x, light.y, light.z, opacity); super.render(pos, camera); GlHelpers.color4(1, 1, 1, 1); GlHelpers.popMatrix(); diff --git a/src/shootergame/entity/particle/ParticleSpark.java b/src/shootergame/entity/particle/ParticleSpark.java index 04ffa93..f4f55c6 100644 --- a/src/shootergame/entity/particle/ParticleSpark.java +++ b/src/shootergame/entity/particle/ParticleSpark.java @@ -42,6 +42,7 @@ public class ParticleSpark extends EntityParticle // Call super super.render(pos, camera); + GlHelpers.color3(1, 1, 1); GlHelpers.popMatrix(); } diff --git a/src/shootergame/entity/particle/ParticleWater.java b/src/shootergame/entity/particle/ParticleWater.java index 4cd68f3..7790a2f 100644 --- a/src/shootergame/entity/particle/ParticleWater.java +++ b/src/shootergame/entity/particle/ParticleWater.java @@ -8,6 +8,7 @@ import shootergame.util.gl.GlHelpers; import shootergame.util.math.MathHelpers; import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; import shootergame.util.math.vec.Vec3d; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -48,7 +49,9 @@ public class ParticleWater extends EntityParticle public void render(Vec2d pos, Camera camera) { GlHelpers.pushMatrix(); GlHelpers.translate(0, 0, height); - GlHelpers.color4(0, 0, 1, 0.4); + Vec3d light = chunk.getLightLevel(new Vec2i( + MathHelpers.floor(pos.x), MathHelpers.floor(pos.y))); + GlHelpers.color4(0, 0, light.z, 0.4); super.render(pos, camera); GlHelpers.color4(1, 1, 1, 1); GlHelpers.popMatrix(); diff --git a/src/shootergame/entity/player/EntityPlayer.java b/src/shootergame/entity/player/EntityPlayer.java index 3d60183..d96d14a 100644 --- a/src/shootergame/entity/player/EntityPlayer.java +++ b/src/shootergame/entity/player/EntityPlayer.java @@ -9,7 +9,6 @@ import shootergame.entity.EntityBullet; import shootergame.entity.EntityInventory; import shootergame.entity.EntityItem; import shootergame.entity.EntityVertical; -import shootergame.init.Items; import shootergame.init.Textures; import shootergame.inventory.Inventory; import shootergame.util.gl.GlHelpers; @@ -17,6 +16,8 @@ import shootergame.util.gl.texture.TextureReference; import shootergame.util.math.ItemStack; import shootergame.util.math.MathHelpers; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -57,10 +58,16 @@ public class EntityPlayer extends EntityVertical implements EntityAlive, EntityI isSolid = true; goThroughSolid = false; crossUnWalkable = false; + emitsLight = true; inventory = new Inventory(6); } + @Override + public Vec3d getLightLevel() { + return new Vec3d(1, 1, 1); + } + @Override public void tick(Chunk chunk, Layer layer) { @@ -149,6 +156,11 @@ public class EntityPlayer extends EntityVertical implements EntityAlive, EntityI GlHelpers.pushMatrix(); GlHelpers.translate(0, 0, height); + // Set the colour due to the lighting + Vec3d light = chunk.getLightLevel(new Vec2i( + MathHelpers.floor(pos.x), MathHelpers.floor(pos.y))); + GlHelpers.color3(light.x, light.y, light.z); + // Moving if(MOVE_BACKWARD || MOVE_FORWARD || moving) super.render(pos, camera, Textures.ENTITY_PLAYER_MOVING, size); diff --git a/src/shootergame/input/KeyCallback.java b/src/shootergame/input/KeyCallback.java index f107585..a27152b 100644 --- a/src/shootergame/input/KeyCallback.java +++ b/src/shootergame/input/KeyCallback.java @@ -1,6 +1,21 @@ package shootergame.input; -import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_1; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_2; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_3; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_4; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_5; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_6; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_A; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_D; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_E; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_ENTER; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_Q; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT_SHIFT; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_S; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_W; +import static org.lwjgl.glfw.GLFW.GLFW_RELEASE; import org.lwjgl.glfw.GLFWKeyCallbackI; diff --git a/src/shootergame/tiles/Tile.java b/src/shootergame/tiles/Tile.java index 8310bb2..49e5a24 100644 --- a/src/shootergame/tiles/Tile.java +++ b/src/shootergame/tiles/Tile.java @@ -7,6 +7,7 @@ import shootergame.entity.Entity; import shootergame.util.math.TileState; import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -19,6 +20,8 @@ public class Tile implements ITransparentObject public double tileHitbox = 0; public double slowness = 0; public boolean unbreakable = false; + protected Vec3d light_dissipation = new Vec3d(1/12.0, 1/12.0, 1/12.0); + public boolean emitsLight = true; public Tile(String id) { this.id = id; @@ -62,4 +65,12 @@ public class Tile implements ITransparentObject public void onActivated(Layer layer, Vec2i tpos, Entity entity, TileState state) { } + + public Vec3d getLightDissipation(TileState state) { + return light_dissipation; + } + + public Vec3d getLightLevel(TileState state) { + return new Vec3d(0, 0, 0); + } } diff --git a/src/shootergame/tiles/TileDirt.java b/src/shootergame/tiles/TileDirt.java index 0689ef0..c7879f5 100644 --- a/src/shootergame/tiles/TileDirt.java +++ b/src/shootergame/tiles/TileDirt.java @@ -1,23 +1,12 @@ package shootergame.tiles; -import shootergame.display.Camera; import shootergame.init.Textures; -import shootergame.util.gl.GlHelpers; -import shootergame.util.math.TileState; -import shootergame.util.math.vec.Vec2d; -public class TileDirt extends TileFlat +public class TileDirt extends TileFlatFaded { public TileDirt(String id) { super(id, Textures.TILE_DIRT); } - - @Override - public void render(Vec2d pos, Camera camera, TileState state) { - GlHelpers.color4(1, 1, 1, (Byte.MAX_VALUE - (double)state.meta) / Byte.MAX_VALUE); - super.render(pos, camera, state); - GlHelpers.color4(1, 1, 1, 1); - } } \ No newline at end of file diff --git a/src/shootergame/tiles/TileFlat.java b/src/shootergame/tiles/TileFlat.java index 73df6e2..39655d5 100644 --- a/src/shootergame/tiles/TileFlat.java +++ b/src/shootergame/tiles/TileFlat.java @@ -6,23 +6,26 @@ import shootergame.util.gl.texture.IHasTexture; import shootergame.util.gl.texture.TextureReference; import shootergame.util.math.TileState; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec3d; public class TileFlat extends Tile implements IHasTexture { private TextureReference tex; + protected boolean rotates = false; + private static final Vec3d default_tile_color = new Vec3d(1, 1, 1); public TileFlat(String id, TextureReference tex) { super(id); this.tex = tex; } - @Override - public void render(Vec2d pos, Camera camera, TileState state) + public void render(Vec2d pos, Camera camera, TileState state, Vec3d color) { // Call super super.render(pos, camera, state); // Render the tile + GlHelpers.color3(state.light.x*color.x, state.light.y*color.y, state.light.z*color.z); GlHelpers.begin(); tex.texCoord(1, 1); GlHelpers.vertex3(pos.x+0, pos.y+0, 0); tex.texCoord(0, 1); GlHelpers.vertex3(pos.x+1, pos.y+0, 0); @@ -30,6 +33,11 @@ public class TileFlat extends Tile implements IHasTexture tex.texCoord(1, 0); GlHelpers.vertex3(pos.x+0, pos.y+1, 0); GlHelpers.end(); } + + @Override + public void render(Vec2d pos, Camera camera, TileState state) { + this.render(pos, camera, state, default_tile_color); + } @Override public TextureReference getTexture() { diff --git a/src/shootergame/tiles/TileFlatFaded.java b/src/shootergame/tiles/TileFlatFaded.java new file mode 100644 index 0000000..79ec8bf --- /dev/null +++ b/src/shootergame/tiles/TileFlatFaded.java @@ -0,0 +1,22 @@ +package shootergame.tiles; + +import shootergame.display.Camera; +import shootergame.util.gl.texture.TextureReference; +import shootergame.util.math.TileState; +import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec3d; + +public class TileFlatFaded extends TileFlat +{ + + public TileFlatFaded(String id, TextureReference tex) { + super(id, tex); + } + + @Override + public void render(Vec2d pos, Camera camera, TileState state) { + double fade_amount = (Byte.MAX_VALUE - (double)state.meta) / Byte.MAX_VALUE; + super.render(pos, camera, state, new Vec3d(fade_amount, fade_amount, fade_amount)); + } + +} diff --git a/src/shootergame/tiles/TileStone.java b/src/shootergame/tiles/TileStone.java index e8bd00e..8a81601 100644 --- a/src/shootergame/tiles/TileStone.java +++ b/src/shootergame/tiles/TileStone.java @@ -1,23 +1,12 @@ package shootergame.tiles; -import shootergame.display.Camera; import shootergame.init.Textures; -import shootergame.util.gl.GlHelpers; -import shootergame.util.math.TileState; -import shootergame.util.math.vec.Vec2d; -public class TileStone extends TileFlat +public class TileStone extends TileFlatFaded { public TileStone(String id) { super(id, Textures.TILE_STONE); } - - @Override - public void render(Vec2d pos, Camera camera, TileState state) { - GlHelpers.color4(1, 1, 1, (Byte.MAX_VALUE - (double)state.meta) / Byte.MAX_VALUE); - super.render(pos, camera, state); - GlHelpers.color4(1, 1, 1, 1); - } } diff --git a/src/shootergame/tiles/TileTree.java b/src/shootergame/tiles/TileTree.java index 5435696..666a291 100644 --- a/src/shootergame/tiles/TileTree.java +++ b/src/shootergame/tiles/TileTree.java @@ -1,10 +1,13 @@ package shootergame.tiles; import shootergame.init.Textures; +import shootergame.util.math.TileState; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec3d; public class TileTree extends TileVertical { + private static final Vec3d light_dissipation = new Vec3d(1/4.0, 1/4.0, 1/4.0); public TileTree(String id) { super(id, Textures.TILE_TREE, new Vec2d(1, 4)); @@ -14,5 +17,12 @@ public class TileTree extends TileVertical this.tileSolid = true; this.tileHitbox = 0.3; } + + @Override + public Vec3d getLightDissipation(TileState state) { + return light_dissipation; + } + + } diff --git a/src/shootergame/tiles/TileVertical.java b/src/shootergame/tiles/TileVertical.java index 7418510..8eeb6c6 100644 --- a/src/shootergame/tiles/TileVertical.java +++ b/src/shootergame/tiles/TileVertical.java @@ -1,6 +1,7 @@ package shootergame.tiles; import shootergame.display.Camera; +import shootergame.util.gl.GlHelpers; import shootergame.util.gl.VerticalRender; import shootergame.util.gl.texture.IHasTexture; import shootergame.util.gl.texture.TextureReference; @@ -23,6 +24,7 @@ public class TileVertical extends Tile implements IHasTexture @Override public void render(Vec2d pos, Camera camera, TileState state) { super.render(pos, camera, state); + GlHelpers.color3(state.light.x, state.light.y, state.light.z); VerticalRender.render(pos, camera, tex, size); } diff --git a/src/shootergame/tiles/TileWall.java b/src/shootergame/tiles/TileWall.java index 7404c2a..6619090 100644 --- a/src/shootergame/tiles/TileWall.java +++ b/src/shootergame/tiles/TileWall.java @@ -1,6 +1,7 @@ package shootergame.tiles; import shootergame.init.Textures; +import shootergame.util.math.vec.Vec3d; public class TileWall extends TileFlat { @@ -11,6 +12,8 @@ public class TileWall extends TileFlat this.tileWalkable = false; this.tileSolid = true; this.tileHitbox = 1; + + this.light_dissipation = new Vec3d(1/4.0, 1/4.0, 1/4.0); } - + } diff --git a/src/shootergame/tiles/TileWater.java b/src/shootergame/tiles/TileWater.java index 29431b2..3fb2f75 100644 --- a/src/shootergame/tiles/TileWater.java +++ b/src/shootergame/tiles/TileWater.java @@ -17,6 +17,7 @@ public class TileWater extends TileFlat public TileWater(String id) { super(id, Textures.TILE_WATER); this.slowness = 0.5; + this.rotates = true; } @Override diff --git a/src/shootergame/util/math/TileState.java b/src/shootergame/util/math/TileState.java index b8333a9..89aba3c 100644 --- a/src/shootergame/util/math/TileState.java +++ b/src/shootergame/util/math/TileState.java @@ -2,7 +2,7 @@ package shootergame.util.math; import shootergame.init.Tiles; import shootergame.tiles.Tile; -import shootergame.util.math.vec.Vec3i; +import shootergame.util.math.vec.Vec3d; public class TileState { @@ -10,7 +10,7 @@ public class TileState public Tile tile; public byte meta; - public Vec3i light = new Vec3i(0, 0, 0); + public Vec3d light = new Vec3d(0, 0, 0); public TileState(Tile tile, byte meta) { this.tile = tile; @@ -21,7 +21,7 @@ public class TileState this(tile, (byte)meta); } - public TileState withLightLevel(Vec3i light) { + public TileState withLightLevel(Vec3d light) { TileState ts = new TileState(tile, meta); ts.light = light; return ts; @@ -29,7 +29,7 @@ public class TileState public TileState withLightLevel(int r, int g, int b) { TileState ts = new TileState(tile, meta); - ts.light = new Vec3i(r, g, b); + ts.light = new Vec3d(r, g, b); return ts; } diff --git a/src/shootergame/util/math/map/Map2D.java b/src/shootergame/util/math/map/Map2D.java index cc4c207..6b2f53e 100644 --- a/src/shootergame/util/math/map/Map2D.java +++ b/src/shootergame/util/math/map/Map2D.java @@ -8,6 +8,7 @@ import shootergame.util.math.vec.Vec2i; public class Map2D implements Iterable> { private ArrayList> elements; + private Map2DElement element_last = null; private IMap2D map2d_i; public Map2D(IMap2D map2d_i) @@ -19,11 +20,19 @@ public class Map2D implements Iterable> public void set(Vec2i pos, T o) { + if(element_last != null) { + if(element_last.pos.equal(pos)) { + element_last.o = o; + return; + } + } + // Loop over the elements for(Map2DElement e : this.elements) { // Set the object if these positions are the same if(e.pos.equal(pos)) { + element_last = e; e.o = o; return; } @@ -32,15 +41,23 @@ public class Map2D implements Iterable> // Create an element and add it Map2DElement e = new Map2DElement(pos, o); this.elements.add(e); + element_last = e; } public T get(Vec2i pos) { + if(element_last != null) { + if(element_last.pos.equal(pos)) { + return element_last.o; + } + } + // Loop over the elements for(Map2DElement e : this.elements) { // Send back the object if these positions are the same if(e.pos.equal(pos)) { + element_last = e; return e.o; } } @@ -51,6 +68,12 @@ public class Map2D implements Iterable> public void remove(Vec2i pos) { + if(element_last != null) { + if(element_last.pos.equal(pos)) { + this.elements.remove(element_last); + } + } + // Loop over the elements for(int i=0;i implements Iterable> this.elements.remove(e); } } + + element_last = null; } public boolean contains(Vec2i pos) { + if(element_last != null) { + if(element_last.pos.equal(pos)) { + return true; + } + } + // Loop over the elements for(int i=0;i public abstract void generateChunk(Chunk chunk, Layer layer, long seed, Random rand, Vec2i pos); public abstract void spawnEntities(Layer layer, Random rand); public abstract TileState getTileDestroyed(); - public abstract Vec3i getLightLevel(); + public abstract Vec3d getLightLevel(); @Override public Chunk getEmpty(Vec2i pos) { diff --git a/src/shootergame/world/layer/layergen/LayerGenCaves.java b/src/shootergame/world/layer/layergen/LayerGenCaves.java index 713d332..64e2299 100644 --- a/src/shootergame/world/layer/layergen/LayerGenCaves.java +++ b/src/shootergame/world/layer/layergen/LayerGenCaves.java @@ -13,7 +13,6 @@ import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2i; import shootergame.util.math.vec.Vec3d; -import shootergame.util.math.vec.Vec3i; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -23,13 +22,20 @@ public class LayerGenCaves extends LayerGen @Override public void generateChunk(Chunk chunk, Layer layer, long seed, Random rand, Vec2i c_pos) { - // Is there going to be a portal in this chunk + // Is there going to be a portal up in this chunk boolean portal = RandomHelpers.randrange(rand, 10) == 0; Vec2i portal_pos = null; if(portal) portal_pos = new Vec2i( RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.mx), RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.my)); + // Is there going to be a portal down in this chunk + boolean portal_down = RandomHelpers.randrange(rand, 10) == 0; + Vec2i portal_down_pos = null; + if(portal_down) portal_down_pos = new Vec2i( + RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.mx), + RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.my)); + // Get some noise generators OpenSimplexNoise noisegen_n = new OpenSimplexNoise(seed); @@ -59,6 +65,11 @@ public class LayerGenCaves extends LayerGen chunk.setFrontTile(new TileState(Tiles.LADDER_UP, (short)0), portal_pos); } + if(portal_down) { + chunk.setBackTile(new TileState(Tiles.PORTAL_DOWN, (short)2), portal_down_pos); + chunk.setFrontTile(Tiles.LADDER.getDefaultState(), portal_down_pos); + } + Vec2i chest_pos = new Vec2i( RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.mx), RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.my)); @@ -104,8 +115,8 @@ public class LayerGenCaves extends LayerGen } @Override - public Vec3i getLightLevel() { - return new Vec3i(0, 0, 0); + public Vec3d getLightLevel() { + return new Vec3d(0, 0, 0); } } diff --git a/src/shootergame/world/layer/layergen/LayerGenEarth.java b/src/shootergame/world/layer/layergen/LayerGenEarth.java index d622f75..0f51b43 100644 --- a/src/shootergame/world/layer/layergen/LayerGenEarth.java +++ b/src/shootergame/world/layer/layergen/LayerGenEarth.java @@ -12,7 +12,6 @@ import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2i; import shootergame.util.math.vec.Vec3d; -import shootergame.util.math.vec.Vec3i; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -59,13 +58,13 @@ public class LayerGenEarth extends LayerGen else if(noise_n < 70) chunk.setBackTile(Tiles.GRASS.getDefaultState(), pos); else if(noise_n < 90) chunk.setBackTile(Tiles.DIRT.getDefaultState(), pos); else chunk.setBackTile(Tiles.STONE.getDefaultState(), pos); - - if(portal) { - chunk.setBackTile(new TileState(Tiles.PORTAL_DOWN, (short)1), portal_pos); - chunk.setFrontTile(Tiles.LADDER.getDefaultState(), portal_pos); - } } } + + if(portal) { + chunk.setBackTile(new TileState(Tiles.PORTAL_DOWN, (short)1), portal_pos); + chunk.setFrontTile(Tiles.LADDER.getDefaultState(), portal_pos); + } } @Override @@ -89,7 +88,7 @@ public class LayerGenEarth extends LayerGen } @Override - public Vec3i getLightLevel() { - return new Vec3i(200, 200, 200); + public Vec3d getLightLevel() { + return new Vec3d(0.3, 0.3, 0.3); } } diff --git a/src/shootergame/world/layer/layergen/LayerGenLavaCaves.java b/src/shootergame/world/layer/layergen/LayerGenLavaCaves.java new file mode 100644 index 0000000..dc850a0 --- /dev/null +++ b/src/shootergame/world/layer/layergen/LayerGenLavaCaves.java @@ -0,0 +1,37 @@ +package shootergame.world.layer.layergen; + +import java.util.Random; + +import shootergame.init.Tiles; +import shootergame.util.math.TileState; +import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; +import shootergame.world.chunk.Chunk; +import shootergame.world.layer.Layer; + +public class LayerGenLavaCaves extends LayerGen +{ + + @Override + public void generateChunk(Chunk chunk, Layer layer, long seed, Random rand, Vec2i pos) + { + // TODO Auto-generated method stub + + } + + @Override + public void spawnEntities(Layer layer, Random rand) { + + } + + @Override + public TileState getTileDestroyed() { + return Tiles.LAVA.getDefaultState(); + } + + @Override + public Vec3d getLightLevel() { + return new Vec3d(0.1, 0.1, 0.1); + } + +}