diff --git a/res/rawImage/playerBlue.xcf b/res/rawImage/playerBlue.xcf new file mode 100644 index 0000000..53c0e86 Binary files /dev/null and b/res/rawImage/playerBlue.xcf differ diff --git a/res/rawImage/playerGreen.xcf b/res/rawImage/playerGreen.xcf new file mode 100644 index 0000000..35746b1 Binary files /dev/null and b/res/rawImage/playerGreen.xcf differ diff --git a/res/rawImage/playerRed.xcf b/res/rawImage/playerRed.xcf new file mode 100644 index 0000000..a65a496 Binary files /dev/null and b/res/rawImage/playerRed.xcf differ diff --git a/res/rawImage/smoothFloor.xcf b/res/rawImage/smoothFloor.xcf new file mode 100644 index 0000000..5ae7221 Binary files /dev/null and b/res/rawImage/smoothFloor.xcf differ diff --git a/res/rawImage/stoneWall.xcf b/res/rawImage/stoneWall.xcf new file mode 100644 index 0000000..dd49e4e Binary files /dev/null and b/res/rawImage/stoneWall.xcf differ diff --git a/res/playerBlue.png b/res/sprites/playerBlue.png similarity index 100% rename from res/playerBlue.png rename to res/sprites/playerBlue.png diff --git a/res/playerGreen.png b/res/sprites/playerGreen.png similarity index 100% rename from res/playerGreen.png rename to res/sprites/playerGreen.png diff --git a/res/playerRed.png b/res/sprites/playerRed.png similarity index 100% rename from res/playerRed.png rename to res/sprites/playerRed.png diff --git a/res/smoothFloor.png b/res/sprites/stoneFloor.png similarity index 100% rename from res/smoothFloor.png rename to res/sprites/stoneFloor.png diff --git a/res/sprites/stoneWall.png b/res/sprites/stoneWall.png new file mode 100644 index 0000000..afa852c Binary files /dev/null and b/res/sprites/stoneWall.png differ diff --git a/shaders/smoothFloor.frag b/shaders/tile.frag similarity index 88% rename from shaders/smoothFloor.frag rename to shaders/tile.frag index 6001a40..caed726 100644 --- a/shaders/smoothFloor.frag +++ b/shaders/tile.frag @@ -8,7 +8,6 @@ in DATA vec3 position; } fs_in; -uniform vec2 player; uniform sampler2D tex; void main() diff --git a/shaders/smoothFloor.vert b/shaders/tile.vert similarity index 50% rename from shaders/smoothFloor.vert rename to shaders/tile.vert index c651c30..ba2a611 100644 --- a/shaders/smoothFloor.vert +++ b/shaders/tile.vert @@ -4,7 +4,8 @@ layout (location = 0) in vec4 position; layout (location = 1) in vec2 tc; uniform mat4 pr_matrix; -uniform mat4 vw_matrix; +uniform mat4 vw_matrix = mat4(1.0); +uniform mat4 ml_matrix = mat4(1.0); out DATA { @@ -14,7 +15,7 @@ out DATA void main() { - gl_Position = pr_matrix * vw_matrix * position; + gl_Position = pr_matrix * vw_matrix * ml_matrix * position; vs_out.tc = tc; - vs_out.position = vec3(vw_matrix * position); + vs_out.position = vec3(vw_matrix * ml_matrix * position); } \ No newline at end of file diff --git a/src/game/Main.java b/src/game/Main.java index c79e01b..c5e18be 100644 --- a/src/game/Main.java +++ b/src/game/Main.java @@ -16,8 +16,8 @@ import game.math.Matrix4f; public class Main implements Runnable { - private int width = 1280; - private int height = 720; + private int width = 1920; + private int height = 1080; private boolean running = false; private Thread gameThread; private long gameWindow; @@ -64,8 +64,8 @@ public class Main implements Runnable { // Set all shader uniform variables here Matrix4f projectionMatrix = Matrix4f.orthographic(-10.0f, 10.0f, -10.0f * 9.0f / 16.0f, 10.0f * 9.0f / 16.0f, -1.0f, 1.0f); - Shader.SMOOTH_FLOOR.setUniformMat4f("pr_matrix", projectionMatrix); - Shader.SMOOTH_FLOOR.setUniform1i("tex", 1); + Shader.TILE.setUniformMat4f("pr_matrix", projectionMatrix); + Shader.TILE.setUniform1i("tex", 1); Shader.PLAYER.setUniformMat4f("pr_matrix", projectionMatrix); Shader.PLAYER.setUniform1i("tex", 1); diff --git a/src/game/Options.java b/src/game/Options.java index c74e0da..7050b24 100644 --- a/src/game/Options.java +++ b/src/game/Options.java @@ -8,9 +8,9 @@ public class Options { private static int chosenBallColor = 0; private static Texture[] ballColors = new Texture[] { - new Texture("res/playerBlue.png"), - new Texture("res/playerRed.png"), - new Texture("res/playerGreen.png"), + new Texture("res/sprites/playerBlue.png"), + new Texture("res/sprites/playerRed.png"), + new Texture("res/sprites/playerGreen.png"), }; public Options() { diff --git a/src/game/entity/Entity.java b/src/game/entity/Entity.java new file mode 100644 index 0000000..b18b2ca --- /dev/null +++ b/src/game/entity/Entity.java @@ -0,0 +1,22 @@ +package game.entity; + +import game.graphics.Texture; +import game.graphics.VertexArray; +import game.math.Vector3f; + +public abstract class Entity { + + protected float SIZE; + protected Vector3f position = new Vector3f(); + protected float speed; + + protected VertexArray mesh; + protected Texture texture; + protected float[] vertices; + protected byte[] indices; + protected float[] tcs; + + public abstract void update(); + public abstract void render(); + +} diff --git a/src/game/entity/Player.java b/src/game/entity/Player.java index a4eb802..8a45c4b 100644 --- a/src/game/entity/Player.java +++ b/src/game/entity/Player.java @@ -4,36 +4,33 @@ import org.lwjgl.glfw.GLFW; import game.Options; import game.graphics.Shader; -import game.graphics.Texture; import game.graphics.VertexArray; import game.input.Input; import game.math.Matrix4f; -import game.math.Vector3f; -public class Player { - - private float SIZE = 1.0f; - private VertexArray mesh; - private Texture texture; - - private Vector3f position = new Vector3f(); +public class Player extends Entity{ private int playerKeyUp, playerKeyDown, playerKeyLeft, playerKeyRight; - public Player() { - float[] vertices = new float[] { + public Player(float x, float y) { + SIZE = 0.5f; + position.x = x; + position.y = y; + speed = .05f; + + vertices = new float[] { -SIZE / 2.0f, -SIZE / 2.0f, 0.2f, -SIZE / 2.0f, SIZE / 2.0f, 0.2f, SIZE / 2.0f, SIZE / 2.0f, 0.2f, SIZE / 2.0f, -SIZE / 2.0f, 0.2f, }; - byte[] indices = new byte[] { + indices = new byte[] { 0, 1, 2, 2, 3, 0 }; - float[] tcs = new float[] { + tcs = new float[] { 0, 1, 0, 0, 1, 0, @@ -42,6 +39,7 @@ public class Player { mesh = new VertexArray(vertices, indices, tcs); + // Just for testing at the moment setColor(); if (Options.isWASD) { @@ -59,20 +57,18 @@ public class Player { public void update() { if (Input.keys[playerKeyUp]) { - position.y += 0.1f; + position.y += speed; } if (Input.keys[playerKeyDown]) { - position.y -= 0.1f; + position.y -= speed; } if (Input.keys[playerKeyLeft]) { - position.x -= 0.1f; + position.x -= speed; } if (Input.keys[playerKeyRight]) { - position.x += 0.1f; + position.x += speed; } -// System.out.println("At (" + position.x + ", " + position.y + ")"); - // Just for testing, change color by pressing corresponding number buttons if (Input.keys[GLFW.GLFW_KEY_0]) { Options.setBallColor(0); diff --git a/src/game/graphics/Shader.java b/src/game/graphics/Shader.java index 11d1fed..101c387 100644 --- a/src/game/graphics/Shader.java +++ b/src/game/graphics/Shader.java @@ -15,7 +15,7 @@ public class Shader { public static final int VERTEX_ATTRIB = 0; public static final int TCOORD_ATTRIB = 1; - public static Shader SMOOTH_FLOOR, PLAYER; + public static Shader TILE, PLAYER; private boolean enabled = false; @@ -27,7 +27,7 @@ public class Shader { } public static void loadAll() { - SMOOTH_FLOOR = new Shader("shaders/smoothFloor.vert", "shaders/smoothFloor.frag"); + TILE = new Shader("shaders/tile.vert", "shaders/tile.frag"); PLAYER = new Shader("shaders/player.vert", "shaders/player.frag"); } diff --git a/src/game/level/Level.java b/src/game/level/Level.java index 232d7a3..ce9c37a 100644 --- a/src/game/level/Level.java +++ b/src/game/level/Level.java @@ -1,53 +1,104 @@ package game.level; +import game.entity.Entity; import game.entity.Player; -import game.graphics.Shader; -import game.graphics.Texture; -import game.graphics.VertexArray; +import game.level.tiles.*; +import game.level.tiles.Tile; public class Level { - // number of tiles -// private int width, height; private Player player; + private int currentLevel; + private int width, height; // Number of tiles + private Tile[][] tiles; + private Entity[] entities; + private float originX, originY; // Where the 'origin' of the level is the lower left of the floor area public Level() { - float[] vertices = new float[] { - -10.0f, -10.0f * 9.0f / 16.0f, 0.0f, - -10.0f, 10.0f * 9.0f / 16.0f, 0.0f, - 0.0f, 10.0f * 9.0f / 16.0f, 0.0f, - 0.0f, -10.0f * 9.0f / 16.0f, 0.0f, - }; + currentLevel = -1; + loadLevel(); + } + + private void loadLevel() { - byte[] indices = new byte[] { - 0, 1, 2, - 2, 3, 0 - }; + // This is a test level + if (currentLevel == -1) { + width = 7; + height = 7; + tiles = new Tile[width][height]; - float[] tcs = new float[] { - 0, 1, - 0, 0, - 1, 0, - 1, 1 - }; - - player = new Player(); - + // Tile coordinates are double entity coordinates, i.e. the entity at (1, 1) is on the tile at (2, 2) + originX = -1.5f; + originY = -1.5f; + + // Load entities and positions, load into array + player = new Player(0f, 0f); + entities = new Entity[1]; + entities[0] = player; + + // Load tiles. Sample, 5 x 5 grid of StoneFloor surrounded by StoneWalls + Tile.create(); + for (int y = 0; y < width; y++) { + for (int x = 0; x < height; x++) { + float convertX = x/2f; // Tile coordinates are double entity coordinates + float convertY = y/2f; + if (y == 0 || y == height - 1 || x == 0 || x == width - 1) { + tiles[x][y] = new StoneWall(convertX + originX, convertY + originY); + } else { + tiles[x][y] = new StoneFloor(convertX + originX, convertY + originY); + } + } + } + } } public void update() { - // tile update - // player update - // enemy update - - player.update(); + updateTiles(); + updateEntities(); } public void render() { - // render tiles - // render player - // render other entities - - player.render(); + renderTiles(); + renderEntities(); + } + + private void updateTiles() { + for (Tile[] tileRow : tiles) { + for (Tile tile : tileRow) { + tile.update(); + } + } + } + + private void updateEntities() { + for (Entity entity : entities) { + entity.update(); + } + } + + private void renderTiles() { + for (Tile[] tileRow : tiles) { + for (Tile tile : tileRow) { + tile.render(); + } + } + } + + private void renderEntities() { + for (Entity entity : entities) { + entity.render(); + } + } + + private void handleCollisions() { + playerWallCollisions(); + // Player-entity collisions + // Entity-wall collisions + } + + private void playerWallCollisions() { + boolean collided = false; + + // Check for collisions, flip flag, move player } } diff --git a/src/game/level/tiles/SmoothFloor.java b/src/game/level/tiles/SmoothFloor.java deleted file mode 100644 index a00fb01..0000000 --- a/src/game/level/tiles/SmoothFloor.java +++ /dev/null @@ -1,26 +0,0 @@ -package game.level.tiles; - -import game.graphics.Texture; -import game.graphics.VertexArray; - -public class SmoothFloor extends Tile{ - - public SmoothFloor() { - super(); - texture = new Texture("/res/SmoothFloor.png"); - } - - @Override - public void render() { - // TODO Auto-generated method stub - texture.bind(); - - } - - @Override - public void update() { - // TODO Auto-generated method stub - - } - -} diff --git a/src/game/level/tiles/StoneFloor.java b/src/game/level/tiles/StoneFloor.java new file mode 100644 index 0000000..0df5489 --- /dev/null +++ b/src/game/level/tiles/StoneFloor.java @@ -0,0 +1,21 @@ +package game.level.tiles; + +import game.graphics.Texture; +import game.math.Matrix4f; + +public class StoneFloor extends Tile{ + + public StoneFloor(float x, float y) { + position.x = x; + position.y = y; + texture = new Texture("res/sprites/stoneFloor.png"); + ml_matrix = Matrix4f.translate(position); + isWall = false; + } + + @Override + public void update() { + return; + } + +} diff --git a/src/game/level/tiles/StoneWall.java b/src/game/level/tiles/StoneWall.java new file mode 100644 index 0000000..86e1e92 --- /dev/null +++ b/src/game/level/tiles/StoneWall.java @@ -0,0 +1,22 @@ +package game.level.tiles; + +import game.graphics.Texture; +import game.math.Matrix4f; + +public class StoneWall extends Tile { + + public StoneWall(float x, float y) { + position.x = x; + position.y = y; + texture = new Texture("res/sprites/stoneWall.png"); + ml_matrix = Matrix4f.translate(position); + isWall = true; + } + + @Override + public void update() { + return; + } + + +} diff --git a/src/game/level/tiles/Tile.java b/src/game/level/tiles/Tile.java index 6c54f7d..e1ba4f4 100644 --- a/src/game/level/tiles/Tile.java +++ b/src/game/level/tiles/Tile.java @@ -1,24 +1,30 @@ package game.level.tiles; +import game.graphics.Shader; import game.graphics.Texture; import game.graphics.VertexArray; +import game.math.Matrix4f; +import game.math.Vector3f; public abstract class Tile { - protected final float SIZE = 1.0f; + protected Vector3f position = new Vector3f(); + protected boolean isWall; + protected static final float SIZE = 1.0f; + protected Texture texture; - protected VertexArray vertexArray; - protected float[] vertices; - protected byte[] indices; - protected float[] tcs; - + protected Matrix4f ml_matrix; + protected static VertexArray mesh; + private static float[] vertices; + private static float[] tcs; + private static byte[] indices; - protected Tile() { + public static void create() { vertices = new float[] { - -SIZE / 2.0f, -SIZE / 2.0f, 0.0f, - -SIZE / 2.0f, SIZE / 2.0f, 0.0f, - SIZE / 2.0f, SIZE / 2.0f, 0.0f, - SIZE / 2.0f, -SIZE / 2.0f, 0.0f, + -SIZE / 2.0f, -SIZE / 2.0f, 0.1f, + -SIZE / 2.0f, SIZE / 2.0f, 0.1f, + SIZE / 2.0f, SIZE / 2.0f, 0.1f, + SIZE / 2.0f, -SIZE / 2.0f, 0.1f, }; indices = new byte[] { @@ -33,10 +39,21 @@ public abstract class Tile { 1, 1 }; + mesh = new VertexArray(vertices, indices, tcs); + } + + public void render() { + Shader.TILE.enable(); + Shader.TILE.setUniformMat4f("vw_matrix", Matrix4f.translate(position)); + texture.bind(); + mesh.bind(); + Shader.TILE.setUniformMat4f("ml_matrix", ml_matrix); + mesh.draw(); + texture.unbind(); + mesh.unbind(); } public abstract void update(); - public abstract void render(); }