All files / games/roguelike/src/game/screens play.ts

100% Statements 58/58
100% Branches 12/12
100% Functions 8/8
100% Lines 55/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139  11x 11x 11x 11x 11x 11x 11x 11x     21x 21x 21x     1x     1x     6x   6x 600x     6x           11x 11x 11x 11x 800x   19200x 19200x     11x           11x     14x 14x 13x 13x     1x 1x       6x 6x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x       13x 5x 5x   13x   14x       11x   11x   11x   11x 11x     11x                                                          
import { Command } from "jga-patterns-command";
 
import { Keys } from "jga-games-commands";
import { DisplayInterface } from "jga-games-display";
import { ComponentName, DrawSystem, Engine, Player, PositionComponent } from "jga-games-engine";
import { Directions } from "jga-games-maps";
import { InputDataInterface, Screen } from "jga-games-screens";
import { Fungus } from "../engine";
import { MoveCommand, RollCommand } from "../commands";
 
export class PlayScreen extends Screen {
    private player: Player;
 
    public constructor(engine: Engine, display: DisplayInterface) {
        super("Play", engine, display);
 
        this.player = engine.getPlayer();
        this.display = display;
    }
 
    public getCurrentX(): number {
        return (this.player.getComponent(ComponentName.POSITION) as PositionComponent).getPosition().x;
    }
 
    public getCurrentY(): number {
        return (this.player.getComponent(ComponentName.POSITION) as PositionComponent).getPosition().y;
    }
 
    public enter(): void {
        super.enter();
 
        // add Fungus
        for (let i = 0; i < 100; i++) {
            this.engine.addEntityAtRandomPosition(new Fungus(`fungus-${i}`));
        }
 
        // start engine
        this.engine.mainLoop();
    }
 
    public exit(): void {
        //
    }
 
    public render(): void {
        const topLeftValues = this.getTopLeftValues(this.display.getWidth(), this.display.getHeight());
        const topLeftX = topLeftValues[0];
        const topLeftY = topLeftValues[1];
 
        for (let x = topLeftX; x < topLeftX + this.display.getWidth(); x++) {
            for (let y = topLeftY; y < topLeftY + this.display.getHeight(); y++) {
                // Fetch the glyph for the tile and render it to the screen
                const glyph = this.engine.getMap().getTile({ x, y }).getGlyph();
                this.display.draw({ x: x - topLeftX, y: y - topLeftY }, glyph);
            }
        }
 
        this.engine.process([
            new DrawSystem(this.engine.getEntities(), this.display, {
                x: topLeftX,
                y: topLeftY,
            }),
        ]);
 
        this.engine.lock();
    }
 
    public handleInput(inputType: string, inputData: InputDataInterface): string | null {
        let result: string | null = null;
 
        if (inputType === "keydown") {
            let command: Command | undefined = undefined;
 
            switch (inputData.code) {
                case Keys.ESCAPE: {
                    // If escape is pressed, go to lose screen
                    result = "lose";
                    break;
                }
                case Keys.RETURN: {
                    // If enter is pressed, go to the win screen
                    result = "win";
                    break;
                }
                case Keys.UP: {
                    command = new MoveCommand(this.player, Directions.UP, this.engine);
                    break;
                }
                case Keys.DOWN: {
                    command = new MoveCommand(this.player, Directions.DOWN, this.engine);
                    break;
                }
                case Keys.LEFT: {
                    command = new MoveCommand(this.player, Directions.LEFT, this.engine);
                    break;
                }
                case Keys.RIGHT: {
                    command = new MoveCommand(this.player, Directions.RIGHT, this.engine);
                    break;
                }
                case Keys.ROLL: {
                    command = new RollCommand();
                    break;
                }
                default:
            }
 
            if (undefined !== command) {
                const cmdResult = this.engine.execute(command);
                this.notify(cmdResult.message);
            }
 
            this.engine.unlock();
        }
 
        return result;
    }
 
    public getTopLeftValues(displayWidth: number, displayHeight: number): number[] {
        // Make sure the x-axis doesn't go to the left of the left bound
        let topLeftX = Math.max(
            0,
            (this.player.getComponent(ComponentName.POSITION) as PositionComponent).getPosition().x - displayWidth / 2,
        );
        // Make sure we still have enough space to fit an entire game screen
        topLeftX = Math.min(topLeftX, this.engine.getMap().getWidth() - displayWidth);
 
        // Make sure the y-axis doesn't above the top bound
        let topLeftY = Math.max(
            0,
            (this.player.getComponent(ComponentName.POSITION) as PositionComponent).getPosition().y - displayHeight / 2,
        );
        // Make sure we still have enough space to fit an entire game screen
        topLeftY = Math.min(topLeftY, this.engine.getMap().getHeight() - displayHeight);
 
        return [topLeftX, topLeftY];
    }
}