diff --git a/.idea/libraries/org_junit_jupiter_junit_jupiter_5_4_2.xml b/.idea/libraries/org_junit_jupiter_junit_jupiter_5_4_2.xml new file mode 100644 index 0000000..32de0a2 --- /dev/null +++ b/.idea/libraries/org_junit_jupiter_junit_jupiter_5_4_2.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 71bba8c..e104ddd 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/jumpGame.iml b/jumpGame.iml index 13770d9..273d838 100644 --- a/jumpGame.iml +++ b/jumpGame.iml @@ -24,5 +24,7 @@ + + \ No newline at end of file diff --git a/resources/JCasC.png b/resources/JCasC.png new file mode 100644 index 0000000..24bc14c --- /dev/null +++ b/resources/JCasC.png Binary files differ diff --git a/resources/bg.png b/resources/bg.png new file mode 100644 index 0000000..0695991 --- /dev/null +++ b/resources/bg.png Binary files differ diff --git a/resources/stage.txt b/resources/stage.txt new file mode 100644 index 0000000..eaed1d1 --- /dev/null +++ b/resources/stage.txt @@ -0,0 +1 @@ +1,2,1,1,1,1,1,1,2,1 \ No newline at end of file diff --git a/src/main/java/GLWindow.java b/src/main/java/GLWindow.java index fc174aa..a86e792 100644 --- a/src/main/java/GLWindow.java +++ b/src/main/java/GLWindow.java @@ -1,4 +1,4 @@ -import entities.GLConfigVariable; +import entities.config.GLConfigVariable; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.opengl.GL; @@ -11,7 +11,15 @@ // ウィンドウ public class GLWindow { - private long hWnd; // ウィンドウハンドル + private long window; // ウィンドウハンドル + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // getter + public long getWindow() { + return this.window; + } + //--------------------------------------------------------------- //--------------------------------------------------------------- @@ -24,15 +32,15 @@ //--------------------------------------------------------------- // 画面のスワップ public void swapWindow() { - glfwSwapBuffers(hWnd); // バッファのスワップ + glfwSwapBuffers(window); // バッファのスワップ glfwPollEvents(); // 入力とかイベントの取得 } //--------------------------------------------------------------- // ウィンドウの破棄 public void destroyWindow() { - glfwFreeCallbacks(hWnd); // ウィンドウコールバックの解放 - glfwDestroyWindow(hWnd); // ウィンドウの破棄 + glfwFreeCallbacks(window); // ウィンドウコールバックの解放 + glfwDestroyWindow(window); // ウィンドウの破棄 glfwTerminate(); // GLFWの破棄 glfwSetErrorCallback(null).free(); // エラーコールバックの解放 } @@ -40,7 +48,7 @@ //--------------------------------------------------------------- // ウィンドウが起動しているかどうか public boolean windowShouldClose() { - return glfwWindowShouldClose(hWnd); + return glfwWindowShouldClose(window); } //--------------------------------------------------------------- @@ -56,14 +64,14 @@ glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // ウィンドウの作成 - hWnd = glfwCreateWindow(GLConfigVariable.WIDTH, GLConfigVariable.HEIGHT, GLConfigVariable.TITLE_NAME, GLConfigVariable.IS_FULL_SCREEN ? glfwGetPrimaryMonitor() : NULL, NULL); - if (hWnd == NULL) + window = glfwCreateWindow(GLConfigVariable.WIDTH, GLConfigVariable.HEIGHT, GLConfigVariable.TITLE_NAME, GLConfigVariable.IS_FULL_SCREEN ? glfwGetPrimaryMonitor() : NULL, NULL); + if (window == NULL) throw new RuntimeException("Failed to create the window."); - glfwMakeContextCurrent(hWnd); //起動したウィンドウをターゲットに + glfwMakeContextCurrent(window); //起動したウィンドウをターゲットに glfwSwapInterval(1); // v-syncの適応 - glfwShowWindow(hWnd); // ウィンドウの表示 + glfwShowWindow(window); // ウィンドウの表示 } //--------------------------------------------------------------- diff --git a/src/main/java/GameEngine.java b/src/main/java/GameEngine.java index 50627dc..0ecdb1f 100644 --- a/src/main/java/GameEngine.java +++ b/src/main/java/GameEngine.java @@ -14,7 +14,7 @@ //--------------------------------------------------------------- // ゲームループ - protected void update() { + protected void update(long window) { } //--------------------------------------------------------------- @@ -34,7 +34,7 @@ // メインループ while (!glWindow.windowShouldClose()) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // バッファのクリア - update(); + update(glWindow.getWindow()); glWindow.swapWindow(); } diff --git a/src/main/java/JumpGame.java b/src/main/java/JumpGame.java index 9927138..314895b 100644 --- a/src/main/java/JumpGame.java +++ b/src/main/java/JumpGame.java @@ -1,52 +1,64 @@ import models.IModel; import models.JumpGameModel; +import org.lwjgl.glfw.GLFWKeyCallback; +import views.BackgroundRenderer; import views.IView; import views.PlayerRenderer; import views.TileMapRenderer; +import static org.lwjgl.glfw.GLFW.*; + import java.util.ArrayList; public class JumpGame { - //--------------------------------------------------------------- - //--------------------------------------------------------------- - // new private ArrayList views = new ArrayList<>(); private IModel model = new JumpGameModel(); + private GLFWKeyCallback keyCallback; + //--------------------------------------------------------------- //--------------------------------------------------------------- // public void gravity(double y) { -// this.time.gravity(y); JumpGameModel jumpGameModel = (JumpGameModel) model; - jumpGameModel.updateGravity(y); + jumpGameModel.gravity(y); //重力の更新 + jumpGameModel.updateGroundFlag();//地面の判定切り替え } //--------------------------------------------------------------- // 初期化 public void init() { // view - views.add(new TileMapRenderer()); - views.add(new PlayerRenderer("resources/chicken.png")); + views.add(new BackgroundRenderer("resources/bg.png")); + views.add(new TileMapRenderer(model)); + views.add(new PlayerRenderer("resources/JCasC.png")); } //--------------------------------------------------------------- // 更新処理 - public void update() { + public void update(long window) { + JumpGameModel jumpGameModel = (JumpGameModel) model; // Viewの更新 - for (IView view : views){ + for (IView view : views) { view.update(model); view.display(); } // Modelの更新 - // // Space キーのインプット - // - // マイフレーム更新 + glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() { + @Override + public void invoke(long window, int key, int scancode, int action, int mods) { + if (key == GLFW_KEY_SPACE && action != GLFW_PRESS) { + jumpGameModel.moveY(256); +// System.out.println("jumping"); + } + } + }); + jumpGameModel.moveX(1); gravity(-256); //重力 } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 445b251..b0f671e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,11 +1,5 @@ -import entities.GLConfigVariable; -import org.lwjgl.glfw.*; -import org.lwjgl.opengl.*; - -import static org.lwjgl.glfw.Callbacks.*; -import static org.lwjgl.glfw.GLFW.*; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.system.MemoryUtil.*; +import javax.swing.*; +import java.awt.*; //--------------------------------------------------------------- // 実行本体 @@ -24,15 +18,15 @@ //--------------------------------------------------------------- // 初期化 @Override - protected void init(){ + protected void init() { jumpGame.init(); } //--------------------------------------------------------------- // 更新処理 @Override - protected void update(){ - jumpGame.update(); + protected void update(long window) { + jumpGame.update(window); } //--------------------------------------------------------------- diff --git a/src/main/java/entities/Acceleration.java b/src/main/java/entities/Acceleration.java new file mode 100644 index 0000000..a0e330e --- /dev/null +++ b/src/main/java/entities/Acceleration.java @@ -0,0 +1,39 @@ +package entities; +import java.util.*; + +public class Acceleration { + private Pair force = new Pair<>(0.0,0.0); + private double mass = 1.0; + private Velocity velocity; + private Onground onground; + private Pair value = new Pair<>(0.0,0.0); + public void updateForce(Pair force) { + this.force = force; + Pair temp_if0; + if (this.onground.getValue()) { + temp_if0 = new Pair<>((force.getLeft()/mass),0.0); + } else { + temp_if0 = new Pair<>((force.getLeft()/mass),(force.getRight()/mass)); + } + value = temp_if0; + velocity.updateAcceleration(value); + } + public void updateMass(double mass) { + this.mass = mass; + Pair temp_if2; + if (this.onground.getValue()) { + temp_if2 = new Pair<>((force.getLeft()/mass),0.0); + } else { + temp_if2 = new Pair<>((force.getLeft()/mass),(force.getRight()/mass)); + } + value = temp_if2; + velocity.updateAcceleration(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 index d2345d4..8ed248c 100644 --- a/src/main/java/entities/Clear.java +++ b/src/main/java/entities/Clear.java @@ -1,21 +1,19 @@ package entities; -import entities.player.Position; +import java.util.*; 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; - } + private boolean value = false; + public void updatePosition(Pair position) { + boolean temp_if5; + if ((position.getLeft()>100.0)) { + temp_if5 = true; + } else { + temp_if5 = false; + } + value = temp_if5; + } + public boolean getValue() { + return value; + } } \ No newline at end of file diff --git a/src/main/java/entities/Color.java b/src/main/java/entities/Color.java deleted file mode 100644 index e7ae250..0000000 --- a/src/main/java/entities/Color.java +++ /dev/null @@ -1,35 +0,0 @@ -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/Force.java b/src/main/java/entities/Force.java new file mode 100644 index 0000000..5e8897f --- /dev/null +++ b/src/main/java/entities/Force.java @@ -0,0 +1,18 @@ +package entities; + +import java.util.*; + +public class Force { + private Acceleration acceleration; + private Pair value = new Pair<>(0.0,0.0); + public Force(Acceleration acceleration) { + this.acceleration = acceleration; + } + public void gravity(double y) { + this.value = new Pair<>(0.0,y); + acceleration.updateForce(value); + } + public Pair getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/entities/GLConfigVariable.java b/src/main/java/entities/GLConfigVariable.java deleted file mode 100644 index adb3001..0000000 --- a/src/main/java/entities/GLConfigVariable.java +++ /dev/null @@ -1,18 +0,0 @@ -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 index b248c05..babc4ae 100644 --- a/src/main/java/entities/Gameover.java +++ b/src/main/java/entities/Gameover.java @@ -1,21 +1,18 @@ package entities; - -import entities.player.Position; +import java.util.*; 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; - } + private boolean value = false; + public void updatePosition(Pair position) { + boolean temp_if6; + if ((position.getRight()<-(1.0))) { + temp_if6 = true; + } else { + temp_if6 = false; + } + value = temp_if6; + } + public boolean getValue() { + return value; + } } \ No newline at end of file diff --git a/src/main/java/entities/Ground.java b/src/main/java/entities/Ground.java index 58ab6f6..69c5290 100644 --- a/src/main/java/entities/Ground.java +++ b/src/main/java/entities/Ground.java @@ -1,9 +1,22 @@ package entities; -public class Ground { - private boolean value; +import java.util.*; - public boolean getValue() { - return value; - } +public class Ground { + private Onground onground; + private boolean value = true; + public Ground(Onground onground) { + this.onground = onground; + } + public void openHole() { + this.value = false; + onground.updateGround(value); + } + public void closeHole() { + this.value = true; + onground.updateGround(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 deleted file mode 100644 index bbbc877..0000000 --- a/src/main/java/entities/Image2D.java +++ /dev/null @@ -1,103 +0,0 @@ -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/Mass.java b/src/main/java/entities/Mass.java new file mode 100644 index 0000000..d8dd95d --- /dev/null +++ b/src/main/java/entities/Mass.java @@ -0,0 +1,17 @@ +package entities; +import java.util.*; + +public class Mass { + private Acceleration acceleration; + private double value = 1.0; + public Mass(Acceleration acceleration) { + this.acceleration = acceleration; + } + public void setMass(double x) { + this.value = x; + acceleration.updateMass(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..b91e605 --- /dev/null +++ b/src/main/java/entities/Move.java @@ -0,0 +1,22 @@ +package entities; + +import java.util.*; + +public class Move { + private Velocity velocity; + private Pair value = new Pair<>(0.0,0.0); + public Move(Velocity velocity) { + this.velocity = velocity; + } + public void moveY(double y) { + this.value = new Pair<>(this.value.getLeft(),y); + velocity.updateMove(value); + } + public void moveX(double x) { + this.value = new Pair<>(x,this.value.getRight()); + velocity.updateMove(value); + } + 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..9a54eb0 --- /dev/null +++ b/src/main/java/entities/Onground.java @@ -0,0 +1,20 @@ +package entities; + +import java.util.*; + +public class Onground { + private boolean ground = true; + private Pair position = new Pair<>(0.0,0.0); + private boolean value = true; + public void updateGround(boolean ground) { + this.ground = ground; + value = ((ground==true)&&(position.getRight()<=0.0)); + } + public void updatePosition(Pair position) { + this.position = position; + value = ((ground==true)&&(position.getRight()<=0.0)); + } + public boolean getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/entities/Pair.java b/src/main/java/entities/Pair.java index 4f17624..025be0a 100644 --- a/src/main/java/entities/Pair.java +++ b/src/main/java/entities/Pair.java @@ -1,19 +1,18 @@ package entities; +import java.util.*; + 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; - } -} + private T left; + private T right; + public Pair(T left, T right) { + this.left = left; + this.right = right; + } + public T getLeft() { + return left; + } + public T getRight() { + return right; + } +} \ No newline at end of file diff --git a/src/main/java/entities/Position.java b/src/main/java/entities/Position.java new file mode 100644 index 0000000..03b164c --- /dev/null +++ b/src/main/java/entities/Position.java @@ -0,0 +1,31 @@ +package entities; +import java.util.*; + +public class Position { + private Onground onground; + private Clear clear; + private Gameover gameover; + private Ground ground; + private Pair value = new Pair<>(0.0,0.0); + public void updateVelocity(Pair velocity) { + Pair temp_if4; + if (((this.ground.getValue()==true)&&((this.value.getRight()+(0.01*velocity.getRight()))<0.0))) { + temp_if4 = new Pair<>((this.value.getLeft()+(0.01*velocity.getLeft())),0.0); + } else { + temp_if4 = new Pair<>((this.value.getLeft()+(0.01*velocity.getLeft())),(this.value.getRight()+(0.01*velocity.getRight()))); + } + value = temp_if4; + onground.updatePosition(value); + clear.updatePosition(value); + gameover.updatePosition(value); + } + public Position(Onground onground, Clear clear, Gameover gameover, Ground ground) { + this.onground = onground; + this.clear = clear; + this.gameover = gameover; + this.ground = ground; + } + 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 deleted file mode 100644 index b84e749..0000000 --- a/src/main/java/entities/Sprite.java +++ /dev/null @@ -1,61 +0,0 @@ -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 deleted file mode 100644 index 7bfb9c6..0000000 --- a/src/main/java/entities/Texture.java +++ /dev/null @@ -1,107 +0,0 @@ -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 deleted file mode 100644 index 178adc0..0000000 --- a/src/main/java/entities/TileType.java +++ /dev/null @@ -1,19 +0,0 @@ -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 index f08de37..5ee12e9 100644 --- a/src/main/java/entities/Time.java +++ b/src/main/java/entities/Time.java @@ -1,13 +1,12 @@ package entities; +import java.util.*; public class Time { - private double value; - - public void gravity(double y) { - this.value = (this.value + 0.01); - } - - public double getValue() { - return value; - } + private double value = 0.0; + 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..1d9d6ff --- /dev/null +++ b/src/main/java/entities/Velocity.java @@ -0,0 +1,40 @@ +package entities; + +import java.util.*; + +public class Velocity { + private Pair move = new Pair<>(0.0,0.0); + private Pair acceleration = new Pair<>(0.0,0.0); + private Position position; + private Onground onground; + private Pair value = new Pair<>(0.0,0.0); + public void updateMove(Pair move) { + this.move = move; + Pair temp_if1; + if ((this.onground.getValue()&&(move.getRight()>=0.0))) { + temp_if1 = move; + } else { + temp_if1 = this.value; + } + value = temp_if1; + position.updateVelocity(value); + } + public void updateAcceleration(Pair acceleration) { + this.acceleration = acceleration; + Pair temp_if3; + if ((this.onground.getValue()&&(this.value.getRight()<0.0))) { + temp_if3 = new Pair<>((this.value.getLeft()+(0.01*acceleration.getLeft())),0.0); + } else { + temp_if3 = new Pair<>((this.value.getLeft()+(0.01*acceleration.getLeft())),(this.value.getRight()+(0.01*acceleration.getRight()))); + } + value = temp_if3; + position.updateVelocity(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/entities/config/GLConfigVariable.java b/src/main/java/entities/config/GLConfigVariable.java new file mode 100644 index 0000000..30869d3 --- /dev/null +++ b/src/main/java/entities/config/GLConfigVariable.java @@ -0,0 +1,18 @@ +package entities.config; + +//--------------------------------------------------------------- +// 画面設定定数 +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/modelExtentions/Stage.java b/src/main/java/entities/modelExtentions/Stage.java new file mode 100644 index 0000000..1146f2e --- /dev/null +++ b/src/main/java/entities/modelExtentions/Stage.java @@ -0,0 +1,77 @@ +package entities.modelExtentions; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class Stage { + + private ArrayList values = new ArrayList<>(); + private int count = 0; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public Stage() { + Path file = Paths.get("resources/stage.txt"); + String[] data = new String[1]; + + try { + List lines = Files.readAllLines(file, Charset.forName("Shift-JIS")); + + for (int i = 0; i < lines.size(); i++) data = lines.get(i).split(","); + for (int i = 0; i < data.length; i++) values.add(Integer.parseInt(data[i])); + for (int v : values) System.out.println(v); + + } catch (IOException e) { + System.out.println("Failed to load stage.txt"); + } + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // getter + public ArrayList getValues() { + return values; + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // フラグのタイミングと一致しているかをチェック + public boolean isFlag(int timing) { + for (int flag : values) + if (timing == flag) return true; + return false; + } + + //--------------------------------------------------------------- + // 奇数番目のフラグで穴を開けるフラグを提示する + public boolean isOpenFlag(double x) { + + if (count % 2 != 0) return false; + + double cx = (double) values.get(count) - 0.5; + if (cx <= x) { + if (count < values.size() - 1) count++; + return true; + } + return false; + } + + //--------------------------------------------------------------- + // 偶数番目のフラグで穴を閉じるフラグを提示する + public boolean isCloseFlag(double x) { + if (count % 2 == 0) return false; + + double cx = (double) values.get(count) - 0.5; + if (cx <= x + 0.25) { + if (count < values.size() - 1) count++; + return true; + } + return false; + } + +} diff --git a/src/main/java/entities/player/Acceleration.java b/src/main/java/entities/player/Acceleration.java deleted file mode 100644 index fe00b13..0000000 --- a/src/main/java/entities/player/Acceleration.java +++ /dev/null @@ -1,50 +0,0 @@ -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 deleted file mode 100644 index 9c034d9..0000000 --- a/src/main/java/entities/player/Force.java +++ /dev/null @@ -1,21 +0,0 @@ -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 deleted file mode 100644 index 16283e6..0000000 --- a/src/main/java/entities/player/Mass.java +++ /dev/null @@ -1,19 +0,0 @@ -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 deleted file mode 100644 index 460d0d1..0000000 --- a/src/main/java/entities/player/Move.java +++ /dev/null @@ -1,25 +0,0 @@ -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 deleted file mode 100644 index 71abfda..0000000 --- a/src/main/java/entities/player/Onground.java +++ /dev/null @@ -1,16 +0,0 @@ -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 deleted file mode 100644 index 05c9f06..0000000 --- a/src/main/java/entities/player/Position.java +++ /dev/null @@ -1,45 +0,0 @@ -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 deleted file mode 100644 index 8209abb..0000000 --- a/src/main/java/entities/player/Velocity.java +++ /dev/null @@ -1,53 +0,0 @@ -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/JumpGameModel.java b/src/main/java/models/JumpGameModel.java deleted file mode 100644 index b0617aa..0000000 --- a/src/main/java/models/JumpGameModel.java +++ /dev/null @@ -1,77 +0,0 @@ -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/views/BackgroundRenderer.java b/src/main/java/views/BackgroundRenderer.java new file mode 100644 index 0000000..4668cac --- /dev/null +++ b/src/main/java/views/BackgroundRenderer.java @@ -0,0 +1,41 @@ +package views; + +import entities.Pair; +import models.IModel; + +//--------------------------------------------------------------- +// +public class BackgroundRenderer implements IView { + + private Sprite sprite; + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + public BackgroundRenderer(String path) { + this.sprite = new Sprite(path); + this.sprite.setScaleValue(1); + this.sprite.setPositionValue(new Pair<>(640d, 480d)); + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // + @Override + public void update(IModel model) { + } + + //--------------------------------------------------------------- + // + @Override + public void display() { + sprite.draw(); + } + + //--------------------------------------------------------------- + // + @Override + public void delete() { + sprite.delete(); + } +} diff --git a/src/main/java/views/Color.java b/src/main/java/views/Color.java new file mode 100644 index 0000000..03c2fb1 --- /dev/null +++ b/src/main/java/views/Color.java @@ -0,0 +1,35 @@ +package views; + +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/views/Image2D.java b/src/main/java/views/Image2D.java new file mode 100644 index 0000000..69c8684 --- /dev/null +++ b/src/main/java/views/Image2D.java @@ -0,0 +1,106 @@ +package views; + +import entities.config.GLConfigVariable; +import entities.Pair; + +import static org.lwjgl.opengl.GL11.*; + +//--------------------------------------------------------------- +// +public class Image2D { + + private int id; // テクスチャのID + private Pair spriteSize; // スプライトの幅高 + 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) + spriteSize = new Pair<>((double) tex.getWidth(), (double) tex.getHeight()); + else + spriteSize = 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.getLeft(), position.getRight(), 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(-spriteSize.getLeft() / 2, spriteSize.getRight() / 2); + glTexCoord2d(0, 1); + glVertex2d(-spriteSize.getLeft() / 2, -spriteSize.getRight() / 2); + glTexCoord2d(1, 0); + glVertex2d(spriteSize.getLeft() / 2, spriteSize.getRight() / 2); + glTexCoord2d(1, 1); + glVertex2d(spriteSize.getLeft() / 2, -spriteSize.getRight() / 2); + glEnd(); + + // 行列の破棄 + glPopMatrix(); + + // テクスチャの解除 + glBindTexture(GL_TEXTURE_2D, 0); + } + + //--------------------------------------------------------------- +} \ No newline at end of file diff --git a/src/main/java/views/PlayerRenderer.java b/src/main/java/views/PlayerRenderer.java index 7d071b1..5baea09 100644 --- a/src/main/java/views/PlayerRenderer.java +++ b/src/main/java/views/PlayerRenderer.java @@ -1,9 +1,10 @@ package views; -import entities.*; +import entities.Pair; import models.IModel; import models.JumpGameModel; + //--------------------------------------------------------------- // public class PlayerRenderer implements IView { @@ -11,18 +12,23 @@ //--------------------------------------------------------------- //--------------------------------------------------------------- + // public PlayerRenderer(String path) { this.sprite = new Sprite(path); - this.sprite.setScaleValue(1); + this.sprite.setScaleValue(0.1); + this.sprite.setPositionValue(new Pair<>(640d, 480d)); } //--------------------------------------------------------------- //--------------------------------------------------------------- - // JumpGameModelから座標を取得 + // @Override - public void update(IModel model){ + public void update(IModel model) { JumpGameModel jumpGameModel = (JumpGameModel) model; - sprite.setPositionValue(jumpGameModel.getPosition().getValue()); + + double x = this.sprite.getPositionValue().getLeft(); + double y = 112 + jumpGameModel.getPosition().getRight(); + this.sprite.setPositionValue(new Pair<>(x, y)); } diff --git a/src/main/java/views/Sprite.java b/src/main/java/views/Sprite.java new file mode 100644 index 0000000..502eca4 --- /dev/null +++ b/src/main/java/views/Sprite.java @@ -0,0 +1,61 @@ +package views; + +import entities.Pair; + +//--------------------------------------------------------------- +// +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/views/Texture.java b/src/main/java/views/Texture.java new file mode 100644 index 0000000..129a8e6 --- /dev/null +++ b/src/main/java/views/Texture.java @@ -0,0 +1,107 @@ +package views; + +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/views/TileMapRenderer.java b/src/main/java/views/TileMapRenderer.java index 71aff43..b7d4c64 100644 --- a/src/main/java/views/TileMapRenderer.java +++ b/src/main/java/views/TileMapRenderer.java @@ -1,9 +1,9 @@ package views; -import entities.GLConfigVariable; import entities.Pair; -import entities.TileType; +import entities.modelExtentions.Stage; import models.IModel; +import models.JumpGameModel; import java.util.ArrayList; @@ -11,15 +11,15 @@ // タイル生成 public class TileMapRenderer implements IView { - private double offsetY = 256d; + private double offsetY = 32d; private TileRenderer newTile = new TileRenderer("resources/tile.png", null, 2); private ArrayList tiles = new ArrayList<>(); - private int tileLen = 150; // //--------------------------------------------------------------- //--------------------------------------------------------------- - public TileMapRenderer() { - initTiles(); + public TileMapRenderer(IModel model) { + JumpGameModel jumpGameModel = (JumpGameModel) model; + initTiles(jumpGameModel.getStage()); } //--------------------------------------------------------------- @@ -48,32 +48,44 @@ //-------------------------------------------------------------- //--------------------------------------------------------------- // 初期タイル作成 - private void initTiles() { + private void initTiles(Stage stage) { + boolean isOpen = false; + int space = 10; - for (int i = 0; i < tileLen; i++) { + // タイルマージン + for (int i = 0; i < space; i++) { double x = 32 * newTile.getScaleValue() * i; + if (i == 12) addNewTile(x, TileType.OPEN); + else addNewTile(x, TileType.CLOSE); + } - newTile = new TileRenderer("resources/tile.png", new Pair<>(x, offsetY)); - newTile.setScaleValue(2); - tiles.add(newTile); + for (int i = 0; i < 120; i++) { + double x = 32 * newTile.getScaleValue() * (i + space); + + // フラグのチェック + if (stage.isFlag(i)) isOpen = !isOpen; + + // タイルの切り替え + if (isOpen) addNewTile(x, TileType.OPEN); + else addNewTile(x, TileType.CLOSE); + } } //-------------------------------------------------------------- // タイルをフラグに応じて生成する - private void createTile(TileType tileType) { + private void addNewTile(double x, TileType tileType) { switch (tileType) { case CLOSE: - newTile = new TileRenderer("resources/tile.png", new Pair<>((double) GLConfigVariable.WIDTH, 0d), 2); + newTile = new TileRenderer("resources/tile.png", new Pair<>(x, offsetY), 2); break; - case OPEN: - newTile = new TileRenderer("resources/hole.png", new Pair<>((double) GLConfigVariable.WIDTH, 0d), 2); + newTile = new TileRenderer("resources/hole.png", new Pair<>(x, offsetY), 2); break; } - tiles.add(newTile); + System.out.println("New tile created."); } //--------------------------------------------------------------- diff --git a/src/main/java/views/TileRenderer.java b/src/main/java/views/TileRenderer.java index 8521a97..55bb5ad 100644 --- a/src/main/java/views/TileRenderer.java +++ b/src/main/java/views/TileRenderer.java @@ -1,19 +1,20 @@ package views; import entities.Pair; -import entities.Sprite; import models.IModel; +import models.JumpGameModel; //--------------------------------------------------------------- // タイルの描画 public class TileRenderer implements IView { private Sprite sprite; - private double moveSpeed = -1; + private Pair initPositionValue; //--------------------------------------------------------------- //--------------------------------------------------------------- // public TileRenderer(String path, Pair initPosition) { + this.initPositionValue = initPosition; this.sprite = new Sprite(path); this.sprite.setScaleValue(1); this.sprite.setPositionValue(initPosition); @@ -21,6 +22,7 @@ //--------------------------------------------------------------- public TileRenderer(String path, Pair initPosition, double scale) { + this.initPositionValue = initPosition; this.sprite = new Sprite(path); this.sprite.setScaleValue(scale); this.sprite.setPositionValue(initPosition); @@ -45,6 +47,12 @@ // @Override public void update(IModel model) { + JumpGameModel jumpGameModel = (JumpGameModel) model; + + Double x = this.initPositionValue.getLeft() - (jumpGameModel.getPosition().getLeft() * 64); + Double y = this.sprite.getPositionValue().getRight(); + + this.sprite.setPositionValue(new Pair<>(x, y)); } //--------------------------------------------------------------- @@ -52,7 +60,6 @@ @Override public void display() { sprite.draw(); - move(); } //--------------------------------------------------------------- @@ -61,15 +68,4 @@ 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)); - } - //--------------------------------------------------------------- } diff --git a/src/main/java/views/TileType.java b/src/main/java/views/TileType.java new file mode 100644 index 0000000..649cdaf --- /dev/null +++ b/src/main/java/views/TileType.java @@ -0,0 +1,11 @@ +package views; + +//--------------------------------------------------------------- +// タイルの種類の定数クラス +public enum TileType { + + CLOSE, + OPEN; + + //--------------------------------------------------------------- +}