diff --git a/src/shootergame/Main.java b/src/shootergame/Main.java index 5de9ec0..0755b18 100644 --- a/src/shootergame/Main.java +++ b/src/shootergame/Main.java @@ -1,23 +1,40 @@ package shootergame; +import java.util.Random; + +import javax.swing.text.html.parser.Entity; + import mainloop.manager.MainloopManager; +import shootergame.display.DisplayStatsEventHandler; import shootergame.display.DisplayWindow; import shootergame.entity.EntityEventHandler; -import shootergame.entity.player.Player; +import shootergame.entity.EntityZombie; +import shootergame.entity.player.EntityPlayer; +import shootergame.init.Entities; import shootergame.init.Textures; +import shootergame.input.JoystickCallback; import shootergame.mainloop.MainloopEventHandler; +import shootergame.util.math.MathHelpers; +import shootergame.util.math.vec.Vec2i; +import shootergame.world.World; +import shootergame.world.chunk.ChunkEventHandler; +import shootergame.world.layer.layergen.LayerGenEarth; public class Main { public static MainloopManager mainloop; public static DisplayWindow window; - public static Player player = new Player(); + public static EntityPlayer player = new EntityPlayer(); + public static World world; public static void main(String[] args) { // Create the mainloop mainloop = new MainloopManager(MainloopEventHandler.MAINLOOP_EVENT_HANDLER); mainloop.register(MainloopEventHandler.MAINLOOP_EVENT_HANDLER); + mainloop.register(ChunkEventHandler.CHUNK_EVENT_HANDLER); + mainloop.register(DisplayStatsEventHandler.DISPLAY_STATS_EVENT_HANDLER); + mainloop.register(JoystickCallback.JOYSTICK_CALLBACK); // Create the display window = new DisplayWindow("ShooterGame"); @@ -26,6 +43,12 @@ public class Main // Initialise the textures Textures.initTextures(window); + // Initialize the gamepad + JoystickCallback.JOYSTICK_CALLBACK.init(); + + // Create the world + world = new World(new Random(), new LayerGenEarth()); + // Initialise the entities mainloop.register(EntityEventHandler.ENTITY_EVENT_HANDLER); diff --git a/src/shootergame/display/Camera.java b/src/shootergame/display/Camera.java index 2134b99..3f82d1b 100644 --- a/src/shootergame/display/Camera.java +++ b/src/shootergame/display/Camera.java @@ -8,12 +8,15 @@ public class Camera { public Vec3d pos; public Vec2d angle; + public int renderDistance; + public double cameraDistance; - public Camera(Vec3d pos, Vec2d angle, double distance) + public Camera(Vec3d pos, Vec2d angle, double cameraDistance, int renderDistance) { - Vec3d p = new Vec3d(pos.x, pos.y+5, pos.z); this.angle = new Vec2d(angle.x, -angle.y); this.pos = pos.subtract(MathHelpers.moveTowards3( - 10, new Vec2d(Math.toRadians(angle.x), Math.toRadians(-angle.y)))); + cameraDistance, new Vec2d(Math.toRadians(angle.x), Math.toRadians(-angle.y)))); + this.cameraDistance = cameraDistance; + this.renderDistance = renderDistance; } } diff --git a/src/shootergame/display/DisplayRender.java b/src/shootergame/display/DisplayRender.java index 6b0de6c..5c597c1 100644 --- a/src/shootergame/display/DisplayRender.java +++ b/src/shootergame/display/DisplayRender.java @@ -6,22 +6,19 @@ import org.joml.Matrix4f; import org.lwjgl.opengl.GL; import org.lwjgl.system.MemoryStack; -import mainloop.task.MainloopTask; import shootergame.Main; import shootergame.display.transparent.TransparentObjects; -import shootergame.entity.Entity; -import shootergame.entity.player.Player; -import shootergame.init.Entities; +import shootergame.entity.player.EntityPlayer; import shootergame.init.Textures; -import shootergame.init.Tiles; +import shootergame.text.Text; import shootergame.util.gl.GlHelpers; -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 DisplayRender { + public static int fps = 0; + public static void render(int w, int h) { // Setup GL and clear the colour @@ -34,9 +31,9 @@ public class DisplayRender // Enable some stuff GlHelpers.enableTexture2d(); + GlHelpers.enableDepthTest(); GlHelpers.enableBlend(); GlHelpers.enableAlpha(); - GlHelpers.enableDepthTest(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set the colour to white @@ -70,37 +67,33 @@ public class DisplayRender 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); - Player player = Main.player; - Camera camera = new Camera(new Vec3d(player.pos.x, player.pos.y, 0), new Vec2d(player.angle, 45), 0); + EntityPlayer player = Main.player; + Camera camera = new Camera(new Vec3d(player.pos.x, player.pos.y, 0), new Vec2d(player.angle, 45), 10, 2); //GlHelpers.translate(0, 0, -5); GlHelpers.rotate(camera.angle.y, 1, 0, 0); GlHelpers.rotate(camera.angle.x, 0, 0, 1); GlHelpers.translate(-camera.pos.x, -camera.pos.y, -camera.pos.z); - int c = 20; - for(int x=-c;x 1000; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public void MainLoopUpdate() + { + // Set the fps from mspf every second + long mspf = MainloopEventHandler.MAINLOOP_EVENT_HANDLER.mspf; + DisplayRender.fps = (int)(1000 / mspf); + } + + +} diff --git a/src/shootergame/display/DisplayWindow.java b/src/shootergame/display/DisplayWindow.java index 1e36241..0610986 100644 --- a/src/shootergame/display/DisplayWindow.java +++ b/src/shootergame/display/DisplayWindow.java @@ -9,6 +9,7 @@ import mainloop.task.IMainloopTask; import shootergame.Main; import shootergame.input.CursorEnterCallback; import shootergame.input.CursorPosCallback; +import shootergame.input.JoystickCallback; import shootergame.input.KeyCallback; import shootergame.input.KeyCharCallback; import shootergame.input.MouseButtonCallback; @@ -67,6 +68,7 @@ public class DisplayWindow implements IMainloopTask GLFW.glfwSetCursorEnterCallback(this.window, new CursorEnterCallback()); GLFW.glfwSetMouseButtonCallback(this.window, new MouseButtonCallback()); GLFW.glfwSetKeyCallback(this.window, new KeyCallback()); + GLFW.glfwSetJoystickCallback(JoystickCallback.JOYSTICK_CALLBACK); // Show the window GLFW.glfwShowWindow(this.window); diff --git a/src/shootergame/entity/Entity.java b/src/shootergame/entity/Entity.java index a981bc5..4d4433a 100644 --- a/src/shootergame/entity/Entity.java +++ b/src/shootergame/entity/Entity.java @@ -1,10 +1,16 @@ package shootergame.entity; +import shootergame.Main; import shootergame.display.Camera; import shootergame.display.transparent.ITransparentObject; import shootergame.display.transparent.TransparentObjects; import shootergame.init.Entities; +import shootergame.tiles.Tile; +import shootergame.util.math.MathHelpers; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; +import shootergame.world.chunk.Chunk; +import shootergame.world.layer.Layer; public class Entity implements ITransparentObject { @@ -26,7 +32,7 @@ public class Entity implements ITransparentObject this(new Vec2d(0, 0), 0); } - public void tick() { + public void tick(Chunk chunk, Layer layer) { } @Override @@ -48,4 +54,29 @@ public class Entity implements ITransparentObject public boolean isOpaqueTile() { return this.opaqueTile; } + + public void moveForward(double speed) { + Vec2d pos = this.pos.add(MathHelpers.moveTowards2(speed, Math.toRadians(this.angle))); + if(this.moveIsLegal(new Vec2d(this.pos.x, pos.y))) this.pos.y = pos.y; + if(this.moveIsLegal(new Vec2d(pos.x, this.pos.y))) this.pos.x = pos.x; + } + + public void moveBackward(double speed) { + Vec2d pos = this.pos.add(MathHelpers.moveTowards2(-speed, Math.toRadians(this.angle))); + if(this.moveIsLegal(new Vec2d(this.pos.x, pos.y))) this.pos.y = pos.y; + if(this.moveIsLegal(new Vec2d(pos.x, this.pos.y))) this.pos.x = pos.x; + } + + public void moveForward() { + this.moveForward(0.1); + } + + public void moveBackward() { + this.moveBackward(0.1); + } + + public boolean moveIsLegal(Vec2d pos) + { + return true; + } } diff --git a/src/shootergame/entity/EntityBullet.java b/src/shootergame/entity/EntityBullet.java new file mode 100644 index 0000000..4669767 --- /dev/null +++ b/src/shootergame/entity/EntityBullet.java @@ -0,0 +1,83 @@ +package shootergame.entity; + +import shootergame.display.Camera; +import shootergame.util.gl.GlHelpers; +import shootergame.util.math.vec.Vec2d; +import shootergame.world.chunk.Chunk; +import shootergame.world.layer.Layer; + +public class EntityBullet extends Entity +{ + private int time = 0; + + public EntityBullet() + { + // Set some settings + this.opaqueTile = false; + } + + @Override + public void tick(Chunk chunk, Layer layer) { + super.tick(chunk, layer); + + // Move forward in the bullets angle, very quickly + this.moveForward(0.1); + + // Loop over the nearby entities + for(Entity e : chunk.getNearbyEntities(pos, 2)) + { + // Is this a zombie + if(e instanceof EntityZombie) + { + System.out.println("Found Zombie"); + + chunk.killEntity(this); + return; + } + } + + // Increase time + time++; + + if(time > 40) { + chunk.killEntity(this); + } + } + + @Override + public void render(Vec2d pos, Camera camera) + { + // Call super + super.render(pos, camera); + + // Push the matrix, disable textures, colour, and translate the bullet + GlHelpers.pushMatrix(); + GlHelpers.color3(1, 1, 0); + GlHelpers.translate(pos.x, pos.y, 0.4); + GlHelpers.disableTexture2d(); + + // Get the angle between the camera and the bullet + double angle_r = camera.angle.x; + + // Make the bullet upright + GlHelpers.translate(0.1, 0, 0); + GlHelpers.translate(pos.x, pos.y, 0); + GlHelpers.rotate(-angle_r, 0, 0, 1); + GlHelpers.translate(-0.1, 0, 0); + + // Draw the bullet + GlHelpers.begin(); + { + GlHelpers.vertex3(0.0f, 0, 0.0f); + GlHelpers.vertex3(0.2f, 0, 0.0f); + GlHelpers.vertex3(0.2f, 0, 0.2f); + GlHelpers.vertex3(0.0f, 0, 0.2f); + } + GlHelpers.end(); + + // Pop the matrix, remove the colour, and enable textures + GlHelpers.popMatrix(); + GlHelpers.enableTexture2d(); + GlHelpers.color3(1, 1, 1); + } +} diff --git a/src/shootergame/entity/EntityEventHandler.java b/src/shootergame/entity/EntityEventHandler.java index 3fc7e96..82e5392 100644 --- a/src/shootergame/entity/EntityEventHandler.java +++ b/src/shootergame/entity/EntityEventHandler.java @@ -3,6 +3,7 @@ package shootergame.entity; import java.util.ArrayList; import mainloop.task.IMainloopTask; +import shootergame.Main; import shootergame.init.Entities; public class EntityEventHandler implements IMainloopTask @@ -22,9 +23,11 @@ public class EntityEventHandler implements IMainloopTask @Override public void MainLoopUpdate() { - // Update each entity - for(Entity e : Entities.entities) { - e.tick(); - } + // Update the world and spawn random entities + Main.world.tickEntities(); + Main.world.spawnRandomEntities(); + Main.player.tick(null, Main.world.getLayer()); + + } } diff --git a/src/shootergame/entity/EntityVertical.java b/src/shootergame/entity/EntityVertical.java index 1177aea..8932cce 100644 --- a/src/shootergame/entity/EntityVertical.java +++ b/src/shootergame/entity/EntityVertical.java @@ -1,9 +1,6 @@ package shootergame.entity; -import shootergame.Main; import shootergame.display.Camera; -import shootergame.entity.player.Player; -import shootergame.util.gl.GlHelpers; import shootergame.util.gl.VerticalRender; import shootergame.util.gl.texture.TextureReference; import shootergame.util.math.vec.Vec2d; @@ -11,16 +8,23 @@ import shootergame.util.math.vec.Vec2d; public class EntityVertical extends Entity { private TextureReference tex; - private double h; + private double height; + + public EntityVertical() { + } public EntityVertical(TextureReference tex, double height) { + this.height = height; this.tex = tex; - this.h = height; + } + + public void render(Vec2d pos, Camera camera, TextureReference tex, double height) { + super.render(pos, camera); + VerticalRender.render(pos, camera, tex, height); } @Override public void render(Vec2d pos, Camera camera) { - super.render(pos, camera); - VerticalRender.render(pos, camera, tex, h); + this.render(pos, camera, tex, height); } } diff --git a/src/shootergame/entity/EntityZombie.java b/src/shootergame/entity/EntityZombie.java new file mode 100644 index 0000000..2aca487 --- /dev/null +++ b/src/shootergame/entity/EntityZombie.java @@ -0,0 +1,39 @@ +package shootergame.entity; + +import java.util.Random; + +import shootergame.Main; +import shootergame.display.Camera; +import shootergame.init.Textures; +import shootergame.util.math.random.OpenSimplexNoise; +import shootergame.util.math.vec.Vec2d; +import shootergame.world.chunk.Chunk; +import shootergame.world.layer.Layer; + +public class EntityZombie extends EntityVertical +{ + private Random rand; + private OpenSimplexNoise noise; + private double time; + + public EntityZombie() { + super(Textures.ENTITY_ZOMBIE, 1); + rand = new Random(); + noise = new OpenSimplexNoise(rand.nextLong()); + time = 0; + } + + @Override + public void tick(Chunk chunk, Layer layer) { + super.tick(chunk, layer); + + double angle = Math.atan2(pos.x - Main.player.pos.x, pos.y - Main.player.pos.y); + + this.angle = Math.toDegrees(angle) + 180; + this.angle += noise.eval(time, 0)*60; + time += 0.001; + + this.moveForward(0.05); + } + +} diff --git a/src/shootergame/entity/player/EntityPlayer.java b/src/shootergame/entity/player/EntityPlayer.java index 4a2108f..002ecba 100644 --- a/src/shootergame/entity/player/EntityPlayer.java +++ b/src/shootergame/entity/player/EntityPlayer.java @@ -1,5 +1,6 @@ package shootergame.entity.player; +import shootergame.display.Camera; import shootergame.entity.Entity; import shootergame.entity.EntityVertical; import shootergame.init.Textures; @@ -7,46 +8,58 @@ 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.world.chunk.Chunk; +import shootergame.world.layer.Layer; -public class Player extends EntityVertical +public class EntityPlayer extends EntityVertical { - public Player() { - super(Textures.ENTITY_PLAYER, 1); - } - public boolean MOVE_FORWARD = false; public boolean MOVE_BACKWARD = false; public boolean MOVE_LEFT = false; public boolean MOVE_RIGHT = false; + public boolean moving = false; + + public EntityPlayer() { + this.pos = new Vec2d(0, 0); + } @Override - public void tick() + public void tick(Chunk chunk, Layer layer) { // Call super - super.tick(); + super.tick(chunk, layer); // Rotate left if(MOVE_LEFT) { this.angle -= 1; - this.angle %= 360; + this.angle = MathHelpers.mod(this.angle, 360); } // Rotate right if(MOVE_RIGHT) { this.angle += 1; - this.angle %= 360; + this.angle = MathHelpers.mod(this.angle, 360); } // Move forward if(MOVE_FORWARD) { - Vec2d t = MathHelpers.moveTowards2(0.1, Math.toRadians(angle)); - pos = new Vec2d(t.x+pos.x, t.y+pos.y); + this.moveForward(); } // Move backward if(MOVE_BACKWARD) { - Vec2d t = MathHelpers.moveTowards2(-0.1, Math.toRadians(angle)); - pos = new Vec2d(t.x+pos.x, t.y+pos.y); + this.moveBackward(); } } + + @Override + public void render(Vec2d pos, Camera camera) + { + // Moving + if(MOVE_BACKWARD || MOVE_FORWARD || moving) + super.render(pos, camera, Textures.ENTITY_PLAYER_MOVING, 1); + + // Standing still + else super.render(pos, camera, Textures.ENTITY_PLAYER_STILL, 1); + } } diff --git a/src/shootergame/init/Textures.java b/src/shootergame/init/Textures.java index 70b21f6..b5fc683 100644 --- a/src/shootergame/init/Textures.java +++ b/src/shootergame/init/Textures.java @@ -23,39 +23,120 @@ public class Textures public static final ArrayList animations = new ArrayList(); - public static final TextureMap texmap = new TextureMap( + public static final TextureMap texmap = new TextureMap(16, "/home/josua/eclipse-workspace/ShooterGame/src/shootergame/resources/texmap.png"); - public static final TextureReference TILE_GRASS = texmap.getTextureReference(0, 16, 0, 16); - public static final TextureReference TILE_SAND = texmap.getTextureReference(16, 32, 0, 16); - public static final TextureReference TILE_STONE = texmap.getTextureReference(32, 48, 0, 16); - public static final TextureReference TILE_DIRT = texmap.getTextureReference(48, 64, 0, 16); - public static final TextureReference TILE_TREE = texmap.getTextureReference(64, 80, 0, 64); + public static final TextureReference TILE_GRASS = texmap.getTextureReference(0, 1, 0, 1); + public static final TextureReference TILE_SAND = texmap.getTextureReference(1, 2, 0, 1); + public static final TextureReference TILE_STONE = texmap.getTextureReference(2, 3, 0, 1); + public static final TextureReference TILE_DIRT = texmap.getTextureReference(3, 4, 0, 1); + public static final TextureReference TILE_TREE = texmap.getTextureReference(4, 5, 0, 4); + public static final TextureReference TILE_ROCK = texmap.getTextureReference(4, 5, 4, 5); // Fire - public static final TextureReference TILE_FIRE_0 = texmap.getTextureReference(0, 16, 16, 32); - public static final TextureReference TILE_FIRE_1 = texmap.getTextureReference(16, 32, 16, 32); - public static final TextureReference TILE_FIRE_2 = texmap.getTextureReference(32, 48, 16, 32); - public static final TextureReference TILE_FIRE_3 = texmap.getTextureReference(48, 64, 16, 32); + 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); + public static final TextureReference TILE_FIRE_2 = texmap.getTextureReference(2, 3, 1, 2); + public static final TextureReference TILE_FIRE_3 = texmap.getTextureReference(3, 4, 1, 2); public static final TextureReference TILE_FIRE = new AnimationReference( 8, TILE_FIRE_0, TILE_FIRE_1, TILE_FIRE_2, TILE_FIRE_3); // Player - public static final TextureReference ENTITY_PLAYER_0 = texmap.getTextureReference(0, 16, 32, 48); - public static final TextureReference ENTITY_PLAYER_1 = texmap.getTextureReference(16, 32, 32, 48); - public static final TextureReference ENTITY_PLAYER_2 = texmap.getTextureReference(32, 48, 32, 48); - public static final TextureReference ENTITY_PLAYER_3 = texmap.getTextureReference(48, 64, 32, 48); - public static final TextureReference ENTITY_PLAYER = new AnimationReference( - 10, ENTITY_PLAYER_0, ENTITY_PLAYER_1, ENTITY_PLAYER_2, ENTITY_PLAYER_3); + public static final TextureReference ENTITY_PLAYER_STILL = texmap.getTextureReference(0, 1, 2, 3); + public static final TextureReference ENTITY_PLAYER_MOVING = new AnimationReference(10, + texmap.getTextureReference(0, 1, 2, 3), + texmap.getTextureReference(1, 2, 2, 3), + texmap.getTextureReference(2, 3, 2, 3), + texmap.getTextureReference(3, 4, 2, 3) + ); // Zombie - public static final TextureReference ENTITY_ZOMBIE_0 = texmap.getTextureReference(0, 16, 48, 64); - public static final TextureReference ENTITY_ZOMBIE_1 = texmap.getTextureReference(16, 32, 48, 64); - public static final TextureReference ENTITY_ZOMBIE_2 = texmap.getTextureReference(32, 48, 48, 64); - public static final TextureReference ENTITY_ZOMBIE_3 = texmap.getTextureReference(48, 64, 48, 64); - public static final TextureReference ENTITY_ZOMBIE = new AnimationReference( - 10, ENTITY_ZOMBIE_0, ENTITY_ZOMBIE_1, ENTITY_ZOMBIE_2, ENTITY_ZOMBIE_3); + public static final TextureReference ENTITY_ZOMBIE = new AnimationReference(10, + texmap.getTextureReference(0, 1, 3, 4), + texmap.getTextureReference(1, 2, 3, 4), + texmap.getTextureReference(2, 3, 3, 4), + texmap.getTextureReference(3, 4, 3, 4) + ); + // Water + public static final TextureReference TILE_WATER = new AnimationReference(20, + texmap.getTextureReference(0, 1, 8, 9), + texmap.getTextureReference(1, 2, 8, 9), + texmap.getTextureReference(2, 3, 8, 9), + texmap.getTextureReference(3, 4, 8, 9), + texmap.getTextureReference(4, 5, 8, 9), + texmap.getTextureReference(5, 6, 8, 9), + texmap.getTextureReference(6, 7, 8, 9), + texmap.getTextureReference(7, 8, 8, 9), + texmap.getTextureReference(8, 9, 8, 9), + texmap.getTextureReference(9, 10, 8, 9), + texmap.getTextureReference(10, 11, 8, 9), + texmap.getTextureReference(11, 12, 8, 9), + texmap.getTextureReference(12, 13, 8, 9), + texmap.getTextureReference(13, 14, 8, 9), + texmap.getTextureReference(14, 15, 8, 9), + texmap.getTextureReference(15, 16, 8, 9) + ); + + // Lava + public static final TextureReference TILE_LAVA = new AnimationReference(10, + texmap.getTextureReference(0, 1, 6, 7), + texmap.getTextureReference(1, 2, 6, 7), + texmap.getTextureReference(2, 3, 6, 7), + texmap.getTextureReference(3, 4, 6, 7), + texmap.getTextureReference(4, 5, 6, 7), + texmap.getTextureReference(5, 6, 6, 7), + texmap.getTextureReference(6, 7, 6, 7), + texmap.getTextureReference(7, 8, 6, 7), + texmap.getTextureReference(8, 9, 6, 7), + texmap.getTextureReference(9, 10, 6, 7), + texmap.getTextureReference(10, 11, 6, 7), + texmap.getTextureReference(11, 12, 6, 7), + texmap.getTextureReference(12, 13, 6, 7), + texmap.getTextureReference(13, 14, 6, 7), + texmap.getTextureReference(14, 15, 6, 7), + texmap.getTextureReference(15, 16, 6, 7) + ); + + // Water flow + public static final TextureReference TILE_WATER_FLOW = new AnimationReference(20, + texmap.getTextureReference(0, 1, 9, 10), + texmap.getTextureReference(1, 2, 9, 10), + texmap.getTextureReference(2, 3, 9, 10), + texmap.getTextureReference(3, 4, 9, 10), + texmap.getTextureReference(4, 5, 9, 10), + texmap.getTextureReference(5, 6, 9, 10), + texmap.getTextureReference(6, 7, 9, 10), + texmap.getTextureReference(7, 8, 9, 10), + texmap.getTextureReference(8, 9, 9, 10), + texmap.getTextureReference(9, 10, 9, 10), + texmap.getTextureReference(10, 11, 9, 10), + texmap.getTextureReference(11, 12, 9, 10), + texmap.getTextureReference(12, 13, 9, 10), + texmap.getTextureReference(13, 14, 9, 10), + texmap.getTextureReference(14, 15, 9, 10), + texmap.getTextureReference(15, 16, 9, 10) + ); + + // Lava flow + public static final TextureReference TILE_LAVA_FLOW = new AnimationReference(10, + texmap.getTextureReference(0, 1, 7, 8), + texmap.getTextureReference(1, 2, 7, 8), + texmap.getTextureReference(2, 3, 7, 8), + texmap.getTextureReference(3, 4, 7, 8), + texmap.getTextureReference(4, 5, 7, 8), + texmap.getTextureReference(5, 6, 7, 8), + texmap.getTextureReference(6, 7, 7, 8), + texmap.getTextureReference(7, 8, 7, 8), + texmap.getTextureReference(8, 9, 7, 8), + texmap.getTextureReference(9, 10, 7, 8), + texmap.getTextureReference(10, 11, 7, 8), + texmap.getTextureReference(11, 12, 7, 8), + texmap.getTextureReference(12, 13, 7, 8), + texmap.getTextureReference(13, 14, 7, 8), + texmap.getTextureReference(14, 15, 7, 8), + texmap.getTextureReference(15, 16, 7, 8) + ); } diff --git a/src/shootergame/init/Tiles.java b/src/shootergame/init/Tiles.java index 9cf53bd..06f0586 100644 --- a/src/shootergame/init/Tiles.java +++ b/src/shootergame/init/Tiles.java @@ -4,9 +4,15 @@ import shootergame.tiles.Tile; import shootergame.tiles.TileDirt; import shootergame.tiles.TileFire; import shootergame.tiles.TileGrass; +import shootergame.tiles.TileLava; +import shootergame.tiles.TileLavaFlow; +import shootergame.tiles.TileRock; import shootergame.tiles.TileSand; import shootergame.tiles.TileStone; import shootergame.tiles.TileTree; +import shootergame.tiles.TileVoid; +import shootergame.tiles.TileWater; +import shootergame.tiles.TileWaterFlow; public class Tiles { @@ -16,4 +22,10 @@ public class Tiles public static final Tile STONE = new TileStone("stone"); public static final Tile DIRT = new TileDirt("dirt"); public static final Tile TREE = new TileTree("tree"); + public static final Tile VOID = new TileVoid("void"); + public static final Tile ROCK = new TileRock("rock"); + public static final Tile LAVA = new TileLava("lava"); + public static final Tile WATER = new TileWater("water"); + public static final Tile LAVA_FLOW = new TileLavaFlow("lava_flow"); + public static final Tile WATER_FLOW = new TileWaterFlow("water_flow"); } diff --git a/src/shootergame/input/JoystickCallback.java b/src/shootergame/input/JoystickCallback.java new file mode 100644 index 0000000..4f2abba --- /dev/null +++ b/src/shootergame/input/JoystickCallback.java @@ -0,0 +1,189 @@ +package shootergame.input; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.FloatBuffer; +import java.util.ArrayList; + +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWGamepadState; +import org.lwjgl.glfw.GLFWJoystickCallbackI; + +import mainloop.task.IMainloopTask; +import shootergame.Main; +import shootergame.entity.Entity; +import shootergame.entity.EntityBullet; +import shootergame.util.math.MathHelpers; +import shootergame.util.math.vec.Vec2d; + +public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask +{ + public static final JoystickCallback JOYSTICK_CALLBACK = new JoystickCallback(); + private ArrayList connections = new ArrayList(); + + @Override + public void invoke(int jid, int event) + { + // Gamepad connect + if(event == GLFW.GLFW_CONNECTED) + { + // Log the event and add the connection + System.out.println("Gamepad "+jid+" connected"); + connections.add(jid); + } + + // Gamepad disconnect + else if(event == GLFW.GLFW_DISCONNECTED) + { + // Log the event + System.out.println("Gamepad "+jid+" disconnected"); + + // Loop over the connections + for(int i=0;i 10; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + private float combineJoystickAxis(float a, float b) + { + if(b > 0.2 || b < -0.2) a += b; + return a; + } + + @Override + public void MainLoopUpdate() + { + + + // Gamepad properties + float left_x = 0; + float left_y = 0; + float left_trigger = 0; + float right_x = 0; + float right_y = 0; + float right_trigger = 0; + boolean left_stick_button = false; + boolean right_stick_button = false; + boolean button_a = false; + boolean button_b = false; + boolean button_x = false; + boolean button_y = false; + boolean button_start = false; + boolean button_back = false; + boolean button_home = false; + boolean dpad_up = false; + boolean dpad_down = false; + boolean dpad_left = false; + boolean dpad_right = false; + boolean shoulder_left = false; + boolean shoulder_right = false; + + // Loop over all the connected gamepads + for(int jid : connections) + { + // Get all the axes + FloatBuffer axes = GLFW.glfwGetJoystickAxes(jid); + + // Store all the axes data + left_x = combineJoystickAxis(left_x, axes.get(0)); + left_y = combineJoystickAxis(left_y, axes.get(1)); + right_x = combineJoystickAxis(right_x, axes.get(3)); + right_y = combineJoystickAxis(right_y, axes.get(4)); + left_trigger = combineJoystickAxis(left_trigger, axes.get(2)); + right_trigger = combineJoystickAxis(right_trigger, axes.get(5)); + + // Get all the buttons + ByteBuffer buttons = GLFW.glfwGetJoystickButtons(jid); + + // Store all the button data + button_a = buttons.get(0) == 1 || button_a; + button_b = buttons.get(1) == 1 || button_b; + button_x = buttons.get(2) == 1 || button_x; + button_y = buttons.get(3) == 1 || button_y; + shoulder_left = buttons.get(4) == 1 || shoulder_left; + shoulder_right = buttons.get(5) == 1 || shoulder_right; + button_back = buttons.get(6) == 1 || button_back; + button_start = buttons.get(7) == 1 || button_start; + button_home = buttons.get(8) == 1 || button_home; + left_stick_button = buttons.get(9) == 1 || left_stick_button; + right_stick_button = buttons.get(10) == 1 || right_stick_button; + dpad_left = buttons.get(11) == 1 || buttons.get(18) == 1 || dpad_left; + dpad_right = buttons.get(12) == 1 || buttons.get(16) == 1 || dpad_right; + dpad_up = buttons.get(13) == 1 || buttons.get(15) == 1 || dpad_up; + dpad_down = buttons.get(14) == 1 || buttons.get(17) == 1 || dpad_down; + } + + // Is the left stick moved into a position (movement stick) + if(left_x > 0.3 || left_x < -0.3 || left_y > 0.3 || left_y < -0.3) + { + // Get the the angle + double angle = Math.toDegrees(Math.atan2(left_y, left_x)) + 90; + + // Move the player in the left sticks angle + Main.player.angle += angle; + Main.player.moveForward(); + Main.player.angle -= angle; + + // Set the players moving to true + Main.player.moving = true; + } + + // 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 + double angle = Math.toDegrees(Math.atan2(right_y, right_x)) + 90; + + // Summon bullets at this angle relative to the player + Entity bullet = new EntityBullet(); + bullet.angle = angle + Main.player.angle; + bullet.pos = new Vec2d(Main.player.pos.x / 2, Main.player.pos.y / 2); + Main.world.getLayer().spawnEntity(bullet); + } + + if(shoulder_left) { + Main.player.angle -= 1; + } + + if(shoulder_right) { + Main.player.angle += 1; + } + } + +} diff --git a/src/shootergame/mainloop/MainloopEventHandler.java b/src/shootergame/mainloop/MainloopEventHandler.java index 7d5ace8..5d5234a 100644 --- a/src/shootergame/mainloop/MainloopEventHandler.java +++ b/src/shootergame/mainloop/MainloopEventHandler.java @@ -9,6 +9,7 @@ public class MainloopEventHandler implements IMainloopEvent, IMainloopTask public static final MainloopEventHandler MAINLOOP_EVENT_HANDLER = new MainloopEventHandler(); public long mspf = 1000/60; + private long max_mspf = 1; @Override public void onClose() { @@ -18,7 +19,7 @@ public class MainloopEventHandler implements IMainloopEvent, IMainloopTask @Override public void onEarly() { mspf -= 1; - if(mspf < 1) mspf = 1; + if(mspf < max_mspf) mspf = 1; } @Override diff --git a/src/shootergame/resources/mono_crash.d4716fb0.0.json b/src/shootergame/resources/mono_crash.d4716fb0.0.json new file mode 100644 index 0000000..2273c0b --- /dev/null +++ b/src/shootergame/resources/mono_crash.d4716fb0.0.json @@ -0,0 +1,76 @@ +{ + "protocol_version" : "0.0.2", + "configuration" : { + "version" : "5.18.0.240 (Debian 5.18.0.240+dfsg-2ubuntu2 Wed Apr 17 23:39:09 UTC 2019)", + "tlc" : "__thread", + "sigsgev" : "altstack", + "notifications" : "epoll", + "architecture" : "amd64", + "disabled_features" : "none", + "smallconfig" : "disabled", + "bigarrays" : "disabled", + "softdebug" : "enabled", + "interpreter" : "enabled", + "llvm_support" : "disabled", + "suspend" : "preemptive" + }, + "memory" : { + "minor_gc_time" : "378907", + "major_gc_time" : "34891", + "minor_gc_count" : "24", + "major_gc_count" : "1", + "major_gc_time_concurrent" : "29295" + }, + "threads" : [ + { + "is_managed" : false, + "crashed" : true, + "managed_thread_ptr" : "0x0", + "native_thread_id" : "0x7fee3d843700", + "thread_info_addr" : "0x0", + "thread_name" : "Finalizer", + "ctx" : { + "IP" : "0x7fee40fd8ed7", + "SP" : "0x7fee3d842690", + "BP" : "0x7fee3d8429e0" + }, + "managed_frames" : [ + { + "native_address" : "unregistered" + } + + ], + "unmanaged_frames" : [ + { + "native_address" : "unregistered" + } + + ] +}, +{ + "is_managed" : false, + "crashed" : false, + "managed_thread_ptr" : "0x0", + "native_thread_id" : "0x7fee40f90780", + "thread_info_addr" : "0x0", + "thread_name" : "mono", + "ctx" : { + "IP" : "0x7fee410a6729", + "SP" : "0x7ffe255e5640", + "BP" : "0x3" + }, + "managed_frames" : [ + { + "native_address" : "unregistered" + } + + ], +"unmanaged_frames" : [ +{ + "native_address" : "unregistered" + } + +] +} +] +} \ No newline at end of file diff --git a/src/shootergame/resources/mono_crash.d4716fb0.1.json b/src/shootergame/resources/mono_crash.d4716fb0.1.json new file mode 100644 index 0000000..cd25f68 --- /dev/null +++ b/src/shootergame/resources/mono_crash.d4716fb0.1.json @@ -0,0 +1,76 @@ +{ + "protocol_version" : "0.0.2", + "configuration" : { + "version" : "5.18.0.240 (Debian 5.18.0.240+dfsg-2ubuntu2 Wed Apr 17 23:39:09 UTC 2019)", + "tlc" : "__thread", + "sigsgev" : "altstack", + "notifications" : "epoll", + "architecture" : "amd64", + "disabled_features" : "none", + "smallconfig" : "disabled", + "bigarrays" : "disabled", + "softdebug" : "enabled", + "interpreter" : "enabled", + "llvm_support" : "disabled", + "suspend" : "preemptive" + }, + "memory" : { + "minor_gc_time" : "41590", + "major_gc_time" : "0", + "minor_gc_count" : "2", + "major_gc_count" : "0", + "major_gc_time_concurrent" : "0" + }, + "threads" : [ + { + "is_managed" : false, + "crashed" : false, + "managed_thread_ptr" : "0x0", + "native_thread_id" : "0x7fa2fae57780", + "thread_info_addr" : "0x0", + "thread_name" : "mono", + "ctx" : { + "IP" : "0x7fa2faef4be9", + "SP" : "0x7ffee36a8db0", + "BP" : "0x100" + }, + "managed_frames" : [ + { + "native_address" : "unregistered" + } + + ], + "unmanaged_frames" : [ + { + "native_address" : "unregistered" + } + + ] +}, +{ + "is_managed" : false, + "crashed" : true, + "managed_thread_ptr" : "0x0", + "native_thread_id" : "0x7fa2f753d700", + "thread_info_addr" : "0x0", + "thread_name" : "Finalizer", + "ctx" : { + "IP" : "0x7fa2fae9fed7", + "SP" : "0x7fa2f753c690", + "BP" : "0x7fa2f753c9e0" + }, + "managed_frames" : [ + { + "native_address" : "unregistered" + } + + ], +"unmanaged_frames" : [ +{ + "native_address" : "unregistered" + } + +] +} +] +} \ No newline at end of file diff --git a/src/shootergame/resources/texmap.png b/src/shootergame/resources/texmap.png index dcc3f83..962f35f 100644 Binary files a/src/shootergame/resources/texmap.png and b/src/shootergame/resources/texmap.png differ diff --git a/src/shootergame/resources/texmap.xcf b/src/shootergame/resources/texmap.xcf new file mode 100644 index 0000000..cb1632a Binary files /dev/null and b/src/shootergame/resources/texmap.xcf differ diff --git a/src/shootergame/text/Text.java b/src/shootergame/text/Text.java new file mode 100644 index 0000000..bce9d2c --- /dev/null +++ b/src/shootergame/text/Text.java @@ -0,0 +1,176 @@ +package shootergame.text; + +import shootergame.init.Textures; +import shootergame.util.gl.GlHelpers; +import shootergame.util.gl.texture.TextureReference; +import shootergame.util.math.vec.Vec2d; + +public class Text +{ + public static final TextureReference CHAR_A = Textures.texmap.getTextureReference(5, 6, 0, 1); + public static final TextureReference CHAR_B = Textures.texmap.getTextureReference(6, 7, 0, 1); + public static final TextureReference CHAR_C = Textures.texmap.getTextureReference(7, 8, 0, 1); + public static final TextureReference CHAR_D = Textures.texmap.getTextureReference(8, 9, 0, 1); + public static final TextureReference CHAR_E = Textures.texmap.getTextureReference(9, 10, 0, 1); + public static final TextureReference CHAR_F = Textures.texmap.getTextureReference(10, 11, 0, 1); + public static final TextureReference CHAR_G = Textures.texmap.getTextureReference(11, 12, 0, 1); + public static final TextureReference CHAR_H = Textures.texmap.getTextureReference(12, 13, 0, 1); + public static final TextureReference CHAR_I = Textures.texmap.getTextureReference(13, 14, 0, 1); + public static final TextureReference CHAR_J = Textures.texmap.getTextureReference(14, 15, 0, 1); + public static final TextureReference CHAR_K = Textures.texmap.getTextureReference(15, 16, 0, 1); + public static final TextureReference CHAR_L = Textures.texmap.getTextureReference(5, 6, 1, 2); + public static final TextureReference CHAR_M = Textures.texmap.getTextureReference(6, 7, 1, 2); + public static final TextureReference CHAR_N = Textures.texmap.getTextureReference(7, 8, 1, 2); + public static final TextureReference CHAR_O = Textures.texmap.getTextureReference(8, 9, 1, 2); + public static final TextureReference CHAR_P = Textures.texmap.getTextureReference(9, 10, 1, 2); + public static final TextureReference CHAR_Q = Textures.texmap.getTextureReference(10, 11, 1, 2); + public static final TextureReference CHAR_R = Textures.texmap.getTextureReference(11, 12, 1, 2); + public static final TextureReference CHAR_S = Textures.texmap.getTextureReference(12, 13, 1, 2); + public static final TextureReference CHAR_T = Textures.texmap.getTextureReference(13, 14, 1, 2); + public static final TextureReference CHAR_U = Textures.texmap.getTextureReference(14, 15, 1, 2); + public static final TextureReference CHAR_V = Textures.texmap.getTextureReference(15, 16, 1, 2); + public static final TextureReference CHAR_W = Textures.texmap.getTextureReference(5, 6, 2, 3); + public static final TextureReference CHAR_X = Textures.texmap.getTextureReference(6, 7, 2, 3); + public static final TextureReference CHAR_Y = Textures.texmap.getTextureReference(7, 8, 2, 3); + public static final TextureReference CHAR_Z = Textures.texmap.getTextureReference(8, 9, 2, 3); + public static final TextureReference CHAR_a = Textures.texmap.getTextureReference(9, 10, 2, 3); + public static final TextureReference CHAR_b = Textures.texmap.getTextureReference(10, 11, 2, 3); + public static final TextureReference CHAR_c = Textures.texmap.getTextureReference(11, 12, 2, 3); + public static final TextureReference CHAR_d = Textures.texmap.getTextureReference(12, 13, 2, 3); + public static final TextureReference CHAR_e = Textures.texmap.getTextureReference(13, 14, 2, 3); + public static final TextureReference CHAR_f = Textures.texmap.getTextureReference(14, 15, 2, 3); + public static final TextureReference CHAR_g = Textures.texmap.getTextureReference(15, 16, 2, 3); + public static final TextureReference CHAR_h = Textures.texmap.getTextureReference(5, 6, 3, 4); + public static final TextureReference CHAR_i = Textures.texmap.getTextureReference(6, 7, 3, 4); + public static final TextureReference CHAR_j = Textures.texmap.getTextureReference(7, 8, 3, 4); + public static final TextureReference CHAR_k = Textures.texmap.getTextureReference(8, 9, 3, 4); + public static final TextureReference CHAR_l = Textures.texmap.getTextureReference(9, 10, 3, 4); + public static final TextureReference CHAR_m = Textures.texmap.getTextureReference(10, 11, 3, 4); + public static final TextureReference CHAR_n = Textures.texmap.getTextureReference(11, 12, 3, 4); + public static final TextureReference CHAR_o = Textures.texmap.getTextureReference(12, 13, 3, 4); + public static final TextureReference CHAR_p = Textures.texmap.getTextureReference(13, 14, 3, 4); + public static final TextureReference CHAR_q = Textures.texmap.getTextureReference(14, 15, 3, 4); + public static final TextureReference CHAR_r = Textures.texmap.getTextureReference(15, 16, 3, 4); + public static final TextureReference CHAR_s = Textures.texmap.getTextureReference(5, 6, 4, 5); + public static final TextureReference CHAR_t = Textures.texmap.getTextureReference(6, 7, 4, 5); + public static final TextureReference CHAR_u = Textures.texmap.getTextureReference(7, 8, 4, 5); + public static final TextureReference CHAR_v = Textures.texmap.getTextureReference(8, 9, 4, 5); + public static final TextureReference CHAR_w = Textures.texmap.getTextureReference(9, 10, 4, 5); + public static final TextureReference CHAR_x = Textures.texmap.getTextureReference(10, 11, 4, 5); + public static final TextureReference CHAR_y = Textures.texmap.getTextureReference(11, 12, 4, 5); + public static final TextureReference CHAR_z = Textures.texmap.getTextureReference(12, 13, 4, 5); + public static final TextureReference CHAR_0 = Textures.texmap.getTextureReference(13, 14, 4, 5); + public static final TextureReference CHAR_1 = Textures.texmap.getTextureReference(14, 15, 4, 5); + public static final TextureReference CHAR_2 = Textures.texmap.getTextureReference(15, 16, 4, 5); + public static final TextureReference CHAR_3 = Textures.texmap.getTextureReference(5, 6, 5, 6); + public static final TextureReference CHAR_4 = Textures.texmap.getTextureReference(6, 7, 5, 6); + public static final TextureReference CHAR_5 = Textures.texmap.getTextureReference(7, 8, 5, 6); + public static final TextureReference CHAR_6 = Textures.texmap.getTextureReference(8, 9, 5, 6); + public static final TextureReference CHAR_7 = Textures.texmap.getTextureReference(9, 10, 5, 6); + public static final TextureReference CHAR_8 = Textures.texmap.getTextureReference(10, 11, 5, 6); + public static final TextureReference CHAR_9 = Textures.texmap.getTextureReference(11, 12, 5, 6); + public static final TextureReference CHAR_PEROID = Textures.texmap.getTextureReference(12, 13, 5, 6); + public static final TextureReference CHAR_COLON = Textures.texmap.getTextureReference(13, 14, 5, 6); + public static final TextureReference CHAR_COMMA = Textures.texmap.getTextureReference(14, 15, 5, 6); + public static final TextureReference CHAR_EXMARK = Textures.texmap.getTextureReference(15, 16, 5, 6); + + public static void render(String text, Vec2d size) + { + // Get the bytes from the string + byte[] text_b = text.getBytes(); + + double sx = size.x; + double sy = size.y; + + // Begin quads + GlHelpers.begin(); + + // Loop over the bytes + for(int i=0;i implements Iterable> } } - // Send back an empty (unloaded) chunk + // Send back an empty object return this.map2d_i.getEmpty(pos); } @@ -64,6 +64,24 @@ public class Map2D implements Iterable> } } } + + public boolean contains(Vec2i pos) + { + // Loop over the elements + for(int i=0;i e = this.elements.get(i); + + // Return true if the position is the same + if(e.pos.equal(pos)) { + return true; + } + } + + // Return false if nothing was found + return false; + } @Override public Iterator> iterator() { diff --git a/src/shootergame/util/math/vec/Vec2d.java b/src/shootergame/util/math/vec/Vec2d.java index 9ec875a..6bd3581 100644 --- a/src/shootergame/util/math/vec/Vec2d.java +++ b/src/shootergame/util/math/vec/Vec2d.java @@ -40,4 +40,8 @@ public class Vec2d public Vec2d divide(Vec2d other) { return new Vec2d(this.x / other.x, this.y / other.y); } + + public Vec2d copy() { + return new Vec2d(x, y); + } } diff --git a/src/shootergame/util/math/vec/Vec2i.java b/src/shootergame/util/math/vec/Vec2i.java index c1bfeea..a4fb59c 100644 --- a/src/shootergame/util/math/vec/Vec2i.java +++ b/src/shootergame/util/math/vec/Vec2i.java @@ -36,10 +36,10 @@ public class Vec2i public static Vec2i fromId(Range2i range, int id) { - int x = id % range.mx; + int x = MathHelpers.mod(id, range.mx); id -= x; id /= range.mx; - int y = id % range.my; + int y = MathHelpers.mod(id, range.my); return new Vec2i(x, y); } @@ -63,4 +63,8 @@ public class Vec2i public Vec2i divide(Vec2i other) { return new Vec2i(this.x / other.x, this.y / other.y); } + + public Vec2i copy() { + return new Vec2i(x, y); + } } diff --git a/src/shootergame/util/math/vec/Vec3d.java b/src/shootergame/util/math/vec/Vec3d.java index 29c3936..4516563 100644 --- a/src/shootergame/util/math/vec/Vec3d.java +++ b/src/shootergame/util/math/vec/Vec3d.java @@ -42,4 +42,8 @@ public class Vec3d public Vec3d divide(Vec3d other) { return new Vec3d(this.x / other.x, this.y / other.y, this.z / other.z); } + + public Vec3d copy() { + return new Vec3d(x, y, z); + } } diff --git a/src/shootergame/util/math/vec/Vec3i.java b/src/shootergame/util/math/vec/Vec3i.java index c87e414..48eecef 100644 --- a/src/shootergame/util/math/vec/Vec3i.java +++ b/src/shootergame/util/math/vec/Vec3i.java @@ -40,13 +40,13 @@ public class Vec3i public static Vec3i fromId(Range3i range, int id) { - int x = id % range.mx; + int x = MathHelpers.mod(id, range.mx); id -= x; id /= range.mx; - int y = id % range.my; + int y = MathHelpers.mod(id, range.my); id -= y; id /= range.my; - int z = id % range.mz; + int z = MathHelpers.mod(id, range.mz); return new Vec3i(x, y, z); } @@ -70,4 +70,8 @@ public class Vec3i public Vec3i divide(Vec3i other) { return new Vec3i(this.x / other.x, this.y / other.y, this.z / other.z); } + + public Vec3i copy() { + return new Vec3i(x, y, z); + } } diff --git a/src/shootergame/world/World.java b/src/shootergame/world/World.java new file mode 100644 index 0000000..aae786f --- /dev/null +++ b/src/shootergame/world/World.java @@ -0,0 +1,52 @@ +package shootergame.world; + +import java.util.ArrayList; +import java.util.Random; + +import shootergame.display.Camera; +import shootergame.world.layer.Layer; +import shootergame.world.layer.layergen.LayerGen; + +public class World +{ + private ArrayList layers = new ArrayList(); + private int layer_id = 0; + private Layer layer; + + public World(Random rand, LayerGen ... layergen) + { + // Loop over the layer generators + for(LayerGen lg : layergen) + { + // Create new layers + layers.add(new Layer(rand, lg)); + } + + // Set the current layer + layer = layers.get(layer_id); + } + + public void render(Camera camera) { + layer.render(camera); + } + + public void tickEntities() { + layer.tickEntities(); + } + + public void spawnRandomEntities() { + layer.spawnRandomEntities(); + } + + public int getLayerID() { + return layer_id; + } + + public void setLayerID(int id) { + layer_id = id; + } + + public Layer getLayer() { + return layer; + } +} diff --git a/src/shootergame/world/chunk/Chunk.java b/src/shootergame/world/chunk/Chunk.java new file mode 100644 index 0000000..d57383e --- /dev/null +++ b/src/shootergame/world/chunk/Chunk.java @@ -0,0 +1,180 @@ +package shootergame.world.chunk; + +import java.util.ArrayList; +import java.util.Random; + +import shootergame.display.Camera; +import shootergame.entity.Entity; +import shootergame.init.Tiles; +import shootergame.tiles.Tile; +import shootergame.util.math.MathHelpers; +import shootergame.util.math.range.Range2i; +import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; +import shootergame.world.layer.Layer; + +public class Chunk +{ + public static final Range2i CHUNK_SIZE = new Range2i(16, 16); + public static final Chunk CHUNK_EMPTY = new ChunkEmpty(); + public static final int CHUNK_INDEX = CHUNK_SIZE.mx * CHUNK_SIZE.my; + public static final int SIMULATION_DISTANCE = 5; + + private Tile tiles_back[] = new Tile[CHUNK_INDEX]; + private Tile tiles_front[] = new Tile[CHUNK_INDEX]; + private ArrayList entities = new ArrayList(); + private Random rand; + private Layer layer; + private Vec2i c_pos; + + public Chunk(Layer layer, Vec2i c_pos, Random rand) + { + // Set some specified values + this.rand = rand; + this.layer = layer; + this.c_pos = c_pos; + + // Loop over all the tiles in the chunk + for(int i=0;i getNearbyEntities(Vec2d pos, double distance) + { + // Get the list of entities to send back + ArrayList nearby_entities = new ArrayList(); + + // Loop over the entities + for(Entity e : entities) + { + if( + e.pos.x + distance < pos.x && + e.pos.x - distance > pos.x && + e.pos.y + distance < pos.y && + e.pos.y - distance > pos.y + ) { + nearby_entities.add(e); + } + } + + // Send back the entities + return nearby_entities; + } + +} diff --git a/src/shootergame/world/chunk/ChunkEmpty.java b/src/shootergame/world/chunk/ChunkEmpty.java new file mode 100644 index 0000000..d0a3c15 --- /dev/null +++ b/src/shootergame/world/chunk/ChunkEmpty.java @@ -0,0 +1,42 @@ +package shootergame.world.chunk; + +import shootergame.display.Camera; +import shootergame.entity.Entity; +import shootergame.init.Tiles; +import shootergame.tiles.Tile; +import shootergame.util.math.vec.Vec2i; + +public class ChunkEmpty extends Chunk +{ + + + public ChunkEmpty() { + super(null, null, null); + } + + @Override + public void render(Camera camera) {} + + @Override + public void tickEntities() {} + + @Override + public void spawnEntity(Entity e) {} + + @Override + public Tile getBackTile(Vec2i pos) { + return Tiles.VOID; + } + + @Override + public Tile getFrontTile(Vec2i pos) { + return Tiles.VOID; + } + + @Override + public void setBackTile(Tile tile, Vec2i pos) {} + + @Override + public void setFrontTile(Tile tile, Vec2i pos) {} + +} diff --git a/src/shootergame/world/chunk/ChunkEventHandler.java b/src/shootergame/world/chunk/ChunkEventHandler.java new file mode 100644 index 0000000..6b41041 --- /dev/null +++ b/src/shootergame/world/chunk/ChunkEventHandler.java @@ -0,0 +1,71 @@ +package shootergame.world.chunk; + +import java.util.Random; + +import mainloop.task.IMainloopTask; +import shootergame.Main; +import shootergame.util.math.map.Map2DElement; +import shootergame.util.math.random.RandomHelpers; +import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; +import shootergame.world.layer.Layer; + +public class ChunkEventHandler implements IMainloopTask +{ + public static final ChunkEventHandler CHUNK_EVENT_HANDLER = new ChunkEventHandler(); + + @Override + public boolean MainLoopDelay(long millis) { + return millis > 100; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public void MainLoopUpdate() + { + // Get the layer + Layer layer = Main.world.getLayer(); + + // Loop over all the chunks in this layer + for(Map2DElement ce : layer.chunks) + { + // Convert the player pos to x and y + int px = (int)Main.player.pos.x / 16; + int py = (int)Main.player.pos.y / 16; + + if( + // Is this chunk beyond the simulation distance + px > ce.pos.x + Chunk.SIMULATION_DISTANCE || + px < ce.pos.x - Chunk.SIMULATION_DISTANCE || + py > ce.pos.y + Chunk.SIMULATION_DISTANCE || + py < ce.pos.y - Chunk.SIMULATION_DISTANCE + ) { + // Unload the chunk + layer.unloadChunk(ce.pos); + } + } + + // Loop over the simulation distance + for(int x=-Chunk.SIMULATION_DISTANCE;x chunks; + private LayerGen layergen; + private Random rand; + private long seed; + + public Layer(Random rand, LayerGen layergen) + { + // Create the array of tiles + this.layergen = layergen; + this.seed = rand.nextLong(); + this.rand = new Random(); + this.chunks = new Map2D(layergen); + } + + public void render(Camera camera) + { + // Render every chunk in the players render distance + for(int x=-camera.renderDistance;x e : chunks) { + e.o.tickEntities(); + } + + // Move every entitiy in every loaded chunk + for(Map2DElement e : chunks) { + e.o.moveEntities(); + } + } + + public void spawnRandomEntities() { + this.layergen.spawnEntities(this, rand); + } + + public void setBackTile(Tile tile, Vec2i pos) + { + // Get the chunk pos + Vec2i c_pos = new Vec2i(pos.x / Chunk.CHUNK_SIZE.my, pos.y / Chunk.CHUNK_SIZE.my); + + // Set the chunks back tile + chunks.get(c_pos).setBackTile(tile, pos); + } + + public void setFrontTile(Tile tile, Vec2i pos) + { + // Get the chunk pos + Vec2i c_pos = new Vec2i(pos.x / Chunk.CHUNK_SIZE.my, pos.y / Chunk.CHUNK_SIZE.my); + + // Set the chunks front tile + chunks.get(c_pos).setFrontTile(tile, pos); + } + + public Tile getBackTile(Vec2i pos) + { + // Get the chunk pos + Vec2i c_pos = new Vec2i(pos.x / Chunk.CHUNK_SIZE.my, pos.y / Chunk.CHUNK_SIZE.my); + + // Get the chunks back tile + return chunks.get(c_pos).getBackTile(pos); + } + + public Tile getFrontTile(Vec2i pos) + { + // Get the chunk pos + Vec2i c_pos = new Vec2i(pos.x / Chunk.CHUNK_SIZE.my, pos.y / Chunk.CHUNK_SIZE.my); + + // Get the chunks front tile + return chunks.get(c_pos).getFrontTile(pos); + } + + public void spawnEntity(Entity entity) + { + // Get the chunk pos + Vec2i c_pos = new Vec2i((int)entity.pos.x / Chunk.CHUNK_SIZE.mx, (int)entity.pos.y / Chunk.CHUNK_SIZE.my); + + // Spawn the entity in the specified chunk + chunks.get(c_pos).spawnEntity(entity); + } + + public void loadChunk(Vec2i pos) { + chunks.set(pos, layergen.generateChunk(this, new Random(seed), pos)); + } + + public void unloadChunk(Vec2i pos) { + chunks.remove(pos); + } + + public boolean chunkLoaded(Vec2i pos) { + return chunks.contains(pos); + } + + public ArrayList getNearbyEntities(Vec2d pos, double distance) + { + // Create the list of nearby entities + ArrayList entities = new ArrayList(); + + Vec2i cpos = new Vec2i((int)pos.x / Chunk.CHUNK_SIZE.mx, (int)pos.y / Chunk.CHUNK_SIZE.my); + + for(int x=-1;x<=1;x++) { + for(int y=-1;y<=1;y++) + { + for(Entity e : chunks.get(new Vec2i(x+cpos.x, y+cpos.y)).getNearbyEntities(pos, distance)) { + entities.add(e); + } + } + } + + // Send back the list of nearby entities + return entities; + } +} diff --git a/src/shootergame/world/layer/layergen/LayerGen.java b/src/shootergame/world/layer/layergen/LayerGen.java new file mode 100644 index 0000000..9044318 --- /dev/null +++ b/src/shootergame/world/layer/layergen/LayerGen.java @@ -0,0 +1,19 @@ +package shootergame.world.layer.layergen; + +import java.util.Random; + +import shootergame.util.math.map.IMap2D; +import shootergame.util.math.vec.Vec2i; +import shootergame.world.chunk.Chunk; +import shootergame.world.layer.Layer; + +public abstract class LayerGen implements IMap2D +{ + public abstract Chunk generateChunk(Layer layer, Random rand, Vec2i pos); + public abstract void spawnEntities(Layer layer, Random rand); + + @Override + public Chunk getEmpty(Vec2i pos) { + return Chunk.CHUNK_EMPTY; + } +} diff --git a/src/shootergame/world/layer/layergen/LayerGenEarth.java b/src/shootergame/world/layer/layergen/LayerGenEarth.java new file mode 100644 index 0000000..561dfcd --- /dev/null +++ b/src/shootergame/world/layer/layergen/LayerGenEarth.java @@ -0,0 +1,80 @@ +package shootergame.world.layer.layergen; + +import java.util.Random; + +import shootergame.Main; +import shootergame.entity.Entity; +import shootergame.entity.EntityBullet; +import shootergame.entity.EntityZombie; +import shootergame.init.Tiles; +import shootergame.tiles.Tile; +import shootergame.util.math.random.OpenSimplexNoise; +import shootergame.util.math.random.RandomHelpers; +import shootergame.util.math.range.Range2i; +import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; +import shootergame.world.chunk.Chunk; +import shootergame.world.layer.Layer; + +public class LayerGenEarth extends LayerGen +{ + + @Override + public Chunk generateChunk(Layer layer, Random rand, Vec2i c_pos) + { + // Create the new chunk + Chunk chunk = new Chunk(layer, c_pos, rand); + + // Get the noise generator + OpenSimplexNoise noisegen_n = new OpenSimplexNoise(rand.nextLong()); + + // Loop over the layer dimensions and create land + for(int x=0;x 0.9) chunk.setFrontTile(Tiles.TREE, pos); + else if(rand.nextDouble() > 0.99) chunk.setFrontTile(Tiles.ROCK, pos); + else chunk.setFrontTile(Tiles.VOID, pos); + + // Terrain generation + if(noise_n < 20) { + chunk.setFrontTile(Tiles.WATER, pos); + chunk.setBackTile(Tiles.DIRT, pos); + } + else if(noise_n < 60) chunk.setBackTile(Tiles.GRASS, pos); + else if(noise_n < 80) chunk.setBackTile(Tiles.DIRT, pos); + else chunk.setBackTile(Tiles.STONE, pos); + } + } + + // Send back the new chunk + return chunk; + } + + @Override + public void spawnEntities(Layer layer, Random rand) + { + if(rand.nextDouble() > 0.99) + { + Entity zombie = new EntityZombie(); + zombie.pos = new Vec2d( + RandomHelpers.randrange(rand, (int)Main.player.pos.x - 100, (int)Main.player.pos.x + 100), + RandomHelpers.randrange(rand, (int)Main.player.pos.y - 100, (int)Main.player.pos.y + 100)); + layer.spawnEntity(zombie); + } + } +}