com.telinc1.rpjg.module.ModuleMap.java Source code

Java tutorial

Introduction

Here is the source code for com.telinc1.rpjg.module.ModuleMap.java

Source

/*
 * Copyright 2015-2016 Telinc1
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.telinc1.rpjg.module;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import com.telinc1.rpjg.Game;
import com.telinc1.rpjg.map.Map;
import com.telinc1.rpjg.map.event.Event;
import com.telinc1.rpjg.map.event.EventTextured;
import com.telinc1.rpjg.map.event.player.Player;
import com.telinc1.rpjg.module.gui.Gui;
import com.telinc1.rpjg.reference.ControlKeys;
import com.telinc1.rpjg.reference.GameOptions;
import com.telinc1.rpjg.script.ScriptEngine;
import com.telinc1.rpjg.texture.Color;
import com.telinc1.rpjg.texture.FontRenderer;
import com.telinc1.rpjg.texture.TextureLoader;
import com.telinc1.rpjg.tileset.tile.Tile;
import com.telinc1.rpjg.util.DrawingUtils;
import com.telinc1.rpjg.util.EnumDirection;

public class ModuleMap extends Module {
    @Override
    public int getWidth() {
        return Display.getWidth();
    }

    @Override
    public int getHeight() {
        return Display.getHeight();
    }

    @Override
    public boolean isModal() {
        return false;
    }

    @Override
    public boolean canHide(Module module) {
        return false;
    }

    @Override
    public boolean canBeHiddenBy(Module module) {
        return true;
    }

    @Override
    public void update() {
        super.update();

        // Get a reference to the game.
        Game game = Game.getGame();

        // Get a reference to the player.
        Player player = game.getPlayer();

        // Check for a change in the facing variable.
        if (player.getFacingDirection().ordinal() != ScriptEngine.variables[ScriptEngine.PLAYER_FACING]) {
            player.setFacingDirection(EnumDirection.values()[ScriptEngine.variables[ScriptEngine.PLAYER_FACING]]);
        }

        if (ScriptEngine.locked) {
            return;
        }

        if (game.getOpenMessage() != null && game.getOpenMessage().getType().shouldBlock()) {
            return;
        }

        // Update the map elements.
        player.update();

        for (Event event : game.getMap().getEvents()) {
            event.update();
        }

        // Collect player input.
        if (Keyboard.isKeyDown(ControlKeys.MOVE_UP)) {
            player.move(EnumDirection.NORTH);
            ScriptEngine.variables[ScriptEngine.PLAYER_FACING] = (short) EnumDirection.NORTH.getDirection();
        } else if (Keyboard.isKeyDown(ControlKeys.MOVE_RIGHT)) {
            player.move(EnumDirection.EAST);
            ScriptEngine.variables[ScriptEngine.PLAYER_FACING] = (short) EnumDirection.EAST.getDirection();
        } else if (Keyboard.isKeyDown(ControlKeys.MOVE_DOWN)) {
            player.move(EnumDirection.SOUTH);
            ScriptEngine.variables[ScriptEngine.PLAYER_FACING] = (short) EnumDirection.SOUTH.getDirection();
        } else if (Keyboard.isKeyDown(ControlKeys.MOVE_LEFT)) {
            player.move(EnumDirection.WEST);
            ScriptEngine.variables[ScriptEngine.PLAYER_FACING] = (short) EnumDirection.WEST.getDirection();
        }
    }

    @Override
    public void renderGraphics() {
        // Get references to our map and player.
        Player player = Game.getGame().getPlayer();
        Map map = Game.getGame().getMap();

        // Render the current map.
        GL11.glPushMatrix();

        // Standard centering algorithm, just need to add half a tile
        // to completely center because of the 16:9 resolution.
        int cameraX = (player.getPixelX() - Display.getWidth() / 2) + (GameOptions.GRID_SIZE / 2);
        int cameraY = (player.getPixelY() - Display.getHeight() / 2) + (GameOptions.GRID_SIZE / 2);

        GL11.glTranslatef(-cameraX, -cameraY, 0);

        // Render the map's tiles.
        DrawingUtils.bindTexture(TextureLoader.tilesetTexture);
        DrawingUtils.startDrawing(GL11.GL_QUADS);
        for (int x = 0; x < map.getWidth(); x++) {
            for (int y = 0; y < map.getHeight(); y++) {
                Tile tile = map.getTileAt(x, y);
                // XXX: Is there a way to make this look nicer?
                DrawingUtils.drawTexturedTile(tile.getDisplayModifier().getTextureID(tile, map, x, y), x, y);
            }
        }
        DrawingUtils.stopDrawing();

        // Render all events, including the player.
        DrawingUtils.drawTexturedEvent(player);

        for (Event event : map.getEvents()) {
            if (event instanceof EventTextured) {
                DrawingUtils.drawTexturedEvent((EventTextured) event);
            }
        }

        DrawingUtils.stopDrawing();
        GL11.glPopMatrix();

        // Render the version in the top corner, just for information purposes.
        FontRenderer.drawString(String.format("%s-%s v.%s", GameOptions.GAME_NAME, GameOptions.GAME_BUILD,
                GameOptions.GAME_VERSION), FontRenderer.small, 3, 3, Color.BLACK);
    }

    @Override
    public void keyPressed(int key) {
        for (Gui gui : this.getGUIs()) {
            if (gui.keyPressed(key)) {
                return;
            }
        }

        Game game = Game.getGame();

        if (game.getOpenMessage() != null && game.getOpenMessage().getType().shouldBlock()) {
            return;
        }

        if (key == ControlKeys.CONFIRM) {
            game.getPlayer().interact();
        }
    }
}