3D RPG 웹게임: WoW 스타일 3인칭 어드벤처
Three.js를 사용하여 월드 오브 워크래프트 스타일의 3인칭 RPG 웹게임을 구현했습니다. WASD로 캐릭터를 이동하고, SPACE로 점프하며, 마우스로 시점을 회전할 수 있습니다.
게임 구현 설명
graph TD
A[게임 초기화] --> B[Three.js 씬 생성]
B --> C[조명 설정]
C --> D[플레이어 모델 로드]
D --> E[월드 생성 - 지형, 나무, 건물]
E --> F[카메라 설정]
F --> G[컨트롤 시스템 - 키보드/마우스]
G --> H[물리 엔진 적용]
H --> I[게임 루프 시작]
게임 기능 구현
// 주요 게임 로직 예시
class ThirdPersonGame {
constructor() {
// Three.js 초기화
this.scene = new THREE.Scene();
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// 플레이어 설정
this.player = new Player();
this.scene.add(this.player.mesh);
// 월드 생성
this.world = new World();
this.scene.add(this.world.group);
// 컨트롤 시스템
this.controls = new PlayerControls(this.player, this.camera);
// 게임 루프 시작
this.animate();
}
animate() {
requestAnimationFrame(() => this.animate());
// 플레이어 업데이트
this.player.update();
// 카메라 플로우
this.controls.updateCameraPosition();
this.renderer.render(this.scene, this.camera);
}
}
아래는 완성된 게임 코드입니다:
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WoW 스타일 3D RPG</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/PointerLockControls.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
overflow: hidden;
background: linear-gradient(135deg, #1a1a2e, #16213e);
color: #fff;
}
#gameContainer {
position: relative;
width: 100vw;
height: 100vh;
}
#uiOverlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 10;
}
#titleScreen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.8);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 20;
transition: opacity 0.5s;
}
#titleScreen h1 {
font-size: 4rem;
margin-bottom: 20px;
background: linear-gradient(to right, #f8b500, #fceabb);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
text-shadow: 0 0 10px rgba(248, 181, 0, 0.5);
}
#titleScreen p {
font-size: 1.2rem;
max-width: 600px;
text-align: center;
margin-bottom: 30px;
line-height: 1.6;
}
#startButton {
background: linear-gradient(to right, #ff8c00, #ffaf40);
border: none;
padding: 15px 40px;
font-size: 1.2rem;
border-radius: 30px;
color: white;
cursor: pointer;
pointer-events: auto;
transition: all 0.3s;
box-shadow: 0 5px 15px rgba(255, 140, 0, 0.4);
}
#startButton:hover {
transform: translateY(-3px);
box-shadow: 0 8px 20px rgba(255, 140, 0, 0.6);
}
#controlsInfo {
position: absolute;
bottom: 20px;
left: 20px;
background: rgba(0, 0, 0, 0.6);
padding: 15px;
border-radius: 10px;
font-size: 0.9rem;
pointer-events: auto;
}
#statsPanel {
position: absolute;
top: 20px;
right: 20px;
background: rgba(0, 0, 0, 0.6);
padding: 15px;
border-radius: 10px;
min-width: 200px;
}
.statBar {
margin: 10px 0;
}
.barLabel {
display: flex;
justify-content: space-between;
}
.barBackground {
height: 10px;
background: #333;
border-radius: 5px;
margin-top: 5px;
overflow: hidden;
}
.barFill {
height: 100%;
border-radius: 5px;
}
.healthBar {
background: linear-gradient(to right, #ff416c, #ff4b2b);
width: 100%;
}
.manaBar {
background: linear-gradient(to right, #4A00E0, #8E2DE2);
width: 80%;
}
#questPanel {
position: absolute;
bottom: 20px;
right: 20px;
background: rgba(0, 0, 0, 0.6);
padding: 15px;
border-radius: 10px;
max-width: 300px;
}
#questPanel h3 {
color: #ffaf40;
margin-bottom: 10px;
}
#crosshair {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 20px;
height: 20px;
pointer-events: none;
}
.crosshair-dot {
position: absolute;
width: 4px;
height: 4px;
background: rgba(255, 255, 255, 0.8);
border-radius: 50%;
top: 8px;
left: 8px;
}
.crosshair-line {
position: absolute;
background: rgba(255, 255, 255, 0.8);
}
#message {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.7);
padding: 20px 40px;
border-radius: 10px;
font-size: 1.5rem;
opacity: 0;
transition: opacity 0.3s;
pointer-events: none;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="uiOverlay">
<div id="titleScreen">
<h1>아제로스의 모험</h1>
<p>고대의 악이 아제로스를 위협하고 있습니다. 강력한 영웅이 필요합니다. 이 황폐한 땅을 탐험하고, 위험에 맞서 싸우며, 고대의 비밀을 밝혀내세요.</p>
<button id="startButton">모험 시작하기</button>
</div>
<div id="statsPanel">
<h3>영웅 상태</h3>
<div class="statBar">
<div class="barLabel">
<span>생명력</span>
<span>850/1000</span>
</div>
<div class="barBackground">
<div class="barFill healthBar"></div>
</div>
</div>
<div class="statBar">
<div class="barLabel">
<span>마나</span>
<span>400/500</span>
</div>
<div class="barBackground">
<div class="barFill manaBar"></div>
</div>
</div>
<div class="statBar">
<div class="barLabel">
<span>레벨</span>
<span>15</span>
</div>
</div>
</div>
<div id="questPanel">
<h3>현재 퀘스트</h3>
<p>마을 북쪽에 있는 오염된 호수를 정화하세요. 호수 중심에 있는 부패의 핵을 파괴하면 됩니다.</p>
<p>보상: 경험치 1500, 골드 25, 마법의 반지</p>
</div>
<div id="controlsInfo">
<p><strong>WASD</strong> - 이동하기</p>
<p><strong>SPACE</strong> - 점프하기</p>
<p><strong>마우스 이동</strong> - 시점 회전</p>
<p><strong>ESC</strong> - 메뉴 열기</p>
</div>
<div id="crosshair">
<div class="crosshair-line" style="width: 8px; height: 2px; top: 9px; left: 0;"></div>
<div class="crosshair-line" style="width: 8px; height: 2px; top: 9px; right: 0;"></div>
<div class="crosshair-line" style="width: 2px; height: 8px; top: 0; left: 9px;"></div>
<div class="crosshair-line" style="width: 2px; height: 8px; bottom: 0; left: 9px;"></div>
<div class="crosshair-dot"></div>
</div>
<div id="message">호수 정화 완료!</div>
</div>
</div>
<script>
// 게임 변수
let scene, camera, renderer, player, controls;
let worldObjects = [];
let keys = {};
let gameStarted = false;
let clock = new THREE.Clock();
// 초기화 함수
function init() {
// Three.js 씬 생성
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
scene.fog = new THREE.Fog(0x87CEEB, 20, 100);
// 렌더러 설정
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.getElementById('gameContainer').appendChild(renderer.domElement);
// 카메라 설정
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 5, 10);
// 조명 설정
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(20, 30, 20);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 1024;
directionalLight.shadow.mapSize.height = 1024;
scene.add(directionalLight);
// 지형 생성
createTerrain();
// 플레이어 생성
createPlayer();
// 월드 오브젝트 생성
createWorldObjects();
// 컨트롤 초기화
initControls();
// 이벤트 리스너
window.addEventListener('resize', onWindowResize);
document.addEventListener('keydown', onKeyDown);
document.addEventListener('keyup', onKeyUp);
// 게임 시작 버튼
document.getElementById('startButton').addEventListener('click', startGame);
// 게임 루프 시작
animate();
}
// 지형 생성
function createTerrain() {
// 지면
const groundGeometry = new THREE.PlaneGeometry(100, 100, 50, 50);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x2E8B57,
wireframe: false
});
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.5;
ground.receiveShadow = true;
scene.add(ground);
// 텍스처 적용 (시뮬레이션)
groundMaterial.color = new THREE.Color(0x3cb371);
// 언덕 생성 (시뮬레이션)
for (let i = 0; i < 20; i++) {
const hillGeometry = new THREE.ConeGeometry(3, 5, 8);
const hillMaterial = new THREE.MeshStandardMaterial({ color: 0x228B22 });
const hill = new THREE.Mesh(hillGeometry, hillMaterial);
hill.position.set(
Math.random() * 80 - 40,
2,
Math.random() * 80 - 40
);
hill.castShadow = true;
scene.add(hill);
worldObjects.push(hill);
}
}
// 플레이어 생성
function createPlayer() {
// 플레이어 메시 (간소화)
const bodyGeometry = new THREE.CapsuleGeometry(0.5, 1, 4, 8);
const bodyMaterial = new THREE.MeshStandardMaterial({ color: 0x4169E1 });
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.position.y = 1.5;
// 머리
const headGeometry = new THREE.SphereGeometry(0.4, 16, 16);
const headMaterial = new THREE.MeshStandardMaterial({ color: 0xFFD700 });
const head = new THREE.Mesh(headGeometry, headMaterial);
head.position.y = 2.7;
// 팔
const armGeometry = new THREE.CylinderGeometry(0.2, 0.2, 1.2, 8);
const armMaterial = new THREE.MeshStandardMaterial({ color: 0x4169E1 });
const leftArm = new THREE.Mesh(armGeometry, armMaterial);
leftArm.position.set(-0.7, 1.8, 0);
leftArm.rotation.z = Math.PI / 4;
const rightArm = leftArm.clone();
rightArm.position.x = 0.7;
rightArm.rotation.z = -Math.PI / 4;
// 다리
const legGeometry = new THREE.CylinderGeometry(0.25, 0.25, 1.2, 8);
const legMaterial = new THREE.MeshStandardMaterial({ color: 0x00008B });
const leftLeg = new THREE.Mesh(legGeometry, legMaterial);
leftLeg.position.set(-0.3, 0.2, 0);
const rightLeg = leftLeg.clone();
rightLeg.position.x = 0.3;
// 플레이어 그룹
player = new THREE.Group();
player.add(body);
player.add(head);
player.add(leftArm);
player.add(rightArm);
player.add(leftLeg);
player.add(rightLeg);
player.position.set(0, 0, 0);
player.castShadow = true;
scene.add(player);
// 플레이어 속성
player.velocity = new THREE.Vector3();
player.direction = new THREE.Vector3();
player.isOnGround = true;
player.moveSpeed = 5.0;
player.jumpStrength = 7.0;
player.rotation = 0;
}
// 월드 오브젝트 생성
function createWorldObjects() {
// 나무 생성
for (let i = 0; i < 30; i++) {
const treeGroup = new THREE.Group();
// 나무 줄기
const trunkGeometry = new THREE.CylinderGeometry(0.3, 0.4, 4, 8);
const trunkMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513 });
const trunk = new THREE.Mesh(trunkGeometry, trunkMaterial);
trunk.position.y = 2;
trunk.castShadow = true;
treeGroup.add(trunk);
// 나뭇잎
const leavesGeometry = new THREE.ConeGeometry(2, 6, 8);
const leavesMaterial = new THREE.MeshStandardMaterial({ color: 0x228B22 });
const leaves = new THREE.Mesh(leavesGeometry, leavesMaterial);
leaves.position.y = 5;
leaves.castShadow = true;
treeGroup.add(leaves);
// 위치 설정
treeGroup.position.set(
Math.random() * 80 - 40,
0,
Math.random() * 80 - 40
);
scene.add(treeGroup);
worldObjects.push(treeGroup);
}
// 바위 생성
for (let i = 0; i < 20; i++) {
const rockGeometry = new THREE.DodecahedronGeometry(0.8, 0);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x808080,
flatShading: true
});
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(
Math.random() * 80 - 40,
0.8,
Math.random() * 80 - 40
);
rock.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
rock.castShadow = true;
scene.add(rock);
worldObjects.push(rock);
}
// 마을 건물 생성
createBuilding(-15, 0, -10, 0xCD5C5C); // 빨간 건물
createBuilding(-10, 0, 5, 0x4682B4); // 파란 건물
createBuilding(5, 0, -8, 0x32CD32); // 초록 건물
}
// 건물 생성
function createBuilding(x, y, z, color) {
const buildingGroup = new THREE.Group();
// 건물 본체
const buildingGeometry = new THREE.BoxGeometry(5, 4, 5);
const buildingMaterial = new THREE.MeshStandardMaterial({ color: color });
const building = new THREE.Mesh(buildingGeometry, buildingMaterial);
building.position.y = 2;
building.castShadow = true;
building.receiveShadow = true;
buildingGroup.add(building);
// 지붕
const roofGeometry = new THREE.ConeGeometry(3.5, 3, 4);
const roofMaterial = new THREE.MeshStandardMaterial({ color: 0x8B0000 });
const roof = new THREE.Mesh(roofGeometry, roofMaterial);
roof.position.y = 5;
roof.rotation.y = Math.PI / 4;
roof.castShadow = true;
buildingGroup.add(roof);
// 문
const doorGeometry = new THREE.BoxGeometry(1, 2, 0.2);
const doorMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513 });
const door = new THREE.Mesh(doorGeometry, doorMaterial);
door.position.set(0, 1, 2.6);
buildingGroup.add(door);
buildingGroup.position.set(x, y, z);
scene.add(buildingGroup);
worldObjects.push(buildingGroup);
}
// 컨트롤 초기화
function initControls() {
// 키보드 컨트롤
keys = {
w: false,
a: false,
s: false,
d: false,
space: false
};
// 마우스 컨트롤
document.addEventListener('mousemove', (event) => {
if (!gameStarted) return;
// 마우스 움직임에 따라 플레이어 회전
player.rotation.y -= event.movementX * 0.002;
// 카메라 회전 (3인칭 시점)
const cameraDistance = 8;
const cameraHeight = 3;
camera.position.x = player.position.x + Math.sin(player.rotation.y) * cameraDistance;
camera.position.z = player.position.z + Math.cos(player.rotation.y) * cameraDistance;
camera.position.y = player.position.y + cameraHeight;
camera.lookAt(player.position.x, player.position.y + 1.5, player.position.z);
});
}
// 게임 시작
function startGame() {
gameStarted = true;
document.getElementById('titleScreen').style.opacity = '0';
document.getElementById('titleScreen').style.pointerEvents = 'none';
// 시작 메시지 표시
showMessage('아제로스에 오신 것을 환영합니다, 영웅이여!');
}
// 메시지 표시
function showMessage(text) {
const message = document.getElementById('message');
message.textContent = text;
message.style.opacity = '1';
setTimeout(() => {
message.style.opacity = '0';
}, 3000);
}
// 키 다운 이벤트
function onKeyDown(event) {
if (!gameStarted) return;
switch (event.key.toLowerCase()) {
case 'w': keys.w = true; break;
case 'a': keys.a = true; break;
case 's': keys.s = true; break;
case 'd': keys.d = true; break;
case ' ':
if (player.isOnGround) {
player.velocity.y = player.jumpStrength;
player.isOnGround = false;
}
keys.space = true;
break;
}
}
// 키 업 이벤트
function onKeyUp(event) {
if (!gameStarted) return;
switch (event.key.toLowerCase()) {
case 'w': keys.w = false; break;
case 'a': keys.a = false; break;
case 's': keys.s = false; break;
case 'd': keys.d = false; break;
case ' ': keys.space = false; break;
}
}
// 윈도우 리사이즈
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// 애니메이션 루프
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
if (gameStarted) {
// 플레이어 이동
player.direction.set(0, 0, 0);
if (keys.w) player.direction.z -= 1;
if (keys.s) player.direction.z += 1;
if (keys.a) player.direction.x -= 1;
if (keys.d) player.direction.x += 1;
// 정규화 및 이동 적용
if (player.direction.length() > 0) {
player.direction.normalize();
// 플레이어 회전 방향으로 이동
const moveX = player.direction.x * Math.cos(player.rotation.y) - player.direction.z * Math.sin(player.rotation.y);
const moveZ = player.direction.x * Math.sin(player.rotation.y) + player.direction.z * Math.cos(player.rotation.y);
player.position.x += moveX * player.moveSpeed * delta;
player.position.z += moveZ * player.moveSpeed * delta;
}
// 중력 적용
player.velocity.y -= 9.8 * delta;
player.position.y += player.velocity.y * delta;
// 지면 체크
if (player.position.y <= 0) {
player.position.y = 0;
player.velocity.y = 0;
player.isOnGround = true;
}
// 카메라 위치 업데이트
const cameraDistance = 8;
const cameraHeight = 3;
camera.position.x = player.position.x + Math.sin(player.rotation.y) * cameraDistance;
camera.position.z = player.position.z + Math.cos(player.rotation.y) * cameraDistance;
camera.position.y = player.position.y + cameraHeight;
camera.lookAt(player.position.x, player.position.y + 1.5, player.position.z);
}
// 오브젝트 애니메이션 (간단한 회전)
worldObjects.forEach(obj => {
if (Math.random() > 0.98) {
obj.rotation.y += Math.PI / 4;
}
});
renderer.render(scene, camera);
}
// 페이지 로드 시 초기화
window.onload = init;
</script>
</body>
</html>
게임 특징
-
WoW 스타일 3D 월드
- 광활한 초원 지형과 나무, 바위, 건물 등 다양한 오브젝트
- 동적인 조명과 그림자 효과
- 안개 효과로 원근감 구현
-
플레이어 컨트롤
- WASD: 전후좌우 이동
- SPACE: 점프
- 마우스 이동: 시점 회전
-
UI 요소
- 제목 화면 및 시작 버튼
- 플레이어 상태 (HP, MP, 레벨)
- 퀘스트 정보 패널
- 조작법 안내
- 게임 내 메시지 시스템
-
물리 효과
게임 실행 방법
- 위 코드를 HTML 파일로 저장합니다 (예:
wow_game.html)
- 웹 브라우저에서 해당 파일을 엽니다
- "모험 시작하기" 버튼을 클릭해 게임을 시작합니다
- WASD 키로 캐릭터를 이동하고, SPACE로 점프하며, 마우스로 시점을 회전합니다
이 게임은 Three.js의 기본 기능을 활용하여 웹에서 WoW 스타일의 3D RPG 게임을 경험할 수 있도록 제작되었습니다. 실제 게임 개발에서는 더 복잡한 지형, 애니메이션, 충돌 감지 등을 추가로 구현할 수 있습니다.