Skip to content

Commit 1ed8ddf

Browse files
committed
(#53) Refactored MovementType class into function
1 parent 8241432 commit 1ed8ddf

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

lib/movementtype.class.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import { MovementType } from "./movementtype.class";
1+
import { linear } from "./movementtype.function";
22

33
describe("MovementType", () => {
44
it("should return a set of linear timesteps, one millisecond per step.", () => {
55
const expected = [1, 1, 1, 1, 1, 1];
6-
expect(MovementType.linear(6, 1000)).toEqual(expected);
6+
expect(linear(6, 1000)).toEqual(expected);
77
});
88

99
it("should threshold movement speed to one pixel per millisecond in case of faster movement.", () => {
1010
const expected = [1, 1, 1, 1, 1, 1];
11-
expect(MovementType.linear(6, 2000)).toEqual(expected);
11+
expect(linear(6, 2000)).toEqual(expected);
1212
});
1313

1414
it("should should return a set of linear timesteps, two milliseconds per step.", () => {
1515
const expected = [2, 2, 2, 2, 2, 2];
16-
expect(MovementType.linear(6, 500)).toEqual(expected);
16+
expect(linear(6, 500)).toEqual(expected);
1717
});
1818

1919
it("should floor movement to three milliseconds per step.", () => {
2020
const expected = [3, 3, 3, 3, 3, 3];
21-
expect(MovementType.linear(6, 300)).toEqual(expected);
21+
expect(linear(6, 300)).toEqual(expected);
2222
});
2323
});

lib/movementtype.function.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const linear = (
2+
amountOfSteps: number,
3+
speedInPixelsPerSecond: number,
4+
): number[] => {
5+
const timeSteps = [];
6+
let stepDuration = Math.floor((1 / speedInPixelsPerSecond) * 1000);
7+
if (stepDuration <= 0) {
8+
stepDuration = 1;
9+
}
10+
for (let idx = 0; idx < amountOfSteps; ++idx) {
11+
timeSteps.push(stepDuration);
12+
}
13+
return timeSteps;
14+
};

0 commit comments

Comments
 (0)