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

40.29% Statements 27/67
75% Branches 3/4
100% Functions 2/2
40.29% Lines 27/67

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 681x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 2x 10x 10x 10x 10x 10x 10x 10x 10x 10x                                                                                 2x 2x 1x  
import { random } from "@jga/algorithms";
 
import { ComponentName, isPositionComponent, isSpreadComponent } 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, ComponentName.POSITION]);
    }
 
    public run(world: World): void {
        return this.entities.forEach((entity): void => {
            const spreadComponent = entity.getComponent(ComponentName.SPREAD);
            const positionComponent = entity.getComponent(ComponentName.POSITION);
 
            if (
                isSpreadComponent(spreadComponent) &&
                isPositionComponent(positionComponent) &&
                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(),
                        );

                    const spreadEntityPosition = spreadEntity.getComponent(ComponentName.POSITION);

                    if (isPositionComponent(spreadEntityPosition)) {
                        spreadEntityPosition.setPosition({
                            x: xSpread,
                            y: ySpread,
                        });
                    }

                    const spreadEntitySpread = spreadEntity.getComponent(ComponentName.SPREAD);

                    if (isSpreadComponent(spreadEntitySpread)) {
                        spreadEntitySpread.spread();
                    }

                    world.addEntity(spreadEntity);
                }
            }
        });
    }
}