Newer
Older
JumpingGame / java / JumpingGame.java
NoranekoFelician on 10 Jun 2 KB Test Commit
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<IView> 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();
        }
    }

}