Skip to content

Commit

Permalink
Export functions to impulse motor
Browse files Browse the repository at this point in the history
  • Loading branch information
littensy committed Apr 24, 2023
1 parent 1557838 commit 9633124
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
22 changes: 4 additions & 18 deletions src/use-motor/use-motor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
MapValues,
MotorState,
groupMotorGetState,
groupMotorImpulse,
groupMotorSetState,
motorGetState,
motorImpulse,
motorSetState,
} from "../utils/motor";

Expand Down Expand Up @@ -92,11 +94,7 @@ function useSingleMotor(initialValue: number, useImplicitConnections = true) {
motor,
setState: (state) => motorSetState(motor, state),
getState: () => motorGetState(motor),
impulse: (impulse) => {
motorSetState(motor, {
velocity: (motorGetState(motor)?.velocity ?? 0) + impulse,
});
},
impulse: (impulse) => motorImpulse(motor, impulse),
};
}, []);

Expand Down Expand Up @@ -127,19 +125,7 @@ function useGroupMotor<T extends GroupMotorValue>(initialValue: T, useImplicitCo
motor,
setState: (states) => groupMotorSetState(motor, states),
getState: () => groupMotorGetState(motor),
impulse: (impulses) => {
const state = groupMotorGetState(motor);
const newState: { [K in keyof T]?: Partial<MotorState> } = {};

for (const [key, impulse] of pairs(impulses)) {
const currentVelocity = state[key as keyof T]?.velocity ?? 0;
newState[key as keyof T] = {
velocity: currentVelocity + (impulse as number),
};
}

groupMotorSetState(motor, newState);
},
impulse: (impulses) => groupMotorImpulse(motor, impulses),
};
}, []);

Expand Down
29 changes: 29 additions & 0 deletions src/utils/motor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ export function motorSetState(motor: SingleMotor, state: Partial<MotorState>) {
}
}

/**
* Applies an impulse to the motor's velocity.
* @param motor The motor to apply the impulse to
* @param impulse The impulse to apply
*/
export function motorImpulse(motor: SingleMotor, impulse: number) {
const currentState = (motor as SingleMotorExtended)._state;

currentState.velocity = (currentState.velocity ?? 0) + impulse;
}

/**
* Gets the state of every key in the group motor.
* @param motor The group motor to get the state of
Expand Down Expand Up @@ -72,3 +83,21 @@ export function groupMotorSetState<T extends GroupMotorValue>(
}
}
}

/**
* Applies impulses to the motor's velocity.
* @param motor The group motor to apply the impulses to
* @param impulses The impulses to apply
*/
export function groupMotorImpulse<T extends GroupMotorValue>(
groupMotor: GroupMotor<T>,
impulses: { [K in keyof T]?: number },
) {
for (const [key, state] of pairs(impulses)) {
const motor = (groupMotor as GroupMotorExtended<T>)._motors.get(key as keyof T);

if (motor) {
motor._state.velocity = (motor._state.velocity ?? 0) + (state as number);
}
}
}

0 comments on commit 9633124

Please sign in to comment.