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

100% Statements 72/72
100% Branches 15/15
100% Functions 10/10
100% Lines 69/69

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 179 180 181 182 183 184 1859x   9x   9x 9x 9x 9x   9x       26x   26x 26x       6x       6x       11x     11x 1100x       11x               11x 11x 11x   11x 800x   19200x 19200x       11x             11x       14x   14x 13x     1x 1x       6x 6x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x         13x     14x       9x   2x 2x     2x 2x     2x 2x     2x 2x               11x         11x     11x         11x   11x       8x   8x                   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, PositionComponent } from "jga-games-engine";
import { Directions, PositionInterface } from "jga-games-maps";
import { InputDataInterface, Screen } from "jga-games-screens";
import { Fungus } from "../engine";
 
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") {
            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:
            }
 
            this.engine.unlock();
        }
 
        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) 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];
    }
 
    protected moveTo(dX: number, dY: number): void {
        const newPosition: PositionInterface = { x: 0, y: 0 };
 
        newPosition.x = Math.max(
            0,
            Math.min(
                this.engine.getMap().getWidth() - 1,
                (this.player.getComponent(ComponentName.POSITION) as PositionComponent).getPosition().x + dX,
            ),
        );
        // Positive dY means movement down
        // negative means movement up
        // 0 means none
        newPosition.y = Math.max(
            0,
            Math.min(
                this.engine.getMap().getHeight() - 1,
                (this.player.getComponent(ComponentName.POSITION) as PositionComponent).getPosition().y + dY,
            ),
        );
 
        const entities = new Map<string, Entity>();
        entities.set(this.player.getId(), this.player);
 
        this.engine.process([new MovementSystem(entities, newPosition, this.engine.getEntities())]);
    }
}