diff --git a/gameblocks/modules/actor-motion/SnakeMotionController.js b/gameblocks/modules/actor-motion/SnakeMotionController.js index f57c664..00dcfee 100644 --- a/gameblocks/modules/actor-motion/SnakeMotionController.js +++ b/gameblocks/modules/actor-motion/SnakeMotionController.js @@ -112,15 +112,21 @@ export class SnakeMotionController { direction = this.initialDirection, startCell = this.startCell, pendingGrowth = 0, - }) { + } = {}) { - this.direction = { + this.initialLength = Math.max(2, Math.floor(initialLength)); + this.initialDirection = { right: direction.right, forward: direction.forward, }; + this.startCell = cloneCell(startCell); + this.direction = { + right: this.initialDirection.right, + forward: this.initialDirection.forward, + }; this.segments = segments ? cloneCells(segments) - : createDefaultSegments(initialLength, direction, startCell); + : createDefaultSegments(this.initialLength, this.direction, this.startCell); this.pendingGrowth = Math.max(0, Math.floor(pendingGrowth)); return this.getSegments(); } diff --git a/gameblocks/modules/actor-motion/SnakeMotionController.test.js b/gameblocks/modules/actor-motion/SnakeMotionController.test.js new file mode 100644 index 0000000..b4c4ccc --- /dev/null +++ b/gameblocks/modules/actor-motion/SnakeMotionController.test.js @@ -0,0 +1,22 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { SnakeMotionController } from './SnakeMotionController.js'; + +test('reset without args uses the last startCell, not the constructor origin', () => { + const snake = new SnakeMotionController({ + initialLength: 3, + startCell: { right: 0, forward: 0 }, + initialDirection: { right: 1, forward: 0 }, + }); + snake.reset({ + startCell: { right: 4, forward: 2 }, + direction: { right: 0, forward: 1 }, + initialLength: 3, + }); + assert.deepEqual(snake.head, { right: 4, forward: 2 }); + snake.move({ left: false, right: false, forward: true, backward: false }); + assert.deepEqual(snake.head, { right: 4, forward: 3 }); + snake.reset(); + assert.deepEqual(snake.head, { right: 4, forward: 2 }); + assert.deepEqual(snake.getDirection(), { right: 0, forward: 1 }); +});