Package game.scenes

Source Code of game.scenes.LevelScene

package game.scenes;

import engine.classes.Colour;
import engine.geometry.Rectangle;
import engine.geometry.Vector;
import engine.hierarchy.DefaultScene;
import engine.interfaces.Clock;
import engine.interfaces.Keyboard;
import engine.interfaces.Mouse;
import engine.interfaces.RenderTarget;
import engine.interfaces.Keyboard.Key;
import engine.utility.Space;
import game.actors.MetalActor;
import game.actors.PlayerActor;
import game.actors.SignActor;
import game.actors.SoldierActor;
import game.layers.BackgroundLayer;
import game.layers.BlocksLayer;
import game.layers.DynamicLayer;
import game.layers.ForegroundLayer;
import game.terrain.Actor;
import game.terrain.Block;
import game.terrain.Chunk;
import game.terrain.Level;

public final class LevelScene extends DefaultScene {
    private static final Colour BACK = new Colour(192, 255, 255);
    private static final Rectangle SCREEN = new Rectangle(0, 0, 640, 480);

    private Space space;
    private Level level;
    private Chunk[][] chunks;
    private Block[][] blocks;
    private Actor[] actors;
    private int viewX;
    private int viewY;

    public LevelScene(final Level level) {
        this.level = level;
        chunks = this.level.getChunks();
        blocks = this.level.getBlocks();
        actors = this.level.getActors();
        space = new Space(chunks[0].length * Chunk.WIDTH, chunks.length * Chunk.HEIGHT);
    }

    @Override
    public void onAdd() {
        addLayer(new BackgroundLayer(level.getChunks()));
        DynamicLayer d = addLayer(new DynamicLayer());
        addLayer(new ForegroundLayer(level.getChunks()));
        addLayer(new BlocksLayer(level.getBlocks())).setVisible(false);

        for (Actor a : actors) {
            if (a.getName().equals("player")) {
                d.addActor(new PlayerActor(new Vector(Double.valueOf(a.getArgs()[0]), Double.valueOf(a.getArgs()[1]))));
            } else if (a.getName().equals("soldier")) {
                d.addActor(new SoldierActor(new Vector(Double.valueOf(a.getArgs()[0]), Double.valueOf(a.getArgs()[1]))));
            } else if (a.getName().equals("sign")) {
                d.addActor(new SignActor(a.getArgs()[4], new Rectangle(Double.valueOf(a.getArgs()[0]), Double.valueOf(a
                        .getArgs()[1]), Double.valueOf(a.getArgs()[2]), Double.valueOf(a.getArgs()[3]))));
            } else if (a.getName().equals("metal")) {
                d.addActor(new MetalActor(new Rectangle(Double.valueOf(a.getArgs()[0]), Double.valueOf(a.getArgs()[1]),
                        Double.valueOf(a.getArgs()[2]), Double.valueOf(a.getArgs()[3]))));
            } else {
                System.out.println("Unknown Actor in level file: " + a.getName());
            }
        }
    }

    @Override
    public void onBeforeMove(final Keyboard keyboard, final Mouse mouse, final Clock clock) {
        if (keyboard.wasReleased(Key.VK_ESCAPE)) {
            getStage().insertSceneAbove(new FadeThroughBlackScene(new MainMenuScene()), this);
        }
    }

    public void restart() {
        getStage().insertSceneAbove(new FadeThroughBlackScene(new LevelScene(level)), this);
    }

    public void viewFollow(double x, double y, double xBorder, double yBorder) {
        if (viewX > x - xBorder) {
            viewX = (int) (x - xBorder);
        }
        if (viewX + SCREEN.getWidth() < x + xBorder) {
            viewX = (int) (x + xBorder - SCREEN.getWidth());
        }
        if (viewY > y - yBorder) {
            viewY = (int) (y - yBorder);
        }
        if (viewY + SCREEN.getHeight() < y + yBorder) {
            viewY = (int) (y + yBorder - SCREEN.getHeight());
        }

        // Make sure view stays in level
        if (viewX < 0) {
            viewX = 0;
        }
        if (viewX + SCREEN.getWidth() > chunks[0].length * Chunk.WIDTH) {
            viewX = chunks[0].length * Chunk.WIDTH - (int) (SCREEN.getWidth());
        }
        if (viewY < 0) {
            viewY = 0;
        }
        if (viewY + SCREEN.getHeight() > chunks.length * Chunk.HEIGHT) {
            viewY = chunks.length * Chunk.HEIGHT - (int) (SCREEN.getHeight());
        }
    }

    public int getViewX() {
        return viewX;
    }

    public int getViewY() {
        return viewY;
    }

    @Override
    public void onDraw(final RenderTarget target) {
        target.fillRectangle(SCREEN, BACK);

        target.transformTranslate(-viewX, -viewY);
    }

    public Block[][] getBlocks() {
        return blocks;
    }

    public Space getSpace() {
        return space;
    }
}
TOP

Related Classes of game.scenes.LevelScene

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.