simplify tilt calculations
This commit is contained in:
4
tools/index.ts
Normal file
4
tools/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from '@/tools/log';
|
||||
export * from '@/tools/reduceTilts';
|
||||
export * from '@/tools/throttle';
|
||||
export * from '@/tools/validTilt';
|
||||
19
tools/log.tsx
Normal file
19
tools/log.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* A logging utility designed to be inserted into functional chains.
|
||||
* Logs all parameters with an optional prefix, then returns the first argument unchanged.
|
||||
*
|
||||
* @param {string} [prefix=''] - A label or message to prepend to the logged output.
|
||||
* @returns {(value: any, ...rest: any[]) => any} - A function that logs its arguments and returns the first one.
|
||||
*
|
||||
* @example
|
||||
* const result = [1, 2, 3]
|
||||
* .map((n) => n * 2)
|
||||
* .map(log('doubled:'))
|
||||
* .filter((n) => n > 2);
|
||||
*/
|
||||
export const log =
|
||||
(prefix: string = ''): ((value: any, ...rest: any[]) => any) =>
|
||||
(...args: any[]) => {
|
||||
console.log(prefix, ...args);
|
||||
return args[0];
|
||||
};
|
||||
34
tools/reduceTilts.ts
Normal file
34
tools/reduceTilts.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { log, validTilt } from '@/tools';
|
||||
import { GameUpdate, Tilt } from '@/types';
|
||||
|
||||
const combineTilts = (tilts: Tilt[]) =>
|
||||
tilts.reduce(
|
||||
({ pX, pY, rX, rY, count }, { percentX, percentY, rotateX, rotateY }) => ({
|
||||
pX: pX + percentX,
|
||||
pY: pY + percentY,
|
||||
rX: rX + rotateX,
|
||||
rY: rY + rotateY,
|
||||
count: count + 1,
|
||||
}),
|
||||
{ pX: 0, pY: 0, rX: 0, rY: 0, count: 0 },
|
||||
);
|
||||
|
||||
export function reduceTilts(gameData: GameUpdate, localTilt: Tilt[]): Tilt[] {
|
||||
const remoteTilts = gameData.tilts;
|
||||
const tiltEnabled = gameData.settings.tilt;
|
||||
const remoteTiltEnabled = gameData.settings.remoteTilt;
|
||||
|
||||
if (!tiltEnabled) return [];
|
||||
if (!remoteTiltEnabled) return Array.from({ length: 5 }, (_, i) => localTilt[i]);
|
||||
|
||||
return Array.from({ length: 5 }, (_, i) => (localTilt[i] ? [localTilt[i]] : []))
|
||||
.map((cardTilts, cardIndex) => [...remoteTilts[cardIndex], ...cardTilts])
|
||||
.map((cardTilts) => cardTilts.filter(validTilt))
|
||||
.map(combineTilts)
|
||||
.map(({ pX, pY, rX, rY, count }) => ({
|
||||
percentX: count ? pX / count : -1,
|
||||
percentY: count ? pY / count : -1,
|
||||
rotateX: count ? rX / count : 0,
|
||||
rotateY: count ? rY / count : 0,
|
||||
}));
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
export default function throttle(func: Function, threshold: number) {
|
||||
export function throttle(func: Function, threshold: number) {
|
||||
let lastCall = 0;
|
||||
|
||||
return (...args: any[]) => {
|
||||
|
||||
4
tools/validTilt.ts
Normal file
4
tools/validTilt.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { Tilt } from '@/types';
|
||||
|
||||
export const validTilt = ({ percentX, percentY, rotateX, rotateY }: Tilt) =>
|
||||
percentX >= 0 && percentY >= 0 && !!rotateX && !!rotateY;
|
||||
Reference in New Issue
Block a user