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/resources/test.png b/resources/test.png new file mode 100644 index 0000000..a42be28 --- /dev/null +++ b/resources/test.png Binary files differ diff --git a/src/main/java/JumpGame.java b/src/main/java/JumpGame.java new file mode 100644 index 0000000..9026eae --- /dev/null +++ b/src/main/java/JumpGame.java @@ -0,0 +1,75 @@ +import entities.*; +import models.IModel; +import models.PlayerModel; +import views.IView; +import views.PlayerRenderer; + +import java.util.ArrayList; + +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); + + //--------------------------------------------------------------- + // new + private ArrayList views = new ArrayList<>(); + private ArrayList models = new ArrayList<>(); + + private PlayerModel playerModel = new PlayerModel(ground); + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public void gravity(double y) { + // this.force.gravity(y); + // this.time.gravity(y); + } + + //--------------------------------------------------------------- + // 初期化 + public void init() { + + // model + models.add(new PlayerModel(ground)); + + // view + views.add(new PlayerRenderer("resources/test.png")); + } + + //--------------------------------------------------------------- + // 更新処理 + public void update() { + + // Viewの更新 + for (IModel model : models) { + for (IView view : views) view.display(model); + } + // Modelの更新 + // + // Space キーのインプット + // + // マイフレーム更新 + + gravity(0.01); //重力 + } + + //--------------------------------------------------------------- + // デストラクタのような処理 + 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 40164fd..15c69c2 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,112 +1,103 @@ +import entities.Const; 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 { - // The window handle - private long window; + // ウィンドウハンドル + private long hWnd; + // + private JumpGame jumpGame = new JumpGame(); - public void run() { - System.out.println("Hello LWJGL " + Version.getVersion() + "!"); + // フルスクリーン + public boolean fullScreen; - try { - init(); - loop(); - - // Release window and window callbacks - glfwFreeCallbacks(window); - glfwDestroyWindow(window); - } finally { - // Terminate GLFW and release the GLFWerrorfun - glfwTerminate(); - glfwSetErrorCallback(null).free(); - } + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // エントリーポイント + public static void main(String[] args) { + new Main().run(false); } + //--------------------------------------------------------------- + // メイン関数 + public void run(boolean fullScreen) { + // フルスクリーンの真偽値を貰う + this.fullScreen = fullScreen; - private void init() { - // Setup an error callback. The default implementation - // will print the error message in System.err. - GLFWErrorCallback.createPrint(System.err).set(); + // 色々初期化 + initWindow(); + initRender(); + jumpGame.init(); - // Initialize GLFW. Most GLFW functions will not work before doing this. - if (!glfwInit()) { + // メインループ + while (!glfwWindowShouldClose(hWnd)) { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // バッファのクリア + + jumpGame.update(); + + glfwSwapBuffers(hWnd); // バッファのスワップ + glfwPollEvents(); // 入力とかイベントの取得 + } + + // 終了処理 + delete(); // デストラクタ + glfwFreeCallbacks(hWnd); // ウィンドウコールバックの解放 + glfwDestroyWindow(hWnd); // ウィンドウの破棄 + glfwTerminate(); // GLFWの破棄 + glfwSetErrorCallback(null).free(); // エラーコールバックの解放 + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // ウィンドウの初期化 + private void initWindow() { + GLFWErrorCallback.createPrint(System.err).set(); // エラーコールバックの設定 + + + // GLFWの初期化 + 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 + // ウィンドウの設定 + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - int WIDTH = 300; - int HEIGHT = 300; + // ウィンドウの作成 + hWnd = glfwCreateWindow(Const.WIDTH, Const.HEIGHT, Const.TITLE_NAME, fullScreen ? glfwGetPrimaryMonitor() : NULL, NULL); + if (hWnd == NULL) + throw new RuntimeException("Failed to create the window."); - // Create the window - window = glfwCreateWindow(WIDTH, HEIGHT, "JumpGame!", NULL, NULL); - if (window == NULL) { - throw new RuntimeException("Failed to create the GLFW window"); - } - - // Escで終了 - 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 + // ウィンドウをタゲにするのか? + glfwMakeContextCurrent(hWnd); + // v-syncの適応 glfwSwapInterval(1); - // Make the window visible - glfwShowWindow(window); + // ウィンドウの表示 + glfwShowWindow(hWnd); } - 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. + //--------------------------------------------------------------- + // 描画プロセス初期化 + private void initRender() { GL.createCapabilities(); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 背景色設定 - // Set the clear color - glClearColor(0.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(); - } + // OpenGLの初期化 + glEnable(GL_TEXTURE_2D); // 二次元テクスチャの有効化 + glEnable(GL_BLEND); // アルファブレンディングの有効化 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // ブレンドモードの設定 } - public static void main(String[] args) { - new Main().run(); + //--------------------------------------------------------------- + // デストラクタ + public void delete() { + jumpGame.delete(); } - + //--------------------------------------------------------------- } \ No newline at end of file diff --git a/src/main/java/entities/Acceleration.java b/src/main/java/entities/Acceleration.java new file mode 100644 index 0000000..68ee34c --- /dev/null +++ b/src/main/java/entities/Acceleration.java @@ -0,0 +1,42 @@ +package entities; + +public class Acceleration { + private double massValue; + private Pair forceValue; + private Velocity velocity; + private Onground onground; + private Pair value; + + 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)); + } + 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); + } else { + 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/Clear.java b/src/main/java/entities/Clear.java new file mode 100644 index 0000000..3f33d64 --- /dev/null +++ b/src/main/java/entities/Clear.java @@ -0,0 +1,19 @@ +package entities; + +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/Const.java b/src/main/java/entities/Const.java new file mode 100644 index 0000000..a45780f --- /dev/null +++ b/src/main/java/entities/Const.java @@ -0,0 +1,11 @@ +package entities; + +public class Const { + private Const() { + } + + public static final String TITLE_NAME = "JumpGame"; + public static final int WIDTH = 1280; + public static final int HEIGHT = 960; + public static final int DEPTH = 100; // 追加。深度 +} \ No newline at end of file diff --git a/src/main/java/entities/Force.java b/src/main/java/entities/Force.java new file mode 100644 index 0000000..9e74af0 --- /dev/null +++ b/src/main/java/entities/Force.java @@ -0,0 +1,19 @@ +package entities; + +public class Force { + private Acceleration acceleration; + private Pair value; + + 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/Gameover.java b/src/main/java/entities/Gameover.java new file mode 100644 index 0000000..751be25 --- /dev/null +++ b/src/main/java/entities/Gameover.java @@ -0,0 +1,19 @@ +package entities; + +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..58bc4fd --- /dev/null +++ b/src/main/java/entities/Image2D.java @@ -0,0 +1,102 @@ +package entities; + +import org.lwjgl.system.CallbackI; + +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<>(30d, 30d); + + 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 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, Const.WIDTH, 0, Const.HEIGHT, -Const.DEPTH, Const.DEPTH); // 正射影投影 + + // ビューポートの範囲 + glViewport(0, 0, Const.WIDTH, Const.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/Mass.java b/src/main/java/entities/Mass.java new file mode 100644 index 0000000..f7d4012 --- /dev/null +++ b/src/main/java/entities/Mass.java @@ -0,0 +1,19 @@ +package entities; + +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/Move.java b/src/main/java/entities/Move.java new file mode 100644 index 0000000..18a5b6d --- /dev/null +++ b/src/main/java/entities/Move.java @@ -0,0 +1,23 @@ +package entities; + +public class Move { + private Velocity velocity; + private Pair value; + + 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/Onground.java b/src/main/java/entities/Onground.java new file mode 100644 index 0000000..6b58b90 --- /dev/null +++ b/src/main/java/entities/Onground.java @@ -0,0 +1,14 @@ +package entities; + +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/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/Position.java b/src/main/java/entities/Position.java new file mode 100644 index 0000000..651c13c --- /dev/null +++ b/src/main/java/entities/Position.java @@ -0,0 +1,42 @@ +package entities; + +//--------------------------------------------------------------- +// +public class Position { + private Ground ground; + private Pair value; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + 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/Sprite.java b/src/main/java/entities/Sprite.java new file mode 100644 index 0000000..0566eb5 --- /dev/null +++ b/src/main/java/entities/Sprite.java @@ -0,0 +1,45 @@ +package entities; + +import views.IView; + +//--------------------------------------------------------------- +// +public class Sprite { + + private Pair positionValue; + private Texture texture; + private Image2D img; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public Sprite(String path) { + this.positionValue = new Pair<>(0d, 0d); + texture = new Texture("player", path); + img = new Image2D(texture); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // setter + public void setPositionValue(Pair positionValue) { + this.positionValue = positionValue; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public void draw() { + 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/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/Velocity.java b/src/main/java/entities/Velocity.java new file mode 100644 index 0000000..9f218ea --- /dev/null +++ b/src/main/java/entities/Velocity.java @@ -0,0 +1,51 @@ +package entities; + + +public class Velocity { + private Pair moveValue; + private Pair accelerationValue; + private Position position; + private Onground onground; + private Pair value; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + 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/PlayerModel.java b/src/main/java/models/PlayerModel.java new file mode 100644 index 0000000..bbdb8cc --- /dev/null +++ b/src/main/java/models/PlayerModel.java @@ -0,0 +1,45 @@ +package models; + +import entities.*; + +//--------------------------------------------------------------- +// +public class PlayerModel implements IModel { + //--------------------------------------------------------------- + private Acceleration acceleration; + private Force force; + private Mass mass; + private Move move; + private Position position; + private Velocity velocity; + private Onground onground; + + //--------------------------------------------------------------- + private double jumpPower = 32; + + //--------------------------------------------------------------- + public PlayerModel(Ground 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); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // getter + public Position getPosition() { + return position; + } + + //--------------------------------------------------------------- + // + public void jump() { + this.move.moveY(jumpPower); + } + //--------------------------------------------------------------- + +} diff --git a/src/main/java/resources/Acceleration.java b/src/main/java/resources/Acceleration.java deleted file mode 100644 index c31e238..0000000 --- a/src/main/java/resources/Acceleration.java +++ /dev/null @@ -1,42 +0,0 @@ -package resources; - -public class Acceleration { - private double massValue; - private Pair forceValue; - private Velocity velocity; - private Onground onground; - private Pair value; - - 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)); - } - 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); - } else { - 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/resources/Clear.java b/src/main/java/resources/Clear.java deleted file mode 100644 index 2325d12..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().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/resources/Force.java b/src/main/java/resources/Force.java deleted file mode 100644 index 2e5f763..0000000 --- a/src/main/java/resources/Force.java +++ /dev/null @@ -1,19 +0,0 @@ -package resources; - -public class Force { - private Acceleration acceleration; - private Pair value; - - 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/resources/Gameover.java b/src/main/java/resources/Gameover.java deleted file mode 100644 index 2bdcac7..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().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/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 c655bb4..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 Pair getAcceleration() { - return acceleration.getValue(); - } - - public Pair getMove() { - return move.getValue(); - } - - public double getMass() { - return mass.getValue(); - } - - public boolean getClear() { - return clear.getClear(); - } - - public boolean getGround() { - return ground.getValue(); - } - - public Pair getForce() { - return force.getValue(); - } - - public Pair getVelocity() { - return velocity.getValue(); - } - - public Pair 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 f258d53..0000000 --- a/src/main/java/resources/Move.java +++ /dev/null @@ -1,23 +0,0 @@ -package resources; - -public class Move { - private Velocity velocity; - private Pair value; - - 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/resources/Onground.java b/src/main/java/resources/Onground.java deleted file mode 100644 index 5e60ef5..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().getSecond() <= 0.0)); } -} \ No newline at end of file diff --git a/src/main/java/resources/Pair.java b/src/main/java/resources/Pair.java deleted file mode 100644 index 29d3dbe..0000000 --- a/src/main/java/resources/Pair.java +++ /dev/null @@ -1,19 +0,0 @@ -package resources; - -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/resources/Position.java b/src/main/java/resources/Position.java deleted file mode 100644 index e79f7a9..0000000 --- a/src/main/java/resources/Position.java +++ /dev/null @@ -1,24 +0,0 @@ -package resources; - -public class Position { - private Ground ground; - private Pair value; - - 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 Pair 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/Velocity.java b/src/main/java/resources/Velocity.java deleted file mode 100644 index 3fa9068..0000000 --- a/src/main/java/resources/Velocity.java +++ /dev/null @@ -1,43 +0,0 @@ -package resources; - - -public class Velocity { - private Pair moveValue; - private Pair accelerationValue; - private Position position; - private Onground onground; - private Pair value; - - 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/views/IView.java b/src/main/java/views/IView.java new file mode 100644 index 0000000..2e9b5f2 --- /dev/null +++ b/src/main/java/views/IView.java @@ -0,0 +1,16 @@ +package views; + +import models.IModel; + +//--------------------------------------------------------------- +// +public interface IView { + + //--------------------------------------------------------------- + // 描画 + void display(IModel model); + + //--------------------------------------------------------------- + // テクスチャの開放 + void delete(); +} diff --git a/src/main/java/views/PlayerRenderer.java b/src/main/java/views/PlayerRenderer.java new file mode 100644 index 0000000..4da715c --- /dev/null +++ b/src/main/java/views/PlayerRenderer.java @@ -0,0 +1,34 @@ +package views; + +import entities.*; +import models.IModel; +import models.PlayerModel; + +//--------------------------------------------------------------- +// +public class PlayerRenderer implements IView { + private Sprite sprite; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public PlayerRenderer(String path) { + this.sprite = new Sprite(path); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // PlayerModelから座標を取得して描画する + public void display(IModel model) { + PlayerModel playerModel = (PlayerModel)model; + + sprite.draw(); + sprite.setPositionValue(playerModel.getPosition().getValue()); + } + + //--------------------------------------------------------------- + // テクスチャの開放 + public void delete() { + sprite.delete(); + } + //--------------------------------------------------------------- +}