Skip to content

Commit 37dd076

Browse files
Merge pull request #18 from MikeFernandez-Pro/moving-direction-refactor
Refactored character moving direction calculation
2 parents 4b3dcc3 + 75bfae3 commit 37dd076

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

src/Ecctrl.tsx

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ export { useGame } from "./stores/useGame";
2626
export { EcctrlJoystick } from "../src/EcctrlJoystick";
2727
export { useJoystickControls } from "./stores/useJoystickControls";
2828

29+
// Retrieve current moving direction of the character
30+
const getMovingDirection = (forward: boolean,
31+
backward: boolean,
32+
leftward: boolean,
33+
rightward: boolean,
34+
pivot: THREE.Object3D)
35+
:number | null => {
36+
if (!forward && !backward && !leftward && !rightward) return null;
37+
if (forward && leftward) return pivot.rotation.y + Math.PI / 4;
38+
if (forward && rightward) return pivot.rotation.y - Math.PI / 4;
39+
if (backward && leftward) return pivot.rotation.y - Math.PI / 4 + Math.PI;
40+
if (backward && rightward) return pivot.rotation.y + Math.PI / 4 + Math.PI;
41+
if (backward) return pivot.rotation.y + Math.PI;
42+
if (leftward) return pivot.rotation.y + Math.PI / 2;
43+
if (rightward) return pivot.rotation.y - Math.PI / 2;
44+
if (forward) return pivot.rotation.y;
45+
};
46+
2947
const Ecctrl = forwardRef<RapierRigidBody, EcctrlProps>(({
3048
children,
3149
debug = false,
@@ -790,33 +808,9 @@ const Ecctrl = forwardRef<RapierRigidBody, EcctrlProps>(({
790808
*/
791809
const { forward, backward, leftward, rightward, jump, run } = getKeys();
792810

793-
// Getting moving directions
794-
if (forward) {
795-
// Apply camera rotation to character model
796-
modelEuler.y = pivot.rotation.y;
797-
} else if (backward) {
798-
// Apply camera rotation to character model
799-
modelEuler.y = pivot.rotation.y + Math.PI;
800-
} else if (leftward) {
801-
// Apply camera rotation to character model
802-
modelEuler.y = pivot.rotation.y + Math.PI / 2;
803-
} else if (rightward) {
804-
// Apply camera rotation to character model
805-
modelEuler.y = pivot.rotation.y - Math.PI / 2;
806-
}
807-
if (forward && leftward) {
808-
// Apply camera rotation to character model
809-
modelEuler.y = pivot.rotation.y + Math.PI / 4;
810-
} else if (forward && rightward) {
811-
// Apply camera rotation to character model
812-
modelEuler.y = pivot.rotation.y - Math.PI / 4;
813-
} else if (backward && leftward) {
814-
// Apply camera rotation to character model
815-
modelEuler.y = pivot.rotation.y - Math.PI / 4 + Math.PI;
816-
} else if (backward && rightward) {
817-
// Apply camera rotation to character model
818-
modelEuler.y = pivot.rotation.y + Math.PI / 4 + Math.PI;
819-
}
811+
// Getting moving directions (IIFE)
812+
modelEuler.y = ((movingDirection) => movingDirection === null ? modelEuler.y : movingDirection)
813+
(getMovingDirection(forward, backward, leftward, rightward, pivot ))
820814

821815
// Move character to the moving direction
822816
if (forward || backward || leftward || rightward)

0 commit comments

Comments
 (0)