All files / games/engine/src/systems spread.ts

55% Statements 11/20
33.33% Branches 3/9
100% Functions 3/3
55% Lines 11/20

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  16x 16x   16x 16x 16x     2x     1x 1x 1x 1x                                                       16x                              
import * as _ from "lodash-es";
 
// import { PositionInterface } from "jga-games-maps";
import { random } from "jga-algorithms-random";
 
import { ComponentName, PositionComponent, SpreadComponent } from "../components";
 
import { Entity } from "../entities";
import { World } from "../world";
import { System } from "./system";
 
export class SpreadSystem extends System {
    public constructor(entities: Map<string, Entity>) {
        super(entities, [ComponentName.SPREAD]);
    }
I
    public run(world: World): void {
        return this.entities.forEach((entity): void => {
            const spreadComponent: SpreadComponent = entity.getComponent(ComponentName.SPREAD) as SpreadComponent;
            const positionComponent: PositionComponent = entity.getComponent(
                ComponentName.POSITION,
            ) as PositionComponent;

            if (spreadComponent.getRemainingGrowth() > 0 && random(0, 100) <= 10) {
                // Generate the coordinates of a random adjacent square by
                // generating an offset between [-1, 0, 1] for both the x and
                // y directions. To do this, we generate a number from 0-2 and then
                // subtract 1.
                const xOffset = random(0, 2) - 1;
                const yOffset = random(0, 2) - 1;
 
                const xSpread = positionComponent.getPosition().x + xOffset;
                const ySpread = positionComponent.getPosition().y + yOffset;
 
                // Make sure we aren't trying to spawn on the same tile as us
                if ((xOffset != 0 || yOffset != 0) && world.isEmptyFloor({ x: xSpread, y: ySpread })) {
                    // Check if we can actually spawn at that location, and if so
                    // then we grow!
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const spreadEntity = world
                        .getEntityFactory()
                        .create(
                            `${entity.getId()}-spread-${spreadComponent.getRemainingGrowth()}`,
                            spreadComponent.getSpreadedName(),
                        );
 
                    (spreadEntity.getComponent(ComponentName.POSITION) as PositionComponent).setPosition({
                        x: xSpread,
                        y: ySpread,
                    });
                    (spreadEntity.getComponent(ComponentName.SPREAD) as SpreadComponent).spread();
 
                    world.addEntity(spreadEntity);
                }
            }
        });
    }
}