-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(engine): updated getNextBoardState function
- Loading branch information
Showing
9 changed files
with
689 additions
and
36 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
src/features/engine/utils/get-next-board-state/__tests__/four-movables-to-empty.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol'; | ||
import { DIRECTIONS, VECTOR_ZERO } from '../../../constants'; | ||
import { createEntity } from '../../create-entity'; | ||
import { getMultipliedVector } from '../../get-multiplied-vector'; | ||
import { getNextBoardState } from '..'; | ||
|
||
test.each( | ||
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]), | ||
)( | ||
'should move one of 4 %s to empty field and stop others', | ||
(_symbol, direction) => { | ||
const firstPosition = direction; | ||
const firstVelocity = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: firstPosition, | ||
}); | ||
const secondPosition = { x: direction.y, y: direction.x }; | ||
const secondVelocity = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: secondPosition, | ||
}); | ||
const thirdPosition = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: direction, | ||
}); | ||
const thirdVelocity = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: thirdPosition, | ||
}); | ||
const fourthPosition = { x: -direction.y, y: -direction.x }; | ||
const fourthVelocity = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: fourthPosition, | ||
}); | ||
|
||
const entities = [ | ||
createEntity('floor'), | ||
createEntity('floor', { position: firstPosition }), | ||
createEntity('floor', { position: secondPosition }), | ||
createEntity('floor', { position: thirdPosition }), | ||
createEntity('floor', { position: fourthPosition }), | ||
createEntity('movable', { | ||
id: 'movable-1', | ||
position: firstPosition, | ||
velocity: firstVelocity, | ||
}), | ||
createEntity('movable', { | ||
id: 'movable-2', | ||
position: secondPosition, | ||
velocity: secondVelocity, | ||
}), | ||
createEntity('movable', { | ||
id: 'movable-3', | ||
position: thirdPosition, | ||
velocity: thirdVelocity, | ||
}), | ||
createEntity('movable', { | ||
id: 'movable-4', | ||
position: fourthPosition, | ||
velocity: fourthVelocity, | ||
}), | ||
]; | ||
|
||
const result = getNextBoardState({ board: { entities } }).board.entities; | ||
|
||
expect(result).toBeDefined(); | ||
expect(result).toHaveLength(9); | ||
expect(result).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
position: VECTOR_ZERO, | ||
type: 'movable', | ||
velocity: expect.not.objectContaining(VECTOR_ZERO), | ||
}), | ||
expect.objectContaining({ | ||
type: 'movable', | ||
velocity: VECTOR_ZERO, | ||
}), | ||
expect.objectContaining({ | ||
type: 'movable', | ||
velocity: VECTOR_ZERO, | ||
}), | ||
expect.objectContaining({ | ||
type: 'movable', | ||
velocity: VECTOR_ZERO, | ||
}), | ||
]), | ||
); | ||
}, | ||
); |
4 changes: 2 additions & 2 deletions
4
...-board-state/get-next-board-state.test.ts → ...te/__tests__/get-next-board-state.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/features/engine/utils/get-next-board-state/__tests__/one-movable-to-empty.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol'; | ||
import { DIRECTIONS } from '../../../constants'; | ||
import { createEntity } from '../../create-entity'; | ||
import { getNextBoardState } from '..'; | ||
|
||
test.each( | ||
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]), | ||
)( | ||
"should move one %s to empty field and preserve it's velocity", | ||
(_symbol, direction) => { | ||
const entities = [ | ||
createEntity('floor'), | ||
createEntity('floor', { position: direction }), | ||
createEntity('movable', { id: 'movable', velocity: direction }), | ||
]; | ||
|
||
const result = getNextBoardState({ board: { entities } }).board.entities; | ||
|
||
expect(result).toBeDefined(); | ||
expect(result).toHaveLength(3); | ||
expect(result).toContainEqual( | ||
expect.objectContaining({ | ||
position: direction, | ||
type: 'movable', | ||
velocity: direction, | ||
}), | ||
); | ||
}, | ||
); |
27 changes: 27 additions & 0 deletions
27
src/features/engine/utils/get-next-board-state/__tests__/one-to-wall.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol'; | ||
import { DIRECTIONS, VECTOR_ZERO } from '../../../constants'; | ||
import { createEntity } from '../../create-entity'; | ||
import { getNextBoardState } from '..'; | ||
|
||
test.each( | ||
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]), | ||
)('should stop one when there is no floor %s', (_symbol, direction) => { | ||
const entities = [ | ||
createEntity('floor'), | ||
createEntity('movable', { id: 'movable', velocity: direction }), | ||
]; | ||
|
||
const result = getNextBoardState({ board: { entities } }).board.entities; | ||
|
||
expect(result).toBeDefined(); | ||
expect(result).toHaveLength(2); | ||
expect(result).toContainEqual( | ||
expect.objectContaining({ | ||
id: 'movable', | ||
position: VECTOR_ZERO, | ||
velocity: VECTOR_ZERO, | ||
}), | ||
); | ||
}); |
74 changes: 74 additions & 0 deletions
74
src/features/engine/utils/get-next-board-state/__tests__/three-movables-to-empty.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol'; | ||
import { DIRECTIONS, VECTOR_ZERO } from '../../../constants'; | ||
import { createEntity } from '../../create-entity'; | ||
import { getMultipliedVector } from '../../get-multiplied-vector'; | ||
import { getNextBoardState } from '..'; | ||
|
||
test.each( | ||
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]), | ||
)( | ||
"should move one of three %s to empty field, preserve it's velocity and stop others", | ||
(_symbol, direction) => { | ||
const firstPosition = direction; | ||
const firstVelocity = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: direction, | ||
}); | ||
const secondPosition = { x: direction.y, y: direction.x }; | ||
const secondVelocity = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: secondPosition, | ||
}); | ||
const thirdPosition = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: direction, | ||
}); | ||
const thirdVelocity = direction; | ||
|
||
const entities = [ | ||
createEntity('floor'), | ||
createEntity('floor', { position: firstPosition }), | ||
createEntity('floor', { position: secondPosition }), | ||
createEntity('floor', { position: thirdPosition }), | ||
createEntity('movable', { | ||
id: 'movable-1', | ||
position: firstPosition, | ||
velocity: firstVelocity, | ||
}), | ||
createEntity('movable', { | ||
id: 'movable-2', | ||
position: secondPosition, | ||
velocity: secondVelocity, | ||
}), | ||
createEntity('movable', { | ||
id: 'movable-3', | ||
position: thirdPosition, | ||
velocity: thirdVelocity, | ||
}), | ||
]; | ||
|
||
const result = getNextBoardState({ board: { entities } }).board.entities; | ||
|
||
expect(result).toBeDefined(); | ||
expect(result).toHaveLength(7); | ||
expect(result).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
position: VECTOR_ZERO, | ||
type: 'movable', | ||
velocity: expect.not.objectContaining(VECTOR_ZERO), | ||
}), | ||
expect.objectContaining({ | ||
type: 'movable', | ||
velocity: VECTOR_ZERO, | ||
}), | ||
expect.objectContaining({ | ||
type: 'movable', | ||
velocity: VECTOR_ZERO, | ||
}), | ||
]), | ||
); | ||
}, | ||
); |
119 changes: 119 additions & 0 deletions
119
src/features/engine/utils/get-next-board-state/__tests__/two-dices-to-empty.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol'; | ||
import { DIRECTIONS, VECTOR_ZERO } from '../../../constants'; | ||
import { createEntity } from '../../create-entity'; | ||
import { getMultipliedVector } from '../../get-multiplied-vector'; | ||
import { getNextBoardState } from '..'; | ||
|
||
test.each( | ||
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]), | ||
)( | ||
'should move both dices %s to empty field, stop them, and remove another (line)', | ||
(_symbol, direction) => { | ||
const entities = [ | ||
createEntity('floor'), | ||
createEntity('floor', { position: direction }), | ||
createEntity('floor', { | ||
position: getMultipliedVector({ multiplier: -1, vector: direction }), | ||
}), | ||
createEntity('dice', { | ||
id: 'dice-1', | ||
position: getMultipliedVector({ | ||
multiplier: -1, | ||
vector: direction, | ||
}), | ||
value: 1, | ||
velocity: direction, | ||
}), | ||
createEntity('dice', { | ||
id: 'dice-2', | ||
position: direction, | ||
value: 1, | ||
velocity: getMultipliedVector({ multiplier: -1, vector: direction }), | ||
}), | ||
]; | ||
|
||
const result = getNextBoardState({ board: { entities } }).board.entities; | ||
|
||
expect(result).toBeDefined(); | ||
expect(result).toHaveLength(5); | ||
expect(result).toContainEqual( | ||
expect.objectContaining({ | ||
isFresh: true, | ||
position: VECTOR_ZERO, | ||
type: 'dice', | ||
value: 2, | ||
velocity: VECTOR_ZERO, | ||
}), | ||
); | ||
expect(result).toContainEqual( | ||
expect.objectContaining({ | ||
isRemoved: true, | ||
position: VECTOR_ZERO, | ||
type: 'dice', | ||
value: 1, | ||
velocity: VECTOR_ZERO, | ||
}), | ||
); | ||
}, | ||
); | ||
|
||
test.each( | ||
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]), | ||
)( | ||
'should move both dice %s to empty field, preserve velocity of removed one (corner)', | ||
(_symbol, direction) => { | ||
const firstPosition = direction; | ||
const firstVelocity = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: firstPosition, | ||
}); | ||
const secondPosition = { x: direction.y, y: direction.x }; | ||
const secondVelocity = getMultipliedVector({ | ||
multiplier: -1, | ||
vector: secondPosition, | ||
}); | ||
const entities = [ | ||
createEntity('floor'), | ||
createEntity('floor', { position: firstPosition }), | ||
createEntity('floor', { position: secondPosition }), | ||
createEntity('dice', { | ||
id: 'movable-1', | ||
position: firstPosition, | ||
value: 1, | ||
velocity: firstVelocity, | ||
}), | ||
createEntity('dice', { | ||
id: 'movable-2', | ||
position: secondPosition, | ||
value: 1, | ||
velocity: secondVelocity, | ||
}), | ||
]; | ||
|
||
const result = getNextBoardState({ | ||
board: { entities }, | ||
}).board.entities; | ||
|
||
expect(result).toBeDefined(); | ||
expect(result).toHaveLength(5); | ||
expect(result).toContainEqual( | ||
expect.objectContaining({ | ||
isRemoved: true, | ||
position: VECTOR_ZERO, | ||
type: 'dice', | ||
velocity: VECTOR_ZERO, | ||
}), | ||
); | ||
expect(result).toContainEqual( | ||
expect.objectContaining({ | ||
isFresh: true, | ||
position: VECTOR_ZERO, | ||
type: 'dice', | ||
value: 2, | ||
velocity: VECTOR_ZERO, | ||
}), | ||
); | ||
}, | ||
); |
Oops, something went wrong.