Fixed issues with converting positions to a chunk position

This commit is contained in:
josua 2019-08-25 11:15:45 +10:00
parent 6299f8a2c8
commit d1f2e0c5c8
10 changed files with 115 additions and 65 deletions

View File

@ -1,6 +1,7 @@
package shootergame.entity; package shootergame.entity;
import shootergame.display.Camera; import shootergame.display.Camera;
import shootergame.init.Textures;
import shootergame.util.gl.GlHelpers; import shootergame.util.gl.GlHelpers;
import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2d;
import shootergame.world.chunk.Chunk; import shootergame.world.chunk.Chunk;
@ -21,15 +22,15 @@ public class EntityBullet extends Entity
super.tick(chunk, layer); super.tick(chunk, layer);
// Move forward in the bullets angle, very quickly // Move forward in the bullets angle, very quickly
this.moveForward(0.1); this.moveForward(0.2);
// Loop over the nearby entities // Loop over the nearby entities
for(Entity e : chunk.getNearbyEntities(pos, 2)) for(Entity e : layer.getNearbyEntities(pos, 0.5))
{ {
// Is this a zombie // Is this a zombie
if(e instanceof EntityZombie) if(e instanceof EntityZombie)
{ {
System.out.println("Found Zombie"); //System.out.println("Found Zombie");
chunk.killEntity(this); chunk.killEntity(this);
return; return;
@ -39,7 +40,7 @@ public class EntityBullet extends Entity
// Increase time // Increase time
time++; time++;
if(time > 40) { if(time > 60) {
chunk.killEntity(this); chunk.killEntity(this);
} }
} }
@ -52,8 +53,7 @@ public class EntityBullet extends Entity
// Push the matrix, disable textures, colour, and translate the bullet // Push the matrix, disable textures, colour, and translate the bullet
GlHelpers.pushMatrix(); GlHelpers.pushMatrix();
GlHelpers.color3(1, 1, 0); GlHelpers.color3(0.8, 0.8, 0);
GlHelpers.translate(pos.x, pos.y, 0.4);
GlHelpers.disableTexture2d(); GlHelpers.disableTexture2d();
// Get the angle between the camera and the bullet // Get the angle between the camera and the bullet
@ -61,7 +61,7 @@ public class EntityBullet extends Entity
// Make the bullet upright // Make the bullet upright
GlHelpers.translate(0.1, 0, 0); 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.rotate(-angle_r, 0, 0, 1);
GlHelpers.translate(-0.1, 0, 0); GlHelpers.translate(-0.1, 0, 0);
@ -76,8 +76,8 @@ public class EntityBullet extends Entity
GlHelpers.end(); GlHelpers.end();
// Pop the matrix, remove the colour, and enable textures // Pop the matrix, remove the colour, and enable textures
GlHelpers.popMatrix();
GlHelpers.enableTexture2d(); GlHelpers.enableTexture2d();
GlHelpers.color3(1, 1, 1); GlHelpers.color3(1, 1, 1);
GlHelpers.popMatrix();
} }
} }

View File

@ -1,7 +1,9 @@
package shootergame.entity.player; package shootergame.entity.player;
import shootergame.Main;
import shootergame.display.Camera; import shootergame.display.Camera;
import shootergame.entity.Entity; import shootergame.entity.Entity;
import shootergame.entity.EntityBullet;
import shootergame.entity.EntityVertical; import shootergame.entity.EntityVertical;
import shootergame.init.Textures; import shootergame.init.Textures;
import shootergame.util.gl.texture.TextureReference; import shootergame.util.gl.texture.TextureReference;
@ -19,8 +21,10 @@ public class EntityPlayer extends EntityVertical
public boolean MOVE_RIGHT = false; public boolean MOVE_RIGHT = false;
public boolean moving = false; public boolean moving = false;
private int bullet_frequency = 0;
public EntityPlayer() { public EntityPlayer() {
this.pos = new Vec2d(0, 0); this.angle = 45;
} }
@Override @Override
@ -62,4 +66,19 @@ public class EntityPlayer extends EntityVertical
// Standing still // Standing still
else super.render(pos, camera, Textures.ENTITY_PLAYER_STILL, 1); 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);
}
}
} }

