|
| 1 | +import React, { useRef } from 'react'; |
| 2 | +import { useFrame, useThree } from '@react-three/fiber'; |
| 3 | +import { RigidBody, CuboidCollider } from '@react-three/rapier'; |
| 4 | +import { Vector3, Quaternion, Euler } from 'three'; |
| 5 | +import { useKeyboardControls, OrbitControls } from '@react-three/drei'; |
| 6 | + |
| 7 | +type Controls = { |
| 8 | + forward: boolean; |
| 9 | + backward: boolean; |
| 10 | + left: boolean; |
| 11 | + right: boolean; |
| 12 | + jump: boolean; |
| 13 | +}; |
| 14 | + |
| 15 | +export default function Player() { |
| 16 | + const playerRef = useRef<any>(); |
| 17 | + const { camera } = useThree(); |
| 18 | + const [, getKeys] = useKeyboardControls<Controls>(); |
| 19 | + |
| 20 | + const MOVE_SPEED = 15; |
| 21 | + const JUMP_FORCE = 5; |
| 22 | + const cameraOffset = new Vector3(0, 3, 8); |
| 23 | + const cameraTarget = new Vector3(); |
| 24 | + const playerRotation = new Quaternion(); |
| 25 | + const targetRotation = new Quaternion(); |
| 26 | + |
| 27 | + useFrame(() => { |
| 28 | + if (!playerRef.current) return; |
| 29 | + |
| 30 | + const { forward, backward, left, right, jump } = getKeys(); |
| 31 | + const position = playerRef.current.translation(); |
| 32 | + |
| 33 | + // Simple directional movement |
| 34 | + const velocity = { |
| 35 | + x: 0, |
| 36 | + y: playerRef.current.linvel().y, |
| 37 | + z: 0 |
| 38 | + }; |
| 39 | + |
| 40 | + if (forward) velocity.z = -MOVE_SPEED; |
| 41 | + if (backward) velocity.z = MOVE_SPEED; |
| 42 | + if (left) velocity.x = -MOVE_SPEED; |
| 43 | + if (right) velocity.x = MOVE_SPEED; |
| 44 | + |
| 45 | + // Apply velocity |
| 46 | + playerRef.current.setLinvel(velocity); |
| 47 | + |
| 48 | + // Rotate player to face movement direction if moving |
| 49 | + if (velocity.x !== 0 || velocity.z !== 0) { |
| 50 | + const angle = Math.atan2(velocity.x, velocity.z); |
| 51 | + targetRotation.setFromEuler(new Euler(0, angle, 0)); |
| 52 | + playerRotation.slerp(targetRotation, 0.2); |
| 53 | + playerRef.current.setRotation(playerRotation); |
| 54 | + } |
| 55 | + |
| 56 | + // Handle jumping |
| 57 | + if (jump && Math.abs(velocity.y) < 0.1) { |
| 58 | + playerRef.current.applyImpulse({ x: 0, y: JUMP_FORCE, z: 0 }); |
| 59 | + } |
| 60 | + |
| 61 | + // Reset position if player falls off the world |
| 62 | + if (position.y < -10) { |
| 63 | + playerRef.current.setTranslation({ x: 0, y: 3, z: 0 }); |
| 64 | + playerRef.current.setLinvel({ x: 0, y: 0, z: 0 }); |
| 65 | + } |
| 66 | + |
| 67 | + // Update camera position and target |
| 68 | + cameraTarget.set(position.x, position.y + 1.5, position.z); |
| 69 | + camera.position.copy(cameraTarget).add(cameraOffset); |
| 70 | + camera.lookAt(cameraTarget); |
| 71 | + }); |
| 72 | + |
| 73 | + return ( |
| 74 | + <> |
| 75 | + <OrbitControls |
| 76 | + target={cameraTarget} |
| 77 | + enablePan={false} |
| 78 | + maxPolarAngle={Math.PI / 2 - 0.1} |
| 79 | + minDistance={4} |
| 80 | + maxDistance={10} |
| 81 | + /> |
| 82 | + <RigidBody |
| 83 | + ref={playerRef} |
| 84 | + colliders={false} |
| 85 | + mass={1} |
| 86 | + type="dynamic" |
| 87 | + position={[0, 3, 0]} |
| 88 | + enabledRotations={[false, true, false]} |
| 89 | + lockRotations={true} |
| 90 | + friction={0.7} |
| 91 | + restitution={0} |
| 92 | + linearDamping={0.95} |
| 93 | + > |
| 94 | + <CuboidCollider args={[0.5, 1, 0.5]} /> |
| 95 | + <group> |
| 96 | + <mesh castShadow position={[0, 1, 0]}> |
| 97 | + <capsuleGeometry args={[0.5, 1, 8]} /> |
| 98 | + <meshStandardMaterial color="#1E88E5" /> |
| 99 | + </mesh> |
| 100 | + {/* Head */} |
| 101 | + <mesh castShadow position={[0, 2, 0]}> |
| 102 | + <sphereGeometry args={[0.4, 16, 16]} /> |
| 103 | + <meshStandardMaterial color="#1E88E5" /> |
| 104 | + </mesh> |
| 105 | + {/* Eyes */} |
| 106 | + <mesh position={[0.2, 2.1, 0.3]}> |
| 107 | + <sphereGeometry args={[0.1, 16, 16]} /> |
| 108 | + <meshStandardMaterial color="white" /> |
| 109 | + </mesh> |
| 110 | + <mesh position={[-0.2, 2.1, 0.3]}> |
| 111 | + <sphereGeometry args={[0.1, 16, 16]} /> |
| 112 | + <meshStandardMaterial color="white" /> |
| 113 | + </mesh> |
| 114 | + </group> |
| 115 | + </RigidBody> |
| 116 | + </> |
| 117 | + ); |
| 118 | +} |
0 commit comments