diff --git a/java/GLWindow.java b/java/GLWindow.java new file mode 100644 index 0000000..00e6feb --- /dev/null +++ b/java/GLWindow.java @@ -0,0 +1,61 @@ +import entities.config.GLConfigVariable; +import org.lwjgl.glfw.GLFWErrorCallback; +import org.lwjgl.opengl.GL; + +import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks; +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.system.MemoryUtil.NULL; + +public class GLWindow { + + private long window; + + public long getWindow() { + return this.window; + } + + public void init() { + initWindow(); + initRender(); + } + + public void swapWindow() { + glfwSwapBuffers(window); + glfwPollEvents(); + } + + public void destroyWindow() { + glfwFreeCallbacks(window); + glfwDestroyWindow(window); + glfwTerminate(); + glfwSetErrorCallback(null).free(); + } + + public boolean windowShouldClose() { + return glfwWindowShouldClose(window); + } + + private void initWindow() { + GLFWErrorCallback.createPrint(System.err).set(); + + if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + 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(window); + glfwSwapInterval(1); + glfwShowWindow(window); + } + + private void initRender() { + GL.createCapabilities(); + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } +} diff --git a/java/GameEngine.java b/java/GameEngine.java new file mode 100644 index 0000000..4cec237 --- /dev/null +++ b/java/GameEngine.java @@ -0,0 +1,30 @@ +import static org.lwjgl.opengl.GL11.*; + +public abstract class GameEngine { + + private GLWindow glWindow = new GLWindow(); + + protected void init() { + } + + protected void update(long window) { + } + + protected void destroy() { + } + + protected void run() { + + glWindow.init(); + init(); + + while (!glWindow.windowShouldClose()) { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + update(glWindow.getWindow()); + glWindow.swapWindow(); + } + + destroy(); + glWindow.destroyWindow(); + } +} diff --git a/java/JumpingGame.java b/java/JumpingGame.java new file mode 100644 index 0000000..223460a --- /dev/null +++ b/java/JumpingGame.java @@ -0,0 +1,81 @@ +import models.IModel; +import models.JumpingGameModel; +import org.lwjgl.glfw.GLFWKeyCallback; +import views.IView; +import views.PlayerRenderer; +import views.TileMapRenderer; + +import javax.swing.*; +import java.util.ArrayList; + +import static org.lwjgl.glfw.GLFW.*; + +public class JumpingGame { + + private ArrayList views = new ArrayList<>(); + private IModel model = new JumpingGameModel(); + + private GLFWKeyCallback keyCallback; + + public void gravity(double y) { + JumpingGameModel jumpingGameModel = (JumpingGameModel) model; + jumpingGameModel.gravity(y); + jumpingGameModel.updateGroundFlag(); + } + + public void init() { + model = new JumpingGameModel(); + + JumpingGameModel jumpingGameModel = (JumpingGameModel) model; + jumpingGameModel.run(2); + + views.add(new TileMapRenderer(model)); + views.add(new PlayerRenderer("resources/JCasC.png")); + } + + public void update(long window) { + + for (IView view : views) { + view.update(model); + view.display(); + } + + 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) { + JumpingGameModel jumpGameModel = (JumpingGameModel) model; + jumpGameModel.jump(256); + } + } + }); + + gameClear(); + gameOver(); + gravity(-256); + + } + + public void delete() { + for (IView view : views) view.delete(); + } + + private void gameClear(){ + JumpingGameModel jumpingGameModel = (JumpingGameModel) model; + if(jumpingGameModel.getClear()){ + JFrame jFrame = new JFrame(); + JOptionPane.showMessageDialog(jFrame, "Clear!"); + init(); + } + } + + private void gameOver(){ + JumpingGameModel jumpingGameModel = (JumpingGameModel) model; + if(jumpingGameModel.getGameover()){ + JFrame jFrame = new JFrame(); + JOptionPane.showMessageDialog(jFrame, "GameOver"); + init(); + } + } + +} \ No newline at end of file diff --git a/java/Main.java b/java/Main.java new file mode 100644 index 0000000..1371320 --- /dev/null +++ b/java/Main.java @@ -0,0 +1,23 @@ +public class Main extends GameEngine { + + private JumpingGame jumpingGame = new JumpingGame(); + + public static void main(String[] args) { + new Main().run(); + } + + @Override + protected void init() { + jumpingGame.init(); + } + + @Override + protected void update(long window) { + jumpingGame.update(window); + } + + @Override + protected void destroy() { + jumpingGame.delete(); + } +} \ No newline at end of file diff --git a/java/entities/Acceleration.java b/java/entities/Acceleration.java new file mode 100644 index 0000000..b07ec67 --- /dev/null +++ b/java/entities/Acceleration.java @@ -0,0 +1,38 @@ +package entities; + +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_if1; + if (this.onground.getValue()) { + temp_if1 = new Pair<>((force.getLeft()/mass),0.0); + } else { + temp_if1 = new Pair<>((force.getLeft()/mass),(force.getRight()/mass)); + } + value = temp_if1; + velocity.updateAcceleration(value); + } + public void updateMass(double mass) { + this.mass = mass; + Pair temp_if5; + if (this.onground.getValue()) { + temp_if5 = new Pair<>((force.getLeft()/mass),0.0); + } else { + temp_if5 = new Pair<>((force.getLeft()/mass),(force.getRight()/mass)); + } + value = temp_if5; + 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/java/entities/Clear.java b/java/entities/Clear.java new file mode 100644 index 0000000..7bbd560 --- /dev/null +++ b/java/entities/Clear.java @@ -0,0 +1,17 @@ +package entities; + +public class Clear { + private boolean value = false; + public void updatePosition(Pair position) { + boolean temp_if2; + if ((position.getLeft()>100.0)) { + temp_if2 = true; + } else { + temp_if2 = false; + } + value = temp_if2; + } + public boolean getValue() { + return value; + } +} \ No newline at end of file diff --git a/java/entities/Force.java b/java/entities/Force.java new file mode 100644 index 0000000..b46046d --- /dev/null +++ b/java/entities/Force.java @@ -0,0 +1,16 @@ +package entities; + +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/java/entities/Gameover.java b/java/entities/Gameover.java new file mode 100644 index 0000000..9d6cd43 --- /dev/null +++ b/java/entities/Gameover.java @@ -0,0 +1,17 @@ +package entities; + +public class Gameover { + private boolean value = false; + public void updatePosition(Pair position) { + boolean temp_if0; + if ((position.getRight()<-(100))) { + temp_if0 = true; + } else { + temp_if0 = false; + } + value = temp_if0; + } + public boolean getValue() { + return value; + } +} \ No newline at end of file diff --git a/java/entities/Ground.java b/java/entities/Ground.java new file mode 100644 index 0000000..15083ea --- /dev/null +++ b/java/entities/Ground.java @@ -0,0 +1,20 @@ +package entities; + +public class Ground { + private Onground onground; + private boolean value = true; + public Ground(Onground onground) { + this.onground = onground; + } + public void closeHole() { + this.value = true; + onground.updateGround(value); + } + public void openHole() { + this.value = false; + onground.updateGround(value); + } + public boolean getValue() { + return value; + } +} \ No newline at end of file diff --git a/java/entities/Mass.java b/java/entities/Mass.java new file mode 100644 index 0000000..bec20c3 --- /dev/null +++ b/java/entities/Mass.java @@ -0,0 +1,16 @@ +package entities; + +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/java/entities/Movex.java b/java/entities/Movex.java new file mode 100644 index 0000000..d0d9ebc --- /dev/null +++ b/java/entities/Movex.java @@ -0,0 +1,16 @@ +package entities; + +public class Movex { + private Velocity velocity; + private double value; + public Movex(Velocity velocity) { + this.velocity = velocity; + } + public void run(double x2) { + this.value = x2; + velocity.updateMovex(value); + } + public double getValue() { + return value; + } +} \ No newline at end of file diff --git a/java/entities/Movey.java b/java/entities/Movey.java new file mode 100644 index 0000000..c812b90 --- /dev/null +++ b/java/entities/Movey.java @@ -0,0 +1,16 @@ +package entities; + +public class Movey { + private Velocity velocity; + private double value; + public Movey(Velocity velocity) { + this.velocity = velocity; + } + public void jump(double y2) { + this.value = y2; + velocity.updateMovey(value); + } + public double getValue() { + return value; + } +} \ No newline at end of file diff --git a/java/entities/Onground.java b/java/entities/Onground.java new file mode 100644 index 0000000..9c3233c --- /dev/null +++ b/java/entities/Onground.java @@ -0,0 +1,18 @@ +package entities; + +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/java/entities/Pair.java b/java/entities/Pair.java new file mode 100644 index 0000000..b0b9e11 --- /dev/null +++ b/java/entities/Pair.java @@ -0,0 +1,16 @@ +package entities; + +public class Pair { + 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/java/entities/Position.java b/java/entities/Position.java new file mode 100644 index 0000000..b652c67 --- /dev/null +++ b/java/entities/Position.java @@ -0,0 +1,30 @@ +package entities; + +public class Position { + private Gameover gameover; + private Clear clear; + private Onground onground; + 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; + gameover.updatePosition(value); + clear.updatePosition(value); + onground.updatePosition(value); + } + public Position(Gameover gameover, Clear clear, Onground onground, Ground ground) { + this.gameover = gameover; + this.clear = clear; + this.onground = onground; + this.ground = ground; + } + public Pair getValue() { + return value; + } +} \ No newline at end of file diff --git a/java/entities/Time.java b/java/entities/Time.java new file mode 100644 index 0000000..0fdbfad --- /dev/null +++ b/java/entities/Time.java @@ -0,0 +1,11 @@ +package entities; + +public class Time { + 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/java/entities/Velocity.java b/java/entities/Velocity.java new file mode 100644 index 0000000..d815ce2 --- /dev/null +++ b/java/entities/Velocity.java @@ -0,0 +1,50 @@ +package entities; + +public class Velocity { + private double movey; + private double movex; + 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 updateMovey(double movey) { + this.movey = movey; + Pair temp_if3; + if (this.onground.getValue()) { + temp_if3 = new Pair<>(this.value.getLeft(),movey); + } else { + temp_if3 = this.value; + } + value = temp_if3; + position.updateVelocity(value); + } + public void updateMovex(double movex) { + this.movex = movex; + Pair temp_if6; + if (this.onground.getValue()) { + temp_if6 = new Pair<>(movex,this.value.getRight()); + } else { + temp_if6 = this.value; + } + value = temp_if6; + position.updateVelocity(value); + } + public void updateAcceleration(Pair acceleration) { + this.acceleration = acceleration; + Pair temp_if7; + if ((this.onground.getValue()&&(this.value.getRight()<0.0))) { + temp_if7 = new Pair<>((this.value.getLeft()+(0.01*acceleration.getLeft())),0.0); + } else { + temp_if7 = new Pair<>((this.value.getLeft()+(0.01*acceleration.getLeft())),(this.value.getRight()+(0.01*acceleration.getRight()))); + } + value = temp_if7; + 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/java/entities/config/GLConfigVariable.java b/java/entities/config/GLConfigVariable.java new file mode 100644 index 0000000..dfcebb9 --- /dev/null +++ b/java/entities/config/GLConfigVariable.java @@ -0,0 +1,13 @@ +package entities.config; + +public class GLConfigVariable { + + private GLConfigVariable() { + } + + public static final String TITLE_NAME = "JumpingGame"; + public static final int WIDTH = 1280; + public static final int HEIGHT = 720; + public static final int DEPTH = 100; + public static final boolean IS_FULL_SCREEN = false; +} \ No newline at end of file diff --git a/java/entities/modelExtentions/Stage.java b/java/entities/modelExtentions/Stage.java new file mode 100644 index 0000000..9906bb6 --- /dev/null +++ b/java/entities/modelExtentions/Stage.java @@ -0,0 +1,62 @@ +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])); + } catch (IOException e) { + System.out.println("Failed to load stage.txt"); + } + } + + 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/java/models/IModel.java b/java/models/IModel.java new file mode 100644 index 0000000..700df1f --- /dev/null +++ b/java/models/IModel.java @@ -0,0 +1,4 @@ +package models; + +public interface IModel { +} diff --git a/java/models/JumpingGameModel.java b/java/models/JumpingGameModel.java new file mode 100644 index 0000000..9e83965 --- /dev/null +++ b/java/models/JumpingGameModel.java @@ -0,0 +1,104 @@ +package models; + +import entities.*; +import entities.modelExtentions.Stage; + +public class JumpingGameModel implements IModel{ + private Gameover gameover; + private Time time; + private Onground onground; + private Ground ground; + private Clear clear; + private Position position; + private Velocity velocity; + private Movey movey; + private Movex movex; + private Acceleration acceleration; + private Mass mass; + private Force force; + + private Stage stage; //added + + public JumpingGameModel() { + gameover = new Gameover(); + time = new Time(); + onground = new Onground(); + ground = new Ground(onground); + clear = new Clear(); + position = new Position(gameover,clear,onground,ground); + velocity = new Velocity(position,onground); + movey = new Movey(velocity); + movex = new Movex(velocity); + acceleration = new Acceleration(velocity,onground); + mass = new Mass(acceleration); + force = new Force(acceleration); + stage = new Stage(); // added + } + public void gravity(double y) { + this.force.gravity(y); + this.time.gravity(y); + } + public void closeHole() { + this.ground.closeHole(); + } + public void openHole() { + this.ground.openHole(); + } + public void jump(double y2) { + this.movey.jump(y2); + } + public void run(double x2) { + this.movex.run(x2); + } + public void setMass(double x) { + this.mass.setMass(x); + } + public Pair getAcceleration() { + return acceleration.getValue(); + } + public double getMovex() { + return movex.getValue(); + } + public double getMass() { + return mass.getValue(); + } + public boolean getClear() { + return clear.getValue(); + } + public boolean getGround() { + return ground.getValue(); + } + public Pair getForce() { + return force.getValue(); + } + public Pair getVelocity() { + return velocity.getValue(); + } + public Pair getPosition() { + return position.getValue(); + } + public boolean getOnground() { + return onground.getValue(); + } + public double getTime() { + return time.getValue(); + } + public double getMovey() { + return movey.getValue(); + } + public boolean getGameover() { + return gameover.getValue(); + } + + // added + public Stage getStage() { + return stage; + } + // added + public void updateGroundFlag() { + double x = position.getValue().getLeft(); + + if (stage.isOpenFlag(x)) ground.openHole(); + if (stage.isCloseFlag(x)) ground.closeHole(); + } +} \ No newline at end of file diff --git a/java/views/Color.java b/java/views/Color.java new file mode 100644 index 0000000..140b0a1 --- /dev/null +++ b/java/views/Color.java @@ -0,0 +1,31 @@ +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/java/views/IView.java b/java/views/IView.java new file mode 100644 index 0000000..663b660 --- /dev/null +++ b/java/views/IView.java @@ -0,0 +1,13 @@ +package views; + +import models.IModel; + + +public interface IView { + + void update(IModel model); + + void display(); + + void delete(); +} diff --git a/java/views/Image2D.java b/java/views/Image2D.java new file mode 100644 index 0000000..43918d3 --- /dev/null +++ b/java/views/Image2D.java @@ -0,0 +1,75 @@ +package views; + +import entities.Pair; +import entities.config.GLConfigVariable; + +import static org.lwjgl.opengl.GL11.*; + +public class Image2D { + + private int 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; + } + + 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/java/views/PlayerRenderer.java b/java/views/PlayerRenderer.java new file mode 100644 index 0000000..b26e7ae --- /dev/null +++ b/java/views/PlayerRenderer.java @@ -0,0 +1,36 @@ +package views; + +import entities.Pair; +import models.IModel; +import models.JumpingGameModel; + + + +public class PlayerRenderer implements IView { + private Sprite sprite; + + public PlayerRenderer(String path) { + this.sprite = new Sprite(path); + this.sprite.setScaleValue(0.1); + this.sprite.setPositionValue(new Pair<>(640d, 480d)); + } + + @Override + public void update(IModel model) { + JumpingGameModel jumpGameModel = (JumpingGameModel) model; + + double x = this.sprite.getPositionValue().getLeft(); + double y = 112 + jumpGameModel.getPosition().getRight(); + this.sprite.setPositionValue(new Pair<>(x, y)); + } + + @Override + public void display() { + sprite.draw(); + } + + @Override + public void delete() { + sprite.delete(); + } +} diff --git a/java/views/Sprite.java b/java/views/Sprite.java new file mode 100644 index 0000000..f0cd2d2 --- /dev/null +++ b/java/views/Sprite.java @@ -0,0 +1,43 @@ +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); + } + + public double getScaleValue() { + return this.scaleValue; + } + + public Pair getPositionValue() { + return this.positionValue; + } + + 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/java/views/Texture.java b/java/views/Texture.java new file mode 100644 index 0000000..d525319 --- /dev/null +++ b/java/views/Texture.java @@ -0,0 +1,73 @@ +package views; + +import org.lwjgl.BufferUtils; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; + +import static org.lwjgl.opengl.GL11.*; + + +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(); + glBindTexture(GL_TEXTURE_2D, 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 void delete() { + glDeleteTextures(id); + } +} \ No newline at end of file diff --git a/java/views/TileMapRenderer.java b/java/views/TileMapRenderer.java new file mode 100644 index 0000000..028cc19 --- /dev/null +++ b/java/views/TileMapRenderer.java @@ -0,0 +1,68 @@ +package views; + +import entities.Pair; +import entities.modelExtentions.Stage; +import models.IModel; +import models.JumpingGameModel; + +import java.util.ArrayList; + +public class TileMapRenderer implements IView { + + private double offsetY = 32d; + private TileRenderer newTile = new TileRenderer("resources/tile.png", null, 2); + private ArrayList tiles = new ArrayList<>(); + + public TileMapRenderer(IModel model) { + JumpingGameModel jumpGameModel = (JumpingGameModel) model; + initTiles(jumpGameModel.getStage()); + } + + @Override + public void update(IModel model) { + for (TileRenderer tile : tiles) tile.update(model); + } + + @Override + public void display() { + for (TileRenderer tile : tiles) tile.display(); + } + + @Override + public void delete() { + for (TileRenderer tile : tiles) tile.delete(); + } + + + private void initTiles(Stage stage) { + boolean isOpen = false; + int space = 10; + + 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); + } + + 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 addNewTile(double x, TileType tileType) { + + switch (tileType) { + case CLOSE: + newTile = new TileRenderer("resources/tile.png", new Pair<>(x, offsetY), 2); + break; + case OPEN: + newTile = new TileRenderer("resources/hole.png", new Pair<>(x, offsetY), 2); + break; + } + tiles.add(newTile); + } +} \ No newline at end of file diff --git a/java/views/TileRenderer.java b/java/views/TileRenderer.java new file mode 100644 index 0000000..168a16b --- /dev/null +++ b/java/views/TileRenderer.java @@ -0,0 +1,41 @@ +package views; + +import entities.Pair; +import models.IModel; +import models.JumpingGameModel; + +public class TileRenderer implements IView { + private Sprite sprite; + private Pair initPositionValue; + + public TileRenderer(String path, Pair initPosition, double scale) { + this.initPositionValue = initPosition; + this.sprite = new Sprite(path); + this.sprite.setScaleValue(scale); + this.sprite.setPositionValue(initPosition); + } + + public double getScaleValue() { + return this.sprite.getScaleValue(); + } + + @Override + public void update(IModel model) { + JumpingGameModel jumpingGameModel = (JumpingGameModel) model; + + Double x = this.initPositionValue.getLeft() - (jumpingGameModel.getPosition().getLeft() * 64); + Double y = this.sprite.getPositionValue().getRight(); + + this.sprite.setPositionValue(new Pair<>(x, y)); + } + + @Override + public void display() { + sprite.draw(); + } + + @Override + public void delete() { + sprite.delete(); + } +} diff --git a/java/views/TileType.java b/java/views/TileType.java new file mode 100644 index 0000000..67b7f51 --- /dev/null +++ b/java/views/TileType.java @@ -0,0 +1,6 @@ +package views; + +public enum TileType { + CLOSE, + OPEN; +} diff --git a/src/main/java/GLWindow.java b/src/main/java/GLWindow.java deleted file mode 100644 index 00e6feb..0000000 --- a/src/main/java/GLWindow.java +++ /dev/null @@ -1,61 +0,0 @@ -import entities.config.GLConfigVariable; -import org.lwjgl.glfw.GLFWErrorCallback; -import org.lwjgl.opengl.GL; - -import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks; -import static org.lwjgl.glfw.GLFW.*; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.system.MemoryUtil.NULL; - -public class GLWindow { - - private long window; - - public long getWindow() { - return this.window; - } - - public void init() { - initWindow(); - initRender(); - } - - public void swapWindow() { - glfwSwapBuffers(window); - glfwPollEvents(); - } - - public void destroyWindow() { - glfwFreeCallbacks(window); - glfwDestroyWindow(window); - glfwTerminate(); - glfwSetErrorCallback(null).free(); - } - - public boolean windowShouldClose() { - return glfwWindowShouldClose(window); - } - - private void initWindow() { - GLFWErrorCallback.createPrint(System.err).set(); - - if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - - 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(window); - glfwSwapInterval(1); - glfwShowWindow(window); - } - - private void initRender() { - GL.createCapabilities(); - glClearColor(1.0f, 1.0f, 1.0f, 1.0f); - - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - } -} diff --git a/src/main/java/GameEngine.java b/src/main/java/GameEngine.java deleted file mode 100644 index 4cec237..0000000 --- a/src/main/java/GameEngine.java +++ /dev/null @@ -1,30 +0,0 @@ -import static org.lwjgl.opengl.GL11.*; - -public abstract class GameEngine { - - private GLWindow glWindow = new GLWindow(); - - protected void init() { - } - - protected void update(long window) { - } - - protected void destroy() { - } - - protected void run() { - - glWindow.init(); - init(); - - while (!glWindow.windowShouldClose()) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - update(glWindow.getWindow()); - glWindow.swapWindow(); - } - - destroy(); - glWindow.destroyWindow(); - } -} diff --git a/src/main/java/JumpingGame.java b/src/main/java/JumpingGame.java deleted file mode 100644 index 223460a..0000000 --- a/src/main/java/JumpingGame.java +++ /dev/null @@ -1,81 +0,0 @@ -import models.IModel; -import models.JumpingGameModel; -import org.lwjgl.glfw.GLFWKeyCallback; -import views.IView; -import views.PlayerRenderer; -import views.TileMapRenderer; - -import javax.swing.*; -import java.util.ArrayList; - -import static org.lwjgl.glfw.GLFW.*; - -public class JumpingGame { - - private ArrayList views = new ArrayList<>(); - private IModel model = new JumpingGameModel(); - - private GLFWKeyCallback keyCallback; - - public void gravity(double y) { - JumpingGameModel jumpingGameModel = (JumpingGameModel) model; - jumpingGameModel.gravity(y); - jumpingGameModel.updateGroundFlag(); - } - - public void init() { - model = new JumpingGameModel(); - - JumpingGameModel jumpingGameModel = (JumpingGameModel) model; - jumpingGameModel.run(2); - - views.add(new TileMapRenderer(model)); - views.add(new PlayerRenderer("resources/JCasC.png")); - } - - public void update(long window) { - - for (IView view : views) { - view.update(model); - view.display(); - } - - 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) { - JumpingGameModel jumpGameModel = (JumpingGameModel) model; - jumpGameModel.jump(256); - } - } - }); - - gameClear(); - gameOver(); - gravity(-256); - - } - - public void delete() { - for (IView view : views) view.delete(); - } - - private void gameClear(){ - JumpingGameModel jumpingGameModel = (JumpingGameModel) model; - if(jumpingGameModel.getClear()){ - JFrame jFrame = new JFrame(); - JOptionPane.showMessageDialog(jFrame, "Clear!"); - init(); - } - } - - private void gameOver(){ - JumpingGameModel jumpingGameModel = (JumpingGameModel) model; - if(jumpingGameModel.getGameover()){ - JFrame jFrame = new JFrame(); - JOptionPane.showMessageDialog(jFrame, "GameOver"); - init(); - } - } - -} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java deleted file mode 100644 index 1371320..0000000 --- a/src/main/java/Main.java +++ /dev/null @@ -1,23 +0,0 @@ -public class Main extends GameEngine { - - private JumpingGame jumpingGame = new JumpingGame(); - - public static void main(String[] args) { - new Main().run(); - } - - @Override - protected void init() { - jumpingGame.init(); - } - - @Override - protected void update(long window) { - jumpingGame.update(window); - } - - @Override - protected void destroy() { - jumpingGame.delete(); - } -} \ No newline at end of file diff --git a/src/main/java/entities/Acceleration.java b/src/main/java/entities/Acceleration.java deleted file mode 100644 index b07ec67..0000000 --- a/src/main/java/entities/Acceleration.java +++ /dev/null @@ -1,38 +0,0 @@ -package entities; - -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_if1; - if (this.onground.getValue()) { - temp_if1 = new Pair<>((force.getLeft()/mass),0.0); - } else { - temp_if1 = new Pair<>((force.getLeft()/mass),(force.getRight()/mass)); - } - value = temp_if1; - velocity.updateAcceleration(value); - } - public void updateMass(double mass) { - this.mass = mass; - Pair temp_if5; - if (this.onground.getValue()) { - temp_if5 = new Pair<>((force.getLeft()/mass),0.0); - } else { - temp_if5 = new Pair<>((force.getLeft()/mass),(force.getRight()/mass)); - } - value = temp_if5; - 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 deleted file mode 100644 index 7bbd560..0000000 --- a/src/main/java/entities/Clear.java +++ /dev/null @@ -1,17 +0,0 @@ -package entities; - -public class Clear { - private boolean value = false; - public void updatePosition(Pair position) { - boolean temp_if2; - if ((position.getLeft()>100.0)) { - temp_if2 = true; - } else { - temp_if2 = false; - } - value = temp_if2; - } - public boolean getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/entities/Force.java b/src/main/java/entities/Force.java deleted file mode 100644 index b46046d..0000000 --- a/src/main/java/entities/Force.java +++ /dev/null @@ -1,16 +0,0 @@ -package entities; - -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/Gameover.java b/src/main/java/entities/Gameover.java deleted file mode 100644 index 9d6cd43..0000000 --- a/src/main/java/entities/Gameover.java +++ /dev/null @@ -1,17 +0,0 @@ -package entities; - -public class Gameover { - private boolean value = false; - public void updatePosition(Pair position) { - boolean temp_if0; - if ((position.getRight()<-(100))) { - temp_if0 = true; - } else { - temp_if0 = false; - } - value = temp_if0; - } - 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 deleted file mode 100644 index 15083ea..0000000 --- a/src/main/java/entities/Ground.java +++ /dev/null @@ -1,20 +0,0 @@ -package entities; - -public class Ground { - private Onground onground; - private boolean value = true; - public Ground(Onground onground) { - this.onground = onground; - } - public void closeHole() { - this.value = true; - onground.updateGround(value); - } - public void openHole() { - this.value = false; - onground.updateGround(value); - } - public boolean getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/entities/Mass.java b/src/main/java/entities/Mass.java deleted file mode 100644 index bec20c3..0000000 --- a/src/main/java/entities/Mass.java +++ /dev/null @@ -1,16 +0,0 @@ -package entities; - -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/Movex.java b/src/main/java/entities/Movex.java deleted file mode 100644 index d0d9ebc..0000000 --- a/src/main/java/entities/Movex.java +++ /dev/null @@ -1,16 +0,0 @@ -package entities; - -public class Movex { - private Velocity velocity; - private double value; - public Movex(Velocity velocity) { - this.velocity = velocity; - } - public void run(double x2) { - this.value = x2; - velocity.updateMovex(value); - } - public double getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/entities/Movey.java b/src/main/java/entities/Movey.java deleted file mode 100644 index c812b90..0000000 --- a/src/main/java/entities/Movey.java +++ /dev/null @@ -1,16 +0,0 @@ -package entities; - -public class Movey { - private Velocity velocity; - private double value; - public Movey(Velocity velocity) { - this.velocity = velocity; - } - public void jump(double y2) { - this.value = y2; - velocity.updateMovey(value); - } - public double 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 deleted file mode 100644 index 9c3233c..0000000 --- a/src/main/java/entities/Onground.java +++ /dev/null @@ -1,18 +0,0 @@ -package entities; - -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 deleted file mode 100644 index b0b9e11..0000000 --- a/src/main/java/entities/Pair.java +++ /dev/null @@ -1,16 +0,0 @@ -package entities; - -public class Pair { - 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 deleted file mode 100644 index b652c67..0000000 --- a/src/main/java/entities/Position.java +++ /dev/null @@ -1,30 +0,0 @@ -package entities; - -public class Position { - private Gameover gameover; - private Clear clear; - private Onground onground; - 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; - gameover.updatePosition(value); - clear.updatePosition(value); - onground.updatePosition(value); - } - public Position(Gameover gameover, Clear clear, Onground onground, Ground ground) { - this.gameover = gameover; - this.clear = clear; - this.onground = onground; - this.ground = ground; - } - public Pair getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/main/java/entities/Time.java b/src/main/java/entities/Time.java deleted file mode 100644 index 0fdbfad..0000000 --- a/src/main/java/entities/Time.java +++ /dev/null @@ -1,11 +0,0 @@ -package entities; - -public class Time { - 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 deleted file mode 100644 index d815ce2..0000000 --- a/src/main/java/entities/Velocity.java +++ /dev/null @@ -1,50 +0,0 @@ -package entities; - -public class Velocity { - private double movey; - private double movex; - 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 updateMovey(double movey) { - this.movey = movey; - Pair temp_if3; - if (this.onground.getValue()) { - temp_if3 = new Pair<>(this.value.getLeft(),movey); - } else { - temp_if3 = this.value; - } - value = temp_if3; - position.updateVelocity(value); - } - public void updateMovex(double movex) { - this.movex = movex; - Pair temp_if6; - if (this.onground.getValue()) { - temp_if6 = new Pair<>(movex,this.value.getRight()); - } else { - temp_if6 = this.value; - } - value = temp_if6; - position.updateVelocity(value); - } - public void updateAcceleration(Pair acceleration) { - this.acceleration = acceleration; - Pair temp_if7; - if ((this.onground.getValue()&&(this.value.getRight()<0.0))) { - temp_if7 = new Pair<>((this.value.getLeft()+(0.01*acceleration.getLeft())),0.0); - } else { - temp_if7 = new Pair<>((this.value.getLeft()+(0.01*acceleration.getLeft())),(this.value.getRight()+(0.01*acceleration.getRight()))); - } - value = temp_if7; - 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 deleted file mode 100644 index dfcebb9..0000000 --- a/src/main/java/entities/config/GLConfigVariable.java +++ /dev/null @@ -1,13 +0,0 @@ -package entities.config; - -public class GLConfigVariable { - - private GLConfigVariable() { - } - - public static final String TITLE_NAME = "JumpingGame"; - public static final int WIDTH = 1280; - public static final int HEIGHT = 720; - public static final int DEPTH = 100; - 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 deleted file mode 100644 index 9906bb6..0000000 --- a/src/main/java/entities/modelExtentions/Stage.java +++ /dev/null @@ -1,62 +0,0 @@ -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])); - } catch (IOException e) { - System.out.println("Failed to load stage.txt"); - } - } - - 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/models/IModel.java b/src/main/java/models/IModel.java deleted file mode 100644 index 700df1f..0000000 --- a/src/main/java/models/IModel.java +++ /dev/null @@ -1,4 +0,0 @@ -package models; - -public interface IModel { -} diff --git a/src/main/java/models/JumpingGameModel.java b/src/main/java/models/JumpingGameModel.java deleted file mode 100644 index 9e83965..0000000 --- a/src/main/java/models/JumpingGameModel.java +++ /dev/null @@ -1,104 +0,0 @@ -package models; - -import entities.*; -import entities.modelExtentions.Stage; - -public class JumpingGameModel implements IModel{ - private Gameover gameover; - private Time time; - private Onground onground; - private Ground ground; - private Clear clear; - private Position position; - private Velocity velocity; - private Movey movey; - private Movex movex; - private Acceleration acceleration; - private Mass mass; - private Force force; - - private Stage stage; //added - - public JumpingGameModel() { - gameover = new Gameover(); - time = new Time(); - onground = new Onground(); - ground = new Ground(onground); - clear = new Clear(); - position = new Position(gameover,clear,onground,ground); - velocity = new Velocity(position,onground); - movey = new Movey(velocity); - movex = new Movex(velocity); - acceleration = new Acceleration(velocity,onground); - mass = new Mass(acceleration); - force = new Force(acceleration); - stage = new Stage(); // added - } - public void gravity(double y) { - this.force.gravity(y); - this.time.gravity(y); - } - public void closeHole() { - this.ground.closeHole(); - } - public void openHole() { - this.ground.openHole(); - } - public void jump(double y2) { - this.movey.jump(y2); - } - public void run(double x2) { - this.movex.run(x2); - } - public void setMass(double x) { - this.mass.setMass(x); - } - public Pair getAcceleration() { - return acceleration.getValue(); - } - public double getMovex() { - return movex.getValue(); - } - public double getMass() { - return mass.getValue(); - } - public boolean getClear() { - return clear.getValue(); - } - public boolean getGround() { - return ground.getValue(); - } - public Pair getForce() { - return force.getValue(); - } - public Pair getVelocity() { - return velocity.getValue(); - } - public Pair getPosition() { - return position.getValue(); - } - public boolean getOnground() { - return onground.getValue(); - } - public double getTime() { - return time.getValue(); - } - public double getMovey() { - return movey.getValue(); - } - public boolean getGameover() { - return gameover.getValue(); - } - - // added - public Stage getStage() { - return stage; - } - // added - public void updateGroundFlag() { - double x = position.getValue().getLeft(); - - if (stage.isOpenFlag(x)) ground.openHole(); - if (stage.isCloseFlag(x)) ground.closeHole(); - } -} \ No newline at end of file diff --git a/src/main/java/views/Color.java b/src/main/java/views/Color.java deleted file mode 100644 index 140b0a1..0000000 --- a/src/main/java/views/Color.java +++ /dev/null @@ -1,31 +0,0 @@ -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/IView.java b/src/main/java/views/IView.java deleted file mode 100644 index 663b660..0000000 --- a/src/main/java/views/IView.java +++ /dev/null @@ -1,13 +0,0 @@ -package views; - -import models.IModel; - - -public interface IView { - - void update(IModel model); - - void display(); - - void delete(); -} diff --git a/src/main/java/views/Image2D.java b/src/main/java/views/Image2D.java deleted file mode 100644 index 43918d3..0000000 --- a/src/main/java/views/Image2D.java +++ /dev/null @@ -1,75 +0,0 @@ -package views; - -import entities.Pair; -import entities.config.GLConfigVariable; - -import static org.lwjgl.opengl.GL11.*; - -public class Image2D { - - private int 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; - } - - 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 deleted file mode 100644 index b26e7ae..0000000 --- a/src/main/java/views/PlayerRenderer.java +++ /dev/null @@ -1,36 +0,0 @@ -package views; - -import entities.Pair; -import models.IModel; -import models.JumpingGameModel; - - - -public class PlayerRenderer implements IView { - private Sprite sprite; - - public PlayerRenderer(String path) { - this.sprite = new Sprite(path); - this.sprite.setScaleValue(0.1); - this.sprite.setPositionValue(new Pair<>(640d, 480d)); - } - - @Override - public void update(IModel model) { - JumpingGameModel jumpGameModel = (JumpingGameModel) model; - - double x = this.sprite.getPositionValue().getLeft(); - double y = 112 + jumpGameModel.getPosition().getRight(); - this.sprite.setPositionValue(new Pair<>(x, y)); - } - - @Override - public void display() { - sprite.draw(); - } - - @Override - public void delete() { - sprite.delete(); - } -} diff --git a/src/main/java/views/Sprite.java b/src/main/java/views/Sprite.java deleted file mode 100644 index f0cd2d2..0000000 --- a/src/main/java/views/Sprite.java +++ /dev/null @@ -1,43 +0,0 @@ -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); - } - - public double getScaleValue() { - return this.scaleValue; - } - - public Pair getPositionValue() { - return this.positionValue; - } - - 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 deleted file mode 100644 index d525319..0000000 --- a/src/main/java/views/Texture.java +++ /dev/null @@ -1,73 +0,0 @@ -package views; - -import org.lwjgl.BufferUtils; - -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; -import java.nio.ByteBuffer; - -import static org.lwjgl.opengl.GL11.*; - - -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(); - glBindTexture(GL_TEXTURE_2D, 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 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 deleted file mode 100644 index 028cc19..0000000 --- a/src/main/java/views/TileMapRenderer.java +++ /dev/null @@ -1,68 +0,0 @@ -package views; - -import entities.Pair; -import entities.modelExtentions.Stage; -import models.IModel; -import models.JumpingGameModel; - -import java.util.ArrayList; - -public class TileMapRenderer implements IView { - - private double offsetY = 32d; - private TileRenderer newTile = new TileRenderer("resources/tile.png", null, 2); - private ArrayList tiles = new ArrayList<>(); - - public TileMapRenderer(IModel model) { - JumpingGameModel jumpGameModel = (JumpingGameModel) model; - initTiles(jumpGameModel.getStage()); - } - - @Override - public void update(IModel model) { - for (TileRenderer tile : tiles) tile.update(model); - } - - @Override - public void display() { - for (TileRenderer tile : tiles) tile.display(); - } - - @Override - public void delete() { - for (TileRenderer tile : tiles) tile.delete(); - } - - - private void initTiles(Stage stage) { - boolean isOpen = false; - int space = 10; - - 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); - } - - 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 addNewTile(double x, TileType tileType) { - - switch (tileType) { - case CLOSE: - newTile = new TileRenderer("resources/tile.png", new Pair<>(x, offsetY), 2); - break; - case OPEN: - newTile = new TileRenderer("resources/hole.png", new Pair<>(x, offsetY), 2); - break; - } - tiles.add(newTile); - } -} \ No newline at end of file diff --git a/src/main/java/views/TileRenderer.java b/src/main/java/views/TileRenderer.java deleted file mode 100644 index 168a16b..0000000 --- a/src/main/java/views/TileRenderer.java +++ /dev/null @@ -1,41 +0,0 @@ -package views; - -import entities.Pair; -import models.IModel; -import models.JumpingGameModel; - -public class TileRenderer implements IView { - private Sprite sprite; - private Pair initPositionValue; - - public TileRenderer(String path, Pair initPosition, double scale) { - this.initPositionValue = initPosition; - this.sprite = new Sprite(path); - this.sprite.setScaleValue(scale); - this.sprite.setPositionValue(initPosition); - } - - public double getScaleValue() { - return this.sprite.getScaleValue(); - } - - @Override - public void update(IModel model) { - JumpingGameModel jumpingGameModel = (JumpingGameModel) model; - - Double x = this.initPositionValue.getLeft() - (jumpingGameModel.getPosition().getLeft() * 64); - Double y = this.sprite.getPositionValue().getRight(); - - this.sprite.setPositionValue(new Pair<>(x, y)); - } - - @Override - public void display() { - sprite.draw(); - } - - @Override - public void delete() { - sprite.delete(); - } -} diff --git a/src/main/java/views/TileType.java b/src/main/java/views/TileType.java deleted file mode 100644 index 67b7f51..0000000 --- a/src/main/java/views/TileType.java +++ /dev/null @@ -1,6 +0,0 @@ -package views; - -public enum TileType { - CLOSE, - OPEN; -}