From 0e4e49b94024ae8be96a81327dadbe868ff123ab Mon Sep 17 00:00:00 2001 From: Alex Ilchenko Date: Wed, 6 Mar 2024 16:41:14 +0100 Subject: [PATCH] feat(engine): added getShouldUpdate function --- .../get-should-update/get-should-update.ts | 28 +++++++++++++++++++ .../engine/utils/get-should-update/index.ts | 1 + 2 files changed, 29 insertions(+) create mode 100644 src/features/engine/utils/get-should-update/get-should-update.ts create mode 100644 src/features/engine/utils/get-should-update/index.ts diff --git a/src/features/engine/utils/get-should-update/get-should-update.ts b/src/features/engine/utils/get-should-update/get-should-update.ts new file mode 100644 index 0000000..beb2284 --- /dev/null +++ b/src/features/engine/utils/get-should-update/get-should-update.ts @@ -0,0 +1,28 @@ +import { Entity, isMovable } from '../../types/entities'; +import { getIsSameVector } from '../get-is-same-vector'; + +export const getShouldUpdate = ({ + entities, + previousEntities, +}: { + entities: Entity[]; + previousEntities: Entity[]; +}): boolean => { + if (previousEntities.length !== entities.length) return true; + + const movables = entities.filter(isMovable); + const previousMovables = previousEntities.filter(isMovable); + + return movables.some((entity) => { + const previousEntity = previousMovables.find( + (current) => current.id === entity.id, + ); + + if (!previousEntity) return true; + + return ( + !getIsSameVector(entity.position, previousEntity.position) || + !getIsSameVector(entity.velocity, previousEntity.velocity) + ); + }); +}; diff --git a/src/features/engine/utils/get-should-update/index.ts b/src/features/engine/utils/get-should-update/index.ts new file mode 100644 index 0000000..eab1581 --- /dev/null +++ b/src/features/engine/utils/get-should-update/index.ts @@ -0,0 +1 @@ +export * from './get-should-update';