From a02f62fe34af65c405b50fabfff2343388b449df Mon Sep 17 00:00:00 2001 From: Alex Ilchenko Date: Wed, 6 Mar 2024 16:40:52 +0100 Subject: [PATCH] feat(engine): added getSerialized function --- .../utils/get-serialized/get-serialized.ts | 87 +++++++++++++++++++ .../engine/utils/get-serialized/index.ts | 1 + 2 files changed, 88 insertions(+) create mode 100644 src/features/engine/utils/get-serialized/get-serialized.ts create mode 100644 src/features/engine/utils/get-serialized/index.ts diff --git a/src/features/engine/utils/get-serialized/get-serialized.ts b/src/features/engine/utils/get-serialized/get-serialized.ts new file mode 100644 index 0000000..443b693 --- /dev/null +++ b/src/features/engine/utils/get-serialized/get-serialized.ts @@ -0,0 +1,87 @@ +import { getArrowSymbol } from '../../../ui/utils/get-arrow-symbol'; +import { VECTOR_ZERO } from '../../constants'; +import { Entity, isDice, isFloor, isMovable } from '../../types/entities'; +import { getIsSameVector } from '../get-is-same-vector'; + +const getSymbol = (entity: Entity) => { + if (isFloor(entity)) { + return entity.direction + ? getArrowSymbol(entity.direction) + : entity.target + ? '!' + : '□'; + } else if (isDice(entity)) { + return getIsSameVector(entity.velocity, VECTOR_ZERO) + ? `${entity.value}` + : getArrowSymbol(entity.velocity); + } else if (isMovable(entity)) { + return getArrowSymbol(entity.velocity); + } +}; + +const getBounds = ({ entities }: { entities: Entity[] }) => { + const topLeft = { x: Number.POSITIVE_INFINITY, y: Number.POSITIVE_INFINITY }; + const bottomRight = { + x: Number.NEGATIVE_INFINITY, + y: Number.NEGATIVE_INFINITY, + }; + + for (const entity of entities) { + if (entity.position.x < topLeft.x) { + topLeft.x = entity.position.x; + } + + if (entity.position.y < topLeft.y) { + topLeft.y = entity.position.y; + } + + if (entity.position.x > bottomRight.x) { + bottomRight.x = entity.position.x; + } + + if (entity.position.y > bottomRight.y) { + bottomRight.y = entity.position.y; + } + } + + return { bottomRight, topLeft }; +}; + +export const getSerialized = ({ entities }: { entities: Entity[] }) => { + const { bottomRight, topLeft } = getBounds({ entities }); + + let result = ''; + for (let { y } = topLeft; y <= bottomRight.y; y++) { + for (let { x } = topLeft; x <= bottomRight.x; x++) { + const entitiesAtPosition = entities.filter((current) => + getIsSameVector(current.position, { x, y }), + ); + + switch (entitiesAtPosition.length) { + case 0: { + result += ' '; + + break; + } + + case 1: { + result += getSymbol(entitiesAtPosition[0]); + + break; + } + + case 2: { + result += isMovable(entitiesAtPosition[0]) + ? getSymbol(entitiesAtPosition[0]) + : getSymbol(entitiesAtPosition[1]); + + break; + } + } + } + + result += '\n'; + } + + return result; +}; diff --git a/src/features/engine/utils/get-serialized/index.ts b/src/features/engine/utils/get-serialized/index.ts new file mode 100644 index 0000000..57d05c5 --- /dev/null +++ b/src/features/engine/utils/get-serialized/index.ts @@ -0,0 +1 @@ +export * from './get-serialized';