From d1f2e0c5c86128e1929ce1ec9983aee818b8f9cc Mon Sep 17 00:00:00 2001 From: josua Date: Sun, 25 Aug 2019 11:15:45 +1000 Subject: [PATCH] Fixed issues with converting positions to a chunk position --- src/shootergame/entity/EntityBullet.java | 16 ++++---- .../entity/player/EntityPlayer.java | 21 +++++++++- src/shootergame/input/JoystickCallback.java | 9 +---- src/shootergame/text/Text.java | 34 +++++++++++++++- src/shootergame/util/gl/GlHelpers.java | 40 ++++++------------- .../util/gl/texture/AnimationReference.java | 2 +- .../util/gl/texture/TextureReference.java | 2 +- src/shootergame/util/math/MathHelpers.java | 11 +++++ src/shootergame/world/chunk/Chunk.java | 20 +++++----- src/shootergame/world/layer/Layer.java | 25 ++++++++---- 10 files changed, 115 insertions(+), 65 deletions(-) diff --git a/src/shootergame/entity/EntityBullet.java b/src/shootergame/entity/EntityBullet.java index 4669767..63db052 100644 --- a/src/shootergame/entity/EntityBullet.java +++ b/src/shootergame/entity/EntityBullet.java @@ -1,6 +1,7 @@ package shootergame.entity; import shootergame.display.Camera; +import shootergame.init.Textures; import shootergame.util.gl.GlHelpers; import shootergame.util.math.vec.Vec2d; import shootergame.world.chunk.Chunk; @@ -21,15 +22,15 @@ public class EntityBullet extends Entity super.tick(chunk, layer); // Move forward in the bullets angle, very quickly - this.moveForward(0.1); + this.moveForward(0.2); // Loop over the nearby entities - for(Entity e : chunk.getNearbyEntities(pos, 2)) + for(Entity e : layer.getNearbyEntities(pos, 0.5)) { // Is this a zombie if(e instanceof EntityZombie) { - System.out.println("Found Zombie"); + //System.out.println("Found Zombie"); chunk.killEntity(this); return; @@ -39,7 +40,7 @@ public class EntityBullet extends Entity // Increase time time++; - if(time > 40) { + if(time > 60) { chunk.killEntity(this); } } @@ -52,8 +53,7 @@ public class EntityBullet extends Entity // 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.color3(0.8, 0.8, 0); GlHelpers.disableTexture2d(); // Get the angle between the camera and the bullet @@ -61,7 +61,7 @@ public class EntityBullet extends Entity // Make the bullet upright GlHelpers.translate(0.1, 0, 0); - GlHelpers.translate(pos.x, pos.y, 0); + GlHelpers.translate(pos.x, pos.y, 0.4); GlHelpers.rotate(-angle_r, 0, 0, 1); GlHelpers.translate(-0.1, 0, 0); @@ -76,8 +76,8 @@ public class EntityBullet extends Entity GlHelpers.end(); // Pop the matrix, remove the colour, and enable textures - GlHelpers.popMatrix(); GlHelpers.enableTexture2d(); GlHelpers.color3(1, 1, 1); + GlHelpers.popMatrix(); } } diff --git a/src/shootergame/entity/player/EntityPlayer.java b/src/shootergame/entity/player/EntityPlayer.java index 002ecba..136b56b 100644 --- a/src/shootergame/entity/player/EntityPlayer.java +++ b/src/shootergame/entity/player/EntityPlayer.java @@ -1,7 +1,9 @@ package shootergame.entity.player; +import shootergame.Main; import shootergame.display.Camera; import shootergame.entity.Entity; +import shootergame.entity.EntityBullet; import shootergame.entity.EntityVertical; import shootergame.init.Textures; import shootergame.util.gl.texture.TextureReference; @@ -19,8 +21,10 @@ public class EntityPlayer extends EntityVertical public boolean MOVE_RIGHT = false; public boolean moving = false; + private int bullet_frequency = 0; + public EntityPlayer() { - this.pos = new Vec2d(0, 0); + this.angle = 45; } @Override @@ -62,4 +66,19 @@ public class EntityPlayer extends EntityVertical // Standing still else super.render(pos, camera, Textures.ENTITY_PLAYER_STILL, 1); } + + public void fireBullet(double angle) + { + bullet_frequency += 1; + bullet_frequency %= 10; + + if(bullet_frequency == 0) + { + // Summon bullets at this angle relative to the player + Entity bullet = new EntityBullet(); + bullet.angle = angle + this.angle; + bullet.pos = pos.copy(); + Main.world.getLayer().spawnEntity(bullet); + } + } } diff --git a/src/shootergame/input/JoystickCallback.java b/src/shootergame/input/JoystickCallback.java index 4f2abba..1094c27 100644 --- a/src/shootergame/input/JoystickCallback.java +++ b/src/shootergame/input/JoystickCallback.java @@ -167,14 +167,9 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask // 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 + // Get the the angle and fire the bullet 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); + Main.player.fireBullet(angle); } if(shoulder_left) { diff --git a/src/shootergame/text/Text.java b/src/shootergame/text/Text.java index bce9d2c..919ed9a 100644 --- a/src/shootergame/text/Text.java +++ b/src/shootergame/text/Text.java @@ -73,6 +73,22 @@ public class Text 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 final TextureReference CHAR_MINUS = Textures.texmap.getTextureReference(0, 1, 10, 11); + public static final TextureReference CHAR_PLUS = Textures.texmap.getTextureReference(1, 2, 10, 11); + public static final TextureReference CHAR_FSLASH = Textures.texmap.getTextureReference(2, 3, 10, 11); + public static final TextureReference CHAR_BSLASH = Textures.texmap.getTextureReference(3, 4, 10, 11); + public static final TextureReference CHAR_EQUALS = Textures.texmap.getTextureReference(4, 5, 10, 11); + public static final TextureReference CHAR_USCORE = Textures.texmap.getTextureReference(5, 6, 10, 11); + public static final TextureReference CHAR_NULL = Textures.texmap.getTextureReference(6, 7, 10, 11); + public static final TextureReference CHAR_BRACKET_O = Textures.texmap.getTextureReference(7, 8, 10, 11); + public static final TextureReference CHAR_BRACKET_C = Textures.texmap.getTextureReference(8, 9, 10, 11); + public static final TextureReference CHAR_PERCENT = Textures.texmap.getTextureReference(9, 10, 10, 11); + public static final TextureReference CHAR_VBAR = Textures.texmap.getTextureReference(10, 11, 10, 11); + public static final TextureReference CHAR_QMARK = Textures.texmap.getTextureReference(11, 12, 10, 11); + public static final TextureReference CHAR_DOLLAR = Textures.texmap.getTextureReference(12, 13, 10, 11); + public static final TextureReference CHAR_HASHTAG = Textures.texmap.getTextureReference(13, 14, 10, 11); + public static final TextureReference CHAR_L_THEN = Textures.texmap.getTextureReference(14, 15, 10, 11); + public static final TextureReference CHAR_G_THEN = Textures.texmap.getTextureReference(15, 16, 10, 11); public static void render(String text, Vec2d size) { @@ -89,8 +105,9 @@ public class Text for(int i=0;i'): l = CHAR_G_THEN; break; + case('<'): l = CHAR_L_THEN; break; } // Is the letter not null diff --git a/src/shootergame/util/gl/GlHelpers.java b/src/shootergame/util/gl/GlHelpers.java index bd6182c..b61578b 100644 --- a/src/shootergame/util/gl/GlHelpers.java +++ b/src/shootergame/util/gl/GlHelpers.java @@ -24,28 +24,28 @@ public class GlHelpers glEnd(); } - public static void vertex3(float x, float y, float z) { - glVertex3f(x/10, y/10, z/10); + public static void vertex3(double x, double y, double z) { + glVertex3d(x/10, y/10, z/10); } - public static void vertex2(float x, float y) { - glVertex2f(x/10, y/10); + public static void vertex2(double x, double y) { + glVertex2d(x/10, y/10); } - public static void color3(float r, float g, float b) { - glColor3f(r, g, b); + public static void color3(double r, double g, double b) { + glColor3d(r, g, b); } - public static void color4(float r, float g, float b, float a) { - glColor4f(r, g, b, a); + public static void color4(double r, double g, double b, double a) { + glColor4d(r, g, b, a); } - public static void rotate(float a, float x, float y, float z) { - glRotatef(a, x, y, z); + public static void rotate(double a, double x, double y, double z) { + glRotated(a, x, y, z); } - public static void translate(float x, float y, float z) { - glTranslatef(x/10, y/10, z/10); + public static void translate(double x, double y, double z) { + glTranslated(x/10, y/10, z/10); } public static void disableCullFace() { @@ -97,20 +97,4 @@ public class GlHelpers glPushMatrix(); MATRIX_COUNT -= 1; } - - public static void translate(double x, double y, double z) { - translate((float)x, (float)y, (float)z); - } - - public static void rotate(double a, float x, float y, float z) { - rotate((float)a, x, y, z); - } - - public static void vertex3(double x, double y, double z) { - vertex3((float)x, (float)y, (float)z); - } - - public static void vertex2(double x, double y) { - vertex2((float)x, (float)y); - } } diff --git a/src/shootergame/util/gl/texture/AnimationReference.java b/src/shootergame/util/gl/texture/AnimationReference.java index b97f7c2..5b7ce19 100644 --- a/src/shootergame/util/gl/texture/AnimationReference.java +++ b/src/shootergame/util/gl/texture/AnimationReference.java @@ -22,7 +22,7 @@ public class AnimationReference extends TextureReference } @Override - public void texCoord(float x, float y) { + public void texCoord(double x, double y) { c.texCoord(x, y); } diff --git a/src/shootergame/util/gl/texture/TextureReference.java b/src/shootergame/util/gl/texture/TextureReference.java index 84d22b8..3b9a9fd 100644 --- a/src/shootergame/util/gl/texture/TextureReference.java +++ b/src/shootergame/util/gl/texture/TextureReference.java @@ -23,7 +23,7 @@ public abstract class TextureReference this.end_y = end_y; } - public void texCoord(float x, float y) + public void texCoord(double x, double y) { // Create texture coordinates float cx = (float) ( MathHelpers.map(x, 0, 1, start_x, end_x) / (double) this.getMaxX() ); diff --git a/src/shootergame/util/math/MathHelpers.java b/src/shootergame/util/math/MathHelpers.java index 6b7cdec..5e2ec51 100644 --- a/src/shootergame/util/math/MathHelpers.java +++ b/src/shootergame/util/math/MathHelpers.java @@ -58,4 +58,15 @@ public class MathHelpers public static int mod(int a, int b) { return (((a % b) + b) % b); } + + public static int floor(double a) + { + if(a < 0) { + return (int)a - 1; + } + + else { + return (int)a; + } + } } diff --git a/src/shootergame/world/chunk/Chunk.java b/src/shootergame/world/chunk/Chunk.java index d57383e..3a1b386 100644 --- a/src/shootergame/world/chunk/Chunk.java +++ b/src/shootergame/world/chunk/Chunk.java @@ -87,14 +87,12 @@ public class Chunk Entity e = entities.get(i); // Has the entity left the chunk - int cx = c_pos.x; - int cy = c_pos.y; - if(((int)e.pos.x) / Chunk.CHUNK_SIZE.mx != cx && ((int)e.pos.y) / Chunk.CHUNK_SIZE.my != cy) + int cx = c_pos.x * CHUNK_SIZE.mx; + int cy = c_pos.y * CHUNK_SIZE.my; + double px = e.pos.x; + double py = e.pos.y; + if(px > cx + CHUNK_SIZE.mx || px < cx || py > cy + CHUNK_SIZE.my || py < cy) { - System.out.print("Moved entity, "+cx+", "+cy+", "+e.pos.x+", "+e.pos.y); - System.out.print(", "+(int)e.pos.x / Chunk.CHUNK_SIZE.mx+", "+(int)e.pos.y / Chunk.CHUNK_SIZE.my); - System.out.println(); - // Process the entity by layer and remove the entity from the array layer.spawnEntity(e); entities.remove(i); @@ -164,10 +162,10 @@ public class Chunk 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 + 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); } diff --git a/src/shootergame/world/layer/Layer.java b/src/shootergame/world/layer/Layer.java index ab74c45..5351a60 100644 --- a/src/shootergame/world/layer/Layer.java +++ b/src/shootergame/world/layer/Layer.java @@ -7,6 +7,7 @@ import shootergame.Main; import shootergame.display.Camera; import shootergame.entity.Entity; import shootergame.tiles.Tile; +import shootergame.util.math.MathHelpers; import shootergame.util.math.map.Map2D; import shootergame.util.math.map.Map2DElement; import shootergame.util.math.vec.Vec2d; @@ -67,7 +68,7 @@ public class Layer 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); + Vec2i c_pos = getChunkPosFromPos(pos); // Set the chunks back tile chunks.get(c_pos).setBackTile(tile, pos); @@ -76,7 +77,7 @@ public class Layer 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); + Vec2i c_pos = getChunkPosFromPos(pos); // Set the chunks front tile chunks.get(c_pos).setFrontTile(tile, pos); @@ -85,16 +86,26 @@ public class Layer 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); + Vec2i c_pos = getChunkPosFromPos(pos); // Get the chunks back tile return chunks.get(c_pos).getBackTile(pos); } + private Vec2i getChunkPosFromPos(Vec2i pos) { + return this.getChunkPosFromPos(new Vec2i(pos.x, pos.y)); + } + + private Vec2i getChunkPosFromPos(Vec2d pos) { + return new Vec2i( + MathHelpers.floor(pos.x / Chunk.CHUNK_SIZE.mx), + MathHelpers.floor(pos.y / Chunk.CHUNK_SIZE.my)); + } + 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); + Vec2i c_pos = getChunkPosFromPos(pos); // Get the chunks front tile return chunks.get(c_pos).getFrontTile(pos); @@ -103,7 +114,7 @@ public class Layer 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); + Vec2i c_pos = getChunkPosFromPos(entity.pos); // Spawn the entity in the specified chunk chunks.get(c_pos).spawnEntity(entity); @@ -125,12 +136,12 @@ public class Layer { // 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); + Vec2i cpos = getChunkPosFromPos(pos); for(int x=-1;x<=1;x++) { for(int y=-1;y<=1;y++) { + //System.out.println("x: "+x+", y: "+y); for(Entity e : chunks.get(new Vec2i(x+cpos.x, y+cpos.y)).getNearbyEntities(pos, distance)) { entities.add(e); }