diff --git a/.idea/libraries/slick2d.xml b/.idea/libraries/slick2d.xml new file mode 100644 index 0000000..2496da3 --- /dev/null +++ b/.idea/libraries/slick2d.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index e2ab130..734b005 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -resources.JumpGame +JumpGame =============== diff --git a/pom.xml b/pom.xml index e461d95..d209eb9 100644 --- a/pom.xml +++ b/pom.xml @@ -8,13 +8,6 @@ jumpGame 1.0-SNAPSHOT - - 3.2.3 - 1.9.24 - natives-windows - UTF-8 - 1.8 - @@ -22,13 +15,23 @@ org.apache.maven.plugins maven-compiler-plugin - 11.0 - 11.0 + 12 + 12 + + 3.2.3 + 1.9.24 + natives-windows + UTF-8 + 1.8 + 12 + 12 + + diff --git a/resources/chicken.png b/resources/chicken.png new file mode 100644 index 0000000..a30f4cf --- /dev/null +++ b/resources/chicken.png Binary files differ diff --git a/resources/hole.png b/resources/hole.png new file mode 100644 index 0000000..21b7d76 --- /dev/null +++ b/resources/hole.png Binary files differ diff --git a/resources/tile.png b/resources/tile.png new file mode 100644 index 0000000..ef8f80c --- /dev/null +++ b/resources/tile.png Binary files differ diff --git a/src/main/java/GLWindow.java b/src/main/java/GLWindow.java new file mode 100644 index 0000000..fc174aa --- /dev/null +++ b/src/main/java/GLWindow.java @@ -0,0 +1,81 @@ +import entities.GLConfigVariable; +import org.lwjgl.glfw.GLFWErrorCallback; +import org.lwjgl.opengl.GL; + +import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks; +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.system.MemoryUtil.NULL; + +//--------------------------------------------------------------- +// ウィンドウ +public class GLWindow { + + private long hWnd; // ウィンドウハンドル + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public void init() { + initWindow(); + initRender(); + } + + //--------------------------------------------------------------- + // 画面のスワップ + public void swapWindow() { + glfwSwapBuffers(hWnd); // バッファのスワップ + glfwPollEvents(); // 入力とかイベントの取得 + } + + //--------------------------------------------------------------- + // ウィンドウの破棄 + public void destroyWindow() { + glfwFreeCallbacks(hWnd); // ウィンドウコールバックの解放 + glfwDestroyWindow(hWnd); // ウィンドウの破棄 + glfwTerminate(); // GLFWの破棄 + glfwSetErrorCallback(null).free(); // エラーコールバックの解放 + } + + //--------------------------------------------------------------- + // ウィンドウが起動しているかどうか + public boolean windowShouldClose() { + return glfwWindowShouldClose(hWnd); + } + + //--------------------------------------------------------------- + // ウィンドウの初期化 + private void initWindow() { + GLFWErrorCallback.createPrint(System.err).set(); // エラーコールバックの設定 + + + // GLFWの初期化 + if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); + + // ウィンドウの設定 + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + // ウィンドウの作成 + hWnd = glfwCreateWindow(GLConfigVariable.WIDTH, GLConfigVariable.HEIGHT, GLConfigVariable.TITLE_NAME, GLConfigVariable.IS_FULL_SCREEN ? glfwGetPrimaryMonitor() : NULL, NULL); + if (hWnd == NULL) + throw new RuntimeException("Failed to create the window."); + + glfwMakeContextCurrent(hWnd); //起動したウィンドウをターゲットに + glfwSwapInterval(1); // v-syncの適応 + + glfwShowWindow(hWnd); // ウィンドウの表示 + } + + //--------------------------------------------------------------- + // 描画プロセス初期化 + private void initRender() { + GL.createCapabilities(); + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // 背景色設定 + + // OpenGLの初期化 + glEnable(GL_TEXTURE_2D); // 二次元テクスチャの有効化 + glEnable(GL_BLEND); // アルファブレンディングの有効化 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // ブレンドモードの設定 + } + //--------------------------------------------------------------- +} diff --git a/src/main/java/GameEngine.java b/src/main/java/GameEngine.java new file mode 100644 index 0000000..50627dc --- /dev/null +++ b/src/main/java/GameEngine.java @@ -0,0 +1,46 @@ +import static org.lwjgl.opengl.GL11.*; + +//--------------------------------------------------------------- +// ゲームループ提供 +public abstract class GameEngine { + + private GLWindow glWindow = new GLWindow(); + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // 初期化 + protected void init() { + } + + //--------------------------------------------------------------- + // ゲームループ + protected void update() { + } + + //--------------------------------------------------------------- + // 破棄処理 + protected void destroy() { + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // 実行系の本体 + protected void run() { + + // 初期化 + glWindow.init(); + init(); + + // メインループ + while (!glWindow.windowShouldClose()) { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // バッファのクリア + update(); + glWindow.swapWindow(); + } + + // 終了処理 + destroy(); // デストラクタ + glWindow.destroyWindow(); // ウィンドウの破棄 + } + //--------------------------------------------------------------- +} diff --git a/src/main/java/JumpGame.java b/src/main/java/JumpGame.java new file mode 100644 index 0000000..9927138 --- /dev/null +++ b/src/main/java/JumpGame.java @@ -0,0 +1,61 @@ +import models.IModel; +import models.JumpGameModel; +import views.IView; +import views.PlayerRenderer; +import views.TileMapRenderer; + +import java.util.ArrayList; + +public class JumpGame { + //--------------------------------------------------------------- + + //--------------------------------------------------------------- + // new + private ArrayList views = new ArrayList<>(); + private IModel model = new JumpGameModel(); + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public void gravity(double y) { +// this.time.gravity(y); + JumpGameModel jumpGameModel = (JumpGameModel) model; + jumpGameModel.updateGravity(y); + } + + //--------------------------------------------------------------- + // 初期化 + public void init() { + // view + views.add(new TileMapRenderer()); + views.add(new PlayerRenderer("resources/chicken.png")); + } + + //--------------------------------------------------------------- + // 更新処理 + public void update() { + + // Viewの更新 + for (IView view : views){ + view.update(model); + view.display(); + } + + // Modelの更新 + // + // Space キーのインプット + // + // マイフレーム更新 + + gravity(-256); //重力 + } + + //--------------------------------------------------------------- + // デストラクタのような処理 + public void delete() { + for (IView view : views) view.delete(); + } + + //--------------------------------------------------------------- + +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index c6d48d4..445b251 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,110 +1,46 @@ +import entities.GLConfigVariable; import org.lwjgl.glfw.*; import org.lwjgl.opengl.*; -import org.lwjgl.Version; import static org.lwjgl.glfw.Callbacks.*; - import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.system.MemoryUtil.*; -public class Main { +//--------------------------------------------------------------- +// 実行本体 +public class Main extends GameEngine { - // The window handle - private long window; + private JumpGame jumpGame = new JumpGame(); - public void run() { - System.out.println("Hello LWJGL " + Version.getVersion() + "!"); - - try { - init(); - loop(); - - // Release window and window callbacks - glfwFreeCallbacks(window); - glfwDestroyWindow(window); - } finally { - // Terminate GLFW and release the GLFWerrorfun - glfwTerminate(); - glfwSetErrorCallback(null).free(); - } - } - - private void init() { - // Setup an error callback. The default implementation - // will print the error message in System.err. - GLFWErrorCallback.createPrint(System.err).set(); - - // Initialize GLFW. Most GLFW functions will not work before doing this. - if (!glfwInit()) { - throw new IllegalStateException("Unable to initialize GLFW"); - } - - // Configure our window - glfwDefaultWindowHints(); // optional, the current window hints are already the default - glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation - glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable - - int WIDTH = 300; - int HEIGHT = 300; - - // Create the window - window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL); - if (window == NULL) { - throw new RuntimeException("Failed to create the GLFW window"); - } - - // Setup a key callback. It will be called every time a key is pressed, repeated or released. - glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> { - if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) { - glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop - } - }); - - // Get the resolution of the primary monitor - GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); - // Center our window - glfwSetWindowPos( - window, - (vidmode.width() - WIDTH) / 2, - (vidmode.height() - HEIGHT) / 2 - ); - - // Make the OpenGL context current - glfwMakeContextCurrent(window); - // Enable v-sync - glfwSwapInterval(1); - - // Make the window visible - glfwShowWindow(window); - } - - private void loop() { - // This line is critical for LWJGL's interoperation with GLFW's - // OpenGL context, or any context that is managed externally. - // LWJGL detects the context that is current in the current thread, - // creates the ContextCapabilities instance and makes the OpenGL - // bindings available for use. - GL.createCapabilities(); - - // Set the clear color - glClearColor(1.0f, 0.0f, 0.0f, 0.0f); - - // Run the rendering loop until the user has attempted to close - // the window or has pressed the ESCAPE key. - while (!glfwWindowShouldClose(window)) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer - - glfwSwapBuffers(window); // swap the color buffers - - // Poll for window events. The key callback above will only be - // invoked during this call. - glfwPollEvents(); - } - } - + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // エントリーポイント public static void main(String[] args) { new Main().run(); } + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // 初期化 + @Override + protected void init(){ + jumpGame.init(); + } + + //--------------------------------------------------------------- + // 更新処理 + @Override + protected void update(){ + jumpGame.update(); + } + + //--------------------------------------------------------------- + // 破棄 + @Override + protected void destroy() { + jumpGame.delete(); + } + + //--------------------------------------------------------------- } \ No newline at end of file diff --git a/src/main/java/entities/Clear.java b/src/main/java/entities/Clear.java new file mode 100644 index 0000000..d2345d4 --- /dev/null +++ b/src/main/java/entities/Clear.java @@ -0,0 +1,21 @@ +package entities; + +import entities.player.Position; + +public class Clear { + private Position position; + + public Clear(Position position) { + this.position = position; + } + + public boolean getClear() { + boolean temp_l4; + if ((this.position.getValue().getFirst() > 100.0)) { + temp_l4 = true; + } else { + temp_l4 = false; + } + return temp_l4; + } +} \ No newline at end of file diff --git a/src/main/java/entities/Color.java b/src/main/java/entities/Color.java new file mode 100644 index 0000000..e7ae250 --- /dev/null +++ b/src/main/java/entities/Color.java @@ -0,0 +1,35 @@ +package entities; + +public class Color { + private float r; + private float g; + private float b; + private float a; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public Color(float r, float g, float b, float a) { + this.r = r; + this.g = g; + this.b = b; + this.a = a; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public float getR() { + return r; + } + + public float getG() { + return g; + } + + public float getB() { + return b; + } + + public float getA() { + return a; + } +} diff --git a/src/main/java/entities/GLConfigVariable.java b/src/main/java/entities/GLConfigVariable.java new file mode 100644 index 0000000..adb3001 --- /dev/null +++ b/src/main/java/entities/GLConfigVariable.java @@ -0,0 +1,18 @@ +package entities; + +//--------------------------------------------------------------- +// 画面設定定数 +public class GLConfigVariable { + //--------------------------------------------------------------- + //--------------------------------------------------------------- + private GLConfigVariable() { + } + + //--------------------------------------------------------------- + public static final String TITLE_NAME = "JumpGame"; + public static final int WIDTH = 1280; + public static final int HEIGHT = 720; + public static final int DEPTH = 100; // 追加。深度 + public static final double TARGET_FPS = 60d; //フレームレート + public static final boolean IS_FULL_SCREEN = false; +} \ No newline at end of file diff --git a/src/main/java/entities/Gameover.java b/src/main/java/entities/Gameover.java new file mode 100644 index 0000000..b248c05 --- /dev/null +++ b/src/main/java/entities/Gameover.java @@ -0,0 +1,21 @@ +package entities; + +import entities.player.Position; + +public class Gameover { + private Position position; + + public Gameover(Position position) { + this.position = position; + } + + public boolean getGameover() { + boolean temp_l6; + if ((this.position.getValue().getSecond() < -(1.0))) { + temp_l6 = true; + } else { + temp_l6 = false; + } + return temp_l6; + } +} \ No newline at end of file diff --git a/src/main/java/entities/Ground.java b/src/main/java/entities/Ground.java new file mode 100644 index 0000000..58ab6f6 --- /dev/null +++ b/src/main/java/entities/Ground.java @@ -0,0 +1,9 @@ +package entities; + +public class Ground { + private boolean value; + + public boolean getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/entities/Image2D.java b/src/main/java/entities/Image2D.java new file mode 100644 index 0000000..bbbc877 --- /dev/null +++ b/src/main/java/entities/Image2D.java @@ -0,0 +1,103 @@ +package entities; + +import static org.lwjgl.opengl.GL11.*; + +//--------------------------------------------------------------- +// +public class Image2D { + + private int id; // テクスチャのID + private Pair wh; // スプライトの幅高 + private Pair position; // スプライトの座標(画面座標) + private Color color; // スプライトの色 + private double rotation; // 回転(度) + private double scale; // 拡大 + private double alpha; // 透明度 + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public Image2D(Texture tex) { + if (tex != null) + this.id = tex.getId(); + else + this.id = 0; + + this.position = new Pair<>(0.0, 0.0); + this.color = new Color(1, 1, 1, 1); + + if (tex != null) + wh = new Pair<>((double) tex.getWidth(), (double) tex.getHeight()); + else + wh = new Pair<>(32d, 20d); + rotation = 0.0; + scale = 1.0; + alpha = 1.0; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // setter + public void setRotation(double rotation) { + this.rotation = rotation; + } + + public void setPosition(Pair position) { + this.position = position; + } + + public void setScale(double scale) { + this.scale = scale; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public void draw() { + // テクスチャの結合 + glBindTexture(GL_TEXTURE_2D, id); + + // 変換行列の追加 + glPushMatrix(); + + // モデルビューモード + glMatrixMode(GL_MODELVIEW); + // 行列の設定 + glLoadIdentity(); // 単位行列化 + glTranslated(position.getFirst(), position.getSecond(), 0); // 移動 + glRotated(rotation, 0, 0, 1); // 回転 + glScaled(scale, scale, 1); // 拡縮 + + // プロジェクションモード + glMatrixMode(GL_PROJECTION); + // 行列の設定 + glLoadIdentity(); // 単位行列化 + glOrtho(0, GLConfigVariable.WIDTH, 0, GLConfigVariable.HEIGHT, -GLConfigVariable.DEPTH, GLConfigVariable.DEPTH); // 正射影投影 + + // ビューポートの範囲 + glViewport(0, 0, GLConfigVariable.WIDTH, GLConfigVariable.HEIGHT); + + // ポリゴンの色 + glColor4d(color.getR(), color.getG(), color.getB(), alpha); + + // ポリゴンの作成とテクスチャの適応 + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2d(0, 0); + glVertex2d(-wh.getFirst() / 2, wh.getSecond() / 2); + glTexCoord2d(0, 1); + glVertex2d(-wh.getFirst() / 2, -wh.getSecond() / 2); + glTexCoord2d(1, 0); + glVertex2d(wh.getFirst() / 2, wh.getSecond() / 2); + glTexCoord2d(1, 1); + glVertex2d(wh.getFirst() / 2, -wh.getSecond() / 2); + glEnd(); + + // 行列の破棄 + glPopMatrix(); + + // テクスチャの解除 + glBindTexture(GL_TEXTURE_2D, 0); + } + + //--------------------------------------------------------------- +} \ No newline at end of file diff --git a/src/main/java/entities/Pair.java b/src/main/java/entities/Pair.java new file mode 100644 index 0000000..4f17624 --- /dev/null +++ b/src/main/java/entities/Pair.java @@ -0,0 +1,19 @@ +package entities; + +public class Pair { + private T first; + private T second; + + public Pair(T first, T second) { + this.first = first; + this.second = second; + } + + public T getFirst() { + return this.first; + } + + public T getSecond() { + return this.second; + } +} diff --git a/src/main/java/entities/Sprite.java b/src/main/java/entities/Sprite.java new file mode 100644 index 0000000..b84e749 --- /dev/null +++ b/src/main/java/entities/Sprite.java @@ -0,0 +1,61 @@ +package entities; + +import views.IView; + +//--------------------------------------------------------------- +// +public class Sprite { + + private Texture texture; + private Image2D img; + private Pair positionValue; + private double scaleValue; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public Sprite(String path) { + this.positionValue = new Pair<>(0d, 0d); + texture = new Texture("player", path); + img = new Image2D(texture); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // getter + public double getScaleValue() { + return this.scaleValue; + } + + public Pair getPositionValue() { + return this.positionValue; + } + + //--------------------------------------------------------------- + // setter + public void setPositionValue(Pair positionValue) { + this.positionValue = positionValue; + } + + public void setScaleValue(double scaleValue) { + this.scaleValue = scaleValue; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public void draw() { + img.setScale(scaleValue); + img.setPosition(positionValue); + img.draw(); + } + + //--------------------------------------------------------------- + // テクスチャの開放 + public void delete() { + texture.delete(); + } + + //--------------------------------------------------------------- + +} diff --git a/src/main/java/entities/Texture.java b/src/main/java/entities/Texture.java new file mode 100644 index 0000000..7bfb9c6 --- /dev/null +++ b/src/main/java/entities/Texture.java @@ -0,0 +1,107 @@ +package entities; + +import static org.lwjgl.opengl.GL11.*; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import javax.imageio.ImageIO; + +import org.lwjgl.BufferUtils; + +//--------------------------------------------------------------- +// +public class Texture { + + private int id; + private int width; + private int height; + private String name; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public Texture(String name, String path) { + this.name = name; + BufferedImage bi; + try { + // イメージファイルの読み込み + bi = ImageIO.read(new File(path)); + width = bi.getWidth(); + height = bi.getHeight(); + + // ピクセルを保存する配列の用意 + int[] pixelsRaw; + pixelsRaw = bi.getRGB(0, 0, width, height, null, 0, width); + ByteBuffer pixels = BufferUtils.createByteBuffer(width * height * 4); + + // 一ピクセルずつ読み込む + for (int i = 0; i < height; ++i) { + for (int j = 0; j < width; ++j) { + int p = pixelsRaw[i * width + j]; + pixels.put((byte) ((p >> 16) & 0xFF)); + pixels.put((byte) ((p >> 8) & 0xFF)); + pixels.put((byte) (p & 0xFF)); + pixels.put((byte) ((p >> 24) & 0xFF)); + } + } + pixels.flip(); + + // テクスチャの作成 + id = glGenTextures(); // IDの取得 + glBindTexture(GL_TEXTURE_2D, id); // IDとテクスチャデータを結合 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); // 作成 + + // テクスチャの設定 + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + // テクスチャの解除 + glBindTexture(GL_TEXTURE_2D, 0); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public int getHeight() { + return height; + } + + //--------------------------------------------------------------- + public int getWidth() { + return width; + } + + //--------------------------------------------------------------- + public int getId() { + return id; + } + + //--------------------------------------------------------------- + public String getName() { + return name; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // テクスチャの使用 + public void bind() { + glBindTexture(GL_TEXTURE_2D, id); + } + + //--------------------------------------------------------------- + // テクスチャの解除 + public void debind() { + glBindTexture(GL_TEXTURE_2D, 0); + } + + //--------------------------------------------------------------- + // テクスチャの破棄 + public void delete() { + glDeleteTextures(id); + } + //--------------------------------------------------------------- +} \ No newline at end of file diff --git a/src/main/java/entities/TileType.java b/src/main/java/entities/TileType.java new file mode 100644 index 0000000..178adc0 --- /dev/null +++ b/src/main/java/entities/TileType.java @@ -0,0 +1,19 @@ +package entities; + +//--------------------------------------------------------------- +// タイルの種類の定数クラス +public enum TileType { + + CLOSE(true), + OPEN(false); + + private final boolean isClose; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + private TileType(boolean isClose) { + this.isClose = isClose; + } + + //--------------------------------------------------------------- +} diff --git a/src/main/java/entities/Time.java b/src/main/java/entities/Time.java new file mode 100644 index 0000000..f08de37 --- /dev/null +++ b/src/main/java/entities/Time.java @@ -0,0 +1,13 @@ +package entities; + +public class Time { + private double value; + + public void gravity(double y) { + this.value = (this.value + 0.01); + } + + public double getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/entities/player/Acceleration.java b/src/main/java/entities/player/Acceleration.java new file mode 100644 index 0000000..fe00b13 --- /dev/null +++ b/src/main/java/entities/player/Acceleration.java @@ -0,0 +1,50 @@ +package entities.player; + +import entities.Pair; + +public class Acceleration { + private double massValue = 1.0; + private Pair forceValue = new Pair<>(0d, 0d); + private Velocity velocity; + private Onground onground; + private Pair value = new Pair<>(0d, 0d); + + //--------------------------------------------------------------- + // + public void updateByMass(double mass) { + this.massValue = mass; + Pair temp_l0; + + if (this.onground.getOnground()) temp_l0 = new Pair((forceValue.getFirst() / mass), 0.0); + else temp_l0 = new Pair((forceValue.getFirst() / mass), (forceValue.getSecond() / mass)); + + this.value = temp_l0; + velocity.updateByAcceleration(value); + } + + //--------------------------------------------------------------- + // + public void updateByForce(Pair force) { + this.forceValue = force; + Pair temp_l1; + + if (this.onground.getOnground()) temp_l1 = new Pair((force.getFirst() / massValue), 0.0); + temp_l1 = new Pair((force.getFirst() / massValue), (force.getSecond() / massValue)); + + value = temp_l1; + velocity.updateByAcceleration(value); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public Acceleration(Velocity velocity, Onground onground) { + this.velocity = velocity; + this.onground = onground; + } + + //--------------------------------------------------------------- + // + public Pair getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/entities/player/Force.java b/src/main/java/entities/player/Force.java new file mode 100644 index 0000000..9c034d9 --- /dev/null +++ b/src/main/java/entities/player/Force.java @@ -0,0 +1,21 @@ +package entities.player; + +import entities.Pair; + +public class Force { + private Acceleration acceleration; + private Pair value = new Pair<>(0.0d, 0.0d); + + public Force(Acceleration acceleration) { + this.acceleration = acceleration; + } + + public void gravity(double y) { + this.value = new Pair(0.0, y); + acceleration.updateByForce(value); + } + + public Pair getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/entities/player/Mass.java b/src/main/java/entities/player/Mass.java new file mode 100644 index 0000000..16283e6 --- /dev/null +++ b/src/main/java/entities/player/Mass.java @@ -0,0 +1,19 @@ +package entities.player; + +public class Mass { + private Acceleration acceleration; + private double value; + + public Mass(Acceleration acceleration) { + this.acceleration = acceleration; + } + + public void setValue(double x) { + this.value = x; + acceleration.updateByMass(value); + } + + public double getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/entities/player/Move.java b/src/main/java/entities/player/Move.java new file mode 100644 index 0000000..460d0d1 --- /dev/null +++ b/src/main/java/entities/player/Move.java @@ -0,0 +1,25 @@ +package entities.player; + +import entities.Pair; + +public class Move { + private Velocity velocity; + private Pair value = new Pair<>(0d, 0d); + + public Move(Velocity velocity) { + this.velocity = velocity; + } + + public void moveX(double x) { + this.value = new Pair(x, this.value.getSecond()); + velocity.updateByMove(value); + } + + public void moveY(double y) { + this.value = new Pair(this.value.getFirst(), y); + } + + public Pair getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/entities/player/Onground.java b/src/main/java/entities/player/Onground.java new file mode 100644 index 0000000..71abfda --- /dev/null +++ b/src/main/java/entities/player/Onground.java @@ -0,0 +1,16 @@ +package entities.player; + +import entities.Ground; + +public class Onground { + private Ground ground; + private Position position; + + public Onground(Ground ground, Position position) { + this.ground = ground; + this.position = position; + } + + public boolean getOnground() { + return ((this.ground.getValue() == true) && (this.position.getValue().getSecond() <= 0.0)); } +} \ No newline at end of file diff --git a/src/main/java/entities/player/Position.java b/src/main/java/entities/player/Position.java new file mode 100644 index 0000000..05c9f06 --- /dev/null +++ b/src/main/java/entities/player/Position.java @@ -0,0 +1,45 @@ +package entities.player; + +import entities.Ground; +import entities.Pair; + +//--------------------------------------------------------------- +// +public class Position { + private Ground ground; + private Pair value = new Pair<>(0d, 0d); + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public void updateByVelocity(Pair velocity) { + Pair temp_l3; + if (((this.ground.getValue() == true) && ((this.value.getSecond() + (0.01 * velocity.getSecond())) < 0.0))) { + temp_l3 = new Pair((this.value.getFirst() + (0.01 * velocity.getFirst())), 0.0); + } else { + temp_l3 = new Pair((this.value.getFirst() + (0.01 * velocity.getFirst())), (this.value.getSecond() + (0.01 * velocity.getSecond()))); + } + value = temp_l3; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public Position(Ground ground) { + this.ground = ground; + } + + //--------------------------------------------------------------- + // + public Position(Pair value, Ground ground) { + this.value = value; + this.ground = ground; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // getter + public Pair getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/entities/player/Velocity.java b/src/main/java/entities/player/Velocity.java new file mode 100644 index 0000000..8209abb --- /dev/null +++ b/src/main/java/entities/player/Velocity.java @@ -0,0 +1,53 @@ +package entities.player; + + +import entities.Pair; + +public class Velocity { + private Pair moveValue = new Pair<>(0d, 0d); + private Pair accelerationValue = new Pair<>(0d, 0d); + private Position position; + private Onground onground; + private Pair value = new Pair<>(0d, 0d); + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public void updateByMove(Pair move) { + this.moveValue = move; + Pair temp_l2; + if ((this.onground.getOnground() && (move.getSecond() >= 0.0))) { + temp_l2 = move; + } else { + temp_l2 = this.value; + } + value = temp_l2; + position.updateByVelocity(value); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public void updateByAcceleration(Pair acceleration) { + this.accelerationValue = acceleration; + Pair temp_l5; + if ((this.onground.getOnground() && (this.value.getSecond() < 0.0))) { + temp_l5 = new Pair((this.value.getFirst() + (0.01 * acceleration.getFirst())), 0.0); + } else { + temp_l5 = new Pair((this.value.getFirst() + (0.01 * acceleration.getFirst())), (this.value.getSecond() + (0.01 * acceleration.getSecond()))); + } + value = temp_l5; + position.updateByVelocity(value); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public Velocity(Position position, Onground onground) { + this.position = position; + this.onground = onground; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public Pair getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/models/IModel.java b/src/main/java/models/IModel.java new file mode 100644 index 0000000..700df1f --- /dev/null +++ b/src/main/java/models/IModel.java @@ -0,0 +1,4 @@ +package models; + +public interface IModel { +} diff --git a/src/main/java/models/JumpGameModel.java b/src/main/java/models/JumpGameModel.java new file mode 100644 index 0000000..b0617aa --- /dev/null +++ b/src/main/java/models/JumpGameModel.java @@ -0,0 +1,77 @@ +package models; + +import entities.*; +import entities.player.*; + +import java.util.ArrayList; + +//--------------------------------------------------------------- +// +public class JumpGameModel implements IModel { + //--------------------------------------------------------------- + // + private Acceleration acceleration; + private Force force; + private Mass mass; + private Move move; + private Position position; + private Velocity velocity; + private Onground onground; + + //--------------------------------------------------------------- + private Time time; + private Gameover gameover; + private Clear clear; + + //--------------------------------------------------------------- + private Ground ground; + + //--------------------------------------------------------------- + private double jumpPower = 32; + private ArrayList flagTimings = new ArrayList<>(); // フラグ切り替え + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public JumpGameModel() { + ground = new Ground(); + position = new Position(new Pair<>(640d, 480d), ground); + onground = new Onground(ground, position); + velocity = new Velocity(position, onground); + acceleration = new Acceleration(velocity, onground); + force = new Force(acceleration); + mass = new Mass(acceleration); + move = new Move(velocity); + time = new Time(); + gameover = new Gameover(position); + clear = new Clear(position); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // getter + public Position getPosition() { + return position; + } + + public Ground getGround() { + return ground; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public void jump() { + this.move.moveY(jumpPower); + } + + //--------------------------------------------------------------- + // + public void updateGravity(double gravity) { + this.time.gravity(gravity); + this.force.gravity(gravity); + System.out.println("swapWindow Gravity"); + } + + //--------------------------------------------------------------- +} diff --git a/src/main/java/resources/Acceleration.java b/src/main/java/resources/Acceleration.java deleted file mode 100644 index f7d33d4..0000000 --- a/src/main/java/resources/Acceleration.java +++ /dev/null @@ -1,42 +0,0 @@ -package resources; - -public class Acceleration { - private double massValue; - private Vector2 forceValue; - private Velocity velocity; - private Onground onground; - private Vector2 value; - - public void updateByMass(double mass) { - this.massValue = mass; - Vector2 temp_l0; - if (this.onground.getOnground()) { - temp_l0 = new Vector2((forceValue.getX() / mass), 0.0); - } else { - temp_l0 = new Vector2((forceValue.getX() / mass), (forceValue.getY() / mass)); - } - value = temp_l0; - velocity.updateByAcceleration(value); - } - - public void updateByForce(Vector2 force) { - this.forceValue = force; - Vector2 temp_l1; - if (this.onground.getOnground()) { - temp_l1 = new Vector2((force.getX() / massValue), 0.0); - } else { - temp_l1 = new Vector2((force.getX() / massValue), (force.getY() / massValue)); - } - value = temp_l1; - velocity.updateByAcceleration(value); - } - - public Acceleration(Velocity velocity, Onground onground) { - this.velocity = velocity; - this.onground = onground; - } - - public Vector2 getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/resources/Clear.java b/src/main/java/resources/Clear.java deleted file mode 100644 index 8076814..0000000 --- a/src/main/java/resources/Clear.java +++ /dev/null @@ -1,19 +0,0 @@ -package resources; - -public class Clear { - private Position position; - - public Clear(Position position) { - this.position = position; - } - - public boolean getClear() { - boolean temp_l4; - if ((this.position.getValue().getX() > 100.0)) { - temp_l4 = true; - } else { - temp_l4 = false; - } - return temp_l4; - } -} \ No newline at end of file diff --git a/src/main/java/resources/Force.java b/src/main/java/resources/Force.java deleted file mode 100644 index 5ee523e..0000000 --- a/src/main/java/resources/Force.java +++ /dev/null @@ -1,19 +0,0 @@ -package resources; - -public class Force { - private Acceleration acceleration; - private Vector2 value; - - public Force(Acceleration acceleration) { - this.acceleration = acceleration; - } - - public void gravity(double y) { - this.value = new Vector2(0.0, y); - acceleration.updateByForce(value); - } - - public Vector2 getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/resources/Gameover.java b/src/main/java/resources/Gameover.java deleted file mode 100644 index 9f95b01..0000000 --- a/src/main/java/resources/Gameover.java +++ /dev/null @@ -1,19 +0,0 @@ -package resources; - -public class Gameover { - private Position position; - - public Gameover(Position position) { - this.position = position; - } - - public boolean getGameover() { - boolean temp_l6; - if ((this.position.getValue().getY() < -(1.0))) { - temp_l6 = true; - } else { - temp_l6 = false; - } - return temp_l6; - } -} \ No newline at end of file diff --git a/src/main/java/resources/Ground.java b/src/main/java/resources/Ground.java deleted file mode 100644 index 2df0f2f..0000000 --- a/src/main/java/resources/Ground.java +++ /dev/null @@ -1,9 +0,0 @@ -package resources; - -public class Ground { - private boolean value; - - public boolean getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/resources/JumpGame.java b/src/main/java/resources/JumpGame.java deleted file mode 100644 index 6689e0e..0000000 --- a/src/main/java/resources/JumpGame.java +++ /dev/null @@ -1,76 +0,0 @@ -package resources; - -public class JumpGame { - private Time time = new Time(); - private Ground ground = new Ground(); - private Position position = new Position(ground); - private Gameover gameover = new Gameover(position); - private Onground onground = new Onground(ground, position); - private Velocity velocity = new Velocity(position, onground); - private Clear clear = new Clear(position); - private Move move = new Move(velocity); - private Acceleration acceleration = new Acceleration(velocity, onground); - private Force force = new Force(acceleration); - private Mass mass = new Mass(acceleration); - - public void gravity(double y) { - this.force.gravity(y); - this.time.gravity(y); - } - - public void moveX(double x) { - this.move.moveX(x); - } - - public void moveY(double y) { - this.move.moveY(y); - } - - public void setMass(double x) { - this.mass.setValue(x); - } - - public Vector2 getAcceleration() { - return acceleration.getValue(); - } - - public Vector2 getMove() { - return move.getValue(); - } - - public double getMass() { - return mass.getValue(); - } - - public boolean getClear() { - return clear.getClear(); - } - - public boolean getGround() { - return ground.getValue(); - } - - public Vector2 getForce() { - return force.getValue(); - } - - public Vector2 getVelocity() { - return velocity.getValue(); - } - - public Vector2 getPosition() { - return position.getValue(); - } - - public boolean getOnground() { - return onground.getOnground(); - } - - public double getTime() { - return time.getValue(); - } - - public boolean getGameover() { - return gameover.getGameover(); - } -} \ No newline at end of file diff --git a/src/main/java/resources/Mass.java b/src/main/java/resources/Mass.java deleted file mode 100644 index c0acc82..0000000 --- a/src/main/java/resources/Mass.java +++ /dev/null @@ -1,19 +0,0 @@ -package resources; - -public class Mass { - private Acceleration acceleration; - private double value; - - public Mass(Acceleration acceleration) { - this.acceleration = acceleration; - } - - public void setValue(double x) { - this.value = x; - acceleration.updateByMass(value); - } - - public double getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/resources/Move.java b/src/main/java/resources/Move.java deleted file mode 100644 index 20be320..0000000 --- a/src/main/java/resources/Move.java +++ /dev/null @@ -1,23 +0,0 @@ -package resources; - -public class Move { - private Velocity velocity; - private Vector2 value; - - public Move(Velocity velocity) { - this.velocity = velocity; - } - - public void moveX(double x) { - this.value = new Vector2(x, this.value.getY()); - velocity.updateByMove(value); - } - - public void moveY(double y) { - this.value = new Vector2(this.value.getX(), y); - } - - public Vector2 getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/resources/Onground.java b/src/main/java/resources/Onground.java deleted file mode 100644 index de8ece5..0000000 --- a/src/main/java/resources/Onground.java +++ /dev/null @@ -1,14 +0,0 @@ -package resources; - -public class Onground { - private Ground ground; - private Position position; - - public Onground(Ground ground, Position position) { - this.ground = ground; - this.position = position; - } - - public boolean getOnground() { - return ((this.ground.getValue() == true) && (this.position.getValue().getY() <= 0.0)); } -} \ No newline at end of file diff --git a/src/main/java/resources/Position.java b/src/main/java/resources/Position.java deleted file mode 100644 index d890660..0000000 --- a/src/main/java/resources/Position.java +++ /dev/null @@ -1,24 +0,0 @@ -package resources; - -public class Position { - private Ground ground; - private Vector2 value; - - public void updateByVelocity(Vector2 velocity) { - Vector2 temp_l3; - if (((this.ground.getValue() == true) && ((this.value.getY() + (0.01 * velocity.getY())) < 0.0))) { - temp_l3 = new Vector2((this.value.getX() + (0.01 * velocity.getX())), 0.0); - } else { - temp_l3 = new Vector2((this.value.getX() + (0.01 * velocity.getX())), (this.value.getY() + (0.01 * velocity.getY()))); - } - value = temp_l3; - } - - public Position(Ground ground) { - this.ground = ground; - } - - public Vector2 getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/resources/Time.java b/src/main/java/resources/Time.java deleted file mode 100644 index 3ba6de2..0000000 --- a/src/main/java/resources/Time.java +++ /dev/null @@ -1,13 +0,0 @@ -package resources; - -public class Time { - private double value; - - public void gravity(double y) { - this.value = (this.value + 0.01); - } - - public double getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/resources/Vector2.java b/src/main/java/resources/Vector2.java deleted file mode 100644 index 8b97696..0000000 --- a/src/main/java/resources/Vector2.java +++ /dev/null @@ -1,19 +0,0 @@ -package resources; - -public class Vector2 { - private double x; - private double y; - - public Vector2(double x, double y) { - this.x = x; - this.y = y; - } - - public double getX() { - return this.x; - } - - public double getY() { - return this.y; - } -} diff --git a/src/main/java/resources/Velocity.java b/src/main/java/resources/Velocity.java deleted file mode 100644 index fcae09b..0000000 --- a/src/main/java/resources/Velocity.java +++ /dev/null @@ -1,42 +0,0 @@ -package resources; - -public class Velocity { - private Vector2 moveValue; - private Vector2 accelerationValue; - private Position position; - private Onground onground; - private Vector2 value; - - public void updateByMove(Vector2 move) { - this.moveValue = move; - Vector2 temp_l2; - if ((this.onground.getOnground() && (move.getY() >= 0.0))) { - temp_l2 = move; - } else { - temp_l2 = this.value; - } - value = temp_l2; - position.updateByVelocity(value); - } - - public void updateByAcceleration(Vector2 acceleration) { - this.accelerationValue = acceleration; - Vector2 temp_l5; - if ((this.onground.getOnground() && (this.value.getY() < 0.0))) { - temp_l5 = new Vector2((this.value.getX() + (0.01 * acceleration.getX())), 0.0); - } else { - temp_l5 = new Vector2((this.value.getX() + (0.01 * acceleration.getX())), (this.value.getY() + (0.01 * acceleration.getY()))); - } - value = temp_l5; - position.updateByVelocity(value); - } - - public Velocity(Position position, Onground onground) { - this.position = position; - this.onground = onground; - } - - public Vector2 getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/views/IView.java b/src/main/java/views/IView.java new file mode 100644 index 0000000..449d420 --- /dev/null +++ b/src/main/java/views/IView.java @@ -0,0 +1,20 @@ +package views; + +import models.IModel; + +//--------------------------------------------------------------- +// +public interface IView { + + //--------------------------------------------------------------- + // モデルの監視 + void update(IModel model); + + //--------------------------------------------------------------- + // 描画 + void display(); + + //--------------------------------------------------------------- + // テクスチャの開放 + void delete(); +} diff --git a/src/main/java/views/PlayerRenderer.java b/src/main/java/views/PlayerRenderer.java new file mode 100644 index 0000000..7d071b1 --- /dev/null +++ b/src/main/java/views/PlayerRenderer.java @@ -0,0 +1,43 @@ +package views; + +import entities.*; +import models.IModel; +import models.JumpGameModel; + +//--------------------------------------------------------------- +// +public class PlayerRenderer implements IView { + private Sprite sprite; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public PlayerRenderer(String path) { + this.sprite = new Sprite(path); + this.sprite.setScaleValue(1); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // JumpGameModelから座標を取得 + @Override + public void update(IModel model){ + JumpGameModel jumpGameModel = (JumpGameModel) model; + sprite.setPositionValue(jumpGameModel.getPosition().getValue()); + } + + + //--------------------------------------------------------------- + // 描画する + @Override + public void display() { + sprite.draw(); + } + + //--------------------------------------------------------------- + // テクスチャの開放 + @Override + public void delete() { + sprite.delete(); + } + //--------------------------------------------------------------- +} diff --git a/src/main/java/views/TileMapRenderer.java b/src/main/java/views/TileMapRenderer.java new file mode 100644 index 0000000..71aff43 --- /dev/null +++ b/src/main/java/views/TileMapRenderer.java @@ -0,0 +1,80 @@ +package views; + +import entities.GLConfigVariable; +import entities.Pair; +import entities.TileType; +import models.IModel; + +import java.util.ArrayList; + +//--------------------------------------------------------------- +// タイル生成 +public class TileMapRenderer implements IView { + + private double offsetY = 256d; + private TileRenderer newTile = new TileRenderer("resources/tile.png", null, 2); + private ArrayList tiles = new ArrayList<>(); + private int tileLen = 150; // + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public TileMapRenderer() { + initTiles(); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + @Override + public void update(IModel model) { + for (TileRenderer tile : tiles) tile.update(model); + } + + //--------------------------------------------------------------- + // + @Override + public void display() { + for (TileRenderer tile : tiles) tile.display(); + } + + //--------------------------------------------------------------- + // 破棄 + @Override + public void delete() { + for (TileRenderer tile : tiles) tile.delete(); + } + + + //-------------------------------------------------------------- + //--------------------------------------------------------------- + // 初期タイル作成 + private void initTiles() { + + for (int i = 0; i < tileLen; i++) { + double x = 32 * newTile.getScaleValue() * i; + + newTile = new TileRenderer("resources/tile.png", new Pair<>(x, offsetY)); + newTile.setScaleValue(2); + tiles.add(newTile); + } + } + + //-------------------------------------------------------------- + // タイルをフラグに応じて生成する + private void createTile(TileType tileType) { + + switch (tileType) { + case CLOSE: + newTile = new TileRenderer("resources/tile.png", new Pair<>((double) GLConfigVariable.WIDTH, 0d), 2); + break; + + case OPEN: + newTile = new TileRenderer("resources/hole.png", new Pair<>((double) GLConfigVariable.WIDTH, 0d), 2); + break; + } + + tiles.add(newTile); + } + + //--------------------------------------------------------------- +} \ No newline at end of file diff --git a/src/main/java/views/TileRenderer.java b/src/main/java/views/TileRenderer.java new file mode 100644 index 0000000..8521a97 --- /dev/null +++ b/src/main/java/views/TileRenderer.java @@ -0,0 +1,75 @@ +package views; + +import entities.Pair; +import entities.Sprite; +import models.IModel; + +//--------------------------------------------------------------- +// タイルの描画 +public class TileRenderer implements IView { + private Sprite sprite; + private double moveSpeed = -1; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public TileRenderer(String path, Pair initPosition) { + this.sprite = new Sprite(path); + this.sprite.setScaleValue(1); + this.sprite.setPositionValue(initPosition); + } + + //--------------------------------------------------------------- + public TileRenderer(String path, Pair initPosition, double scale) { + this.sprite = new Sprite(path); + this.sprite.setScaleValue(scale); + this.sprite.setPositionValue(initPosition); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // getter + public double getScaleValue() { + return this.sprite.getScaleValue(); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // setter + public void setScaleValue(double scaleValue) { + this.sprite.setScaleValue(scaleValue); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + @Override + public void update(IModel model) { + } + + //--------------------------------------------------------------- + // 描画 + @Override + public void display() { + sprite.draw(); + move(); + } + + //--------------------------------------------------------------- + // テクスチャの開放 + @Override + public void delete() { + sprite.delete(); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // 移動 + private void move() { + double x = sprite.getPositionValue().getFirst() + moveSpeed; + double y = sprite.getPositionValue().getSecond(); + + sprite.setPositionValue(new Pair<>(x, y)); + } + //--------------------------------------------------------------- +}