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

80% Statements 24/30
16.67% Branches 1/6
85.71% Functions 6/7
80% Lines 24/30

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 866x   6x       6x                       6x           5x 5x     5x     5x   5x 5x   5x         5x 5x   5x 5x     5x       5x                               1x       4x       3x 3x   3x   3x 3x      
import { Display, DisplayInterface } from "jga-games-display";
 
import { Engine } from "jga-games-engine";
 
import { ScreenInterface } from "jga-games-screens";
 
import {
    GameInterface,
    ScreenFactoryCreateParamsInterface,
    ScreenFactory,
} from "./index";
 
export interface GameInterface {
    getCurrentScreen(): ScreenInterface;
    getDisplay(): DisplayInterface;
    switchScreen(screen: ScreenInterface): void;
}
 
export class Game implements GameInterface {
    protected display: DisplayInterface;
    protected currentScreen: ScreenInterface;
    protected screenFactory: ScreenFactory;
    protected engine: Engine;
 
    private screenHeight = 24;
    private screenWidth = 80;
 
    public constructor() {
        this.engine = new Engine();
 
        // Create player;
        this.engine.createPlayer();
 
        this.display = new Display(this.screenHeight, this.screenWidth);
        this.screenFactory = new ScreenFactory();
 
        const createParams: ScreenFactoryCreateParamsInterface = {
            engine: this.engine,
            type: "start",
        };
 
        this.currentScreen = this.screenFactory.create(createParams);
        this.currentScreen.render(this.display);
 
        const bindEventToScreen = (event: string): void => {
            window.addEventListener(event, this.handleInput(event));
        };
 
        bindEventToScreen("keydown");
    }
 
    public handleInput(event: string): EventListenerOrEventListenerObject {
        return (e): void => {
            if (this.currentScreen !== null) {
                const newScreenName = this.currentScreen.handleInput(event, e);
                if (null !== newScreenName) {
                    const newScreen = this.screenFactory.create(
                        newScreenName,
                        this.engine,
                    );
                    this.switchScreen(newScreen);
                }
                this.currentScreen.render(this.getDisplay());
            }
        };
    }
 
    public getCurrentScreen(): ScreenInterface {
        return this.currentScreen;
    }
 
    public getDisplay(): DisplayInterface {
        return this.display;
    }
 
    public switchScreen(screen: ScreenInterface): void {
        Eif (this.currentScreen !== null) {
            this.currentScreen.exit();
        }
        this.getDisplay().clear();
 
        this.currentScreen = screen;
        this.currentScreen.enter();
    }
}