commit c85083fc926fc797a3cc5289f2b299f3032e5329 Author: josua Date: Tue May 5 22:04:33 2020 +1000 first commit diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..d6270b3 --- /dev/null +++ b/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..a81845c --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + RoboticsAndGameDesignMazeGame + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/bin/maze_game/Main.class b/bin/maze_game/Main.class new file mode 100644 index 0000000..af60830 Binary files /dev/null and b/bin/maze_game/Main.class differ diff --git a/bin/maze_game/entity/Entity.class b/bin/maze_game/entity/Entity.class new file mode 100644 index 0000000..158a5db Binary files /dev/null and b/bin/maze_game/entity/Entity.class differ diff --git a/bin/maze_game/entity/EntityPlayer.class b/bin/maze_game/entity/EntityPlayer.class new file mode 100644 index 0000000..eec4fb4 Binary files /dev/null and b/bin/maze_game/entity/EntityPlayer.class differ diff --git a/bin/maze_game/graphics/GameWindow.class b/bin/maze_game/graphics/GameWindow.class new file mode 100644 index 0000000..697e332 Binary files /dev/null and b/bin/maze_game/graphics/GameWindow.class differ diff --git a/bin/maze_game/graphics/GraphicsHandler.class b/bin/maze_game/graphics/GraphicsHandler.class new file mode 100644 index 0000000..c79427c Binary files /dev/null and b/bin/maze_game/graphics/GraphicsHandler.class differ diff --git a/bin/maze_game/graphics/GraphicsShader.class b/bin/maze_game/graphics/GraphicsShader.class new file mode 100644 index 0000000..359a57d Binary files /dev/null and b/bin/maze_game/graphics/GraphicsShader.class differ diff --git a/bin/maze_game/helpers/MathHelpers.class b/bin/maze_game/helpers/MathHelpers.class new file mode 100644 index 0000000..91d4668 Binary files /dev/null and b/bin/maze_game/helpers/MathHelpers.class differ diff --git a/bin/maze_game/helpers/ResourceLoader.class b/bin/maze_game/helpers/ResourceLoader.class new file mode 100644 index 0000000..17a6a9a Binary files /dev/null and b/bin/maze_game/helpers/ResourceLoader.class differ diff --git a/bin/maze_game/helpers/range/Range2i.class b/bin/maze_game/helpers/range/Range2i.class new file mode 100644 index 0000000..6e67e11 Binary files /dev/null and b/bin/maze_game/helpers/range/Range2i.class differ diff --git a/bin/maze_game/helpers/range/Range3i.class b/bin/maze_game/helpers/range/Range3i.class new file mode 100644 index 0000000..11f2bff Binary files /dev/null and b/bin/maze_game/helpers/range/Range3i.class differ diff --git a/bin/maze_game/helpers/range/Range4i.class b/bin/maze_game/helpers/range/Range4i.class new file mode 100644 index 0000000..17b2ee8 Binary files /dev/null and b/bin/maze_game/helpers/range/Range4i.class differ diff --git a/bin/maze_game/helpers/vec/Vec2d.class b/bin/maze_game/helpers/vec/Vec2d.class new file mode 100644 index 0000000..52b4caa Binary files /dev/null and b/bin/maze_game/helpers/vec/Vec2d.class differ diff --git a/bin/maze_game/helpers/vec/Vec2i.class b/bin/maze_game/helpers/vec/Vec2i.class new file mode 100644 index 0000000..d7d0838 Binary files /dev/null and b/bin/maze_game/helpers/vec/Vec2i.class differ diff --git a/bin/maze_game/helpers/vec/Vec3d.class b/bin/maze_game/helpers/vec/Vec3d.class new file mode 100644 index 0000000..816571d Binary files /dev/null and b/bin/maze_game/helpers/vec/Vec3d.class differ diff --git a/bin/maze_game/helpers/vec/Vec3i.class b/bin/maze_game/helpers/vec/Vec3i.class new file mode 100644 index 0000000..a58347f Binary files /dev/null and b/bin/maze_game/helpers/vec/Vec3i.class differ diff --git a/bin/maze_game/helpers/vec/Vec4d.class b/bin/maze_game/helpers/vec/Vec4d.class new file mode 100644 index 0000000..930ab04 Binary files /dev/null and b/bin/maze_game/helpers/vec/Vec4d.class differ diff --git a/bin/maze_game/helpers/vec/Vec4i.class b/bin/maze_game/helpers/vec/Vec4i.class new file mode 100644 index 0000000..a0835ae Binary files /dev/null and b/bin/maze_game/helpers/vec/Vec4i.class differ diff --git a/bin/maze_game/init/Entities.class b/bin/maze_game/init/Entities.class new file mode 100644 index 0000000..4789a8c Binary files /dev/null and b/bin/maze_game/init/Entities.class differ diff --git a/bin/maze_game/init/Mazes.class b/bin/maze_game/init/Mazes.class new file mode 100644 index 0000000..82146b4 Binary files /dev/null and b/bin/maze_game/init/Mazes.class differ diff --git a/bin/maze_game/init/Models.class b/bin/maze_game/init/Models.class new file mode 100644 index 0000000..d072273 Binary files /dev/null and b/bin/maze_game/init/Models.class differ diff --git a/bin/maze_game/input/InputKeyboard.class b/bin/maze_game/input/InputKeyboard.class new file mode 100644 index 0000000..1ce1069 Binary files /dev/null and b/bin/maze_game/input/InputKeyboard.class differ diff --git a/bin/maze_game/mainloop/FpsCounter.class b/bin/maze_game/mainloop/FpsCounter.class new file mode 100644 index 0000000..36d9e1e Binary files /dev/null and b/bin/maze_game/mainloop/FpsCounter.class differ diff --git a/bin/maze_game/mainloop/MainloopHandler.class b/bin/maze_game/mainloop/MainloopHandler.class new file mode 100644 index 0000000..00a2ca7 Binary files /dev/null and b/bin/maze_game/mainloop/MainloopHandler.class differ diff --git a/bin/maze_game/maze/Maze.class b/bin/maze_game/maze/Maze.class new file mode 100644 index 0000000..dce6737 Binary files /dev/null and b/bin/maze_game/maze/Maze.class differ diff --git a/bin/maze_game/maze/MazeDrawer.class b/bin/maze_game/maze/MazeDrawer.class new file mode 100644 index 0000000..edd861e Binary files /dev/null and b/bin/maze_game/maze/MazeDrawer.class differ diff --git a/bin/maze_game/models/Model.class b/bin/maze_game/models/Model.class new file mode 100644 index 0000000..354a73a Binary files /dev/null and b/bin/maze_game/models/Model.class differ diff --git a/bin/maze_game/models/ModelPlayer.class b/bin/maze_game/models/ModelPlayer.class new file mode 100644 index 0000000..ad62186 Binary files /dev/null and b/bin/maze_game/models/ModelPlayer.class differ diff --git a/bin/resources/maze/maze0.txt b/bin/resources/maze/maze0.txt new file mode 100644 index 0000000..1e2a0b0 --- /dev/null +++ b/bin/resources/maze/maze0.txt @@ -0,0 +1,32 @@ +################################ +# # +# # +# # +# # +# # +# # +# # +# # +# &&&&& &&& &&& &&& # +# & & & & & & & # +# & & & &&& & && # +# & & & & & & & # +# & & & & & &&& &&& # +# # +# # +# &&& &&& &&&&& &&& # +# & & & & & & & # +# & & &&& & & & && # +# & & & & & & & & # +# &&& & & & & & &&& # +# # +# # +# # +# # +# # +# # +# # +# # +# # +#! 1# +################################ \ No newline at end of file diff --git a/bin/resources/maze/maze1.txt b/bin/resources/maze/maze1.txt new file mode 100644 index 0000000..1f33a90 --- /dev/null +++ b/bin/resources/maze/maze1.txt @@ -0,0 +1,32 @@ + ################ + #&&&& &# + #&&&& # + ###### ##### # + ###### ##### &# + ## @ # &&# +########## #& ### ### # &&# +# # #& # @ # # # +# ###### # #& # #3# # # # +# # # #& # # # # #&& # +###### # # #& # #2# # #&& # +# # # # #& # # # # #&& # +# # # # # #& # # # # # +#&&# # # # #& ##### # # # +#&&# #4#3# #& # # &&# +#### ###### ## #### # &&# + # # #########&& # # + # #### # # #&& ## + # # ##### ##### ######### + # &&# #### @ @ # + # &&# # &# ##### #### #### + # # # @ && # + # # #5###### ############# # + #&& # ### @ # # # + #&& # # #### ### # # # # & # + # # ##### @ # # # # & # + # # ## ### # # # +## ########## 2############## +# & & ###### # +# # +# #&&&&&&# #&&&&&&# # +################################ \ No newline at end of file diff --git a/bin/resources/maze/maze2.txt b/bin/resources/maze/maze2.txt new file mode 100644 index 0000000..c0b3d0c --- /dev/null +++ b/bin/resources/maze/maze2.txt @@ -0,0 +1,32 @@ + + + + + + + + ####### + # # + # ### # + # # # # + # # #1# + # # ### + # # + # # + # # + # # + # # + # # + # # + # # + # # + # # + # # + # # + # # + ### # + #1 # + ##### + + + \ No newline at end of file diff --git a/bin/resources/maze/maze3.txt b/bin/resources/maze/maze3.txt new file mode 100644 index 0000000..27eaf6d --- /dev/null +++ b/bin/resources/maze/maze3.txt @@ -0,0 +1,32 @@ + + + + ####### + # # + # ### # + # # # # + # # #1# + # # # # + ######### ### # + # @ # + # ###### ### # + ### # # #&& # + #1# # # # # + # # # # ##### + # # # # # + # # # # + ###### # # + # # + # # + # # + # # + # # + ### # + #1 # + ##### + + + + + + \ No newline at end of file diff --git a/bin/resources/maze/maze4.txt b/bin/resources/maze/maze4.txt new file mode 100644 index 0000000..7ccbd6a --- /dev/null +++ b/bin/resources/maze/maze4.txt @@ -0,0 +1,32 @@ +################################ +################################ +################ ####### +########## ##### +######### #### +###### #### +#### #### +#### ### +### ### +## ### +## ### +## ### +# ### ####### ####### ### +# #1#&# #&# # ### +# ### ### ### ### ### ##### ## +# @ @ @ @ @ # ## +# ### &&& &&& &&& &&& &&& # ## +# # &&& &&& &&& &&& &&& # ## +# ### &&& &&& &&& &&& &&& # # +# # @ @ @ @ @ # # +# # # &&& &&& &&& &&& ##### # +# # # &&& &&& &&& &&& # # +# # # &&& &&& &&& &&& ##### # +# # @ @ @ @ @ # # +# ### &&& ### &&& ### &&& # # +# # &&& # # &&& # # &&& # # +# # &&& # # &&& # # &&& # # +#&&&&# #&# #&# #&55# +#&&&&#######&#######&#######&&&# +#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&# +#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&# +################################ \ No newline at end of file diff --git a/bin/resources/maze/maze5.txt b/bin/resources/maze/maze5.txt new file mode 100644 index 0000000..59bc791 --- /dev/null +++ b/bin/resources/maze/maze5.txt @@ -0,0 +1,32 @@ +################################ +#### ########################### +### ########################### +##&&&&########################## +##&&&&########################## +##&&&&&######################### +###&&########################### +######### ################### +#### ######## +## ########## #### +## #&&&&&####### ######### ## +## ###&######### ######### # +## ############## ########## # +## ############### ######### # +## ############### ####### # +### ###############&&&&&##### # +#### ##########&&&&##### # +#####&&&## 6######&&###### # +############################# # +#&&### ##### ## +#& # ########## ##### +#& ###1# ######&&&&&&###### +#& ##### #######&&######## +#& #### ################## +#& #&&&&&&&&&################### +#& ##&&&&&&&#################### +#& ####&&####################44# +## ####&## ############# # +# ##### # # ########### # +# # ####&### ## # +# ######## ## # +##&&&&&&##########&&&&&&###&&&&# \ No newline at end of file diff --git a/bin/resources/maze/maze6.txt b/bin/resources/maze/maze6.txt new file mode 100644 index 0000000..42fc959 --- /dev/null +++ b/bin/resources/maze/maze6.txt @@ -0,0 +1,35 @@ +################################### +# # +# # +# # +# # +# ### # # ### # # # # ### # +# # # # # # ## # ## # # +# # ### ### # # # # ### # +# # # # # # # ## ## # # +# # # # # # # # # # ### # +# # +# ### ### ### # +# # # # # # # +# ## # # ### # +# # # # ## # +# # ### # # # +# # +# ### # ### # # ### # # ### # +# # # # # # # # # ## # # # +# ### # ### ### # # # # # # # +# # # # # # # # ## # # # +# # ### # # # ### # # ### # +# # +# # +# # +# # +# # +# ! # +# # +# # +# # +# # +# # +#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&# +################################### \ No newline at end of file diff --git a/bin/resources/maze/maze7.txt b/bin/resources/maze/maze7.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/resources/maze/maze8.txt b/bin/resources/maze/maze8.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/resources/maze/maze9.txt b/bin/resources/maze/maze9.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/resources/shader/entity.fsh b/bin/resources/shader/entity.fsh new file mode 100644 index 0000000..1c3c39c --- /dev/null +++ b/bin/resources/shader/entity.fsh @@ -0,0 +1,9 @@ +#version 330 + +in vec3 color; +out vec4 FragColor; + +void main() +{ + FragColor = vec4(color, 0); +} \ No newline at end of file diff --git a/bin/resources/shader/entity.vsh b/bin/resources/shader/entity.vsh new file mode 100644 index 0000000..c475429 --- /dev/null +++ b/bin/resources/shader/entity.vsh @@ -0,0 +1,16 @@ +#version 330 + +layout (location = 0) in vec2 aPos; +layout (location = 1) in vec3 aColor; + +out vec3 color; + +uniform vec2 pos; +uniform ivec2 maze; + +void main() +{ + vec2 cPos = (aPos + pos) / maze; + gl_Position = vec4(cPos * 2 - 1, 0, 1); + color = aColor; +} \ No newline at end of file diff --git a/bin/resources/shader/environment.fsh b/bin/resources/shader/environment.fsh new file mode 100644 index 0000000..1c3c39c --- /dev/null +++ b/bin/resources/shader/environment.fsh @@ -0,0 +1,9 @@ +#version 330 + +in vec3 color; +out vec4 FragColor; + +void main() +{ + FragColor = vec4(color, 0); +} \ No newline at end of file diff --git a/bin/resources/shader/environment.vsh b/bin/resources/shader/environment.vsh new file mode 100644 index 0000000..7fbd167 --- /dev/null +++ b/bin/resources/shader/environment.vsh @@ -0,0 +1,12 @@ +#version 330 + +layout (location = 0) in vec2 aPos; +layout (location = 1) in vec3 aColor; + +out vec3 color; + +void main() +{ + gl_Position = vec4(aPos.x * 2 - 1, aPos.y * 2 - 1, 0, 1); + color = aColor; +} \ No newline at end of file diff --git a/src/maze_game/Main.java b/src/maze_game/Main.java new file mode 100644 index 0000000..c5ccfd5 --- /dev/null +++ b/src/maze_game/Main.java @@ -0,0 +1,34 @@ +package maze_game; + +import maze_game.entity.EntityPlayer; +import maze_game.graphics.GameWindow; +import maze_game.helpers.ResourceLoader; +import maze_game.helpers.vec.Vec2d; +import maze_game.init.Entities; +import maze_game.init.Mazes; +import maze_game.mainloop.MainloopHandler; +import maze_game.maze.Maze; +import maze_game.maze.MazeDrawer; + +public class Main +{ + public static GameWindow window; + public static MainloopHandler ml_handler; + public static EntityPlayer player; + public static Maze maze; + + public static void main(String[] args) + { + maze = Mazes.MAZE[0]; + + window = new GameWindow(); + ml_handler = new MainloopHandler(); + + player = new EntityPlayer(); + player.pos = maze.player_pos.copy(); + + Entities.entities.add(player); + + ml_handler.mainloop.start(); + } +} diff --git a/src/maze_game/entity/Entity.java b/src/maze_game/entity/Entity.java new file mode 100644 index 0000000..979fb06 --- /dev/null +++ b/src/maze_game/entity/Entity.java @@ -0,0 +1,64 @@ +package maze_game.entity; + +import org.lwjgl.opengl.GL33; + +import maze_game.Main; +import maze_game.graphics.GraphicsShader; +import maze_game.helpers.vec.Vec2d; +import maze_game.helpers.vec.Vec3d; +import maze_game.models.Model; + +public class Entity +{ + public static int glsl_pos; + + public Vec2d pos; + private Model model; + + public Entity(Model model) { + this.pos = new Vec2d(0, 0); + this.model = model; + } + + public void render() + { + // Bind the vertex array + model.bind(); + + // Set entity position and render + GL33.glUniform2f(glsl_pos, (float)pos.x, (float)pos.y); + GL33.glDrawArrays(GL33.GL_TRIANGLES, 0, model.s); + } + + public void move(Vec2d pos) + { + pos = this.pos.add(pos); + + // Teleport + while(Main.maze.collision(pos, 3)) + { + if(pos.x > this.pos.x) { + pos.x += 1; + } + + if(pos.x < this.pos.x) { + pos.x -= 1; + } + + if(pos.y > this.pos.y) { + pos.y += 1; + } + + if(pos.y < this.pos.y) { + pos.y -= 1; + } + } + + // Wall collisions + if(!Main.maze.collision(this.pos.x, pos.y, 1)) { + this.pos.y = pos.y; + } if(!Main.maze.collision(pos.x, this.pos.y, 1)) { + this.pos.x = pos.x; + } + } +} diff --git a/src/maze_game/entity/EntityPlayer.java b/src/maze_game/entity/EntityPlayer.java new file mode 100644 index 0000000..d22c740 --- /dev/null +++ b/src/maze_game/entity/EntityPlayer.java @@ -0,0 +1,49 @@ +package maze_game.entity; + +import static org.lwjgl.opengl.GL20.glGetUniformLocation; +import static org.lwjgl.opengl.GL20.glUniform2i; + +import maze_game.Main; +import maze_game.helpers.vec.Vec2d; +import maze_game.init.Mazes; +import maze_game.init.Models; + +public class EntityPlayer extends Entity +{ + public EntityPlayer() { + super(Models.MODEL_PLAYER); + } + + @Override + public void move(Vec2d pos) { + + pos = pos.add(this.pos); + + // Level teleportation + for(int i=0;i<10;i++) { + if(Main.maze.collision(pos, i + 4)) { + Main.maze = Mazes.MAZE[i]; + if(Main.maze.player_pos != null) + { + pos = Main.maze.player_pos.copy(); + + Main.window.entity_shader.use(); + + int glsl_maze_size = glGetUniformLocation(Main.window.entity_shader.program, "maze"); + glUniform2i(glsl_maze_size, Main.maze.width, Main.maze.height); + } + } + } + + pos = pos.subtract(this.pos); + + super.move(pos); + + // Death + if(Main.maze.collision(this.pos, 2)) { + Main.maze = Mazes.MAZE[0]; + this.pos = Main.maze.player_pos.copy(); + return; + } + } +} diff --git a/src/maze_game/graphics/GameWindow.java b/src/maze_game/graphics/GameWindow.java new file mode 100644 index 0000000..166534c --- /dev/null +++ b/src/maze_game/graphics/GameWindow.java @@ -0,0 +1,118 @@ +package maze_game.graphics; + +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glClear; +import static org.lwjgl.opengl.GL11.glClearColor; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glUniform4f; +import static org.lwjgl.opengl.GL20.glUseProgram; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL33.*; + +import org.lwjgl.glfw.GLFW; +import org.lwjgl.opengl.GL; + +import maze_game.Main; +import maze_game.entity.Entity; +import maze_game.helpers.ResourceLoader; +import maze_game.init.Entities; +import maze_game.input.InputKeyboard; +import maze_game.mainloop.FpsCounter; +import maze_game.maze.Maze; +import maze_game.maze.MazeDrawer; + +public class GameWindow +{ + public GraphicsShader maze_shader; + public GraphicsShader entity_shader; + public long window; + + public GameWindow() + { + if(!glfwInit()) { + throw new RuntimeException("GLFW intialization failed"); + } + + // Setup OpenGL with the core profile + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); + glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_FALSE); + + window = glfwCreateWindow(800, 600, "Maze Game", 0, 0); + + if(window == 0) { + glfwTerminate(); + throw new RuntimeException("GLFW window initialization failed"); + } + + glfwSetKeyCallback(window, InputKeyboard.INPUT_KEYBOARD); + + glfwMakeContextCurrent(window); + glfwShowWindow(window); + + GL.createCapabilities(); + glViewport(0, 0, 800, 600); + + // Load and compile the shader + maze_shader = new GraphicsShader("environment"); + entity_shader = new GraphicsShader("entity"); + + maze_shader.use(); + + Entity.glsl_pos = glGetUniformLocation(entity_shader.program, "pos"); + + entity_shader.use(); + + int glsl_maze_size = glGetUniformLocation(entity_shader.program, "maze"); + glUniform2i(glsl_maze_size, Main.maze.width, Main.maze.height); + } + + public void render() + { + int w[] = {0}; + int h[] = {0}; + + glfwGetFramebufferSize(window, w, h); + + double w_r = (double)w[0] / (double)h[0]; + double m_r = (double)Main.maze.width / (double)Main.maze.height; + int o_x = 0; + int o_y = 0; + + if(w_r < m_r) { + o_y = (int)(h[0] * w_r / m_r) - h[0]; + } + + if(w_r > m_r) { + o_x = (int)(w[0] / w_r * m_r) - w[0]; + } + + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT); + glViewport(-o_x/2, -o_y/2, w[0] + o_x, h[0] + o_y); + + maze_shader.use(); + + Main.maze.bind(); + glDrawArrays(GL_TRIANGLES, 0, Main.maze.s / 5); + + entity_shader.use(); + + for(Entity entity : Entities.entities) { + entity.render(); + } + + glfwSwapBuffers(window); + glfwPollEvents(); + + FpsCounter.fps += 1; + } +} diff --git a/src/maze_game/graphics/GraphicsHandler.java b/src/maze_game/graphics/GraphicsHandler.java new file mode 100644 index 0000000..5f845e4 --- /dev/null +++ b/src/maze_game/graphics/GraphicsHandler.java @@ -0,0 +1,25 @@ +package maze_game.graphics; + +import mainloop.task.IMainloopTask; +import maze_game.Main; + +public class GraphicsHandler implements IMainloopTask +{ + public static int mspf = 1000 / 60; + + @Override + public boolean MainLoopDelay(long millis) { + return millis > mspf; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public void MainLoopUpdate() { + Main.window.render(); + } + +} diff --git a/src/maze_game/graphics/GraphicsShader.java b/src/maze_game/graphics/GraphicsShader.java new file mode 100644 index 0000000..4f8ed5e --- /dev/null +++ b/src/maze_game/graphics/GraphicsShader.java @@ -0,0 +1,34 @@ +package maze_game.graphics; + +import org.lwjgl.opengl.GL33; + +import maze_game.helpers.ResourceLoader; + +public class GraphicsShader +{ + public int program; + + public GraphicsShader(String id) { + program = ResourceLoader.loadShader(id); + } + + public void use() { + GL33.glUseProgram(program); + } + + public void setInt(int v) { + GL33.glUniform1i(program, v); + } + + public void setFloat(float v) { + GL33.glUniform1f(program, v); + } + + public void setIntv(int[] v) { + GL33.glUniform1iv(program, v); + } + + public void setFloatv(float[] v) { + GL33.glUniform1fv(program, v); + } +} diff --git a/src/maze_game/helpers/MathHelpers.java b/src/maze_game/helpers/MathHelpers.java new file mode 100755 index 0000000..2e496e0 --- /dev/null +++ b/src/maze_game/helpers/MathHelpers.java @@ -0,0 +1,112 @@ +package maze_game.helpers; + +import maze_game.helpers.vec.Vec2d; +import maze_game.helpers.vec.Vec3d; + +public class MathHelpers +{ + public static final double FallSpeed = 0.00098; + + public static double squared(double x) { + return x*x; + } + + public static double pow(int c, double x) + { + double res = 1; + + for(int i=0;i b) return a; + else return b; + } + + public static double smallest(double a, double b) + { + if(a < b) return a; + else return b; + } +} diff --git a/src/maze_game/helpers/ResourceLoader.java b/src/maze_game/helpers/ResourceLoader.java new file mode 100644 index 0000000..d51791e --- /dev/null +++ b/src/maze_game/helpers/ResourceLoader.java @@ -0,0 +1,86 @@ +package maze_game.helpers; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; + +import org.lwjgl.PointerBuffer; +import org.lwjgl.glfw.GLFW; + +import static org.lwjgl.opengl.GL33.*; + +public class ResourceLoader +{ + public static byte[] loadResource(String name) + { + try + { + InputStream in = ResourceLoader.class.getResourceAsStream(name); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + byte buffer[] = new byte[1024]; + int size = 0; + + while((size = in.read(buffer)) != -1) { + out.write(buffer, 0, size); + } + + return out.toByteArray(); + } + + catch(IOException e) { + e.printStackTrace(); + return new byte[0]; + } + } + + public static int loadShader(String name) + { + int vsh = glCreateShader(GL_VERTEX_SHADER); + int fsh = glCreateShader(GL_FRAGMENT_SHADER); + + byte[] vsh_source = loadResource("/resources/shader/" + name + ".vsh"); + byte[] fsh_source = loadResource("/resources/shader/" + name + ".fsh"); + + StringBuilder vsh_sb = new StringBuilder(new String(vsh_source)); + StringBuilder fsh_sb = new StringBuilder(new String(fsh_source)); + glShaderSource(vsh, vsh_sb); + glShaderSource(fsh, fsh_sb); + glCompileShader(vsh); + glCompileShader(fsh); + int vsh_success[] = new int[1]; + int fsh_success[] = new int[1]; + + glGetShaderiv(vsh, GL_COMPILE_STATUS, vsh_success); + glGetShaderiv(fsh, GL_COMPILE_STATUS, fsh_success); + + if(vsh_success[0] == 0) { + System.err.println("ERROR:SHADER:VERTEX:" + name + ":" + glGetShaderInfoLog(vsh)); + System.exit(1); + } + + if(fsh_success[0] == 0) { + System.err.println("ERROR:SHADER:FRAGMENT:" + name + ":" + glGetShaderInfoLog(fsh)); + System.exit(1); + } + + int program = glCreateProgram(); + glAttachShader(program, vsh); + glAttachShader(program, fsh); + glLinkProgram(program); + + int success[] = {0}; + glGetProgramiv(program, GL_LINK_STATUS, success); + + if(success[0] == 0) { + System.err.println("ERROR:PROGRAM:LINKING:" + name + ":" + glGetProgramInfoLog(program)); + System.exit(1); + } + + glDeleteShader(vsh); + glDeleteShader(fsh); + + return program; + } +} diff --git a/src/maze_game/helpers/range/Range2i.java b/src/maze_game/helpers/range/Range2i.java new file mode 100755 index 0000000..e64d333 --- /dev/null +++ b/src/maze_game/helpers/range/Range2i.java @@ -0,0 +1,17 @@ +package maze_game.helpers.range; + +public class Range2i +{ + public int mx; + public int my; + + public Range2i(int mx, int my) + { + this.mx = mx; + this.my = my; + } + + public int maxValue() { + return mx * my; + } +} diff --git a/src/maze_game/helpers/range/Range3i.java b/src/maze_game/helpers/range/Range3i.java new file mode 100755 index 0000000..a0089c6 --- /dev/null +++ b/src/maze_game/helpers/range/Range3i.java @@ -0,0 +1,19 @@ +package maze_game.helpers.range; + +public class Range3i +{ + public int mx; + public int my; + public int mz; + + public Range3i(int mx, int my, int mz) + { + this.mx = mx; + this.my = my; + this.mz = mz; + } + + public int maxValue() { + return mx * my * mz; + } +} diff --git a/src/maze_game/helpers/range/Range4i.java b/src/maze_game/helpers/range/Range4i.java new file mode 100755 index 0000000..722dc47 --- /dev/null +++ b/src/maze_game/helpers/range/Range4i.java @@ -0,0 +1,21 @@ +package maze_game.helpers.range; + +public class Range4i +{ + public int mx; + public int my; + public int mz; + public int mm; + + public Range4i(int mx, int my, int mz, int mm) + { + this.mx = mx; + this.my = my; + this.mz = mz; + this.mm = mm; + } + + public int maxValue() { + return mx * my * mz * mm; + } +} diff --git a/src/maze_game/helpers/vec/Vec2d.java b/src/maze_game/helpers/vec/Vec2d.java new file mode 100755 index 0000000..d97da94 --- /dev/null +++ b/src/maze_game/helpers/vec/Vec2d.java @@ -0,0 +1,74 @@ +package maze_game.helpers.vec; + +import maze_game.helpers.MathHelpers; + +public class Vec2d +{ + public double x; + public double y; + + public Vec2d(double x, double y) + { + this.x = x; + this.y = y; + } + + public double distance(Vec2d other) { + return MathHelpers.distance2d(x, y, other.x, other.y); + } + + public static double distance(Vec2d v1, Vec2d v2) { + return v1.distance(v2); + } + + 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); + } + + public Vec2d add(double v) { + return new Vec2d(this.x + v, this.y + v); + } + + public Vec2d subtract(double v) { + return new Vec2d(this.x - v, this.y - v); + } + + public Vec2d multiply(double v) { + return new Vec2d(this.x * v, this.y * v); + } + + public Vec2d divide(double v) { + return new Vec2d(this.x / v, this.y / v); + } + + public Vec2d copy() { + return new Vec2d(x, y); + } + + public double squareDistance(Vec2d other) + { + double dx = MathHelpers.positive(other.x - x); + double dy = MathHelpers.positive(other.y - y); + return MathHelpers.biggest(dx, dy); + } + + public Vec2i toInt() { + return new Vec2i(MathHelpers.floor(x), MathHelpers.floor(y)); + } +} diff --git a/src/maze_game/helpers/vec/Vec2i.java b/src/maze_game/helpers/vec/Vec2i.java new file mode 100755 index 0000000..7932045 --- /dev/null +++ b/src/maze_game/helpers/vec/Vec2i.java @@ -0,0 +1,100 @@ +package maze_game.helpers.vec; + +import maze_game.helpers.MathHelpers; +import maze_game.helpers.range.Range2i; + +public class Vec2i +{ + public int x; + public int y; + + public Vec2i(int x, int y) + { + this.x = x; + this.y = y; + } + + public double distance(Vec2i other) { + return MathHelpers.distance2d(x, y, other.x, other.y); + } + + public static double distance(Vec2i v1, Vec2i v2) { + return v1.distance(v2); + } + + public int getId(Range2i range) + { + int x = MathHelpers.mod(this.x, range.mx); + int y = MathHelpers.mod(this.y, range.my); + + int id = 0; + int m = 1; + + id += x; + m = range.mx; + id += y*m; + + return id; + } + + public static Vec2i fromId(Range2i range, int id) + { + int x = MathHelpers.mod(id, range.mx); + id -= x; + id /= range.mx; + int y = MathHelpers.mod(id, range.my); + + return new Vec2i(x, y); + } + + 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); + } + + public Vec2i add(int v) { + return new Vec2i(this.x + v, this.y + v); + } + + public Vec2i subtract(int v) { + return new Vec2i(this.x - v, this.y - v); + } + + public Vec2i multiply(int v) { + return new Vec2i(this.x * v, this.y * v); + } + + public Vec2i divide(int v) { + return new Vec2i(this.x / v, this.y / v); + } + + public Vec2i copy() { + return new Vec2i(x, y); + } + + public double squareDistance(Vec2i other) + { + int dx = MathHelpers.positive(other.x - x); + int dy = MathHelpers.positive(other.y - y); + return MathHelpers.biggest(dx, dy); + } + + public Vec2d toDouble() { + return new Vec2d(x, y); + } +} diff --git a/src/maze_game/helpers/vec/Vec3d.java b/src/maze_game/helpers/vec/Vec3d.java new file mode 100755 index 0000000..715622a --- /dev/null +++ b/src/maze_game/helpers/vec/Vec3d.java @@ -0,0 +1,80 @@ +package maze_game.helpers.vec; + +import maze_game.helpers.MathHelpers; + +public class Vec3d +{ + public double x; + public double y; + public double z; + + public Vec3d(double x, double y, double z) + { + this.x = x; + this.y = y; + this.z = z; + } + + public double distance(Vec3d other) { + return MathHelpers.distance3d(x, y, z, other.x, other.y, other.z); + } + + public static double distance(Vec3d v1, Vec3d v2) { + return v1.distance(v2); + } + + 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); + } + + public Vec3d add(double v) { + return new Vec3d(this.x + v, this.y + v, this.z + v); + } + + public Vec3d subtract(double v) { + return new Vec3d(this.x - v, this.y - v, this.z - v); + } + + public Vec3d multiply(double v) { + return new Vec3d(this.x * v, this.y * v, this.z * v); + } + + public Vec3d divide(double v) { + return new Vec3d(this.x / v, this.y / v, this.z / v); + } + + public Vec3d copy() { + return new Vec3d(x, y, z); + } + + public double squareDistance(Vec3d other) + { + double dx = MathHelpers.positive(other.x - x); + double dy = MathHelpers.positive(other.y - y); + double dz = MathHelpers.positive(other.z - z); + + if(dx > dy) return dx; + if(dy > dz) return dy; + else return dz; + } + + public Vec3i toInt() { + return new Vec3i(MathHelpers.floor(x), MathHelpers.floor(y), MathHelpers.floor(z)); + } +} diff --git a/src/maze_game/helpers/vec/Vec3i.java b/src/maze_game/helpers/vec/Vec3i.java new file mode 100755 index 0000000..04b3dec --- /dev/null +++ b/src/maze_game/helpers/vec/Vec3i.java @@ -0,0 +1,112 @@ +package maze_game.helpers.vec; + +import maze_game.helpers.MathHelpers; +import maze_game.helpers.range.Range3i; + +public class Vec3i +{ + public int x; + public int y; + public int z; + + public Vec3i(int x, int y, int z) + { + this.x = x; + this.y = y; + this.z = z; + } + + public double distance(Vec3i other) { + return MathHelpers.distance3d(x, y, z, other.x, other.y, other.z); + } + + public static double distance(Vec3i v1, Vec3i v2) { + return v1.distance(v2); + } + + public int getId(Range3i range) + { + int x = MathHelpers.mod(this.x, range.mx); + int y = MathHelpers.mod(this.y, range.my); + int z = MathHelpers.mod(this.z, range.mz); + + int id = 0; + int m = 1; + + id += x; + m = range.mx; + id += y*m; + m *= range.my; + id += z*m; + + return id; + } + + public static Vec3i fromId(Range3i range, int id) + { + int x = MathHelpers.mod(id, range.mx); + id -= x; + id /= range.mx; + int y = MathHelpers.mod(id, range.my); + id -= y; + id /= range.my; + int z = MathHelpers.mod(id, range.mz); + + return new Vec3i(x, y, z); + } + + 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); + } + + public Vec3i add(int v) { + return new Vec3i(this.x + v, this.y + v, this.z + v); + } + + public Vec3i subtract(int v) { + return new Vec3i(this.x - v, this.y - v, this.z - v); + } + + public Vec3i multiply(int v) { + return new Vec3i(this.x * v, this.y * v, this.z * v); + } + + public Vec3i divide(int v) { + return new Vec3i(this.x / v, this.y / v, this.z / v); + } + + public Vec3i copy() { + return new Vec3i(x, y, z); + } + + public int squareDistance(Vec3i other) + { + int dx = MathHelpers.positive(other.x - x); + int dy = MathHelpers.positive(other.y - y); + int dz = MathHelpers.positive(other.z - z); + + if(dx > dy) return dx; + if(dy > dz) return dy; + else return dz; + } + + public Vec3d toDouble() { + return new Vec3d(x, y, z); + } +} diff --git a/src/maze_game/helpers/vec/Vec4d.java b/src/maze_game/helpers/vec/Vec4d.java new file mode 100755 index 0000000..4ae094c --- /dev/null +++ b/src/maze_game/helpers/vec/Vec4d.java @@ -0,0 +1,88 @@ +package maze_game.helpers.vec; + +import maze_game.helpers.MathHelpers; + +public class Vec4d +{ + public double x; + public double y; + public double z; + public double m; + + public Vec4d(double x, double y, double z, double m) + { + this.x = x; + this.y = y; + this.z = z; + this.m = m; + } + + public double distance(Vec4d other) { + return Math.sqrt( + MathHelpers.squared(this.x - other.x) + + MathHelpers.squared(this.y - other.y) + + MathHelpers.squared(this.z - other.z) + + MathHelpers.squared(this.m - other.m)); + } + + public static double distance(Vec4d v1, Vec4d v2) { + return v1.distance(v2); + } + + public boolean equal(Vec4d other) { + return x == other.x && y == other.y && z == other.z && m == other.m; + } + + public Vec4d add(Vec4d other) { + return new Vec4d(this.x + other.x, this.y + other.y, this.z + other.z, this.m + other.m); + } + + public Vec4d subtract(Vec4d other) { + return new Vec4d(this.x - other.x, this.y - other.y, this.z - other.z, this.m - other.m); + } + + public Vec4d multiply(Vec4d other) { + return new Vec4d(this.x * other.x, this.y * other.y, this.z * other.z, this.m * other.m); + } + + public Vec4d divide(Vec4d other) { + return new Vec4d(this.x / other.x, this.y / other.y, this.z / other.z, this.m / other.m); + } + + public Vec4d add(double v) { + return new Vec4d(this.x + v, this.y + v, this.z + v, this.m + v); + } + + public Vec4d subtract(double v) { + return new Vec4d(this.x - v, this.y - v, this.z - v, this.m - v); + } + + public Vec4d multiply(double v) { + return new Vec4d(this.x * v, this.y * v, this.z * v, this.m * v); + } + + public Vec4d divide(double v) { + return new Vec4d(this.x / v, this.y / v, this.z / v, this.m / v); + } + + public Vec4d copy() { + return new Vec4d(x, y, z, m); + } + + public double squareDistance(Vec4d other) + { + double dx = MathHelpers.positive(other.x - x); + double dy = MathHelpers.positive(other.y - y); + double dz = MathHelpers.positive(other.z - z); + double dm = MathHelpers.positive(other.m - m); + + if(dx > dy) return dx; + if(dy > dz) return dy; + if(dz > dm) return dz; + else return dm; + } + + public Vec4i toInt() { + return new Vec4i(MathHelpers.floor(x), MathHelpers.floor(y), MathHelpers.floor(z), MathHelpers.floor(m)); + } +} diff --git a/src/maze_game/helpers/vec/Vec4i.java b/src/maze_game/helpers/vec/Vec4i.java new file mode 100755 index 0000000..14f8b76 --- /dev/null +++ b/src/maze_game/helpers/vec/Vec4i.java @@ -0,0 +1,125 @@ +package maze_game.helpers.vec; + +import maze_game.helpers.MathHelpers; +import maze_game.helpers.range.Range4i; + +public class Vec4i { + public int x; + public int y; + public int z; + public int m; + + public Vec4i(int x, int y, int z, int m) + { + this.x = x; + this.y = y; + this.z = z; + this.m = m; + } + + public double distance(Vec4i other) { + return Math.sqrt( + MathHelpers.squared(this.x - other.x) + + MathHelpers.squared(this.y - other.y) + + MathHelpers.squared(this.z - other.z) + + MathHelpers.squared(this.m - other.m)); + } + + public static double distance(Vec4i v1, Vec4i v2) { + return v1.distance(v2); + } + + public int getId(Range4i range) + { + int x = MathHelpers.mod(this.x, range.mx); + int y = MathHelpers.mod(this.y, range.my); + int z = MathHelpers.mod(this.z, range.mz); + int m = MathHelpers.mod(this.m, range.mm); + + int id = 0; + int mu = 1; + + id += x; + mu = range.mx; + id += y*mu; + mu *= range.my; + id += z*mu; + mu *= range.mz; + id += m*mu; + + return id; + } + + public static Vec4i fromId(Range4i range, int id) + { + int x = MathHelpers.mod(id, range.mx); + id -= x; + id /= range.mx; + int y = MathHelpers.mod(id, range.my); + id -= y; + id /= range.my; + int z = MathHelpers.mod(id, range.mz); + id -= z; + id /= range.mz; + int m = MathHelpers.mod(id, range.mm); + + return new Vec4i(x, y, z, m); + } + + public boolean equal(Vec4i other) { + return x == other.x && y == other.y && z == other.z; + } + + public Vec4i add(Vec4i other) { + return new Vec4i(this.x + other.x, this.y + other.y, this.z + other.z, this.m + other.m); + } + + public Vec4i subtract(Vec4i other) { + return new Vec4i(this.x - other.x, this.y - other.y, this.z - other.z, this.m - other.m); + } + + public Vec4i multiply(Vec4i other) { + return new Vec4i(this.x * other.x, this.y * other.y, this.z * other.z, this.m * other.m); + } + + public Vec4i divide(Vec4i other) { + return new Vec4i(this.x / other.x, this.y / other.y, this.z / other.z, this.m / other.m); + } + + public Vec4i add(int v) { + return new Vec4i(this.x + v, this.y + v, this.z + v, this.m + v); + } + + public Vec4i subtract(int v) { + return new Vec4i(this.x - v, this.y - v, this.z - v, this.m - v); + } + + public Vec4i multiply(int v) { + return new Vec4i(this.x * v, this.y * v, this.z * v, this.m * v); + } + + public Vec4i divide(int v) { + return new Vec4i(this.x / v, this.y / v, this.z / v, this.m / v); + } + + public Vec4i copy() { + return new Vec4i(x, y, z, m); + } + + public int squareDistance(Vec4i other) + { + int dx = MathHelpers.positive(other.x - x); + int dy = MathHelpers.positive(other.y - y); + int dz = MathHelpers.positive(other.z - z); + int dm = MathHelpers.positive(other.m - m); + + if(dx > dy) return dx; + if(dy > dz) return dy; + if(dz > dm) return dz; + else return dm; + } + + public Vec4d toDouble() { + return new Vec4d(x, y, z, m); + } +} diff --git a/src/maze_game/init/Entities.java b/src/maze_game/init/Entities.java new file mode 100644 index 0000000..789c5d9 --- /dev/null +++ b/src/maze_game/init/Entities.java @@ -0,0 +1,12 @@ +package maze_game.init; + +import java.util.ArrayList; + +import maze_game.Main; +import maze_game.entity.Entity; +import maze_game.helpers.vec.Vec3d; + +public class Entities +{ + public static ArrayList entities = new ArrayList(); +} diff --git a/src/maze_game/init/Mazes.java b/src/maze_game/init/Mazes.java new file mode 100644 index 0000000..758a51a --- /dev/null +++ b/src/maze_game/init/Mazes.java @@ -0,0 +1,20 @@ +package maze_game.init; + +import maze_game.maze.Maze; +import maze_game.maze.MazeDrawer; + +public class Mazes +{ + public static final Maze[] MAZE = { + MazeDrawer.loadMazeF("maze0"), + MazeDrawer.loadMazeF("maze1"), + MazeDrawer.loadMazeF("maze2"), + MazeDrawer.loadMazeF("maze3"), + MazeDrawer.loadMazeF("maze4"), + MazeDrawer.loadMazeF("maze5"), + MazeDrawer.loadMazeF("maze6"), + MazeDrawer.loadMazeF("maze7"), + MazeDrawer.loadMazeF("maze8"), + MazeDrawer.loadMazeF("maze9"), + }; +} diff --git a/src/maze_game/init/Models.java b/src/maze_game/init/Models.java new file mode 100644 index 0000000..d264c55 --- /dev/null +++ b/src/maze_game/init/Models.java @@ -0,0 +1,9 @@ +package maze_game.init; + +import maze_game.models.Model; +import maze_game.models.ModelPlayer; + +public class Models +{ + public static final Model MODEL_PLAYER = new ModelPlayer(); +} diff --git a/src/maze_game/input/InputKeyboard.java b/src/maze_game/input/InputKeyboard.java new file mode 100644 index 0000000..34d4abc --- /dev/null +++ b/src/maze_game/input/InputKeyboard.java @@ -0,0 +1,78 @@ +package maze_game.input; + +import org.lwjgl.glfw.GLFWKeyCallbackI; +import static org.lwjgl.glfw.GLFW.*; + +import mainloop.task.IMainloopTask; +import maze_game.Main; +import maze_game.helpers.MathHelpers; +import maze_game.helpers.vec.Vec2d; + +public class InputKeyboard implements GLFWKeyCallbackI, IMainloopTask +{ + public static final InputKeyboard INPUT_KEYBOARD = new InputKeyboard(); + + boolean key_w = false; + boolean key_a = false; + boolean key_s = false; + boolean key_d = false; + + @Override + public boolean MainLoopDelay(long millis) { + return millis > 1; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public void MainLoopUpdate() + { + Vec2d direction = new Vec2d(0, 0); + + if(key_w) { + direction.y += 1; + } + + if(key_a) { + direction.x -= 1; + } + + if(key_s) { + direction.y -= 1; + } + + if(key_d) { + direction.x += 1; + } + + if(direction.x != 0 || direction.y != 0) { + double angle = Math.atan2(direction.x, direction.y); + Main.player.move(MathHelpers.moveTowards2(0.01, angle)); + } + } + + public void invoke(long window, int key, int scancode, int action, int mods) + { + boolean pressed = ! ( action == GLFW_RELEASE ); + + if(key == GLFW_KEY_W) { + key_w = pressed; + } + + if(key == GLFW_KEY_A) { + key_a = pressed; + } + + if(key == GLFW_KEY_S) { + key_s = pressed; + } + + if(key == GLFW_KEY_D) { + key_d = pressed; + } + } + +} diff --git a/src/maze_game/mainloop/FpsCounter.java b/src/maze_game/mainloop/FpsCounter.java new file mode 100644 index 0000000..4a0bc8c --- /dev/null +++ b/src/maze_game/mainloop/FpsCounter.java @@ -0,0 +1,25 @@ +package maze_game.mainloop; + +import mainloop.task.IMainloopTask; +import maze_game.graphics.GraphicsHandler; + +public class FpsCounter implements IMainloopTask +{ + public static int fps = 0; + + @Override + public boolean MainLoopDelay(long millis) { + return millis > 1000; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public void MainLoopUpdate() { + System.out.println("FPS: " + fps + ", " + 1000 / GraphicsHandler.mspf); + fps = 0; + } +} diff --git a/src/maze_game/mainloop/MainloopHandler.java b/src/maze_game/mainloop/MainloopHandler.java new file mode 100644 index 0000000..ad678fc --- /dev/null +++ b/src/maze_game/mainloop/MainloopHandler.java @@ -0,0 +1,70 @@ +package maze_game.mainloop; + +import org.lwjgl.glfw.GLFW; + +import mainloop.event.IMainloopEvent; +import mainloop.manager.MainloopManager; +import mainloop.task.IMainloopTask; +import maze_game.Main; +import maze_game.graphics.GraphicsHandler; +import maze_game.input.InputKeyboard; + +public class MainloopHandler implements IMainloopTask, IMainloopEvent +{ + public MainloopManager mainloop; + + public MainloopHandler() + { + mainloop = new MainloopManager(this); + mainloop.register(this); + + mainloop.register(new GraphicsHandler()); + mainloop.register(new FpsCounter()); + + mainloop.register(InputKeyboard.INPUT_KEYBOARD); + } + + @Override + public void onClose() { + System.out.println("Mainloop closed"); + } + + @Override + public void onEarly() { + GraphicsHandler.mspf -= 1; + if(GraphicsHandler.mspf < 1) { + GraphicsHandler.mspf = 1; + } + } + + @Override + public void onLate() { + GraphicsHandler.mspf += 1; + if(GraphicsHandler.mspf > 1000) { + GraphicsHandler.mspf = 1000; + } + } + + @Override + public void onStart() { + + } + + @Override + public boolean MainLoopDelay(long millis) { + return millis > 10; + } + + @Override + public boolean MainLoopRepeat() { + return true; + } + + @Override + public void MainLoopUpdate() { + GLFW.glfwPollEvents(); + if(GLFW.glfwWindowShouldClose(Main.window.window)) { + mainloop.stop(); + } + } +} diff --git a/src/maze_game/maze/Maze.java b/src/maze_game/maze/Maze.java new file mode 100644 index 0000000..8e6773e --- /dev/null +++ b/src/maze_game/maze/Maze.java @@ -0,0 +1,82 @@ +package maze_game.maze; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; + +import org.lwjgl.opengl.GL33; + +import maze_game.helpers.MathHelpers; +import maze_game.helpers.vec.Vec2d; +import maze_game.helpers.vec.Vec2i; + +public class Maze +{ + public int width; + public int height; + public int[] maze; + public Vec2d player_pos; + private int vao = -1; + public int s; + + public void bind() { + + if(vao == -1) { + this.init(); + } + + GL33.glBindVertexArray(vao); + } + + public void init() + { + float[] verticies = MazeDrawer.drawMazeVerticies(maze, width, height); + + int vao = GL33.glGenVertexArrays(); + int vbo = GL33.glGenBuffers(); + + GL33.glBindVertexArray(vao); + GL33.glBindBuffer(GL33.GL_ARRAY_BUFFER, vbo); + GL33.glBufferData(GL33.GL_ARRAY_BUFFER, verticies, GL33.GL_STATIC_DRAW); + + glVertexAttribPointer(0, 2, GL_FLOAT, false, Float.BYTES * 5, 0); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, false, Float.BYTES * 5, 2 * Float.BYTES); + glEnableVertexAttribArray(1); + + this.vao = vao; + this.s = verticies.length; + } + + public boolean collision(Vec2d pos, int tile) { + return this.collision(pos.x, pos.y, tile); + } + + public boolean collision(double px, double py, int tile) + { + double c = 0.01; + int x1 = MathHelpers.floor(px + c) + 0; + int y1 = MathHelpers.floor(py + c) + 0; + int x2 = MathHelpers.floor(px - c) + 1; + int y2 = MathHelpers.floor(py - c) + 1; + + if( + x1 < 0 || y1 < 0 || x1 >= width || y1 >= height || + x2 < 0 || y2 < 0 || x2 >= width || y2 >= height + ) { + return false; + } + + if( + maze[y1 * width + x1] == tile || + maze[y2 * width + x1] == tile || + maze[y2 * width + x2] == tile || + maze[y1 * width + x2] == tile + ) { + return true; + } + + return false; + } +} diff --git a/src/maze_game/maze/MazeDrawer.java b/src/maze_game/maze/MazeDrawer.java new file mode 100644 index 0000000..adde269 --- /dev/null +++ b/src/maze_game/maze/MazeDrawer.java @@ -0,0 +1,203 @@ +package maze_game.maze; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; + +import org.lwjgl.opengl.GL33; + +import maze_game.helpers.ResourceLoader; +import maze_game.helpers.vec.Vec2d; + +public class MazeDrawer +{ + public static Maze loadMazeF(String filename) { + return loadMaze(new String(ResourceLoader.loadResource("/resources/maze/" + filename + ".txt"))); + } + + public static Maze loadMaze(String data) + { + String[] rows = data.split("\n"); + + int width = 0; + int height = 0; + + for(String row : rows) + { + int l = row.length(); + + if(l == 0) { + continue; + } + + if(l > width) { + width = l; + } + + height += 1; + } + + Vec2d player_pos = null; + int maze[] = new int[width * height]; + int it = 0; + + for(int r=rows.length-1;r>=0;r-=1) + { + String row = rows[r]; + + for(int i=0;i 3 && m < 14) { + color[0] = 0.61f; + color[1] = 0.64f; + color[2] = 0.3f; + } + + // Teleport + if(m == 3) { + color[0] = 0.2f; + color[1] = 0.39f; + color[2] = 0.17f; + } + + // Lava + if(m == 2) { + color[0] = 0.82f; + color[1] = 0.23f; + color[2] = 0.1f; + } + + return color; + } + + public static float[] drawMazePositions(Maze maze) { + return drawMazePositions(maze.maze, maze.width, maze.height); + } + + public static float[] drawMazeVerticies(Maze maze) { + return drawMazeVerticies(maze.maze, maze.width, maze.height); + } + + public static float[] drawMazePositions(int[] maze, int width, int height) + { + float[] colors = new float[maze.length * 3]; + + for(int x=0;x