Newer
Older
JumpingGame / src / main / java / JumpGame.java
import models.IModel;
import models.JumpGameModel;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWKeyCallback;
import views.BackgroundRenderer;
import views.IView;
import views.PlayerRenderer;
import views.TileMapRenderer;

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;

import java.util.ArrayList;

public class JumpGame {

    private ArrayList<IView> views = new ArrayList<>();
    private IModel model = new JumpGameModel();

    private GLFWKeyCallback keyCallback;

    //---------------------------------------------------------------
    //---------------------------------------------------------------
    //
    public void gravity(double y) {
        JumpGameModel jumpGameModel = (JumpGameModel) model;
        jumpGameModel.updateGravity(y); //重力の更新
        jumpGameModel.updateGroundFlag();//地面の判定切り替え
    }

    //---------------------------------------------------------------
    // 初期化
    public void init() {
        // view
        views.add(new BackgroundRenderer("resources/bg.png"));
        views.add(new TileMapRenderer(model));
        views.add(new PlayerRenderer("resources/JCasC.png"));
    }

    //---------------------------------------------------------------
    // 更新処理
    public void update(long window) {

        // Viewの更新
        for (IView view : views) {
            view.update(model);
            view.display();
        }

        // Modelの更新
        // Space キーのインプット
        glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
            @Override
            public void invoke(long window, int key, int scancode, int action, int mods) {
                if (key == GLFW_KEY_SPACE && action != GLFW_PRESS) {
                    JumpGameModel jumpGameModel = (JumpGameModel) model;
                    jumpGameModel.jump();
//                    System.out.println("jumping");
                }
            }
        });

        gravity(-256); //重力
    }

    //---------------------------------------------------------------
    // デストラクタのような処理
    public void delete() {
        for (IView view : views) view.delete();
    }

    //---------------------------------------------------------------

}