diff --git a/src/main/java/GLWindow.java b/src/main/java/GLWindow.java index fc174aa..9ce64be 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; diff --git a/src/main/java/JumpGame.java b/src/main/java/JumpGame.java index 6596763..1004826 100644 --- a/src/main/java/JumpGame.java +++ b/src/main/java/JumpGame.java @@ -22,8 +22,9 @@ //--------------------------------------------------------------- // 初期化 public void init() { + JumpGameModel jumpGameModel = (JumpGameModel)model; // view - views.add(new TileMapRenderer(((JumpGameModel) model).getFlags())); + views.add(new TileMapRenderer(jumpGameModel.getFlags().getValues())); views.add(new PlayerRenderer("resources/chicken.png")); } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 445b251..4cb5cb0 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,12 +1,3 @@ -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.*; - //--------------------------------------------------------------- // 実行本体 public class Main extends GameEngine { diff --git a/src/main/java/entities/Acceleration.java b/src/main/java/entities/Acceleration.java new file mode 100644 index 0000000..0d80c79 --- /dev/null +++ b/src/main/java/entities/Acceleration.java @@ -0,0 +1,48 @@ +package entities; + +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/Clear.java b/src/main/java/entities/Clear.java index d2345d4..3f33d64 100644 --- a/src/main/java/entities/Clear.java +++ b/src/main/java/entities/Clear.java @@ -1,7 +1,5 @@ package entities; -import entities.player.Position; - public class Clear { private Position position; 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..5a544e0 --- /dev/null +++ b/src/main/java/entities/Force.java @@ -0,0 +1,19 @@ +package entities; + +public class Force { + private Acceleration acceleration; + private Pair value = new Pair<>(-1.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/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..751be25 100644 --- a/src/main/java/entities/Gameover.java +++ b/src/main/java/entities/Gameover.java @@ -1,7 +1,5 @@ package entities; -import entities.player.Position; - public class Gameover { private Position position; diff --git a/src/main/java/entities/Image2D.java b/src/main/java/entities/Image2D.java deleted file mode 100644 index 641b827..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 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.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(-spriteSize.getFirst() / 2, spriteSize.getSecond() / 2); - glTexCoord2d(0, 1); - glVertex2d(-spriteSize.getFirst() / 2, -spriteSize.getSecond() / 2); - glTexCoord2d(1, 0); - glVertex2d(spriteSize.getFirst() / 2, spriteSize.getSecond() / 2); - glTexCoord2d(1, 1); - glVertex2d(spriteSize.getFirst() / 2, -spriteSize.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..c7e92ac --- /dev/null +++ b/src/main/java/entities/Move.java @@ -0,0 +1,23 @@ +package entities; + +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/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/Position.java b/src/main/java/entities/Position.java new file mode 100644 index 0000000..fbafd51 --- /dev/null +++ b/src/main/java/entities/Position.java @@ -0,0 +1,45 @@ +package entities; + +import entities.Ground; +import entities.Pair; + +//--------------------------------------------------------------- +// +public class Position { + private Ground ground; + private Pair value = new Pair<>(-20d, 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/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 c1b45c9..0000000 --- a/src/main/java/entities/TileType.java +++ /dev/null @@ -1,11 +0,0 @@ -package entities; - -//--------------------------------------------------------------- -// タイルの種類の定数クラス -public enum TileType { - - CLOSE, - OPEN; - - //--------------------------------------------------------------- -} diff --git a/src/main/java/entities/Velocity.java b/src/main/java/entities/Velocity.java new file mode 100644 index 0000000..4e7c568 --- /dev/null +++ b/src/main/java/entities/Velocity.java @@ -0,0 +1,58 @@ +package entities; + + +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<>(1d, 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 Velocity(Pair value,Position position, Onground onground) { + this.value = value; + 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/Flags.java b/src/main/java/entities/modelExtentions/Flags.java new file mode 100644 index 0000000..a4c0f79 --- /dev/null +++ b/src/main/java/entities/modelExtentions/Flags.java @@ -0,0 +1,39 @@ +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 Flags { + + private ArrayList values = new ArrayList<>(); + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + public Flags() { + 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])); + + } catch (IOException e) { + System.out.println("Failed to load stage.txt"); + } + } + + //--------------------------------------------------------------- + //--------------------------------------------------------------- + // getter + public ArrayList getValues() { + return values; + } + //--------------------------------------------------------------- +} 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 index 95ade5e..58ad441 100644 --- a/src/main/java/models/JumpGameModel.java +++ b/src/main/java/models/JumpGameModel.java @@ -1,10 +1,8 @@ package models; import entities.*; -import entities.player.*; +import entities.modelExtentions.Flags; -import java.io.BufferedReader; -import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; @@ -37,18 +35,17 @@ private double jumpPower = 32; //--------------------------------------------------------------- - private ArrayList flagTimings; // フラグ切り替え + private Flags flags; private boolean tileType = true; - private int flag = 0; //--------------------------------------------------------------- //--------------------------------------------------------------- // public JumpGameModel() { ground = new Ground(); - position = new Position(new Pair<>(640d, 480d), ground); + position = new Position(ground); onground = new Onground(ground, position); - velocity = new Velocity(position, onground); + velocity = new Velocity(new Pair<>(1d, 0d), position, onground); acceleration = new Acceleration(velocity, onground); force = new Force(acceleration); mass = new Mass(acceleration); @@ -56,23 +53,22 @@ time = new Time(); gameover = new Gameover(position); clear = new Clear(position); - - flagTimings = loadText(); + flags = new Flags(); } //--------------------------------------------------------------- //--------------------------------------------------------------- // getter - public boolean getTileType() { - return tileType; + public Flags getFlags() { + return flags; } public Position getPosition() { return position; } - public ArrayList getFlags() { - return flagTimings; + public Velocity getVelocity() { + return velocity; } public Ground getGround() { @@ -91,30 +87,9 @@ public void updateGravity(double gravity) { this.time.gravity(gravity); this.force.gravity(gravity); - System.out.println("swapWindow Gravity"); +// System.out.println("swapWindow Gravity"); } - //--------------------------------------------------------------- - //--------------------------------------------------------------- - // ステージデータの読み込み - private ArrayList loadText() { - ArrayList flagTimings = new ArrayList<>(); - - 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++) flagTimings.add(Integer.parseInt(data[i])); - - } catch (IOException e) { - System.out.println("Failed to load stage.txt"); - } - - return flagTimings; - } //--------------------------------------------------------------- // 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..45644af --- /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.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(-spriteSize.getFirst() / 2, spriteSize.getSecond() / 2); + glTexCoord2d(0, 1); + glVertex2d(-spriteSize.getFirst() / 2, -spriteSize.getSecond() / 2); + glTexCoord2d(1, 0); + glVertex2d(spriteSize.getFirst() / 2, spriteSize.getSecond() / 2); + glTexCoord2d(1, 1); + glVertex2d(spriteSize.getFirst() / 2, -spriteSize.getSecond() / 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..2eabe4f 100644 --- a/src/main/java/views/PlayerRenderer.java +++ b/src/main/java/views/PlayerRenderer.java @@ -1,6 +1,6 @@ package views; -import entities.*; +import entities.Pair; import models.IModel; import models.JumpGameModel; @@ -14,15 +14,14 @@ public PlayerRenderer(String path) { this.sprite = new Sprite(path); this.sprite.setScaleValue(1); + this.sprite.setPositionValue(new Pair<>(640d, 480d)); } //--------------------------------------------------------------- //--------------------------------------------------------------- - // JumpGameModelから座標を取得 + // @Override - public void update(IModel model){ - JumpGameModel jumpGameModel = (JumpGameModel) model; - sprite.setPositionValue(jumpGameModel.getPosition().getValue()); + public void update(IModel model) { } 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 ea006b9..677531d 100644 --- a/src/main/java/views/TileMapRenderer.java +++ b/src/main/java/views/TileMapRenderer.java @@ -1,7 +1,6 @@ package views; import entities.Pair; -import entities.TileType; import models.IModel; import java.util.ArrayList; @@ -10,7 +9,7 @@ // タイル生成 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<>(); @@ -57,9 +56,13 @@ for (int i = 0; i < 100; i++) { double x = 32 * newTile.getScaleValue() * (i + space); - // フラグのチェック - if (isFlag(i, flags)) isOpen = !isOpen; + System.out.println(isFlag(i, flags)); + + if (isFlag(i, flags)) { + isOpen = !isOpen; + } + if (isOpen) addNewTile(x, TileType.OPEN); else addNewTile(x, TileType.CLOSE); @@ -86,7 +89,8 @@ //--------------------------------------------------------------- // フラグのタイミングと一致しているかをチェック private boolean isFlag(int timing, ArrayList flags) { - for (int flag : flags) return (timing == flag); + for (int flag : flags) + if (timing == flag) return true; return false; } //--------------------------------------------------------------- diff --git a/src/main/java/views/TileRenderer.java b/src/main/java/views/TileRenderer.java index 8521a97..ec61865 100644 --- a/src/main/java/views/TileRenderer.java +++ b/src/main/java/views/TileRenderer.java @@ -1,14 +1,13 @@ 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; //--------------------------------------------------------------- //--------------------------------------------------------------- @@ -45,6 +44,8 @@ // @Override public void update(IModel model) { + JumpGameModel jumpGameModel = (JumpGameModel) model; + subPositionByVelocity(jumpGameModel.getVelocity().getValue()); } //--------------------------------------------------------------- @@ -52,7 +53,6 @@ @Override public void display() { sprite.draw(); - move(); } //--------------------------------------------------------------- @@ -64,12 +64,10 @@ //--------------------------------------------------------------- //--------------------------------------------------------------- - // 移動 - private void move() { - double x = sprite.getPositionValue().getFirst() + moveSpeed; - double y = sprite.getPositionValue().getSecond(); - - sprite.setPositionValue(new Pair<>(x, y)); + // + private void subPositionByVelocity(Pair velocity) { + Double x = this.sprite.getPositionValue().getFirst() - velocity.getFirst(); + Double y = this.sprite.getPositionValue().getSecond(); + this.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; + + //--------------------------------------------------------------- +} diff --git a/src/test/java/TextLoadTest.java b/src/test/java/TextLoadTest.java deleted file mode 100644 index 366316f..0000000 --- a/src/test/java/TextLoadTest.java +++ /dev/null @@ -1,12 +0,0 @@ -import models.JumpGameModel; -import org.junit.jupiter.api.Test; - -public class TextLoadTest { - @Test - public void Test() { - JumpGameModel model = new JumpGameModel(); - for (int i = 0; i < model.getFlags().size(); i++) System.out.println("flag: " + model.getFlags().get(i)); - - } - -}