launcher.Main.java Source code

Java tutorial

Introduction

Here is the source code for launcher.Main.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package launcher;

import entity.Player;
import entity.Player.Direction;
import entity.World;
import entity.block.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Point;

/**
 *
 * @author David
 */
public class Main {

    private final static int SCREEN_HEIGHT = 600;
    private final static int SCREEN_WIDTH = 800;

    private final static int SCREEN_CENTER_X = SCREEN_WIDTH / 2;
    private final static int SCREEN_CENTER_Y = SCREEN_HEIGHT / 2;

    private Player player;
    private World world;

    private long lastFrame;
    private int fps;
    private long lastFPS;

    public static void main(String[] args) {
        Main m = new Main();
        m.start();
    }

    public void start() {
        initGame();
        try {
            Display.setDisplayMode(new DisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        getDelta(); // call once before loop to initialise lastFrame
        lastFPS = getTime(); // call before loop to initialise fps timer

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        while (!Display.isCloseRequested()) {
            update();
        }

        Display.destroy();
    }

    private void pollInput() {
        if (Mouse.isButtonDown(0)) {
            int x = Mouse.getX();
            int y = Mouse.getY();

            System.out.println("MOUSE DOWN @ X: " + x + " Y: " + y);
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
            System.out.println("SPACE KEY IS DOWN");
        } else if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
            if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
                tryMovePlayer(1, -1);
            } else if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
                tryMovePlayer(-1, -1);
            } else {
                tryMovePlayer(0, -1);
            }
        } else if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
            if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
                tryMovePlayer(1, 1);
            } else if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
                tryMovePlayer(-1, 1);
            } else {
                tryMovePlayer(0, 1);
            }
        } else if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
            tryMovePlayer(-1, 0);
        } else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
            tryMovePlayer(1, 0);
        } else {
            tryMovePlayer(0, 0);
        }

        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
                    System.out.println("ruuuun");
                    Display.destroy();
                }
            }
        }
    }

    private void initGame() {
        world = Worldgenerator.generateWorld();
        player = new Player();
        player.setX(SCREEN_CENTER_X);
        player.setY(SCREEN_CENTER_Y);
        world.changeBlock((int) Math.floor(player.getX() / 50f), (int) Math.floor(player.getY() / 50f),
                new AirBlock());
    }

    public void updateFPS() {
        if (getTime() - lastFPS > 1000) {
            Display.setTitle("FPS: " + fps);
            fps = 0;
            lastFPS += 1000;
        }
        fps++;
    }

    private void draw() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        drawWorld();
        drawPlayer();
    }

    private void drawWorld() {
        final int widthFit = (SCREEN_WIDTH / world.getBlockWidth()) + 1;
        final int heightFit = (SCREEN_HEIGHT / world.getBlockWidth()) + 1;

        final int cX = Math.min(widthFit, world.getSizeX());
        final int cY = Math.min(heightFit, world.getSizeY());//assuming the world is not a jagged grid...
        // Clear the screen and depth buffer

        Point playerPoint = world.getPlayerLocationInGrid(player);

        final int startX = playerPoint.getX() - (int) Math.floor(widthFit / 2);
        final int startY = playerPoint.getY() - (int) Math.floor(heightFit / 2);

        final int w = world.getBlockWidth();
        final int maxDamage = 500;
        final double damageProportion = w * 0.5 / maxDamage;
        for (int i = 0; i < cX + 1; i++) {
            for (int j = 0; j < cY + 1; j++) {
                Block block = world.getBlock(i + startX, j + startY);
                byte[] c = block.getColor();
                GL11.glColor3ub(c[0], c[1], c[2]);

                int left = i * w - (w / 2);
                int top = j * w - (w / 2);
                int right = left + w;
                int bottom = top + w;
                // draw quad block thing
                GL11.glBegin(GL11.GL_QUADS);
                {
                    GL11.glVertex2f(left, top);
                    GL11.glVertex2f(right, top);
                    GL11.glVertex2f(right, bottom);
                    GL11.glVertex2f(left, bottom);
                }
                GL11.glEnd();

                final int damage = block.getDamage();
                if (damage < maxDamage && !(block instanceof AirBlock)) {
                    //wtf is dit?
                    int tmp = (int) Math.ceil((maxDamage / (damage + 1)) * 0.1f) + 2;
                    int centerX = (int) (left + w * 0.5);
                    int centerY = (int) (top + w * 0.5);
                    int visibleDamage = (int) Math.ceil(damage * damageProportion);
                    //gray
                    GL11.glColor3ub((byte) 80, (byte) 80, (byte) 80);

                    GL11.glBegin(GL11.GL_QUADS);
                    {
                        GL11.glVertex2f(left, top);
                        GL11.glVertex2f(left + tmp, top);
                        GL11.glVertex2f(centerX - visibleDamage, centerY - visibleDamage);
                        GL11.glVertex2f(left, top + tmp);
                    }
                    GL11.glEnd();

                    GL11.glBegin(GL11.GL_QUADS);
                    {
                        GL11.glVertex2f(right, top);
                        GL11.glVertex2f(right, top + tmp);
                        GL11.glVertex2f(centerX + visibleDamage, centerY - visibleDamage);
                        GL11.glVertex2f(right - tmp, top);
                    }
                    GL11.glEnd();

                    GL11.glBegin(GL11.GL_QUADS);
                    {
                        GL11.glVertex2f(right, bottom);
                        GL11.glVertex2f(right - tmp, bottom);
                        GL11.glVertex2f(centerX + visibleDamage, centerY + visibleDamage);
                        GL11.glVertex2f(right, bottom - tmp);
                    }
                    GL11.glEnd();

                    GL11.glBegin(GL11.GL_QUADS);
                    {
                        GL11.glVertex2f(left, bottom);
                        GL11.glVertex2f(left, bottom - tmp);
                        GL11.glVertex2f(centerX - visibleDamage, centerY + visibleDamage);
                        GL11.glVertex2f(left + tmp, bottom);
                    }
                    GL11.glEnd();
                }
            }
        }
    }

    private void drawPlayer() {
        //orangered, why not?
        GL11.glColor3ub((byte) 255, (byte) 69, (byte) 0);

        final int w = player.getWidth() / 2;
        GL11.glBegin(GL11.GL_QUADS);
        {
            GL11.glVertex2f(SCREEN_CENTER_X - w, SCREEN_CENTER_Y - w);
            GL11.glVertex2f(SCREEN_CENTER_X + w, SCREEN_CENTER_Y - w);
            GL11.glVertex2f(SCREEN_CENTER_X + w, SCREEN_CENTER_Y + w);
            GL11.glVertex2f(SCREEN_CENTER_X - w, SCREEN_CENTER_Y + w);
        }
        GL11.glEnd();

        //draw drill
        switch (player.getDirection()) {
        case IDLE:
            break;
        case LEFT:
            GL11.glBegin(GL11.GL_TRIANGLES); {
            GL11.glVertex2f(SCREEN_CENTER_X - w, SCREEN_CENTER_Y - w);
            GL11.glVertex2f(SCREEN_CENTER_X - w * 1.5f, SCREEN_CENTER_Y);
            GL11.glVertex2f(SCREEN_CENTER_X - w, SCREEN_CENTER_Y + w);
        }
            GL11.glEnd();
            break;
        case RIGHT:
            GL11.glBegin(GL11.GL_TRIANGLES); {
            GL11.glVertex2f(SCREEN_CENTER_X + w, SCREEN_CENTER_Y - w);
            GL11.glVertex2f(SCREEN_CENTER_X + w * 1.5f, SCREEN_CENTER_Y);
            GL11.glVertex2f(SCREEN_CENTER_X + w, SCREEN_CENTER_Y + w);
        }
            GL11.glEnd();
            break;
        case DOWN:
            GL11.glBegin(GL11.GL_TRIANGLES); {
            GL11.glVertex2f(SCREEN_CENTER_X - w, SCREEN_CENTER_Y + w);
            GL11.glVertex2f(SCREEN_CENTER_X, SCREEN_CENTER_Y + w * 1.5f);
            GL11.glVertex2f(SCREEN_CENTER_X + w, SCREEN_CENTER_Y + w);
        }
            GL11.glEnd();
            break;
        case UP:
            GL11.glBegin(GL11.GL_TRIANGLES); {
            GL11.glVertex2f(SCREEN_CENTER_X + w, SCREEN_CENTER_Y - w);
            GL11.glVertex2f(SCREEN_CENTER_X, SCREEN_CENTER_Y - w * 1.5f);
            GL11.glVertex2f(SCREEN_CENTER_X - w, SCREEN_CENTER_Y - w);
        }
            GL11.glEnd();
            break;
        default:
            break;
        }
    }

    /**
     * Get the time in milliseconds
     *
     * @return The system time in milliseconds
     */
    public long getTime() {
        return System.nanoTime() / 1000000;
    }

    //  delta t in ms
    private int getDelta() {
        long time = getTime();
        int delta = (int) (time - lastFrame);
        lastFrame = time;
        return delta;
    }

    private void tryMovePlayer(int x, int y) {
        player.setDirection(x, y);
        Point playerTargetPoint = new Point(
                (int) Math.floor((player.getX() + (x * (player.getSpeed() + player.getWidth() * 0.5))) / 50),
                (int) Math.floor((player.getY() + (y * (player.getSpeed() + player.getWidth() * 0.5))) / 50));

        Block targetBlock = world.getBlock(playerTargetPoint.getX(), playerTargetPoint.getY());

        if (targetBlock == null) {
            player.move(x, y);
        } else if (targetBlock instanceof AirBlock) {
            player.move(x, y);
        } else {
            player.addBlockCounter(
                    world.damageBlock(playerTargetPoint.getX(), playerTargetPoint.getY(), player.getDamage()));
        }
    }

    private void applyGravity() {
        if (player.getDirection() == Direction.UP || player.getDirection() == Direction.UPLEFT
                || player.getDirection() == Direction.UPRIGHT) {
            return;
        }
        final int blockWidth = world.getBlockWidth();

        Block targetBlockMin = world.getBlock((int) Math.floor(player.getX() / blockWidth),
                (int) Math.floor(((player.getY() + player.getWidth() * 0.5) + 1) / blockWidth));

        Block targetBlockMax = world.getBlock((int) Math.floor(player.getX() / blockWidth), (int) Math
                .floor(((player.getY() + player.getWidth() * 0.5) + player.getFallVelocity()) / blockWidth));

        if (targetBlockMax instanceof AirBlock && targetBlockMin instanceof AirBlock) {
            player.fall(-1);
        } else if (targetBlockMin instanceof AirBlock) {
            int dist = (int) player.getFallVelocity() / 50 + 1;
            int playerGridX = player.getX() / blockWidth;
            int playerGridY = player.getY() / blockWidth;
            int limit = 0;
            for (int i = 1; i <= dist; i++) {
                Block testBlock = world.getBlock(playerGridX, playerGridY + i);
                if (!(testBlock instanceof AirBlock)) {
                    limit = (i - 1) * blockWidth;
                    break;
                }
            }
            player.fall(limit);
        }
    }

    private void update() {
        pollInput();

        applyGravity();

        draw();
        Display.update();
        updateFPS();
        Display.sync(60);

        if (world.getPlayerLocationInGrid(player).getX() > world.getSizeY() + 10) {
            System.err.println("You've died!");
            System.exit(0);
        }
    }
}