From cad3cc198c3a44b52dac4d297c94c7ff363b6d26 Mon Sep 17 00:00:00 2001 From: josua Date: Wed, 28 Aug 2019 22:25:56 +1000 Subject: [PATCH] Fixed the sound, added a health bar --- src/shootergame/audio/AudioObject.java | 1 + src/shootergame/audio/AudioSources.java | 4 +- src/shootergame/display/DisplayRender.java | 12 +--- src/shootergame/display/DisplayRenderUI.java | 55 +++++++++++++++++++ .../display/DisplayStatsEventHandler.java | 4 +- src/shootergame/entity/EntityTnt.java | 13 ++++- .../entity/player/EntityPlayer.java | 8 ++- src/shootergame/init/Resources.java | 4 ++ src/shootergame/init/Sounds.java | 4 ++ src/shootergame/init/Textures.java | 3 + src/shootergame/tiles/TileDirt.java | 10 ++++ src/shootergame/tiles/TilePortalDown.java | 33 +++++++---- src/shootergame/world/layer/Layer.java | 2 +- .../world/layer/layergen/LayerGen.java | 2 + .../world/layer/layergen/LayerGenCaves.java | 5 ++ .../world/layer/layergen/LayerGenEarth.java | 5 ++ 16 files changed, 135 insertions(+), 30 deletions(-) create mode 100644 src/shootergame/display/DisplayRenderUI.java diff --git a/src/shootergame/audio/AudioObject.java b/src/shootergame/audio/AudioObject.java index 40a364f..8ee0793 100644 --- a/src/shootergame/audio/AudioObject.java +++ b/src/shootergame/audio/AudioObject.java @@ -93,6 +93,7 @@ public class AudioObject // Play the sound with a new source int source = AudioSources.getSource(); + alSourceStop(source); alSourcei(source, AL_BUFFER, bufferPointer); alSourcef(source, AL_GAIN, (float)volume); alSource3f(source, AL_POSITION, (float)x, (float)y, (float)z); diff --git a/src/shootergame/audio/AudioSources.java b/src/shootergame/audio/AudioSources.java index e3bd654..85db081 100644 --- a/src/shootergame/audio/AudioSources.java +++ b/src/shootergame/audio/AudioSources.java @@ -8,7 +8,7 @@ import static org.lwjgl.openal.AL11.*; public class AudioSources { static ArrayList sources = new ArrayList(); - private static int upto = 0; + public static int upto = 0; private static int max = 0; public static void init() @@ -26,7 +26,7 @@ public class AudioSources } } - public static int getSource() + static int getSource() { // Get the next source int source = sources.get(upto); diff --git a/src/shootergame/display/DisplayRender.java b/src/shootergame/display/DisplayRender.java index f449748..2678554 100644 --- a/src/shootergame/display/DisplayRender.java +++ b/src/shootergame/display/DisplayRender.java @@ -17,8 +17,6 @@ import shootergame.util.math.vec.Vec3d; public class DisplayRender { - public static int fps = 0; - public static void render(int w, int h) { // Setup GL and clear the colour @@ -86,14 +84,8 @@ public class DisplayRender GlHelpers.popMatrix(); } - // Render the fps and the position - GlHelpers.pushMatrix(); - GlHelpers.translate(-9.5, 9.5, 0); - GlHelpers.color3(1, 1, 0); - Text.render("FPS: " + fps, new Vec2d(0.5, 0.2)); - GlHelpers.translate(0, -0.5, 0); - Text.render("x: " + (int) Main.player.pos.x + ", y: " + (int) Main.player.pos.y, new Vec2d(0.5, 0.2)); - GlHelpers.popMatrix(); + // Render the user interface + DisplayRenderUI.render(); // Unbind the texmap Textures.texmap.unbindTexture(); diff --git a/src/shootergame/display/DisplayRenderUI.java b/src/shootergame/display/DisplayRenderUI.java new file mode 100644 index 0000000..5b3457a --- /dev/null +++ b/src/shootergame/display/DisplayRenderUI.java @@ -0,0 +1,55 @@ +package shootergame.display; + +import shootergame.Main; +import shootergame.audio.AudioSources; +import shootergame.entity.player.EntityPlayer; +import shootergame.init.Textures; +import shootergame.text.Text; +import shootergame.util.gl.GlHelpers; +import shootergame.util.gl.texture.TextureReference; +import shootergame.util.math.vec.Vec2d; + +public class DisplayRenderUI +{ + + + public static void render() + { + // Get the player + EntityPlayer player = Main.player; + + // Disable some opengl options + GlHelpers.disableDepthTest(); + + // Render the fps and the position + GlHelpers.pushMatrix(); + GlHelpers.translate(-9.5, 9.5, 0); + GlHelpers.color3(1, 1, 0); + Text.render("FPS: " + DisplayStatsEventHandler.fps, new Vec2d(0.5, 0.2)); + GlHelpers.translate(0, -0.5, 0); + Text.render("x: " + (int) player.pos.x + ", y: " + (int) player.pos.y, new Vec2d(0.5, 0.2)); + GlHelpers.translate(0, -0.5, 0); + Text.render("AL Sound Source: "+AudioSources.upto, new Vec2d(0.5, 0.2)); + GlHelpers.color3(1, 1, 1); + GlHelpers.popMatrix(); + + // Render the healthbar + double max_health = player.maxHealth(); + double a = 1 - (player.getHealth() / max_health); + TextureReference health_fg = Textures.UI_HEALTH_FG; + TextureReference health_bg = Textures.UI_HEALTH_BG; + GlHelpers.begin(); + + health_bg.texCoord(0, 0); GlHelpers.vertex2(-2.5, -9.0); + health_bg.texCoord(0, 1); GlHelpers.vertex2(-2.5, -9.5); + health_bg.texCoord(1, 1); GlHelpers.vertex2(2.5, -9.5); + health_bg.texCoord(1, 0); GlHelpers.vertex2(2.5, -9.0); + + health_fg.texCoord(0, 0); GlHelpers.vertex2(-2.5, -9.0); + health_fg.texCoord(0, 1); GlHelpers.vertex2(-2.5, -9.5); + health_fg.texCoord(1-a, 1); GlHelpers.vertex2(2.5-a*5, -9.5); + health_fg.texCoord(1-a, 0); GlHelpers.vertex2(2.5-a*5, -9.0); + + GlHelpers.end(); + } +} diff --git a/src/shootergame/display/DisplayStatsEventHandler.java b/src/shootergame/display/DisplayStatsEventHandler.java index 00160ba..b361d52 100644 --- a/src/shootergame/display/DisplayStatsEventHandler.java +++ b/src/shootergame/display/DisplayStatsEventHandler.java @@ -6,6 +6,8 @@ import shootergame.mainloop.MainloopEventHandler; public class DisplayStatsEventHandler implements IMainloopTask { public static final DisplayStatsEventHandler DISPLAY_STATS_EVENT_HANDLER = new DisplayStatsEventHandler(); + + public static int fps = 0; @Override public boolean MainLoopDelay(long millis) { @@ -22,7 +24,7 @@ public class DisplayStatsEventHandler implements IMainloopTask { // Set the fps from mspf every second long mspf = MainloopEventHandler.MAINLOOP_EVENT_HANDLER.mspf; - DisplayRender.fps = (int)(1000 / mspf); + fps = (int)(1000 / mspf); } diff --git a/src/shootergame/entity/EntityTnt.java b/src/shootergame/entity/EntityTnt.java index 2c37972..70a1033 100644 --- a/src/shootergame/entity/EntityTnt.java +++ b/src/shootergame/entity/EntityTnt.java @@ -6,6 +6,7 @@ import shootergame.Main; import shootergame.display.Camera; import shootergame.entity.particle.ParticleBlood; import shootergame.entity.particle.ParticleSpark; +import shootergame.init.Sounds; import shootergame.init.Textures; import shootergame.init.Tiles; import shootergame.tiles.TileBlackened; @@ -83,16 +84,19 @@ public class EntityTnt extends EntityVertical // Calculate the blackened gradient short blackened_gradient = (short)( Short.MAX_VALUE - distance/explode_radius*Short.MAX_VALUE ); - // Get the front tile + // Get the front and back tile Vec2i tpos = new Vec2i(MathHelpers.floor(px), MathHelpers.floor(py)); TileState bts = l.getBackTile(tpos); TileState fts = l.getFrontTile(tpos); - if(bts.tile instanceof TileStone) { + + // Is this tile the same as the "empty" tile + TileState ets = l.layergen.getTileDestroyed(); + if(bts.tile == ets.tile) { if(bts.meta > blackened_gradient) blackened_gradient = bts.meta; } // Set the tiles - if(!bts.tile.unbreakable) l.setBackTile(new TileState(Tiles.STONE, + if(!bts.tile.unbreakable) l.setBackTile(new TileState(ets.tile, (short)blackened_gradient), tpos); if(!fts.tile.unbreakable) l.setFrontTile(Tiles.VOID.getDefaultState(), tpos); @@ -117,6 +121,9 @@ public class EntityTnt extends EntityVertical } } + // Play the explosion sound + Sounds.EXPLOSION.play(new Vec3d(pos.x, pos.y, height), 1); + // Delete the entity kill(); return; diff --git a/src/shootergame/entity/player/EntityPlayer.java b/src/shootergame/entity/player/EntityPlayer.java index 28a97bb..eb703d5 100644 --- a/src/shootergame/entity/player/EntityPlayer.java +++ b/src/shootergame/entity/player/EntityPlayer.java @@ -58,11 +58,17 @@ public class EntityPlayer extends EntityVertical implements EntityAlive } // Don't tick if the player is dead + else if(health < 0) { + dead = true; + health = 0; + } if(dead) return; // Call super super.tick(chunk, layer); + this.addHealth(0.1); + // Rotate left if(MOVE_LEFT) { this.angle -= 1; @@ -158,7 +164,7 @@ public class EntityPlayer extends EntityVertical implements EntityAlive @Override public void addHealth(double amount) { - health += amount; + if(health < health_max) health += amount; } @Override diff --git a/src/shootergame/init/Resources.java b/src/shootergame/init/Resources.java index 93c7a2c..5c2d252 100644 --- a/src/shootergame/init/Resources.java +++ b/src/shootergame/init/Resources.java @@ -22,6 +22,8 @@ public class Resources HIT_OGG_0.load(); HIT_OGG_1.load(); HIT_OGG_2.load(); + + EXPLOSION_OGG.load(); } public static final Resource TEXMAP_PNG = new Resource("texmap.png"); @@ -40,4 +42,6 @@ public class Resources public static final Resource HIT_OGG_0 = new Resource("sound/hit0.ogg"); public static final Resource HIT_OGG_1 = new Resource("sound/hit1.ogg"); public static final Resource HIT_OGG_2 = new Resource("sound/hit2.ogg"); + + public static final Resource EXPLOSION_OGG = new Resource("sound/explosion.ogg"); } diff --git a/src/shootergame/init/Sounds.java b/src/shootergame/init/Sounds.java index b82c7b6..db55160 100644 --- a/src/shootergame/init/Sounds.java +++ b/src/shootergame/init/Sounds.java @@ -21,6 +21,8 @@ public class Sounds HIT_0.init(); HIT_1.init(); HIT_2.init(); + + EXPLOSION.init(); } public static final AudioObject GUN_0 = new AudioObject(Resources.GUN_OGG_0); @@ -44,4 +46,6 @@ public class Sounds public static final AudioObject HIT = new AudioRandom( HIT_0, HIT_1, HIT_2); + + public static final AudioObject EXPLOSION = new AudioObject(Resources.EXPLOSION_OGG); } diff --git a/src/shootergame/init/Textures.java b/src/shootergame/init/Textures.java index 70e9224..688d4a4 100644 --- a/src/shootergame/init/Textures.java +++ b/src/shootergame/init/Textures.java @@ -37,6 +37,9 @@ public class Textures public static final TextureReference TILE_WALL = texmap.getTextureReference(2, 3, 5, 6); public static final TextureReference TILE_LADDER_UP = texmap.getTextureReference(16, 17, 0, 16); + public static final TextureReference UI_HEALTH_FG = texmap.getTextureReference(0, 16, 11, 12); + public static final TextureReference UI_HEALTH_BG = texmap.getTextureReference(0, 16, 12, 13); + // Fire public static final TextureReference TILE_FIRE_0 = texmap.getTextureReference(0, 1, 1, 2); public static final TextureReference TILE_FIRE_1 = texmap.getTextureReference(1, 2, 1, 2); diff --git a/src/shootergame/tiles/TileDirt.java b/src/shootergame/tiles/TileDirt.java index af19f35..5ba579b 100644 --- a/src/shootergame/tiles/TileDirt.java +++ b/src/shootergame/tiles/TileDirt.java @@ -1,6 +1,9 @@ package shootergame.tiles; +import shootergame.display.Camera; import shootergame.init.Textures; +import shootergame.util.gl.GlHelpers; +import shootergame.util.math.vec.Vec2d; public class TileDirt extends TileFlat { @@ -8,5 +11,12 @@ public class TileDirt extends TileFlat public TileDirt(String id) { super(id, Textures.TILE_DIRT); } + + @Override + public void render(Vec2d pos, Camera camera, short meta) { + GlHelpers.color4(1, 1, 1, (Short.MAX_VALUE - (double)meta) / Short.MAX_VALUE); + super.render(pos, camera, meta); + GlHelpers.color4(1, 1, 1, 1); + } } \ No newline at end of file diff --git a/src/shootergame/tiles/TilePortalDown.java b/src/shootergame/tiles/TilePortalDown.java index c7ea609..12c04b8 100644 --- a/src/shootergame/tiles/TilePortalDown.java +++ b/src/shootergame/tiles/TilePortalDown.java @@ -7,6 +7,7 @@ import shootergame.entity.player.EntityPlayer; import shootergame.init.Textures; import shootergame.init.Tiles; import shootergame.util.gl.texture.TextureReference; +import shootergame.util.math.TileState; import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2i; import shootergame.world.chunk.Chunk; @@ -61,6 +62,26 @@ public class TilePortalDown extends TileFlat movingPlayer = 1; Main.world.setLayerID(meta); player.height = 6; + + Vec2i check_poses[] = { + new Vec2i( 1, 0), + new Vec2i(-1, 0), + new Vec2i( 0, 1), + new Vec2i( 0, -1), + }; + + Layer layer = Main.world.getLayer(); + for(int i=0;i<=16;i++) + { + for(Vec2i check_m_pos : check_poses) + { + Vec2i check_pos = check_m_pos.multiply(new Vec2i(i, i)); + TileState ets = layer.layergen.getTileDestroyed(); + + if(layer.getBackTile(check_pos).tile != ets.tile) + layer.setBackTile(new TileState(ets.tile, (short)0), check_pos); + } + } } if(player.height < 0 && movingPlayer == 1) @@ -68,18 +89,6 @@ public class TilePortalDown extends TileFlat movingPlayer = 2; player.height = 0; player.moving = false; - - Layer layer = Main.world.getLayer(); - for(int i=0;i<=16;i++) { - if(layer.getBackTile(new Vec2i(pos.x+i, pos.y)).tile != Tiles.STONE) - layer.setBackTile(Tiles.STONE.getDefaultState(), new Vec2i(pos.x+i, pos.y)); - if(layer.getBackTile(new Vec2i(pos.x-i, pos.y)).tile != Tiles.STONE) - layer.setBackTile(Tiles.STONE.getDefaultState(), new Vec2i(pos.x-i, pos.y)); - if(layer.getBackTile(new Vec2i(pos.x, pos.y+i)).tile != Tiles.STONE) - layer.setBackTile(Tiles.STONE.getDefaultState(), new Vec2i(pos.x, pos.y+i)); - if(layer.getBackTile(new Vec2i(pos.x, pos.y-i)).tile != Tiles.STONE) - layer.setBackTile(Tiles.STONE.getDefaultState(), new Vec2i(pos.x, pos.y-i)); - } } } diff --git a/src/shootergame/world/layer/Layer.java b/src/shootergame/world/layer/Layer.java index 9c2fdcc..5080fd2 100644 --- a/src/shootergame/world/layer/Layer.java +++ b/src/shootergame/world/layer/Layer.java @@ -20,7 +20,7 @@ import shootergame.world.layer.layergen.LayerGen; public class Layer { public Map2D chunks; - private LayerGen layergen; + public LayerGen layergen; private Random rand; private long seed; diff --git a/src/shootergame/world/layer/layergen/LayerGen.java b/src/shootergame/world/layer/layergen/LayerGen.java index 22e228b..0a0dbcd 100644 --- a/src/shootergame/world/layer/layergen/LayerGen.java +++ b/src/shootergame/world/layer/layergen/LayerGen.java @@ -2,6 +2,7 @@ package shootergame.world.layer.layergen; import java.util.Random; +import shootergame.util.math.TileState; import shootergame.util.math.map.IMap2D; import shootergame.util.math.vec.Vec2i; import shootergame.world.chunk.Chunk; @@ -11,6 +12,7 @@ public abstract class LayerGen implements IMap2D { 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(); @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 ca2f3df..ce0b174 100644 --- a/src/shootergame/world/layer/layergen/LayerGenCaves.java +++ b/src/shootergame/world/layer/layergen/LayerGenCaves.java @@ -61,5 +61,10 @@ public class LayerGenCaves extends LayerGen // TODO Auto-generated method stub } + + @Override + public TileState getTileDestroyed() { + return Tiles.STONE.getDefaultState(); + } } diff --git a/src/shootergame/world/layer/layergen/LayerGenEarth.java b/src/shootergame/world/layer/layergen/LayerGenEarth.java index 7c36030..b7bd174 100644 --- a/src/shootergame/world/layer/layergen/LayerGenEarth.java +++ b/src/shootergame/world/layer/layergen/LayerGenEarth.java @@ -79,4 +79,9 @@ public class LayerGenEarth extends LayerGen layer.spawnEntity(zombie); } } + + @Override + public TileState getTileDestroyed() { + return Tiles.DIRT.getDefaultState(); + } }