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

100% Statements 65/65
100% Branches 15/15
100% Functions 10/10
100% Lines 63/63

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 1668x   8x   8x 8x 8x   8x           26x   26x 26x   26x       6x       6x       15x   15x               6x 6x 6x   6x 480x   11520x 11520x       6x                 14x   14x 13x     1x 1x       6x 6x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x           14x       9x   2x 2x     2x 2x     2x 2x     2x 2x               6x   6x     6x         6x   6x       8x   8x             8x         8x      
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 { Directions, GameMap, PositionInterface } from "jga-games-maps";
import { InputDataInterface, Screen } from "jga-games-screens";
 
export class PlayScreen extends Screen {
    private map: GameMap;
    private player: Player;
    private entities: Map<string, Entity>;
 
    public constructor(engine: Engine, map: GameMap) {
        super("Play", engine);
 
        this.player = engine.getPlayer();
        this.entities = engine.getEntities();
 
        this.map = map;
    }
 
    public getCurrentX(): number {
        return this.player.getComponent(ComponentName.POSITION).getPosition().x;
    }
 
    public getCurrentY(): number {
        return this.player.getComponent(ComponentName.POSITION).getPosition().y;
    }
 
    public enter(): void {
        super.enter();
 
        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.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: {
                    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)]);
    }
}