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

100% Statements 12/12
100% Branches 0/0
100% Functions 4/4
100% Lines 12/12

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 657x                   7x                               7x   48x         48x   48x 48x   48x 48x       13x       6x       3x                          
import {
    Observable,
    ObservableInterface,
    ObserverInterface,
} from "jga-patterns-observer";
 
import { Keys } from "jga-games-commands";
import { DisplayInterface } from "jga-games-display";
import { Engine, Entity, Player } from "jga-games-engine";
 
import { ScreenObserver } from "../index";
 
export interface InputDataInterface {
    keyCode: Keys;
}
 
export interface ScreenInterface {
    enter(): void;
    exit(): void;
    render(display: DisplayInterface): void;
    handleInput(
        inputType?: string,
        inputData?: InputDataInterface,
    ): string | null;
}
 
export abstract class Screen extends Observable
    implements ObservableInterface, ScreenInterface {
    protected observers: ObserverInterface[] = [];
    protected name: string;
    protected engine: Engine;
 
    public constructor(name: string, engine: Engine) {
        super();
 
        this.name = name;
        this.engine = engine;
 
        const observer = new ScreenObserver();
        this.attach(observer);
    }
 
    public enter(): void {
        this.notify(`Enter ${this.name} screen`);
    }
 
    public exit(): void {
        this.notify(`Exit ${this.name} screen`);
    }
 
    public getObservers(): ObserverInterface[] {
        return this.observers;
    }
 
    public abstract render(
        display: DisplayInterface,
        entities?: Entity[],
        player?: Player,
    ): void;
    public abstract handleInput(
        inputType?: string,
        inputData?: InputDataInterface,
    ): string | null;
}