View File

@ -167,14 +167,9 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
// Is the right stick moved into a position (gun stick) // 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) 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; double angle = Math.toDegrees(Math.atan2(right_y, right_x)) + 90;
Main.player.fireBullet(angle);
// 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) { if(shoulder_left) {

View File

@ -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_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_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_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) public static void render(String text, Vec2d size)
{ {
@ -89,8 +105,9 @@ public class Text
for(int i=0;i<text_b.length;i++) for(int i=0;i<text_b.length;i++)
{ {
// Get the character from the string // Get the character from the string
TextureReference l = null; TextureReference l = CHAR_NULL;
switch(text_b[i]) { switch(text_b[i]) {
case(' '): l = null; break;
case('Q'): l = CHAR_Q; break; case('Q'): l = CHAR_Q; break;
case('W'): l = CHAR_W; break; case('W'): l = CHAR_W; break;
case('E'): l = CHAR_E; break; case('E'): l = CHAR_E; break;
@ -157,6 +174,21 @@ public class Text
case(','): l = CHAR_COMMA; break; case(','): l = CHAR_COMMA; break;
case('.'): l = CHAR_PEROID; break; case('.'): l = CHAR_PEROID; break;
case('!'): l = CHAR_EXMARK; break; case('!'): l = CHAR_EXMARK; break;
case('-'): l = CHAR_MINUS; break;
case('+'): l = CHAR_PLUS; break;
case('/'): l = CHAR_FSLASH; break;
case('\\'): l = CHAR_BSLASH; break;
case('='): l = CHAR_EQUALS; break;
case('_'): l = CHAR_USCORE; break;
case('('): l = CHAR_BRACKET_O; break;
case(')'): l = CHAR_BRACKET_C; break;
case('%'): l = CHAR_PERCENT; break;
case('|'): l = CHAR_VBAR; break;
case('?'): l = CHAR_QMARK; break;
case('$'): l = CHAR_DOLLAR; break;
case('#'): l = CHAR_HASHTAG; break;
case('>'): l = CHAR_G_THEN; break;
case('<'): l = CHAR_L_THEN; break;
} }
// Is the letter not null // Is the letter not null

View File

@ -24,28 +24,28 @@ public class GlHelpers
glEnd(); glEnd();
} }
public static void vertex3(float x, float y, float z) { public static void vertex3(double x, double y, double z) {
glVertex3f(x/10, y/10, z/10); glVertex3d(x/10, y/10, z/10);
} }
public static void vertex2(float x, float y) { public static void vertex2(double x, double y) {
glVertex2f(x/10, y/10); glVertex2d(x/10, y/10);
} }
public static void color3(float r, float g, float b) { public static void color3(double r, double g, double b) {
glColor3f(r, g, b); glColor3d(r, g, b);
} }
public static void color4(float r, float g, float b, float a) { public static void color4(double r, double g, double b, double a) {
glColor4f(r, g, b, a); glColor4d(r, g, b, a);
} }
public static void rotate(float a, float x, float y, float z) { public static void rotate(double a, double x, double y, double z) {
glRotatef(a, x, y, z); glRotated(a, x, y, z);
} }
public static void translate(float x, float y, float z) { public static void translate(double x, double y, double z) {
glTranslatef(x/10, y/10, z/10); glTranslated(x/10, y/10, z/10);
} }
public static void disableCullFace() { public static void disableCullFace() {
@ -97,20 +97,4 @@ public class GlHelpers
glPushMatrix(); glPushMatrix();
MATRIX_COUNT -= 1; 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);
}
} }

View File

@ -22,7 +22,7 @@ public class AnimationReference extends TextureReference
} }
@Override @Override
public void texCoord(float x, float y) { public void texCoord(double x, double y) {
c.texCoord(x, y); c.texCoord(x, y);
} }

View File

