diff --git a/src/shootergame/Main.java b/src/shootergame/Main.java index 9457f28..5de9ec0 100644 --- a/src/shootergame/Main.java +++ b/src/shootergame/Main.java @@ -2,6 +2,8 @@ package shootergame; import mainloop.manager.MainloopManager; import shootergame.display.DisplayWindow; +import shootergame.entity.EntityEventHandler; +import shootergame.entity.player.Player; import shootergame.init.Textures; import shootergame.mainloop.MainloopEventHandler; @@ -9,6 +11,7 @@ public class Main { public static MainloopManager mainloop; public static DisplayWindow window; + public static Player player = new Player(); public static void main(String[] args) { @@ -20,9 +23,12 @@ public class Main window = new DisplayWindow("ShooterGame"); window.init(); - // Initialize the textures + // Initialise the textures Textures.initTextures(window); + // Initialise the entities + mainloop.register(EntityEventHandler.ENTITY_EVENT_HANDLER); + // Start the mainloop mainloop.start(); } diff --git a/src/shootergame/display/Camera.java b/src/shootergame/display/Camera.java new file mode 100644 index 0000000..2134b99 --- /dev/null +++ b/src/shootergame/display/Camera.java @@ -0,0 +1,19 @@ +package shootergame.display; + +import shootergame.util.math.MathHelpers; +import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec3d; + +public class Camera +{ + public Vec3d pos; + public Vec2d angle; + + public Camera(Vec3d pos, Vec2d angle, double distance) + { + 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)))); + } +} diff --git a/src/shootergame/display/DisplayRender.java b/src/shootergame/display/DisplayRender.java index f4a43f2..6b0de6c 100644 --- a/src/shootergame/display/DisplayRender.java +++ b/src/shootergame/display/DisplayRender.java @@ -2,12 +2,23 @@ package shootergame.display; import static org.lwjgl.opengl.GL11.*; +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.init.Textures; import shootergame.init.Tiles; 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 { @@ -15,11 +26,18 @@ public class DisplayRender { // Setup GL and clear the colour GL.createCapabilities(); - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, w, h); + // Push the matrix + GlHelpers.pushMatrix(); + // Enable some stuff GlHelpers.enableTexture2d(); + GlHelpers.enableBlend(); + GlHelpers.enableAlpha(); + GlHelpers.enableDepthTest(); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set the colour to white GlHelpers.color4(1, 1, 1, 1); @@ -27,9 +45,66 @@ public class DisplayRender // Bind the texmap Textures.texmap.bindTexture(); - Tiles.GRASS.render(new Vec2i(0, 0)); + GlHelpers.pushMatrix(); + + { + // Set matrix mode + glMatrixMode(GL_MODELVIEW); + + TransparentObjects.clear(); + + // Set the colour to white + GlHelpers.color4(1, 1, 1, 1); + + // Create a matrix + Matrix4f m = new Matrix4f(); + + // Set the perspective + m.setPerspective((float) Math.toRadians(45.0f), ((float)w)/((float)h), 0.01f, 100.0f); + try (MemoryStack stack = MemoryStack.stackPush()) { + glLoadMatrixf(m.get(stack.mallocFloat(16))); + } + + // Set the camera angle + m.setLookAt(0.0f, 0.0f, 10.0f, + 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); + + //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 objects = new ArrayList(); + + public static void clear() { + objects.clear(); + } + + public static void register(ITransparentObject object, Camera camera, Vec2d pos) + { + TransparentObject r_to = new TransparentObject(object, camera, pos); + ArrayList objects_n = new ArrayList(); + boolean added = false; + + // Loop over the transparent object items + for(TransparentObject to : objects) + { + if(r_to.distance > to.distance && !added) { + added = true; + objects_n.add(r_to); + } + + objects_n.add(to); + } + + if(!added) { + objects_n.add(r_to); + } + + objects = objects_n; + } + + public static void render(Camera camera) + { + // Loop over the objects and render all of them + for(TransparentObject to : objects) { + to.object.render(to.pos, camera); + } + } +} diff --git a/src/shootergame/entity/Entity.java b/src/shootergame/entity/Entity.java new file mode 100644 index 0000000..a981bc5 --- /dev/null +++ b/src/shootergame/entity/Entity.java @@ -0,0 +1,51 @@ +package shootergame.entity; + +import shootergame.display.Camera; +import shootergame.display.transparent.ITransparentObject; +import shootergame.display.transparent.TransparentObjects; +import shootergame.init.Entities; +import shootergame.util.math.vec.Vec2d; + +public class Entity implements ITransparentObject +{ + public Vec2d pos; + public double angle; + public boolean opaqueTile = true; + + public Entity(Vec2d pos, double angle) + { + // Add this entity to the list of entities + Entities.entities.add(this); + + // Store the specified values + this.angle = angle; + this.pos = pos; + } + + public Entity() { + this(new Vec2d(0, 0), 0); + } + + public void tick() { + } + + @Override + public void render(Vec2d pos, Camera camera) { + } + + public void doRender(Vec2d pos, Camera camera) + { + if(this.opaqueTile) { + TransparentObjects.register(this, camera, pos); + } + + else { + this.render(pos, camera); + } + } + + @Override + public boolean isOpaqueTile() { + return this.opaqueTile; + } +} diff --git a/src/shootergame/entity/EntityEventHandler.java b/src/shootergame/entity/EntityEventHandler.java new file mode 100644 index 0000000..3fc7e96 --- /dev/null +++ b/src/shootergame/entity/EntityEventHandler.java @@ -0,0 +1,30 @@ +package shootergame.entity; + +import java.util.ArrayList; + +import mainloop.task.IMainloopTask; +import shootergame.init.Entities; + +public class EntityEventHandler implements IMainloopTask +{ + public static final EntityEventHandler ENTITY_EVENT_HANDLER = new EntityEventHandler(); + + @Override + public boolean MainLoopDelay(long millis) { + return millis > 10; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public void MainLoopUpdate() + { + // Update each entity + for(Entity e : Entities.entities) { + e.tick(); + } + } +} diff --git a/src/shootergame/entity/EntityVertical.java b/src/shootergame/entity/EntityVertical.java new file mode 100644 index 0000000..1177aea --- /dev/null +++ b/src/shootergame/entity/EntityVertical.java @@ -0,0 +1,26 @@ +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; + +public class EntityVertical extends Entity +{ + private TextureReference tex; + private double h; + + public EntityVertical(TextureReference tex, double height) { + this.tex = tex; + this.h = height; + } + + @Override + public void render(Vec2d pos, Camera camera) { + super.render(pos, camera); + VerticalRender.render(pos, camera, tex, h); + } +} diff --git a/src/shootergame/entity/player/Player.java b/src/shootergame/entity/player/Player.java new file mode 100644 index 0000000..4a2108f --- /dev/null +++ b/src/shootergame/entity/player/Player.java @@ -0,0 +1,52 @@ +package shootergame.entity.player; + +import shootergame.entity.Entity; +import shootergame.entity.EntityVertical; +import shootergame.init.Textures; +import shootergame.util.gl.texture.TextureReference; +import shootergame.util.math.MathHelpers; +import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec2i; + +public class Player 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; + + @Override + public void tick() + { + // Call super + super.tick(); + + // Rotate left + if(MOVE_LEFT) { + this.angle -= 1; + this.angle %= 360; + } + + // Rotate right + if(MOVE_RIGHT) { + this.angle += 1; + 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); + } + + // 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); + } + } +} diff --git a/src/shootergame/init/Entities.java b/src/shootergame/init/Entities.java new file mode 100644 index 0000000..68b11d4 --- /dev/null +++ b/src/shootergame/init/Entities.java @@ -0,0 +1,10 @@ +package shootergame.init; + +import java.util.ArrayList; + +import shootergame.entity.Entity; + +public class Entities +{ + public static final ArrayList entities = new ArrayList(); +} diff --git a/src/shootergame/init/Textures.java b/src/shootergame/init/Textures.java index cdeda90..70b21f6 100644 --- a/src/shootergame/init/Textures.java +++ b/src/shootergame/init/Textures.java @@ -7,7 +7,7 @@ import org.lwjgl.opengl.GL; import shootergame.display.DisplayWindow; import shootergame.util.gl.texture.TextureMap; import shootergame.util.gl.texture.TextureReference; -import shootergame.util.gl.texture.TextureReferenceAnimation; +import shootergame.util.gl.texture.AnimationReference; public class Textures { @@ -21,9 +21,41 @@ public class Textures texmap.init(); } - public static final ArrayList animations = new ArrayList(); + public static final ArrayList animations = new ArrayList(); + + public static final TextureMap texmap = new TextureMap( + "/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); + + // 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 = 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); + + // 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 TextureMap texmap = new TextureMap("/home/josua/eclipse-workspace/ShooterGame/src/shootergame/resources/texmap.png"); - public static final TextureReference TILE_GRASS = texmap.getTextureReference(0, 0, 16, 16); } diff --git a/src/shootergame/init/Tiles.java b/src/shootergame/init/Tiles.java index 885c823..9cf53bd 100644 --- a/src/shootergame/init/Tiles.java +++ b/src/shootergame/init/Tiles.java @@ -1,9 +1,19 @@ package shootergame.init; import shootergame.tiles.Tile; +import shootergame.tiles.TileDirt; +import shootergame.tiles.TileFire; import shootergame.tiles.TileGrass; +import shootergame.tiles.TileSand; +import shootergame.tiles.TileStone; +import shootergame.tiles.TileTree; public class Tiles { public static final Tile GRASS = new TileGrass("grass"); + public static final Tile FIRE = new TileFire("fire"); + public static final Tile SAND = new TileSand("sand"); + public static final Tile STONE = new TileStone("stone"); + public static final Tile DIRT = new TileDirt("dirt"); + public static final Tile TREE = new TileTree("tree"); } diff --git a/src/shootergame/input/KeyCallback.java b/src/shootergame/input/KeyCallback.java index 2a864f1..0b8254c 100644 --- a/src/shootergame/input/KeyCallback.java +++ b/src/shootergame/input/KeyCallback.java @@ -1,14 +1,41 @@ package shootergame.input; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_A; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_D; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_SHIFT; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_S; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_SPACE; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_W; +import static org.lwjgl.glfw.GLFW.GLFW_RELEASE; + import org.lwjgl.glfw.GLFWKeyCallbackI; +import shootergame.Main; + public class KeyCallback implements GLFWKeyCallbackI { @Override - public void invoke(long arg0, int arg1, int arg2, int arg3, int arg4) { - // TODO Auto-generated method stub + public void invoke(long window, int key, int scancode, int action, int mods) + { + boolean pressed = ! ( action == GLFW_RELEASE ); + + if(key == GLFW_KEY_W) { + Main.player.MOVE_FORWARD = pressed; + } + + if(key == GLFW_KEY_S) { + Main.player.MOVE_BACKWARD = pressed; + } + + if(key == GLFW_KEY_A) { + Main.player.MOVE_LEFT = pressed; + } + + if(key == GLFW_KEY_D) { + Main.player.MOVE_RIGHT = pressed; + } } } diff --git a/src/shootergame/resources/texmap.png b/src/shootergame/resources/texmap.png index d6973fa..dcc3f83 100644 Binary files a/src/shootergame/resources/texmap.png and b/src/shootergame/resources/texmap.png differ diff --git a/src/shootergame/tiles/Tile.java b/src/shootergame/tiles/Tile.java index 9816529..85a76d2 100644 --- a/src/shootergame/tiles/Tile.java +++ b/src/shootergame/tiles/Tile.java @@ -1,10 +1,16 @@ package shootergame.tiles; +import shootergame.display.Camera; +import shootergame.display.transparent.ITransparentObject; +import shootergame.display.transparent.TransparentObjects; +import shootergame.entity.player.Player; +import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2i; -public class Tile +public class Tile implements ITransparentObject { private String id; + public boolean opaqueTile = false; public Tile(String id) { this.id = id; @@ -14,6 +20,23 @@ public class Tile return this.id; } - public void render(Vec2i pos) { + @Override + public void render(Vec2d pos, Camera camera) { + } + + public void doRender(Vec2d pos, Camera camera) + { + if(this.opaqueTile) { + TransparentObjects.register(this, camera, pos); + } + + else { + this.render(pos, camera); + } + } + + @Override + public boolean isOpaqueTile() { + return this.opaqueTile; } } diff --git a/src/shootergame/tiles/TileDirt.java b/src/shootergame/tiles/TileDirt.java new file mode 100644 index 0000000..af19f35 --- /dev/null +++ b/src/shootergame/tiles/TileDirt.java @@ -0,0 +1,12 @@ +package shootergame.tiles; + +import shootergame.init.Textures; + +public class TileDirt extends TileFlat +{ + + public TileDirt(String id) { + super(id, Textures.TILE_DIRT); + } + +} \ No newline at end of file diff --git a/src/shootergame/tiles/TileFire.java b/src/shootergame/tiles/TileFire.java new file mode 100644 index 0000000..5fd0b5e --- /dev/null +++ b/src/shootergame/tiles/TileFire.java @@ -0,0 +1,15 @@ +package shootergame.tiles; + +import shootergame.init.Textures; + +public class TileFire extends TileVertical +{ + + public TileFire(String id) { + super(id, Textures.TILE_FIRE, 6); + + // Set some settings + this.opaqueTile = true; + } + +} diff --git a/src/shootergame/tiles/TileFlat.java b/src/shootergame/tiles/TileFlat.java index 0427ace..ef2d26c 100644 --- a/src/shootergame/tiles/TileFlat.java +++ b/src/shootergame/tiles/TileFlat.java @@ -1,8 +1,12 @@ package shootergame.tiles; +import shootergame.display.Camera; +import shootergame.entity.player.Player; import shootergame.util.gl.GlHelpers; import shootergame.util.gl.texture.TextureReference; +import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2i; +import shootergame.util.math.vec.Vec3d; public class TileFlat extends Tile { @@ -14,17 +18,17 @@ public class TileFlat extends Tile } @Override - public void render(Vec2i pos) + public void render(Vec2d pos, Camera camera) { // Call super - super.render(pos); + super.render(pos, camera); // Render the tile GlHelpers.begin(); - tex.texCoord(0, 0); GlHelpers.vertex2(pos.x+0, pos.y+0); - tex.texCoord(1, 0); GlHelpers.vertex2(pos.x+1, pos.y+0); - tex.texCoord(1, 1); GlHelpers.vertex2(pos.x+1, pos.y+1); - tex.texCoord(0, 1); GlHelpers.vertex2(pos.x+0, pos.y+1); + tex.texCoord(1, 1); GlHelpers.vertex3(pos.x+0, pos.y+0, 0); + tex.texCoord(0, 1); GlHelpers.vertex3(pos.x+1, pos.y+0, 0); + tex.texCoord(0, 0); GlHelpers.vertex3(pos.x+1, pos.y+1, 0); + tex.texCoord(1, 0); GlHelpers.vertex3(pos.x+0, pos.y+1, 0); GlHelpers.end(); } } diff --git a/src/shootergame/tiles/TileSand.java b/src/shootergame/tiles/TileSand.java new file mode 100644 index 0000000..7793604 --- /dev/null +++ b/src/shootergame/tiles/TileSand.java @@ -0,0 +1,12 @@ +package shootergame.tiles; + +import shootergame.init.Textures; + +public class TileSand extends TileFlat +{ + + public TileSand(String id) { + super(id, Textures.TILE_SAND); + } + +} \ No newline at end of file diff --git a/src/shootergame/tiles/TileStone.java b/src/shootergame/tiles/TileStone.java new file mode 100644 index 0000000..bd38935 --- /dev/null +++ b/src/shootergame/tiles/TileStone.java @@ -0,0 +1,12 @@ +package shootergame.tiles; + +import shootergame.init.Textures; + +public class TileStone extends TileFlat +{ + + public TileStone(String id) { + super(id, Textures.TILE_STONE); + } + +} diff --git a/src/shootergame/tiles/TileTree.java b/src/shootergame/tiles/TileTree.java new file mode 100644 index 0000000..862c04c --- /dev/null +++ b/src/shootergame/tiles/TileTree.java @@ -0,0 +1,15 @@ +package shootergame.tiles; + +import shootergame.init.Textures; + +public class TileTree extends TileVertical +{ + + public TileTree(String id) { + super(id, Textures.TILE_TREE, 4); + + // Set some settings + this.opaqueTile = true; + } + +} diff --git a/src/shootergame/tiles/TileVertical.java b/src/shootergame/tiles/TileVertical.java new file mode 100644 index 0000000..155203b --- /dev/null +++ b/src/shootergame/tiles/TileVertical.java @@ -0,0 +1,31 @@ +package shootergame.tiles; + +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; +import shootergame.util.math.vec.Vec2i; + +public class TileVertical extends Tile +{ + private TextureReference tex; + private int h; + + public TileVertical(String id, TextureReference tex, int height) { + super(id); + + // Store some variables + this.tex = tex; + this.h = height; + } + + @Override + public void render(Vec2d pos, Camera camera) { + super.render(pos, camera); + VerticalRender.render(pos, camera, tex, h); + } + +} diff --git a/src/shootergame/util/gl/GlHelpers.java b/src/shootergame/util/gl/GlHelpers.java index 565d477..bd6182c 100644 --- a/src/shootergame/util/gl/GlHelpers.java +++ b/src/shootergame/util/gl/GlHelpers.java @@ -25,11 +25,11 @@ public class GlHelpers } public static void vertex3(float x, float y, float z) { - glVertex3f(x, y, z); + glVertex3f(x/10, y/10, z/10); } public static void vertex2(float x, float y) { - glVertex2f(x/Main.window.getWidth()*100, y/Main.window.getHeight()*100); + glVertex2f(x/10, y/10); } public static void color3(float r, float g, float b) { @@ -45,7 +45,7 @@ public class GlHelpers } public static void translate(float x, float y, float z) { - glTranslatef(x, y, z); + glTranslatef(x/10, y/10, z/10); } public static void disableCullFace() { @@ -105,4 +105,12 @@ public class GlHelpers 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/VerticalRender.java b/src/shootergame/util/gl/VerticalRender.java new file mode 100644 index 0000000..f2c7f50 --- /dev/null +++ b/src/shootergame/util/gl/VerticalRender.java @@ -0,0 +1,37 @@ +package shootergame.util.gl; + +import shootergame.display.Camera; +import shootergame.entity.player.Player; +import shootergame.util.gl.texture.TextureReference; +import shootergame.util.math.vec.Vec2d; + +public class VerticalRender +{ + public static void render(Vec2d pos, Camera camera, TextureReference tex, double height) + { + double h = height; + + // Push the matrix + GlHelpers.pushMatrix(); + + // Get the angle between the camera and the tile + double angle_r = Math.atan2(pos.y - camera.pos.y, pos.x - camera.pos.x); + + // Make the tile upright + GlHelpers.translate(0.5, 0, 0); + GlHelpers.translate(pos.x, pos.y, 0); + GlHelpers.rotate(Math.toDegrees(angle_r)+90, 0, 0, 1); + GlHelpers.translate(-0.5, 0, 0); + + // Render the tile + GlHelpers.begin(); + tex.texCoord(1, 1); GlHelpers.vertex3(0, 0, 0); + tex.texCoord(0, 1); GlHelpers.vertex3(1, 0, 0); + tex.texCoord(0, 0); GlHelpers.vertex3(1, 0, h); + tex.texCoord(1, 0); GlHelpers.vertex3(0, 0, h); + GlHelpers.end(); + + // Pop the matrix + GlHelpers.popMatrix(); + } +} diff --git a/src/shootergame/util/gl/texture/AnimationEventHandler.java b/src/shootergame/util/gl/texture/AnimationEventHandler.java new file mode 100644 index 0000000..c1894d8 --- /dev/null +++ b/src/shootergame/util/gl/texture/AnimationEventHandler.java @@ -0,0 +1,30 @@ +package shootergame.util.gl.texture; + +import mainloop.task.IMainloopTask; +import mainloop.task.MainloopTask; +import shootergame.init.Textures; + +public class AnimationEventHandler implements IMainloopTask +{ + public static final AnimationEventHandler ANIMATION_EVENT_HANDLER = new AnimationEventHandler(); + + @Override + public boolean MainLoopDelay(long millis) { + return millis > 10; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public void MainLoopUpdate() + { + // Loop over the animations and update them all + for(AnimationReference r : Textures.animations) { + r.tick(); + } + } + +} diff --git a/src/shootergame/util/gl/texture/AnimationReference.java b/src/shootergame/util/gl/texture/AnimationReference.java new file mode 100644 index 0000000..ab5d816 --- /dev/null +++ b/src/shootergame/util/gl/texture/AnimationReference.java @@ -0,0 +1,46 @@ +package shootergame.util.gl.texture; + +import shootergame.init.Textures; + +public class AnimationReference extends TextureReference +{ + private TextureReference[] references; + private int upto = 0; + private int speed; + private TextureReference c; + + public AnimationReference(int speed, TextureReference ... references) + { + // Store the texture references and the speed + this.references = references; + this.speed = speed; + + // Register the animation + Textures.animations.add(this); + c = references[0]; + } + + @Override + public void texCoord(float x, float y) { + c.texCoord(x, y); + } + + public void tick() + { + // Cycle through all the textures + upto += 1; + upto %= references.length * speed; + c = references[upto / speed]; + } + + @Override + public int getMaxX() { + return c.getMaxX(); + } + + @Override + public int getMaxY() { + return c.getMaxY(); + } + +} diff --git a/src/shootergame/util/gl/texture/TextureMap.java b/src/shootergame/util/gl/texture/TextureMap.java index de5ba0c..61982ee 100644 --- a/src/shootergame/util/gl/texture/TextureMap.java +++ b/src/shootergame/util/gl/texture/TextureMap.java @@ -37,8 +37,8 @@ public class TextureMap //texture.free(); } - public TextureReference getTextureReference(int start_x, int start_y, int end_x, int end_y) { - return new TextureReference(start_x, start_y, end_x, end_y) + public TextureReference getTextureReference(int start_x, int end_x, int start_y, int end_y) { + return new TextureReference(start_x, end_x, start_y, end_y) { @Override diff --git a/src/shootergame/util/gl/texture/TextureReferenceAnimation.java b/src/shootergame/util/gl/texture/TextureReferenceAnimation.java deleted file mode 100644 index 9a3b0dc..0000000 --- a/src/shootergame/util/gl/texture/TextureReferenceAnimation.java +++ /dev/null @@ -1,25 +0,0 @@ -package shootergame.util.gl.texture; - -public abstract class TextureReferenceAnimation extends TextureReference -{ - private TextureReference[] references; - private int upto = 0; - - public TextureReferenceAnimation(TextureReference[] references) - { - this.references = references; - } - - @Override - public void texCoord(float x, float y) { - references[upto].texCoord(x, y); - } - - public void tick() - { - // Cycle through all the textures - upto += 1; - upto %= references.length; - } - -} diff --git a/src/shootergame/util/math/MathHelpers.java b/src/shootergame/util/math/MathHelpers.java index b6a9ca9..a409735 100644 --- a/src/shootergame/util/math/MathHelpers.java +++ b/src/shootergame/util/math/MathHelpers.java @@ -1,6 +1,7 @@ package shootergame.util.math; import shootergame.util.math.vec.Vec2d; +import shootergame.util.math.vec.Vec3d; public class MathHelpers { @@ -32,6 +33,21 @@ public class MathHelpers } public static Vec2d moveTowards2(double amount, double angle) { - return new Vec2d(Math.sin(angle), Math.cos(angle)); + return new Vec2d(Math.sin(angle)*amount, Math.cos(angle)*amount); + } + + public static Vec3d moveTowards3(double amount, Vec2d angles) + { + // Create some varibles + Vec3d points = new Vec3d(0, 0, 0); + double m; + + points.z = Math.sin(angles.y) * amount; + m = Math.cos(angles.y); + points.x = Math.sin(angles.x) * m * amount; + m = Math.cos(angles.x) * m; + points.y = m * amount; + + return points; } } diff --git a/src/shootergame/util/math/vec/Vec2d.java b/src/shootergame/util/math/vec/Vec2d.java index f87e7da..9ec875a 100644 --- a/src/shootergame/util/math/vec/Vec2d.java +++ b/src/shootergame/util/math/vec/Vec2d.java @@ -24,4 +24,20 @@ public class Vec2d public boolean equal(Vec2d other) { return x == other.x && y == other.y; } + + public Vec2d add(Vec2d other) { + return new Vec2d(this.x + other.x, this.y + other.y); + } + + public Vec2d subtract(Vec2d other) { + return new Vec2d(this.x - other.x, this.y - other.y); + } + + public Vec2d multiply(Vec2d other) { + return new Vec2d(this.x * other.x, this.y * other.y); + } + + public Vec2d divide(Vec2d other) { + return new Vec2d(this.x / other.x, this.y / other.y); + } } diff --git a/src/shootergame/util/math/vec/Vec2i.java b/src/shootergame/util/math/vec/Vec2i.java index beac2a2..c1bfeea 100644 --- a/src/shootergame/util/math/vec/Vec2i.java +++ b/src/shootergame/util/math/vec/Vec2i.java @@ -47,4 +47,20 @@ public class Vec2i public boolean equal(Vec2i other) { return x == other.x && y == other.y; } + + public Vec2i add(Vec2i other) { + return new Vec2i(this.x + other.x, this.y + other.y); + } + + public Vec2i subtract(Vec2i other) { + return new Vec2i(this.x - other.x, this.y - other.y); + } + + public Vec2i multiply(Vec2i other) { + return new Vec2i(this.x * other.x, this.y * other.y); + } + + public Vec2i divide(Vec2i other) { + return new Vec2i(this.x / other.x, this.y / other.y); + } } diff --git a/src/shootergame/util/math/vec/Vec3d.java b/src/shootergame/util/math/vec/Vec3d.java index 42bb4f9..29c3936 100644 --- a/src/shootergame/util/math/vec/Vec3d.java +++ b/src/shootergame/util/math/vec/Vec3d.java @@ -26,4 +26,20 @@ public class Vec3d public boolean equal(Vec3d other) { return x == other.x && y == other.y && z == other.z; } + + public Vec3d add(Vec3d other) { + return new Vec3d(this.x + other.x, this.y + other.y, this.z + other.z); + } + + public Vec3d subtract(Vec3d other) { + return new Vec3d(this.x - other.x, this.y - other.y, this.z - other.z); + } + + public Vec3d multiply(Vec3d other) { + return new Vec3d(this.x * other.x, this.y * other.y, this.z * other.z); + } + + public Vec3d divide(Vec3d other) { + return new Vec3d(this.x / other.x, this.y / other.y, this.z / other.z); + } } diff --git a/src/shootergame/util/math/vec/Vec3i.java b/src/shootergame/util/math/vec/Vec3i.java index 4c99fbf..c87e414 100644 --- a/src/shootergame/util/math/vec/Vec3i.java +++ b/src/shootergame/util/math/vec/Vec3i.java @@ -54,4 +54,20 @@ public class Vec3i public boolean equal(Vec3i other) { return x == other.x && y == other.y && z == other.z; } + + public Vec3i add(Vec3i other) { + return new Vec3i(this.x + other.x, this.y + other.y, this.z + other.z); + } + + public Vec3i subtract(Vec3i other) { + return new Vec3i(this.x - other.x, this.y - other.y, this.z - other.z); + } + + public Vec3i multiply(Vec3i other) { + return new Vec3i(this.x * other.x, this.y * other.y, this.z * other.z); + } + + public Vec3i divide(Vec3i other) { + return new Vec3i(this.x / other.x, this.y / other.y, this.z / other.z); + } }