Skip to content

Commit

Permalink
feat(engine): added getShouldUpdate function
Browse files Browse the repository at this point in the history
  • Loading branch information
wialy committed Mar 6, 2024
1 parent a02f62f commit 0e4e49b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/features/engine/utils/get-should-update/get-should-update.ts
Original file line number Diff line number Diff line change
@@ -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)
);
});
};
1 change: 1 addition & 0 deletions src/features/engine/utils/get-should-update/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './get-should-update';

0 comments on commit 0e4e49b

Please sign in to comment.