-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from Crazy-Cow/feature/CD-85
feat: 물리엔진 제거 [CD-85]
- Loading branch information
Showing
5 changed files
with
31 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Position } from 'game/objects/player' | ||
|
||
type ClientCharacter = { | ||
id: string | ||
position: Position | ||
velocity: Position | ||
isOnGround: boolean | ||
} | ||
|
||
export type SocketMoveData = { | ||
// TODO: 시간정보 delta, datetime 정보 발생 시점 | ||
shift: boolean | ||
character: ClientCharacter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,13 @@ | ||
import { Character, Directions } from '../objects/player' | ||
import * as CANNON from 'cannon-es' | ||
import { Character } from 'game/objects/player' | ||
import { SocketMoveData } from './index.type' | ||
|
||
const MAX_SPEED = 10 | ||
|
||
const handleMove = (character: Character, directions: Directions) => { | ||
if (character && character.cannonBody) { | ||
let angleRad = character.angleRad || 0 | ||
|
||
// -π ~ π 범위로 각도를 제한 | ||
angleRad = ((angleRad + Math.PI) % (2 * Math.PI)) - Math.PI | ||
|
||
// 카메라 각도에 따른 방향 벡터 계산 | ||
const forwardX = Math.sin(angleRad) | ||
const forwardZ = Math.cos(angleRad) | ||
const rightX = Math.cos(angleRad) | ||
const rightZ = -Math.sin(angleRad) | ||
|
||
let targetVelocityX = 0 | ||
let targetVelocityZ = 0 | ||
|
||
if (directions.up) { | ||
targetVelocityX += forwardX * MAX_SPEED | ||
targetVelocityZ += forwardZ * MAX_SPEED | ||
} | ||
if (directions.down) { | ||
targetVelocityX -= forwardX * MAX_SPEED | ||
targetVelocityZ -= forwardZ * MAX_SPEED | ||
} | ||
if (directions.left) { | ||
targetVelocityX += rightX * MAX_SPEED | ||
targetVelocityZ += rightZ * MAX_SPEED | ||
} | ||
if (directions.right) { | ||
targetVelocityX -= rightX * MAX_SPEED | ||
targetVelocityZ -= rightZ * MAX_SPEED | ||
} | ||
|
||
// 현재 속도와 목표 속도 간의 차이를 줄여 이동이 부드럽게 되도록 설정 | ||
character.cannonBody.velocity.x = targetVelocityX | ||
character.cannonBody.velocity.z = targetVelocityZ | ||
|
||
// 키가 떼어진 상태라면 속도를 완전히 0으로 설정 | ||
if (targetVelocityX === 0 && targetVelocityZ === 0) { | ||
character.cannonBody.velocity.x = 0 | ||
character.cannonBody.velocity.z = 0 | ||
character.cannonBody.angularVelocity.set(0, 0, 0) // 각속도도 0으로 설정 | ||
} | ||
|
||
const facingAngleRad = Math.atan2( | ||
character.cannonBody.velocity.x, | ||
character.cannonBody.velocity.z | ||
) | ||
character.facingAngleRad = facingAngleRad | ||
} | ||
} | ||
|
||
const handleJump = (character: Character, jump: boolean) => { | ||
if (character && jump && character.cannonBody && character.isOnGround) { | ||
character.cannonBody.applyImpulse( | ||
new CANNON.Vec3(0, 5, 0), | ||
character.cannonBody.position | ||
) | ||
character.isOnGround = false | ||
} | ||
} | ||
|
||
const handleAngle = (character: Character, angleRad: number) => { | ||
if (character) { | ||
character.angleRad = angleRad | ||
} | ||
} | ||
|
||
const handleSift = (character: Character, shift: boolean) => { | ||
if (character) { | ||
character.shift = shift | ||
} | ||
const handleMove = (character: Character, data: SocketMoveData) => { | ||
character.shift = data.shift | ||
character.position = data.character.position | ||
character.velocity = data.character.velocity | ||
character.isOnGround = data.character.isOnGround | ||
} | ||
|
||
export default { | ||
handleMove, | ||
handleJump, | ||
handleAngle, | ||
handleSift, | ||
} |