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

67.14% Statements 47/70
33.33% Branches 5/15
81.82% Functions 9/11
66.18% Lines 45/68

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 1796x   6x   6x 6x 6x 6x   6x           11x   11x 11x       6x       6x       10x       6x   6x 6x   6x 6x           6x               1x 1x 1x   1x 80x   1920x 1920x       1x                                                                                                               5x   1x 1x     1x 1x     1x 1x     1x 1x               1x   1x     1x         1x   1x       4x   4x             4x         4x      
import { Roll } from "jga-games-opend6-dices";
 
import { Keys } from "jga-games-commands";
import { DisplayInterface } from "jga-games-display";
import { ComponentName, DrawSystem, Engine, Entity, MovementSystem, Player } from "jga-games-engine";
import { CellularMapGenerator, Directions, GameMap, PositionInterface } from "jga-games-maps";
import { InputDataInterface, Screen } from "jga-games-screens";
import { TileFactory } from "jga-games-tiles";
 
export class PlayScreen extends Screen {
    private map: GameMap;
    private player: Player;
    private entities: Map<string, Entity>;
 
    public constructor(engine: Engine) {
        super("Play", engine);
 
        this.player = engine.getPlayer();
        this.entities = engine.getEntities();
    }
 
    public getCurrentX(): number {
        return this.player.getComponent(ComponentName.POSITION).getPosition().x;
    }
 
    public getCurrentY(): number {
        return this.player.getComponent(ComponentName.POSITION).getPosition().y;
    }
 
    public getMap(): GameMap {
        return this.map;
    }
 
    public enter(): void {
        super.enter();
 
        const mapWidth = 500;
        const mapHeight = 500;
 
        const generator = new CellularMapGenerator(mapWidth, mapHeight, new TileFactory());
        this.map = new GameMap({
            generator,
            height: mapHeight,
            width: mapWidth,
        });
 
        this.player.getComponent(ComponentName.POSITION).setPosition(this.map.getRandomFloorPosition());
    }
 
    public exit(): void {
        //
    }
 
    public render(display: DisplayInterface): void {
        const topLeftValues = this.getTopLeftValues(display.getWidth(), display.getHeight());
        const topLeftX = topLeftValues[0];
        const topLeftY = topLeftValues[1];
 
        for (let x = topLeftX; x < topLeftX + display.getWidth(); x++) {
            for (let y = topLeftY; y < topLeftY + display.getHeight(); y++) {
                // Fetch the glyph for the tile and render it to the screen
                const glyph = this.map.getTile({ x, y }).getGlyph();
                display.draw({ x: x - topLeftX, y: y - topLeftY }, glyph);
            }
        }
 
        this.engine.process([
            new DrawSystem(this.engine.getEntities(), display, {
                x: topLeftX,
                y: topLeftY,
            }),
        ]);
    }
 
    public handleInput(inputType: string, inputData: InputDataInterface): string | null {
        let result: string | null = null;
 
        if (inputType === "keydown") {
            switch (inputData.keyCode) {
                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: {
                    this.move(Directions.UP);
                    this.notify(`Move ${Directions.UP}`);
                    break;
                }
                case Keys.DOWN: {
                    this.move(Directions.DOWN);
                    this.notify(`Move ${Directions.DOWN}`);
                    break;
                }
                case Keys.LEFT: {
                    this.move(Directions.LEFT);
                    this.notify(`Move ${Directions.LEFT}`);
                    break;
                }
                case Keys.RIGHT: {
                    this.move(Directions.RIGHT);
                    this.notify(`Move ${Directions.RIGHT}`);
                    break;
                }
                case Keys.ROLL: {
                    const roll = new Roll();
                    this.notify(`Roll => ${roll.getResult()}`);
                    break;
                }
                default:
            }
        }
 
        return result;
    }
 
    public move(direction: string): void {
        switch (direction) {
            case Directions.DOWN: {
                this.moveTo(0, 1);
                break;
            }
            case Directions.LEFT: {
                this.moveTo(-1, 0);
                break;
            }
            case Directions.RIGHT: {
                this.moveTo(1, 0);
                break;
            }
            case Directions.UP: {
                this.moveTo(0, -1);
                break;
            }
            default:
        }
    }
 
    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).getPosition().x - displayWidth / 2);
        // Make sure we still have enough space to fit an entire game screen
        topLeftX = Math.min(topLeftX, this.map.getWidth() - displayWidth);
 
        // Make sure the y-axis doesn't above the top bound
        let topLeftY = Math.max(
            0,
            this.player.getComponent(ComponentName.POSITION).getPosition().y - displayHeight / 2,
        );
        // Make sure we still have enough space to fit an entire game screen
        topLeftY = Math.min(topLeftY, this.map.getHeight() - displayHeight);
 
        return [topLeftX, topLeftY];
    }
 
    protected moveTo(dX: number, dY: number): void {
        const newPosition: PositionInterface = { x: 0, y: 0 };
 
        newPosition.x = Math.max(
            0,
            Math.min(this.map.getWidth() - 1, this.player.getComponent(ComponentName.POSITION).getPosition().x + dX),
        );
        // Positive dY means movement down
        // negative means movement up
        // 0 means none
        newPosition.y = Math.max(
            0,
            Math.min(this.map.getHeight() - 1, this.player.getComponent(ComponentName.POSITION).getPosition().y + dY),
        );
 
        this.engine.process([new MovementSystem(this.engine.getEntities(), newPosition, this.map)]);
    }
}