Skip to content

Commit

Permalink
Merge pull request #35 from Crazy-Cow/CD-91
Browse files Browse the repository at this point in the history
Fix: Random position
  • Loading branch information
seven05 authored Nov 17, 2024
2 parents 43bd683 + 91fb9ce commit 5af617d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
32 changes: 28 additions & 4 deletions src/game/maps/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const GROUND_POS = {
z: 0,
}

const MIN_DISTANCE = 2

export class CommonMap {
private updateInterval: number
characters: Character[] = []
Expand All @@ -17,11 +19,33 @@ export class CommonMap {

private generateRandomPosition(): Position {
// TODO: 안겹치게 생성되도록
return {
x: GROUND_POS.x + Math.random() * 10,
y: GROUND_POS.y + 2,
z: GROUND_POS.z + Math.random() * 10,
let position: Position
do {
position = {
x: GROUND_POS.x + Math.random() * 10,
y: GROUND_POS.y + 2,
z: GROUND_POS.z + Math.random() * 10,
}
} while (!this.isValidPosition(position))

return position
}

private isValidPosition(newPos: Position): boolean {
// 기존 캐릭터 위치들과의 충돌 검사
for (const character of this.characters) {
const distance = this.calculateDistance(newPos, character.position)
if (distance < MIN_DISTANCE) {
return false
}
}
return true
}

public calculateDistance(pos1: Position, pos2: Position): number {
const dx = pos1.x - pos2.x
const dz = pos1.z - pos2.z
return Math.sqrt(dx * dx + dz * dz)
}

findCharacter(id: string) {
Expand Down
21 changes: 2 additions & 19 deletions src/game/maps/tail-tag.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { Character, Position } from '../objects/player'
import { Character } from '../objects/player'
import { CommonMap } from './common'

const TAIL_STEAL_DISTANCE = 5

export class TailTagMap extends CommonMap {
private calculateDistance(pos1: Position, pos2: Position) {
const dx = pos1.x - pos2.x
const dz = pos1.z - pos2.z
return Math.sqrt(dx * dx + dz * dz)
}

handleCatch(character: Character) {
if (character.hasTail) return // 이미 꼬리를 가지고 있다면 훔치지 않음

Expand All @@ -19,7 +13,7 @@ export class TailTagMap extends CommonMap {
other.hasTail &&
!other.isBeingStolen // 다른 캐릭터가 훔쳐지는 중인지 확인
) {
const distance = this.calculateDistance(
const distance = super.calculateDistance(
character.position,
other.position
)
Expand Down Expand Up @@ -49,17 +43,6 @@ export class TailTagMap extends CommonMap {
character.isBeingStolen = false

if (character.shift) this.handleCatch(character)

// 꼬리가 있을 때 색상 변경
if (character.hasTail) {
character.bodyColor = '#888888'
character.hairColor = '#888888'
character.bellyColor = '#888888'
} else {
character.bodyColor = '#FFFFFF'
character.hairColor = '#FFFFFF'
character.bellyColor = '#FFFFFF'
}
})
}
}
2 changes: 1 addition & 1 deletion src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ctrl from '../controller/users'

const router = express.Router()

router.get('/random-nickname', ctrl.getRandomNicknameController)
router.post('/random-nickname', ctrl.getRandomNicknameController)
router.post('/enter', ctrl.createUserController)

export default router

0 comments on commit 5af617d

Please sign in to comment.