@@ -26,6 +26,24 @@ export { useGame } from "./stores/useGame";
26
26
export { EcctrlJoystick } from "../src/EcctrlJoystick" ;
27
27
export { useJoystickControls } from "./stores/useJoystickControls" ;
28
28
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
+
29
47
const Ecctrl = forwardRef < RapierRigidBody , EcctrlProps > ( ( {
30
48
children,
31
49
debug = false ,
@@ -790,33 +808,9 @@ const Ecctrl = forwardRef<RapierRigidBody, EcctrlProps>(({
790
808
*/
791
809
const { forward, backward, leftward, rightward, jump, run } = getKeys ( ) ;
792
810
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 ) )
820
814
821
815
// Move character to the moving direction
822
816
if ( forward || backward || leftward || rightward )
0 commit comments