From 5ab2075f293c76b6dabc0f8abad677e4e5eb9382 Mon Sep 17 00:00:00 2001 From: josua Date: Sun, 25 Aug 2019 15:31:58 +1000 Subject: [PATCH] Fixed the health system for the player --- src/shootergame/display/DisplayWindow.java | 3 + src/shootergame/entity/Entity.java | 66 +++++++++++++++++++ src/shootergame/entity/EntityBullet.java | 7 +- .../entity/EntityEventHandler.java | 10 ++- src/shootergame/entity/EntityParticle.java | 4 ++ src/shootergame/entity/EntityTnt.java | 6 ++ src/shootergame/entity/EntityZombie.java | 33 ++++++++-- .../ParticleBlood.java} | 7 +- .../entity/particle/ParticleSpark.java | 44 +++++++++++++ .../entity/player/EntityPlayer.java | 8 ++- src/shootergame/input/JoystickCallback.java | 30 ++++++--- src/shootergame/tiles/Tile.java | 1 + src/shootergame/world/chunk/Chunk.java | 3 +- src/shootergame/world/layer/Layer.java | 13 +++- 14 files changed, 210 insertions(+), 25 deletions(-) create mode 100644 src/shootergame/entity/EntityTnt.java rename src/shootergame/entity/{EntityBlood.java => particle/ParticleBlood.java} (88%) create mode 100644 src/shootergame/entity/particle/ParticleSpark.java diff --git a/src/shootergame/display/DisplayWindow.java b/src/shootergame/display/DisplayWindow.java index 0610986..7eb7727 100644 --- a/src/shootergame/display/DisplayWindow.java +++ b/src/shootergame/display/DisplayWindow.java @@ -70,6 +70,9 @@ public class DisplayWindow implements IMainloopTask GLFW.glfwSetKeyCallback(this.window, new KeyCallback()); GLFW.glfwSetJoystickCallback(JoystickCallback.JOYSTICK_CALLBACK); + // Make the cursor invisible + GLFW.glfwSetInputMode(this.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN); + // Show the window GLFW.glfwShowWindow(this.window); diff --git a/src/shootergame/entity/Entity.java b/src/shootergame/entity/Entity.java index acd4b4e..cef9d83 100644 --- a/src/shootergame/entity/Entity.java +++ b/src/shootergame/entity/Entity.java @@ -1,5 +1,7 @@ package shootergame.entity; +import java.util.ArrayList; + import shootergame.Main; import shootergame.display.Camera; import shootergame.display.transparent.ITransparentObject; @@ -9,6 +11,7 @@ import shootergame.tiles.Tile; import shootergame.util.math.MathHelpers; import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2i; +import shootergame.world.World; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; @@ -19,6 +22,7 @@ public class Entity implements ITransparentObject public boolean opaqueTile = true; public double hitbox = 1; public boolean isSolid = false; + public Chunk chunk; public Entity(Vec2d pos, double angle) { @@ -77,8 +81,70 @@ public class Entity implements ITransparentObject this.moveBackward(0.1); } + public void kill() { + chunk.killEntity(this); + } + public boolean moveIsLegal(Vec2d pos) { + /*// Is this entity solid + if(isSolid) + { + // Get the layer + Layer l = Main.world.getLayer(); + + // Get some tiles + Vec2i[] tiles = { + new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0), + new Vec2i(MathHelpers.floor(pos.x)+1, MathHelpers.floor(pos.y)+0), + new Vec2i(MathHelpers.floor(pos.x)+1, MathHelpers.floor(pos.y)+1), + new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+1) + }; + + for(Vec2i tpos : tiles) + { + { + // Get the tile + Tile t = l.getBackTile(tpos); + + // Send false if the tile isn't walkable + if(!t.tileWalkable) { + System.out.println("1"); + return false; + } + } + + { + // Get the front tile + Tile t = l.getFrontTile(tpos); + + // Send false if the tile isn't walkable + if(!t.tileWalkable) { + System.out.println("2"); + return false; + } + + // Check the tiles hitbox if the tile is solid + if(t.tileSolid) + { + // Is the entity in the tiles hitbox + if( + tpos.x + t.tileHitbox < pos.x || + tpos.x - t.tileHitbox > pos.x || + tpos.y + t.tileHitbox < pos.y || + tpos.y - t.tileHitbox > pos.y + ) { + System.out.println("3"); + + // Send back false + return false; + } + } + } + } + }*/ + + // Send back true if everything passes return true; } } diff --git a/src/shootergame/entity/EntityBullet.java b/src/shootergame/entity/EntityBullet.java index b4fafbd..14c27f1 100644 --- a/src/shootergame/entity/EntityBullet.java +++ b/src/shootergame/entity/EntityBullet.java @@ -3,6 +3,7 @@ package shootergame.entity; import java.util.Random; import shootergame.display.Camera; +import shootergame.entity.particle.ParticleBlood; import shootergame.util.gl.GlHelpers; import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.vec.Vec2d; @@ -42,8 +43,8 @@ public class EntityBullet extends EntityParticle EntityAlive ea = (EntityAlive)e; // Spawn some blood particles - for(int i=0;i<20;i++) { - chunk.spawnEntity(new EntityBlood(rand, pos.copy(), angle)); + for(int i=0;i<10;i++) { + chunk.spawnEntity(new ParticleBlood(rand, pos.copy(), angle)); } // Harm the entity @@ -67,7 +68,7 @@ public class EntityBullet extends EntityParticle public void render(Vec2d pos, Camera camera) { // Set the colour - GlHelpers.color3(0.6, 0.6, 0); + GlHelpers.color3(1, 0.8, 0.3); // Call super super.render(pos, camera); diff --git a/src/shootergame/entity/EntityEventHandler.java b/src/shootergame/entity/EntityEventHandler.java index 82e5392..0979ecb 100644 --- a/src/shootergame/entity/EntityEventHandler.java +++ b/src/shootergame/entity/EntityEventHandler.java @@ -1,14 +1,22 @@ package shootergame.entity; import java.util.ArrayList; +import java.util.Random; import mainloop.task.IMainloopTask; import shootergame.Main; import shootergame.init.Entities; +import shootergame.mainloop.MainloopEventHandler; +import shootergame.util.math.map.Map2DElement; +import shootergame.util.math.random.RandomHelpers; +import shootergame.world.chunk.Chunk; +import shootergame.world.layer.Layer; public class EntityEventHandler implements IMainloopTask { public static final EntityEventHandler ENTITY_EVENT_HANDLER = new EntityEventHandler(); + + private Random rand = new Random(); @Override public boolean MainLoopDelay(long millis) { @@ -27,7 +35,5 @@ public class EntityEventHandler implements IMainloopTask Main.world.tickEntities(); Main.world.spawnRandomEntities(); Main.player.tick(null, Main.world.getLayer()); - - } } diff --git a/src/shootergame/entity/EntityParticle.java b/src/shootergame/entity/EntityParticle.java index 3c6941c..c8f4f34 100644 --- a/src/shootergame/entity/EntityParticle.java +++ b/src/shootergame/entity/EntityParticle.java @@ -17,6 +17,10 @@ public class EntityParticle extends Entity this.size = size; } + public void setSize(double size) { + this.size = size; + } + @Override public void render(Vec2d pos, Camera camera) { diff --git a/src/shootergame/entity/EntityTnt.java b/src/shootergame/entity/EntityTnt.java new file mode 100644 index 0000000..1ee34d2 --- /dev/null +++ b/src/shootergame/entity/EntityTnt.java @@ -0,0 +1,6 @@ +package shootergame.entity; + +public class EntityTnt extends EntityVertical +{ + +} diff --git a/src/shootergame/entity/EntityZombie.java b/src/shootergame/entity/EntityZombie.java index a85fd40..4abd607 100644 --- a/src/shootergame/entity/EntityZombie.java +++ b/src/shootergame/entity/EntityZombie.java @@ -11,15 +11,20 @@ import shootergame.world.layer.Layer; public class EntityZombie extends EntityVertical implements EntityAlive { private Random rand; - private OpenSimplexNoise noise; + private OpenSimplexNoise noise_movement; + private OpenSimplexNoise noise_gun_fire; + private OpenSimplexNoise noise_gun_angle; private double time; private double health_max = 100; private double health = health_max; + private int gun_interval = 0; public EntityZombie() { super(Textures.ENTITY_ZOMBIE, 1); rand = new Random(); - noise = new OpenSimplexNoise(rand.nextLong()); + noise_movement = new OpenSimplexNoise(rand.nextLong()); + noise_gun_fire = new OpenSimplexNoise(rand.nextLong()); + noise_gun_angle = new OpenSimplexNoise(rand.nextLong()); time = 0; // Set some settings @@ -31,13 +36,33 @@ public class EntityZombie extends EntityVertical implements EntityAlive public void tick(Chunk chunk, Layer layer) { super.tick(chunk, layer); + // Get the angle between the player and the zombie double angle = Math.atan2(pos.x - Main.player.pos.x, pos.y - Main.player.pos.y); + // Move forward towards the player this.angle = Math.toDegrees(angle) + 180; - this.angle += noise.eval(time, 0)*60; + this.angle += noise_movement.eval(time, 0)*60; + this.moveForward(0.05); + + if(noise_gun_fire.eval(time, 0) > 0) + { + gun_interval += 1; + gun_interval %= 10; + + if(gun_interval == 0) + { + // Aim the gun at the player + double angle_gun = Math.toDegrees(angle) + 180; + angle_gun += noise_gun_angle.eval(time, 0)*20; + + // Fire the gun + layer.spawnEntity(new EntityBullet(rand, pos.copy(), this, angle_gun)); + } + } + + // Increase time time += 0.001; - this.moveForward(0.05); } @Override diff --git a/src/shootergame/entity/EntityBlood.java b/src/shootergame/entity/particle/ParticleBlood.java similarity index 88% rename from src/shootergame/entity/EntityBlood.java rename to src/shootergame/entity/particle/ParticleBlood.java index f2f5ef6..9fb49a7 100644 --- a/src/shootergame/entity/EntityBlood.java +++ b/src/shootergame/entity/particle/ParticleBlood.java @@ -1,15 +1,16 @@ -package shootergame.entity; +package shootergame.entity.particle; import java.util.Random; import shootergame.display.Camera; +import shootergame.entity.EntityParticle; import shootergame.util.gl.GlHelpers; import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.vec.Vec2d; import shootergame.world.chunk.Chunk; import shootergame.world.layer.Layer; -public class EntityBlood extends EntityParticle +public class ParticleBlood extends EntityParticle { private double r_color; private double speed = 0.1; @@ -17,7 +18,7 @@ public class EntityBlood extends EntityParticle private double height = 0; private double height_velocity = 0; - public EntityBlood(Random rand, Vec2d pos, double angle) { + public ParticleBlood(Random rand, Vec2d pos, double angle) { super(rand.nextDouble() / 5, 0); this.pos = pos; diff --git a/src/shootergame/entity/particle/ParticleSpark.java b/src/shootergame/entity/particle/ParticleSpark.java new file mode 100644 index 0000000..d3bb730 --- /dev/null +++ b/src/shootergame/entity/particle/ParticleSpark.java @@ -0,0 +1,44 @@ +package shootergame.entity.particle; + +import shootergame.display.Camera; +import shootergame.entity.EntityParticle; +import shootergame.util.gl.GlHelpers; +import shootergame.util.math.vec.Vec2d; +import shootergame.world.chunk.Chunk; +import shootergame.world.layer.Layer; + +public class ParticleSpark extends EntityParticle +{ + private double size = 1; + + public ParticleSpark(int height) { + super(1, height); + } + + @Override + public void tick(Chunk chunk, Layer layer) { + super.tick(chunk, layer); + + // Reduce the size + size -= 0.01; + setSize(size); + + // Is the size zero + if(size <= 0) + { + // Destroy this particle + kill(); + } + } + + @Override + public void render(Vec2d pos, Camera camera) + { + // Set some settings + GlHelpers.color3(1, 1, 0); + + // Call super + super.render(pos, camera); + } + +} diff --git a/src/shootergame/entity/player/EntityPlayer.java b/src/shootergame/entity/player/EntityPlayer.java index 2c3b183..d12a094 100644 --- a/src/shootergame/entity/player/EntityPlayer.java +++ b/src/shootergame/entity/player/EntityPlayer.java @@ -26,7 +26,7 @@ public class EntityPlayer extends EntityVertical implements EntityAlive private int bullet_frequency = 0; private Random rand; - private double health_max = 100; + private double health_max = 1000; private double health = health_max; public EntityPlayer() { @@ -41,6 +41,9 @@ public class EntityPlayer extends EntityVertical implements EntityAlive @Override public void tick(Chunk chunk, Layer layer) { + // Don't tick if the player is dead + if(this.getHealth() < 0) return; + // Call super super.tick(chunk, layer); @@ -70,6 +73,9 @@ public class EntityPlayer extends EntityVertical implements EntityAlive @Override public void render(Vec2d pos, Camera camera) { + // Don't render if the player is dead + if(this.getHealth() < 0) return; + // Moving if(MOVE_BACKWARD || MOVE_FORWARD || moving) super.render(pos, camera, Textures.ENTITY_PLAYER_MOVING, 1); diff --git a/src/shootergame/input/JoystickCallback.java b/src/shootergame/input/JoystickCallback.java index 1094c27..ea9ba82 100644 --- a/src/shootergame/input/JoystickCallback.java +++ b/src/shootergame/input/JoystickCallback.java @@ -164,20 +164,30 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask // Set the players moving to false else Main.player.moving = false; - // Is the right stick moved into a position (gun stick) - if(right_x > 0.3 || right_x < -0.3 || right_y > 0.3 || right_y < -0.3) - { - // Get the the angle and fire the bullet - double angle = Math.toDegrees(Math.atan2(right_y, right_x)) + 90; - Main.player.fireBullet(angle); + // Is the right x axis stick moved into a position (camera stick) + if(right_x > 0.3 || right_x < -0.3) { + Main.player.angle += right_x; } - if(shoulder_left) { - Main.player.angle -= 1; + // Gun trigger + if(right_trigger > 0.3) { + Main.player.fireBullet(0); } - if(shoulder_right) { - Main.player.angle += 1; + if(dpad_up) { + Main.player.fireBullet(0); + } + + if(dpad_down) { + Main.player.fireBullet(180); + } + + if(dpad_left) { + Main.player.fireBullet(270); + } + + if(dpad_right) { + Main.player.fireBullet(90); } } diff --git a/src/shootergame/tiles/Tile.java b/src/shootergame/tiles/Tile.java index ca83e6b..06eae1b 100644 --- a/src/shootergame/tiles/Tile.java +++ b/src/shootergame/tiles/Tile.java @@ -13,6 +13,7 @@ public class Tile implements ITransparentObject public boolean opaqueTile = false; public boolean tileSolid = false; public boolean tileWalkable = true; + public double tileHitbox = 0; public Tile(String id) { this.id = id; diff --git a/src/shootergame/world/chunk/Chunk.java b/src/shootergame/world/chunk/Chunk.java index 04fc1fa..8c7d42b 100644 --- a/src/shootergame/world/chunk/Chunk.java +++ b/src/shootergame/world/chunk/Chunk.java @@ -23,7 +23,7 @@ public class Chunk private Tile tiles_back[] = new Tile[CHUNK_INDEX]; private Tile tiles_front[] = new Tile[CHUNK_INDEX]; - private ArrayList entities = new ArrayList(); + public ArrayList entities = new ArrayList(); private Random rand; private Layer layer; private Vec2i c_pos; @@ -65,6 +65,7 @@ public class Chunk } public void spawnEntity(Entity e) { + e.chunk = this; entities.add(e); } diff --git a/src/shootergame/world/layer/Layer.java b/src/shootergame/world/layer/Layer.java index 9b6f893..43ba0de 100644 --- a/src/shootergame/world/layer/Layer.java +++ b/src/shootergame/world/layer/Layer.java @@ -93,7 +93,7 @@ public class Layer } private Vec2i getChunkPosFromPos(Vec2i pos) { - return this.getChunkPosFromPos(new Vec2i(pos.x, pos.y)); + return this.getChunkPosFromPos(new Vec2d(pos.x, pos.y)); } private Vec2i getChunkPosFromPos(Vec2d pos) { @@ -148,6 +148,17 @@ public class Layer } } + // Check if the player is close enough + if( + Main.player.pos.x + distance > pos.x && + Main.player.pos.x - distance < pos.x && + Main.player.pos.y + distance > pos.y && + Main.player.pos.y - distance < pos.y + ) { + // Add the player to the list of entities + entities.add(Main.player); + } + // Send back the list of nearby entities return entities; }