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

93.33% Statements 56/60
86.66% Branches 13/15
100% Functions 7/7
94.73% Lines 54/57

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156  12x                           12x 12x   12x       18x   18x 18x       1x   1x       1x   1x       3x       3x 30x       3x       1x 1x 1x     1x                   1x               1x   1x       9x   9x 8x   8x     1x 1x       1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x         8x 5x 5x     8x     9x       1x   1x   1x   1x   1x     1x   1x   1x     1x      
import { Command } from "@jga/patterns";
import {
    InputDataInterface,
    Screen,
    Directions,
    DisplayInterface,
    Keys,
    ComponentName,
    DrawSystem,
    World,
    Player,
    isPositionComponent,
    SpreadSystem,
} from "@jga/games";
 
import { Fungus } from "../engine";
import { MoveCommand, RollCommand } from "../commands";
 
export class PlayScreen extends Screen {
    private readonly player: Player;
 
    public constructor(engine: World, display: DisplayInterface) {
        super("Play", engine, display);
 
        this.player = engine.getPlayer();
        this.display = display;
    }
 
    public getCurrentX(): number {
        const playerPosition = this.player.getComponent(ComponentName.POSITION);
 
        return isPositionComponent(playerPosition) ? playerPosition.getPosition().x : 0;
    }
 
    public getCurrentY(): number {
        const playerPosition = this.player.getComponent(ComponentName.POSITION);
 
        return isPositionComponent(playerPosition) ? playerPosition.getPosition().y : 0;
    }
 
    public enter(): void {
        super.enter();
 
        // add Fungus
        // eslint-disable-next-line no-loops/no-loops
        for (let i = 0; i < 10; i++) {
            this.engine.addEntityAtRandomPosition(new Fungus(`fungus-${i}`));
        }
 
        // start engine
        this.engine.mainLoop();
    }
 
    public render(): void {
        const topLeftValues = this.getTopLeftValues(this.display.getWidth(), this.display.getHeight());
        const topLeftX = topLeftValues[0];
        const topLeftY = topLeftValues[1];
 
        // eslint-disable-next-line no-loops/no-loops
        for (let x = topLeftX; x < topLeftX + this.display.getWidth(); x++) {
            // eslint-disable-next-line no-loops/no-loops
            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);
            }
        }
 
        // register systems
        this.engine.registerSystems([
            new SpreadSystem(this.engine.getEntities()),
            new DrawSystem(this.engine.getEntities(), this.display, {
                x: topLeftX,
                y: topLeftY,
            }),
        ]);
 
        this.engine.process();
 
        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[] {
        const playerPosition = this.player.getComponent(ComponentName.POSITION);
 
        let topLeftValues = [0, 0];
 
        if (isPositionComponent(playerPosition)) {
            // Make sure the x-axis doesn't go to the left of the left bound
            let topLeftX = Math.max(0, playerPosition.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, playerPosition.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);
 
            topLeftValues = [topLeftX, topLeftY];
        }
 
        return topLeftValues;
    }
}