@ -23,7 +23,7 @@ public abstract class TextureReference
this.end_y = end_y; this.end_y = end_y;
} }
public void texCoord(float x, float y) public void texCoord(double x, double y)
{ {
// Create texture coordinates // Create texture coordinates
float cx = (float) ( MathHelpers.map(x, 0, 1, start_x, end_x) / (double) this.getMaxX() ); float cx = (float) ( MathHelpers.map(x, 0, 1, start_x, end_x) / (double) this.getMaxX() );

View File

@ -58,4 +58,15 @@ public class MathHelpers
public static int mod(int a, int b) { public static int mod(int a, int b) {
return (((a % b) + b) % b); return (((a % b) + b) % b);
} }
public static int floor(double a)
{
if(a < 0) {
return (int)a - 1;
}
else {
return (int)a;
}
}
} }

View File

@ -87,14 +87,12 @@ public class Chunk
Entity e = entities.get(i); Entity e = entities.get(i);
// Has the entity left the chunk // Has the entity left the chunk
int cx = c_pos.x; int cx = c_pos.x * CHUNK_SIZE.mx;
int cy = c_pos.y; int cy = c_pos.y * CHUNK_SIZE.my;
if(((int)e.pos.x) / Chunk.CHUNK_SIZE.mx != cx && ((int)e.pos.y) / Chunk.CHUNK_SIZE.my != cy) 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 // Process the entity by layer and remove the entity from the array
layer.spawnEntity(e); layer.spawnEntity(e);
entities.remove(i); entities.remove(i);
@ -164,10 +162,10 @@ public class Chunk
for(Entity e : entities) for(Entity e : entities)
{ {
if( if(
e.pos.x + distance < pos.x && e.pos.x + distance > pos.x &&
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.y - distance > pos.y e.pos.y - distance < pos.y
) { ) {
nearby_entities.add(e); nearby_entities.add(e);
} }

View File

@ -7,6 +7,7 @@ import shootergame.Main;
import shootergame.display.Camera; import shootergame.display.Camera;
import shootergame.entity.Entity; import shootergame.entity.Entity;
import shootergame.tiles.Tile; import shootergame.tiles.Tile;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.map.Map2D; import shootergame.util.math.map.Map2D;
import shootergame.util.math.map.Map2DElement; import shootergame.util.math.map.Map2DElement;
import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2d;
@ -67,7 +68,7 @@ public class Layer
public void setBackTile(Tile tile, Vec2i pos) public void setBackTile(Tile tile, Vec2i pos)
{ {
// Get the chunk 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 // Set the chunks back tile
chunks.get(c_pos).setBackTile(tile, pos); chunks.get(c_pos).setBackTile(tile, pos);
@ -76,7 +77,7 @@ public class Layer
public void setFrontTile(Tile tile, Vec2i pos) public void setFrontTile(Tile tile, Vec2i pos)
{ {
// Get the chunk 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 // Set the chunks front tile
chunks.get(c_pos).setFrontTile(tile, pos); chunks.get(c_pos).setFrontTile(tile, pos);
@ -85,16 +86,26 @@ public class Layer
public Tile getBackTile(Vec2i pos) public Tile getBackTile(Vec2i pos)
{ {
// Get the chunk 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 // Get the chunks back tile
return chunks.get(c_pos).getBackTile(pos); 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) public Tile getFrontTile(Vec2i pos)
{ {
// Get the chunk 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 // Get the chunks front tile
return chunks.get(c_pos).getFrontTile(pos); return chunks.get(c_pos).getFrontTile(pos);
@ -103,7 +114,7 @@ public class Layer
public void spawnEntity(Entity entity) public void spawnEntity(Entity entity)
{ {
// Get the chunk pos // 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 // Spawn the entity in the specified chunk
chunks.get(c_pos).spawnEntity(entity); chunks.get(c_pos).spawnEntity(entity);
@ -125,12 +136,12 @@ public class Layer
{ {
// Create the list of nearby entities // Create the list of nearby entities
ArrayList<Entity> entities = new ArrayList<Entity>(); ArrayList<Entity> entities = new ArrayList<Entity>();
Vec2i cpos = getChunkPosFromPos(pos);
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 x=-1;x<=1;x++) {
for(int y=-1;y<=1;y++) 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)) { for(Entity e : chunks.get(new Vec2i(x+cpos.x, y+cpos.y)).getNearbyEntities(pos, distance)) {
entities.add(e); entities.add(e);
} }