Ver3.5 ๐ฎ JavaScript๋ก ๊ฒ์ ๋ง๋ค๊ธฐ2D & 3D ๊ฒ์ ์์ง ์์ ์ ๋ณต

๐ฎ JavaScript๋ก ๊ฒ์ ๋ง๋ค๊ธฐ
2D & 3D ๊ฒ์ ์์ง ์์ ์ ๋ณต
๋ธ๋ผ์ฐ์ ํ๋๋ก ์ธ์์ ์ ๋ณตํ๋ ๊ฒ์ ๊ฐ๋ฐ์์ ๊ธธ ๐
์ผ, ์์งํ ๋งํด๋ด. ์ด๋ฆด ๋ ๊ฒ์ ํ๋ฉด์ "๋๋ ์ด๋ฐ ๊ฑฐ ๋ง๋ค ์ ์์๊น?" ํ ๋ฒ์ฏค ์๊ฐํด๋ดค์์? ๐ค
๊ทธ๋ฐ๋ฐ ๋๋๊ฒ๋ JavaScript ํ๋๋ฉด ์ง์ง๋ก ๊ฒ์์ ๋ง๋ค ์ ์์ด. ๋ธ๋ผ์ฐ์ ์ด๊ณ , ์ฝ๋ ๋ช ์ค ์น๋ฉด ์บ๋ฆญํฐ๊ฐ ์์ง์ด๊ณ , ์ด์์ด ๋ ์๋ค๋๊ณ , 3D ์ธ๊ณ๊ฐ ํผ์ณ์ง๋ ๊ฑฐ์ผ.
์ด ๊ธ์์๋ JavaScript๋ก ๊ฒ์์ ๋ง๋๋ ํต์ฌ ์๋ฆฌ๋ถํฐ, 2D ๊ฒ์ ์์ง, 3D ๊ฒ์ ์์ง ๊ตฌํ๊น์ง ์ง์ง ์ค์ฉ์ ์ธ ๋ด์ฉ์ผ๋ก ์ญ ํ์ด๋ณผ๊ฒ. ์ค๋น๋์ด? ๋ ๋ฒจ 1๋ถํฐ ์์ํ์! ๐ฎ
๐น๏ธ ์ JavaScript๋ก ๊ฒ์์ ๋ง๋๋๊ณ ?
๋จผ์ "์ ๊ตณ์ด JavaScript์ผ?" ๋ผ๋ ์ง๋ฌธ์ ๋ตํด์ผ๊ฒ ์ง. Unity ์๊ณ , Unreal ์๊ณ , Godot๋ ์๋๋ฐ ๋ง์ด์ผ.
๊ทผ๋ฐ JavaScript ๊ฒ์ ๊ฐ๋ฐ์๋ ์ง์ง ๊ฐ๋ ฅํ ์ด์ ๋ค์ด ์์ด.
๐จ Canvas API - 2D ๊ฒ์์ ์ฌ์ฅ
JavaScript 2D ๊ฒ์์ ํต์ฌ์ HTML5 Canvas API์ผ. Canvas๋ ๋ง ๊ทธ๋๋ก "์บ๋ฒ์ค", ์ฆ ๊ทธ๋ฆผ์ ๊ทธ๋ฆด ์ ์๋ ๊ณต๊ฐ์ด์ผ.
ํฝ์
๋จ์๋ก ์ง์ ์ ์ดํ ์ ์์ด์ ๊ฒ์ ๊ฐ๋ฐ์ ๋ฑ์ด์ง.
Canvas ๊ธฐ๋ณธ ์ธํ
๊ฒ์ ๋ฃจํ๋ฅผ ๋ง๋ค๊ธฐ ์ ์ Canvas๋ฅผ ์ธํ ํ๋ ๋ฐฉ๋ฒ๋ถํฐ ์์๋ณด์.
// HTML
<canvas id="gameCanvas" width="800" height="600"></canvas>
// JavaScript
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// ์บ๋ฒ์ค ํฌ๊ธฐ๋ฅผ ํ๋ฉด์ ๋ง๊ฒ ์กฐ์
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// ๋ฆฌ์ฌ์ด์ฆ ๋์
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
๐ ๊ฒ์ ๋ฃจํ (Game Loop) - ๊ฒ์์ ์ฌ์ฅ๋ฐ๋
๊ฒ์ ๋ฃจํ๋ ๊ฒ์์ ํต์ฌ ์ค์ ํต์ฌ์ด์ผ. ๋งค ํ๋ ์๋ง๋ค ์
๋ฐ์ดํธ โ ๋ ๋๋ง์ ๋ฐ๋ณตํ๋ ๊ตฌ์กฐ์ผ.
requestAnimationFrame์ ์ฌ์ฉํ๋ฉด ๋ธ๋ผ์ฐ์ ์ ํ๋ฉด ๊ฐฑ์ ์ฃผ๊ธฐ(๋ณดํต 60fps)์ ๋ง์ถฐ ์ต์ ํ๋ ๋ฃจํ๋ฅผ ๋ง๋ค ์ ์์ด.
// ๊ธฐ๋ณธ ๊ฒ์ ๋ฃจํ ๊ตฌ์กฐ
let lastTime = 0;
function gameLoop(timestamp) {
// delta time ๊ณ์ฐ (ํ๋ ์ ๋
๋ฆฝ์ ์์ง์์ ์ํด)
const deltaTime = timestamp - lastTime;
lastTime = timestamp;
// 1. ์
๋ฐ์ดํธ: ๊ฒ์ ๋ก์ง ์ฒ๋ฆฌ
update(deltaTime);
// 2. ๋ ๋๋ง: ํ๋ฉด ๊ทธ๋ฆฌ๊ธฐ
render();
// ๋ค์ ํ๋ ์ ์์ฒญ
requestAnimationFrame(gameLoop);
}
// ๊ฒ์ ์์
requestAnimationFrame(gameLoop);
function update(dt) {
// ํ๋ ์ด์ด ์์น ์
๋ฐ์ดํธ
player.x += player.vx * (dt / 1000);
player.y += player.vy * (dt / 1000);
}
function render() {
// ํ๋ฉด ์ด๊ธฐํ
ctx.clearRect(0, 0, canvas.width, canvas.height);
// ๋ฐฐ๊ฒฝ ๊ทธ๋ฆฌ๊ธฐ
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// ํ๋ ์ด์ด ๊ทธ๋ฆฌ๊ธฐ
ctx.fillStyle = '#5b9bd5';
ctx.fillRect(player.x, player.y, player.width, player.height);
}
๐๏ธ 2D ๊ฒ์ ์์ง ์ง์ ๊ตฌํํ๊ธฐ
๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ด ์์ JavaScript๋ก ๋ฏธ๋ 2D ๊ฒ์ ์์ง์ ๋ง๋ค์ด๋ณด์.
์ด๊ฑธ ์ดํดํ๋ฉด Phaser ๊ฐ์ ํ๋ ์์ํฌ๊ฐ ๋ด๋ถ์ ์ผ๋ก ์ด๋ป๊ฒ ๋์ํ๋์ง๋ ์์ฐ์ค๋ฝ๊ฒ ์ดํด๋ผ. ๐ง
๐ฆ Entity-Component ๊ตฌ์กฐ
ํ๋ ๊ฒ์ ์์ง์ ํต์ฌ ํจํด์ Entity-Component System (ECS)์ด์ผ.
๊ฒ์ ์ค๋ธ์ ํธ(Entity)์ ์ปดํฌ๋ํธ๋ฅผ ๋ถ์ฌ์ ๊ธฐ๋ฅ์ ์กฐํฉํ๋ ๋ฐฉ์์ด์ง.
// ๊ธฐ๋ณธ Entity ํด๋์ค
class Entity {
constructor(x, y) {
this.x = x;
this.y = y;
this.components = {};
this.active = true;
}
addComponent(name, component) {
this.components[name] = component;
component.entity = this;
return this;
}
getComponent(name) {
return this.components[name];
}
update(dt) {
Object.values(this.components).forEach(comp => {
if (comp.update) comp.update(dt);
});
}
render(ctx) {
Object.values(this.components).forEach(comp => {
if (comp.render) comp.render(ctx);
});
}
}
// Transform ์ปดํฌ๋ํธ
class Transform {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.rotation = 0;
this.scaleX = 1;
this.scaleY = 1;
}
}
// Renderer ์ปดํฌ๋ํธ
class SpriteRenderer {
constructor(color) {
this.color = color;
}
render(ctx) {
const t = this.entity.getComponent('transform');
ctx.save();
ctx.translate(t.x + t.width / 2, t.y + t.height / 2);
ctx.rotate(t.rotation);
ctx.scale(t.scaleX, t.scaleY);
ctx.fillStyle = this.color;
ctx.fillRect(-t.width / 2, -t.height / 2, t.width, t.height);
ctx.restore();
}
}
// ์ฌ์ฉ ์์
const player = new Entity(100, 100);
player
.addComponent('transform', new Transform(100, 100, 40, 40))
.addComponent('renderer', new SpriteRenderer('#5b9bd5'));
๐ฅ ์ถฉ๋ ๊ฐ์ง (Collision Detection)
๊ฒ์์์ ์ถฉ๋ ๊ฐ์ง๋ ํ์์ผ. ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ AABB (Axis-Aligned Bounding Box) ์ถฉ๋๋ถํฐ ์์๋ณด์.
// AABB ์ถฉ๋ ๊ฐ์ง
function checkAABB(a, b) {
return (
a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y
);
}
// ์ํ ์ถฉ๋ ๊ฐ์ง
function checkCircle(a, b) {
const dx = a.cx - b.cx;
const dy = a.cy - b.cy;
const distance = Math.sqrt(dx * dx + dy * dy);
return distance < a.radius + b.radius;
}
// ์ถฉ๋ ์๋ต (๋ฐ๋ฐ๋ ฅ ๊ณ์ฐ)
function resolveCollision(a, b) {
const overlapX = (a.x + a.width / 2) - (b.x + b.width / 2);
const overlapY = (a.y + a.height / 2) - (b.y + b.height / 2);
if (Math.abs(overlapX) > Math.abs(overlapY)) {
// ์ข์ฐ ์ถฉ๋
a.vx = -a.vx * 0.8; // ๋ฐ๋ฐ ๊ณ์
} else {
// ์ํ ์ถฉ๋
a.vy = -a.vy * 0.8;
if (overlapY < 0) a.onGround = true; // ๋ฐ๋ฅ ์ฐฉ์ง
}
}
โจ๏ธ ์ ๋ ฅ ์์คํ (Input Manager)
// ์
๋ ฅ ๊ด๋ฆฌ์
class InputManager {
constructor() {
this.keys = {};
this.mouse = { x: 0, y: 0, buttons: {} };
this.touches = [];
// ํค๋ณด๋ ์ด๋ฒคํธ
window.addEventListener('keydown', e => {
this.keys[e.code] = true;
});
window.addEventListener('keyup', e => {
this.keys[e.code] = false;
});
// ๋ง์ฐ์ค ์ด๋ฒคํธ
window.addEventListener('mousemove', e => {
this.mouse.x = e.clientX;
this.mouse.y = e.clientY;
});
window.addEventListener('mousedown', e => {
this.mouse.buttons[e.button] = true;
});
window.addEventListener('mouseup', e => {
this.mouse.buttons[e.button] = false;
});
// ํฐ์น ์ด๋ฒคํธ (๋ชจ๋ฐ์ผ ์ง์)
window.addEventListener('touchstart', e => {
this.touches = Array.from(e.touches).map(t => ({
x: t.clientX, y: t.clientY, id: t.identifier
}));
});
}
isKeyDown(code) { return !!this.keys[code]; }
isMouseDown(button = 0) { return !!this.mouse.buttons[button]; }
}
// ์ฌ์ฉ ์์
const input = new InputManager();
function update(dt) {
if (input.isKeyDown('ArrowLeft')) player.vx = -200;
if (input.isKeyDown('ArrowRight')) player.vx = 200;
if (input.isKeyDown('Space') && player.onGround) {
player.vy = -500; // ์ ํ!
player.onGround = false;
}
}
๐ ์ฌ ๊ด๋ฆฌ (Scene Manager)
๊ฒ์์๋ ๋ฉ์ธ ๋ฉ๋ด, ๊ฒ์ ํ๋ ์ด, ๊ฒ์ ์ค๋ฒ ๋ฑ ์ฌ๋ฌ ์ฌ์ด ์์ด. ์ฌ ๋งค๋์ ๋ก ์ด๋ฅผ ๊ด๋ฆฌํด๋ณด์.
// ์ฌ ๋งค๋์
class SceneManager {
constructor() {
this.scenes = {};
this.currentScene = null;
}
addScene(name, scene) {
this.scenes[name] = scene;
}
switchTo(name) {
if (this.currentScene && this.currentScene.onExit) {
this.currentScene.onExit();
}
this.currentScene = this.scenes[name];
if (this.currentScene.onEnter) {
this.currentScene.onEnter();
}
}
update(dt) {
if (this.currentScene) this.currentScene.update(dt);
}
render(ctx) {
if (this.currentScene) this.currentScene.render(ctx);
}
}
// ์ฌ ์์
const mainMenuScene = {
onEnter() { console.log('๋ฉ์ธ ๋ฉ๋ด ์ง์
'); },
update(dt) { /* ๋ฉ๋ด ์ ๋๋ฉ์ด์
*/ },
render(ctx) {
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#fff';
ctx.font = '48px Arial';
ctx.textAlign = 'center';
ctx.fillText('๊ฒ์ ์์', canvas.width / 2, canvas.height / 2);
}
};
const sceneManager = new SceneManager();
sceneManager.addScene('mainMenu', mainMenuScene);
sceneManager.switchTo('mainMenu');
๐ฏ Phaser.js - 2D ๊ฒ์ ๊ฐ๋ฐ์ ๊ฐ์
์์ JavaScript๋ก ์์ง์ ๋ง๋๋ ๊ฑด ๊ณต๋ถ์ ์ข์ง๋ง, ์ค์ ๊ฒ์์ ๋น ๋ฅด๊ฒ ๋ง๋ค๊ณ ์ถ๋ค๋ฉด Phaser.js๋ฅผ ์จ์ผ ํด.
Phaser๋ ํ์ฌ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ HTML5 2D ๊ฒ์ ํ๋ ์์ํฌ์ผ. GitHub ์คํ 37,000๊ฐ ์ด์! โญ
Phaser 3 ๊ธฐ๋ณธ ๊ตฌ์กฐ
// Phaser 3 ๊ฒ์ ์ค์
const config = {
type: Phaser.AUTO, // WebGL ์ฐ์ , ์์ผ๋ฉด Canvas
width: 800,
height: 600,
backgroundColor: '#1a1a2e',
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: [BootScene, GameScene, UIScene]
};
const game = new Phaser.Game(config);
// ๊ฒ์ ์ฌ ํด๋์ค
class GameScene extends Phaser.Scene {
constructor() {
super({ key: 'GameScene' });
}
preload() {
// ๋ฆฌ์์ค ๋ก๋
this.load.image('player', 'assets/player.png');
this.load.tilemapTiledJSON('map', 'assets/level1.json');
this.load.spritesheet('explosion', 'assets/explosion.png', {
frameWidth: 64, frameHeight: 64
});
}
create() {
// ํ์ผ๋งต ์์ฑ
const map = this.make.tilemap({ key: 'map' });
const tileset = map.addTilesetImage('tiles', 'tiles');
const groundLayer = map.createLayer('Ground', tileset);
groundLayer.setCollisionByProperty({ collides: true });
// ํ๋ ์ด์ด ์์ฑ
this.player = this.physics.add.sprite(100, 300, 'player');
this.player.setBounce(0.2);
this.player.setCollideWorldBounds(true);
// ๋ฌผ๋ฆฌ ์ถฉ๋ ์ค์
this.physics.add.collider(this.player, groundLayer);
// ์นด๋ฉ๋ผ ์ค์
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
this.cameras.main.startFollow(this.player, true, 0.08, 0.08);
// ์
๋ ฅ ์ค์
this.cursors = this.input.keyboard.createCursorKeys();
// ์ ๋๋ฉ์ด์
์์ฑ
this.anims.create({
key: 'explode',
frames: this.anims.generateFrameNumbers('explosion', { start: 0, end: 15 }),
frameRate: 20,
repeat: 0
});
}
update() {
const { left, right, up } = this.cursors;
if (left.isDown) {
this.player.setVelocityX(-160);
this.player.setFlipX(true);
} else if (right.isDown) {
this.player.setVelocityX(160);
this.player.setFlipX(false);
} else {
this.player.setVelocityX(0);
}
if (up.isDown && this.player.body.blocked.down) {
this.player.setVelocityY(-330);
}
}
}
Matter.js: ์ค์ ๋ฌผ๋ฆฌ ์๋ฎฌ๋ ์ด์ . ํผ์ฆ, ํ๋ณผ ๊ฒ์์ ์ ํฉ.
Impact Physics: Phaser 3์์๋ ์ ์ ์. ๋ ๊ฑฐ์.
๐ WebGL๊ณผ 3D ๊ฒ์์ ์ธ๊ณ
์ด์ ์ง์ง 3D์ ์ธ๊ณ๋ก ๋ค์ด๊ฐ๋ณด์! ๐
WebGL์ ๋ธ๋ผ์ฐ์ ์์ GPU๋ฅผ ์ง์ ํ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ์ ์์ค API์ผ. OpenGL ES 2.0 ๊ธฐ๋ฐ์ด๊ณ , ์์ด๋ ํ๋ก๊ทธ๋๋ฐ์ด ํ์ํด์ ์ง์
์ฅ๋ฒฝ์ด ๋์.
๊ทธ๋์ ๋๋ถ๋ถ์ Three.js๋ Babylon.js ๊ฐ์ ์ถ์ํ ๋ ์ด์ด๋ฅผ ์ฌ์ฉํด.
WebGL ๊ธฐ๋ณธ ์๋ฆฌ ์ดํดํ๊ธฐ
Three.js๋ฅผ ์ฐ๋๋ผ๋ WebGL์ ๊ธฐ๋ณธ ๊ฐ๋ ์ ์์์ผ ํด. ์๋๋ฉด ์ฑ๋ฅ ์ต์ ํ๋ ์ปค์คํ ์์ด๋๋ฅผ ์ธ ๋ ๋ฐ๋์ ํ์ํ๊ฑฐ๋ .
| ๊ฐ๋ | ์ค๋ช | ๋น์ |
|---|---|---|
| Vertex Shader | 3D ์ขํ๋ฅผ 2D ํ๋ฉด ์ขํ๋ก ๋ณํ | ์นด๋ฉ๋ผ ์์น ๊ณ์ฐ |
| Fragment Shader | ๊ฐ ํฝ์ ์ ์์ ๊ฒฐ์ | ํ์ธํธ์น |
| Buffer | GPU์ ์ ๋ฌํ๋ ๋ฐ์ดํฐ (์ ์ , UV ๋ฑ) | ์ฌ๋ฃ ์ฐฝ๊ณ |
| Texture | 3D ์ค๋ธ์ ํธ์ ์ ํ๋ ์ด๋ฏธ์ง | ๋ฒฝ์ง/์คํฐ์ปค |
| MVP Matrix | Model-View-Projection ๋ณํ ํ๋ ฌ | ์นด๋ฉ๋ผ ๋ ์ฆ ์ค์ |
| Framebuffer | ๋ ๋๋ง ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๋ ๋ฒํผ | ์ฌ์ง ํ์์ง |
๐บ Three.js๋ก 3D ๊ฒ์ ๋ง๋ค๊ธฐ
Three.js๋ WebGL์ ์ถ์ํํด์ ํจ์ฌ ์ฝ๊ฒ 3D ๊ทธ๋ํฝ์ ๋ง๋ค ์ ์๊ฒ ํด์ค.
Scene, Camera, Renderer ์ธ ๊ฐ์ง๊ฐ ํต์ฌ์ด์ผ.
import * as THREE from 'three';
// 1. ์ฌ ์์ฑ
const scene = new THREE.Scene();
scene.background = new THREE.Color('#1a1a2e');
scene.fog = new THREE.Fog('#1a1a2e', 10, 100); // ์๊ฐ ํจ๊ณผ
// 2. ์นด๋ฉ๋ผ ์ค์
const camera = new THREE.PerspectiveCamera(
75, // FOV (์์ผ๊ฐ)
window.innerWidth / window.innerHeight, // ์ข
ํก๋น
0.1, // near clipping plane
1000 // far clipping plane
);
camera.position.set(0, 5, 10);
camera.lookAt(0, 0, 0);
// 3. ๋ ๋๋ฌ ์ค์
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true; // ๊ทธ๋ฆผ์ ํ์ฑํ
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
// 4. ์กฐ๋ช
์ค์
const ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 10);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
// 5. ์งํ ์์ฑ
const groundGeometry = new THREE.PlaneGeometry(50, 50);
const groundMaterial = new THREE.MeshLambertMaterial({ color: 0x3a5a3a });
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
// 6. ํ๋ ์ด์ด (์บก์ ํํ)
const playerGeometry = new THREE.CapsuleGeometry(0.5, 1, 4, 8);
const playerMaterial = new THREE.MeshPhongMaterial({ color: 0x5b9bd5 });
const player = new THREE.Mesh(playerGeometry, playerMaterial);
player.position.set(0, 1.5, 0);
player.castShadow = true;
scene.add(player);
// 7. ๊ฒ์ ๋ฃจํ
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
// ํ๋ ์ด์ด ์ด๋
if (keys['KeyW']) player.position.z -= 5 * delta;
if (keys['KeyS']) player.position.z += 5 * delta;
if (keys['KeyA']) player.position.x -= 5 * delta;
if (keys['KeyD']) player.position.x += 5 * delta;
// ์นด๋ฉ๋ผ๊ฐ ํ๋ ์ด์ด ๋ฐ๋ผ๊ฐ๊ธฐ
camera.position.x = player.position.x;
camera.position.z = player.position.z + 10;
camera.lookAt(player.position);
renderer.render(scene, camera);
}
animate();
๐จ ์ปค์คํ ์์ด๋ (GLSL)
Three.js์์ ์ปค์คํ
์์ด๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋
ํนํ ์๊ฐ ํจ๊ณผ๋ฅผ ๋ง๋ค ์ ์์ด.
GLSL(OpenGL Shading Language)๋ก ์์ฑํ๋๋ฐ, ์ด๊ฒ ์ง์ง ๊ฒ์ ๊ทธ๋ํฝ์ ๊ฝ์ด์ผ! ๐ธ
// ์ปค์คํ
์์ด๋ ๋จธํฐ๋ฆฌ์ผ ์์ (๋ฌผ๊ฒฐ ํจ๊ณผ)
const waterMaterial = new THREE.ShaderMaterial({
uniforms: {
time: { value: 0 },
color: { value: new THREE.Color(0x5b9bd5) }
},
vertexShader: `
uniform float time;
varying vec2 vUv;
void main() {
vUv = uv;
vec3 pos = position;
// ๋ฌผ๊ฒฐ ํจ๊ณผ
pos.y += sin(pos.x * 2.0 + time * 2.0) * 0.3;
pos.y += cos(pos.z * 2.0 + time * 1.5) * 0.2;
gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
}
`,
fragmentShader: `
uniform vec3 color;
uniform float time;
varying vec2 vUv;
void main() {
// ๊น์ด์ ๋ฐ๋ฅธ ์์ ๋ณํ
float depth = vUv.y;
vec3 deepColor = vec3(0.1, 0.2, 0.5);
vec3 shallowColor = color;
vec3 finalColor = mix(deepColor, shallowColor, depth);
// ๋ฐ์ง์ ํจ๊ณผ
float sparkle = sin(vUv.x * 20.0 + time * 3.0) * 0.1 + 0.9;
gl_FragColor = vec4(finalColor * sparkle, 0.8);
}
`,
transparent: true
});
// ์ ๋๋ฉ์ด์
์์ time ์
๋ฐ์ดํธ
function animate() {
waterMaterial.uniforms.time.value += 0.016;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
โก Babylon.js - ์์ ํ 3D ๊ฒ์ ์์ง
Three.js๊ฐ ๊ทธ๋ํฝ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ผ๋ฉด, Babylon.js๋ ์ง์ง ๊ฒ์ ์์ง์ด์ผ.
Microsoft๊ฐ ์ง์ํ๊ณ , ๋ฌผ๋ฆฌ ์์ง, ํํฐํด ์์คํ
, ์ ๋๋ฉ์ด์
, GUI, ์ค๋์ค ๋ฑ ๊ฒ์์ ํ์ํ ๋ชจ๋ ๊ฒ์ด ๋ด์ฅ๋์ด ์์ด.
- 1 ๋ฌผ๋ฆฌ ์์ง: Havok, Cannon.js, Ammo.js ์ง์
- 2 PBR ๋จธํฐ๋ฆฌ์ผ: ๋ฌผ๋ฆฌ ๊ธฐ๋ฐ ๋ ๋๋ง
- 3 ํํฐํด ์์คํ : GPU ํํฐํด ์ง์
- 4 ์ ๋๋ฉ์ด์ : ์ค์ผ๋ ํ ์ ๋๋ฉ์ด์
- 5 XR ์ง์: WebXR (VR/AR)
- 6 GUI: ๋ด์ฅ UI ์์คํ
- ๋ ๊ฐ๋ฒผ์, ์ ์ฐ์ฑ ๋์
- ๊ทธ๋ํฝ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฑ๊ฒฉ
- ์ปค๋ฎค๋ํฐ ๋ ํผ
- ๊ฒ์ ๊ธฐ๋ฅ์ ์ง์ ๊ตฌํ
Babylon.js
- ์์ ํ ๊ฒ์ ์์ง
- ๋ฌผ๋ฆฌ, GUI ๋ฑ ๋ด์ฅ
- TypeScript ๋ค์ดํฐ๋ธ
- ๋ ๋ฌด๊ฑฐ์
// Babylon.js ๊ธฐ๋ณธ ์ค์
const canvas = document.getElementById('renderCanvas');
const engine = new BABYLON.Engine(canvas, true);
const createScene = () => {
const scene = new BABYLON.Scene(engine);
// ์นด๋ฉ๋ผ (ArcRotateCamera: ์ค๋ธ์ ํธ ์ค์ฌ ํ์ )
const camera = new BABYLON.ArcRotateCamera(
'camera', -Math.PI / 2, Math.PI / 4, 10,
BABYLON.Vector3.Zero(), scene
);
camera.attachControl(canvas, true);
// ์กฐ๋ช
const light = new BABYLON.HemisphericLight(
'light', new BABYLON.Vector3(0, 1, 0), scene
);
light.intensity = 0.7;
const dirLight = new BABYLON.DirectionalLight(
'dirLight', new BABYLON.Vector3(-1, -2, -1), scene
);
dirLight.position = new BABYLON.Vector3(20, 40, 20);
// ๊ทธ๋ฆผ์ ์์ฑ๊ธฐ
const shadowGenerator = new BABYLON.ShadowGenerator(1024, dirLight);
shadowGenerator.useBlurExponentialShadowMap = true;
// ์งํ
const ground = BABYLON.MeshBuilder.CreateGround(
'ground', { width: 20, height: 20 }, scene
);
const groundMat = new BABYLON.StandardMaterial('groundMat', scene);
groundMat.diffuseColor = new BABYLON.Color3(0.3, 0.5, 0.3);
ground.material = groundMat;
ground.receiveShadows = true;
// ํ๋ ์ด์ด (์บก์)
const player = BABYLON.MeshBuilder.CreateCapsule(
'player', { height: 2, radius: 0.5 }, scene
);
player.position.y = 1;
const playerMat = new BABYLON.StandardMaterial('playerMat', scene);
playerMat.diffuseColor = new BABYLON.Color3(0.36, 0.61, 0.84);
player.material = playerMat;
shadowGenerator.addShadowCaster(player);
// ๋ฌผ๋ฆฌ ์์ง ํ์ฑํ
scene.enablePhysics(
new BABYLON.Vector3(0, -9.81, 0),
new BABYLON.CannonJSPlugin()
);
player.physicsImpostor = new BABYLON.PhysicsImpostor(
player, BABYLON.PhysicsImpostor.CapsuleImpostor,
{ mass: 1, restitution: 0.1 }, scene
);
ground.physicsImpostor = new BABYLON.PhysicsImpostor(
ground, BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 0, restitution: 0.1 }, scene
);
// ํํฐํด ์์คํ
(ํญ๋ฐ ํจ๊ณผ)
const particleSystem = new BABYLON.ParticleSystem('particles', 2000, scene);
particleSystem.particleTexture = new BABYLON.Texture('flare.png', scene);
particleSystem.emitter = player;
particleSystem.minEmitBox = new BABYLON.Vector3(-0.5, 0, -0.5);
particleSystem.maxEmitBox = new BABYLON.Vector3(0.5, 0, 0.5);
particleSystem.color1 = new BABYLON.Color4(1, 0.5, 0, 1);
particleSystem.color2 = new BABYLON.Color4(1, 0.2, 0, 1);
particleSystem.minLifeTime = 0.3;
particleSystem.maxLifeTime = 1.5;
particleSystem.emitRate = 100;
return scene;
};
const scene = createScene();
// ๋ ๋ ๋ฃจํ
engine.runRenderLoop(() => {
scene.render();
});
// ๋ฆฌ์ฌ์ด์ฆ ๋์
window.addEventListener('resize', () => {
engine.resize();
});
๐ ๊ฒ์ ์ฑ๋ฅ ์ต์ ํ ์ ๋ต
๊ฒ์์ ๋ง๋ค์๋๋ฐ ํ๋ ์์ด ๋๋ ๋จ์ด์ง๋ฉด ์๋ฌด๋ ์ ํด. ๐
JavaScript ๊ฒ์ ์ฑ๋ฅ ์ต์ ํ๋ ํฌ๊ฒ ๋ ๋๋ง ์ต์ ํ์ ๋ก์ง ์ต์ ํ๋ก ๋๋ ์ ์์ด.
๐จ ๋ ๋๋ง ์ต์ ํ
- 1 ์คํ์คํฌ๋ฆฐ ์บ๋ฒ์ค ์ฌ์ฉ: ๋ณต์กํ ๋ฐฐ๊ฒฝ์ ๋ณ๋ ์บ๋ฒ์ค์ ๋ฏธ๋ฆฌ ๊ทธ๋ ค๋๊ณ ๋ณต์ฌ
- 2 clearRect ์ต์ํ: ๋ณ๊ฒฝ๋ ์์ญ๋ง ์ง์ฐ๊ธฐ (dirty rectangle)
- 3 ctx.save/restore ์ต์ํ: ์ํ ๋ณ๊ฒฝ ๋น์ฉ์ด ํผ
- 4 ์ด๋ฏธ์ง ์บ์ฑ: ์์ฃผ ์ฐ๋ ์ด๋ฏธ์ง๋ ๋ฏธ๋ฆฌ ๋ก๋ํด์ ์บ์
- 5 ๋ฐฐ์น ๋ ๋๋ง: ๊ฐ์ ํ ์ค์ฒ์ ์ค๋ธ์ ํธ๋ ํ ๋ฒ์ ๊ทธ๋ฆฌ๊ธฐ
// ์คํ์คํฌ๋ฆฐ ์บ๋ฒ์ค ํ์ฉ
const offscreenCanvas = document.createElement('canvas');
offscreenCanvas.width = 800;
offscreenCanvas.height = 600;
const offCtx = offscreenCanvas.getContext('2d');
// ๋ฐฐ๊ฒฝ์ ์คํ์คํฌ๋ฆฐ์ ํ ๋ฒ๋ง ๊ทธ๋ฆฌ๊ธฐ
function drawBackground() {
offCtx.fillStyle = '#1a1a2e';
offCtx.fillRect(0, 0, 800, 600);
// ๋ณต์กํ ๋ฐฐ๊ฒฝ ์์๋ค...
for (let i = 0; i < 200; i++) {
offCtx.fillStyle = `rgba(255,255,255,${Math.random() * 0.5})`;
offCtx.fillRect(Math.random() * 800, Math.random() * 600, 2, 2);
}
}
drawBackground(); // ํ ๋ฒ๋ง ์คํ
// ๋ฉ์ธ ๋ ๋๋ง์์๋ ๋ณต์ฌ๋ง
function render() {
ctx.drawImage(offscreenCanvas, 0, 0); // ๋น ๋ฆ!
// ๋์ ์์๋ค๋ง ๊ทธ๋ฆฌ๊ธฐ
drawPlayer();
drawEnemies();
}
// Three.js ์ต์ ํ
// 1. ์ธ์คํด์ค ๋ฉ์ (๊ฐ์ ์ค๋ธ์ ํธ ์ฌ๋ฌ ๊ฐ)
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshLambertMaterial({ color: 0x5b9bd5 });
const instancedMesh = new THREE.InstancedMesh(geometry, material, 1000);
const matrix = new THREE.Matrix4();
for (let i = 0; i < 1000; i++) {
matrix.setPosition(
Math.random() * 100 - 50,
Math.random() * 10,
Math.random() * 100 - 50
);
instancedMesh.setMatrixAt(i, matrix);
}
scene.add(instancedMesh);
// 2. LOD (Level of Detail)
const lod = new THREE.LOD();
lod.addLevel(highDetailMesh, 0); // ๊ฐ๊น์ธ ๋
lod.addLevel(medDetailMesh, 50); // ์ค๊ฐ ๊ฑฐ๋ฆฌ
lod.addLevel(lowDetailMesh, 200); // ๋ฉ ๋
scene.add(lod);
โ๏ธ ๋ก์ง ์ต์ ํ
// ๊ณต๊ฐ ๋ถํ (Spatial Partitioning) - ์ถฉ๋ ๊ฐ์ง ์ต์ ํ
class QuadTree {
constructor(bounds, maxObjects = 10, maxLevels = 5, level = 0) {
this.bounds = bounds;
this.maxObjects = maxObjects;
this.maxLevels = maxLevels;
this.level = level;
this.objects = [];
this.nodes = [];
}
insert(obj) {
if (this.nodes.length) {
const index = this.getIndex(obj);
if (index !== -1) {
this.nodes[index].insert(obj);
return;
}
}
this.objects.push(obj);
if (this.objects.length > this.maxObjects && this.level < this.maxLevels) {
if (!this.nodes.length) this.split();
// ๊ธฐ์กด ์ค๋ธ์ ํธ๋ค ์ฌ๋ฐฐ์น
this.objects = this.objects.filter(o => {
const index = this.getIndex(o);
if (index !== -1) { this.nodes[index].insert(o); return false; }
return true;
});
}
}
retrieve(obj) {
const index = this.getIndex(obj);
let returnObjects = [...this.objects];
if (this.nodes.length) {
if (index !== -1) {
returnObjects = returnObjects.concat(this.nodes[index].retrieve(obj));
} else {
this.nodes.forEach(node => {
returnObjects = returnObjects.concat(node.retrieve(obj));
});
}
}
return returnObjects;
}
}
// ์ค๋ธ์ ํธ ํ๋ง (Object Pooling) - GC ์ต์ ํ
class ObjectPool {
constructor(createFn, resetFn, initialSize = 50) {
this.createFn = createFn;
this.resetFn = resetFn;
this.pool = Array.from({ length: initialSize }, createFn);
}
get() {
return this.pool.length > 0 ? this.pool.pop() : this.createFn();
}
release(obj) {
this.resetFn(obj);
this.pool.push(obj);
}
}
// ์ด์ ํ ์์
const bulletPool = new ObjectPool(
() => ({ x: 0, y: 0, vx: 0, vy: 0, active: false }),
(bullet) => { bullet.active = false; }
);
// ์ด์ ๋ฐ์ฌ
function fireBullet(x, y, angle) {
const bullet = bulletPool.get();
bullet.x = x;
bullet.y = y;
bullet.vx = Math.cos(angle) * 500;
bullet.vy = Math.sin(angle) * 500;
bullet.active = true;
activeBullets.push(bullet);
}
// ์ด์ ์ ๊ฑฐ (ํ๋ก ๋ฐํ)
function removeBullet(bullet) {
bulletPool.release(bullet);
activeBullets.splice(activeBullets.indexOf(bullet), 1);
}
๐ต ์ค๋์ค, ์ ์ฅ, ๋คํธ์ํฌ - ๊ฒ์ ์์ฑ๋ ๋์ด๊ธฐ
๐ Web Audio API๋ก ๊ฒ์ ์ฌ์ด๋
// Web Audio API ๊ธฐ๋ฐ ์ฌ์ด๋ ๋งค๋์
class AudioManager {
constructor() {
this.context = new AudioContext();
this.masterGain = this.context.createGain();
this.masterGain.connect(this.context.destination);
this.sounds = {};
this.bgmSource = null;
}
async loadSound(name, url) {
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
this.sounds[name] = await this.context.decodeAudioData(arrayBuffer);
}
play(name, options = {}) {
const { volume = 1, loop = false, pitch = 1 } = options;
const source = this.context.createBufferSource();
source.buffer = this.sounds[name];
source.loop = loop;
source.playbackRate.value = pitch;
const gainNode = this.context.createGain();
gainNode.gain.value = volume;
source.connect(gainNode);
gainNode.connect(this.masterGain);
source.start(0);
return source;
}
// 3D ๊ณต๊ฐ ์ค๋์ค
play3D(name, position, listenerPosition) {
const source = this.context.createBufferSource();
source.buffer = this.sounds[name];
const panner = this.context.createPanner();
panner.panningModel = 'HRTF';
panner.distanceModel = 'inverse';
panner.setPosition(position.x, position.y, position.z);
source.connect(panner);
panner.connect(this.masterGain);
source.start(0);
}
setMasterVolume(value) {
this.masterGain.gain.setTargetAtTime(value, this.context.currentTime, 0.01);
}
}
// ์ฌ์ฉ ์์
const audio = new AudioManager();
await audio.loadSound('jump', 'sounds/jump.mp3');
await audio.loadSound('bgm', 'sounds/bgm.mp3');
audio.play('bgm', { loop: true, volume: 0.5 });
// ์ ํ ์
audio.play('jump', { pitch: 1 + Math.random() * 0.2 }); // ์ฝ๊ฐ์ ํผ์น ๋ณํ
๐พ ๊ฒ์ ์ ์ฅ ์์คํ
// ๊ฒ์ ์ ์ฅ/๋ถ๋ฌ์ค๊ธฐ ์์คํ
class SaveSystem {
constructor(gameId) {
this.gameId = gameId;
this.dbName = `${gameId}_saves`;
}
// LocalStorage ๊ธฐ๋ฐ (๊ฐ๋จํ ๋ฐ์ดํฐ)
saveLocal(slot, data) {
const saveData = {
...data,
timestamp: Date.now(),
version: '1.0.0'
};
localStorage.setItem(`${this.gameId}_slot${slot}`, JSON.stringify(saveData));
}
loadLocal(slot) {
const raw = localStorage.getItem(`${this.gameId}_slot${slot}`);
return raw ? JSON.parse(raw) : null;
}
// IndexedDB ๊ธฐ๋ฐ (๋์ฉ๋ ๋ฐ์ดํฐ)
async saveDB(slot, data) {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName, 1);
request.onupgradeneeded = (e) => {
e.target.result.createObjectStore('saves', { keyPath: 'slot' });
};
request.onsuccess = (e) => {
const db = e.target.result;
const tx = db.transaction('saves', 'readwrite');
tx.objectStore('saves').put({ slot, data, timestamp: Date.now() });
tx.oncomplete = resolve;
tx.onerror = reject;
};
});
}
// ์๋ ์ ์ฅ
startAutoSave(getDataFn, interval = 60000) {
this.autoSaveTimer = setInterval(() => {
this.saveLocal('auto', getDataFn());
console.log('์๋ ์ ์ฅ ์๋ฃ!');
}, interval);
}
}
๐ ๋ฉํฐํ๋ ์ด์ด - WebSocket
// WebSocket ๊ธฐ๋ฐ ๋ฉํฐํ๋ ์ด์ด
class NetworkManager {
constructor(serverUrl) {
this.ws = new WebSocket(serverUrl);
this.playerId = null;
this.players = {};
this.handlers = {};
this.ws.onopen = () => console.log('์๋ฒ ์ฐ๊ฒฐ๋จ!');
this.ws.onmessage = (e) => this.handleMessage(JSON.parse(e.data));
this.ws.onclose = () => this.handleDisconnect();
}
handleMessage(msg) {
switch (msg.type) {
case 'INIT':
this.playerId = msg.playerId;
this.players = msg.players;
break;
case 'PLAYER_MOVE':
if (this.players[msg.playerId]) {
// ๋ณด๊ฐ(Interpolation)์ผ๋ก ๋ถ๋๋ฝ๊ฒ ์ด๋
this.players[msg.playerId].targetX = msg.x;
this.players[msg.playerId].targetY = msg.y;
}
break;
case 'PLAYER_JOIN':
this.players[msg.playerId] = msg.playerData;
break;
case 'PLAYER_LEAVE':
delete this.players[msg.playerId];
break;
}
if (this.handlers[msg.type]) this.handlers[msg.type](msg);
}
sendMove(x, y) {
this.send({ type: 'MOVE', x, y });
}
send(data) {
if (this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(data));
}
}
// ํด๋ผ์ด์ธํธ ์ฌ์ด๋ ์์ธก (Client-Side Prediction)
predictMove(player, input, dt) {
const predictedX = player.x + input.vx * dt;
const predictedY = player.y + input.vy * dt;
// ์๋ฒ ์๋ต ์ ์ ๋ฏธ๋ฆฌ ์ด๋ ํ์
player.x = predictedX;
player.y = predictedY;
this.sendMove(predictedX, predictedY);
}
}
๐ฑ ๋ชจ๋ฐ์ผ ๊ฒ์ ์ต์ ํ์ ๋ฐฐํฌ
JavaScript ๊ฒ์์ ํฐ ์ฅ์ ์ค ํ๋๋ ๋ชจ๋ฐ์ผ์์๋ ๋์๊ฐ๋ค๋ ๊ฑฐ์ผ.
ํ์ง๋ง ๋ชจ๋ฐ์ผ ํน์ฑ์ ๋ง๊ฒ ์ต์ ํ๊ฐ ํ์ํด.
๐ฒ ๋ชจ๋ฐ์ผ ํฐ์น ์ปจํธ๋กค
// ๊ฐ์ ์กฐ์ด์คํฑ ๊ตฌํ
class VirtualJoystick {
constructor(canvas) {
this.canvas = canvas;
this.active = false;
this.startX = 0;
this.startY = 0;
this.currentX = 0;
this.currentY = 0;
this.radius = 60;
this.knobRadius = 25;
canvas.addEventListener('touchstart', this.onTouchStart.bind(this));
canvas.addEventListener('touchmove', this.onTouchMove.bind(this));
canvas.addEventListener('touchend', this.onTouchEnd.bind(this));
}
onTouchStart(e) {
e.preventDefault();
const touch = e.touches[0];
const rect = this.canvas.getBoundingClientRect();
this.startX = touch.clientX - rect.left;
this.startY = touch.clientY - rect.top;
this.currentX = this.startX;
this.currentY = this.startY;
this.active = true;
}
onTouchMove(e) {
e.preventDefault();
if (!this.active) return;
const touch = e.touches[0];
const rect = this.canvas.getBoundingClientRect();
const x = touch.clientX - rect.left;
const y = touch.clientY - rect.top;
const dx = x - this.startX;
const dy = y - this.startY;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist > this.radius) {
this.currentX = this.startX + (dx / dist) * this.radius;
this.currentY = this.startY + (dy / dist) * this.radius;
} else {
this.currentX = x;
this.currentY = y;
}
}
onTouchEnd() { this.active = false; }
getInput() {
if (!this.active) return { x: 0, y: 0 };
return {
x: (this.currentX - this.startX) / this.radius,
y: (this.currentY - this.startY) / this.radius
};
}
render(ctx) {
if (!this.active) return;
// ๋ฒ ์ด์ค ์
ctx.beginPath();
ctx.arc(this.startX, this.startY, this.radius, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(255,255,255,0.2)';
ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,0.5)';
ctx.lineWidth = 2;
ctx.stroke();
// ์กฐ์ด์คํฑ ๋
ธ๋ธ
ctx.beginPath();
ctx.arc(this.currentX, this.currentY, this.knobRadius, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(255,255,255,0.6)';
ctx.fill();
}
}
๐ ๋ฐฐํฌ ๋ฐฉ๋ฒ ๋น๊ต
| ํ๋ซํผ | ํน์ง | ๋น์ฉ | ์ถ์ฒ ์ฉ๋ |
|---|---|---|---|
| GitHub Pages | ์ ์ ํ์ผ ํธ์คํ , CI/CD ์ฐ๋ | ๋ฌด๋ฃ | ๊ฐ์ธ ํ๋ก์ ํธ, ํฌํธํด๋ฆฌ์ค |
| Netlify | ์๋ ๋ฐฐํฌ, CDN, ํผ ์ฒ๋ฆฌ | ๋ฌด๋ฃ (๊ธฐ๋ณธ) | ์๊ท๋ชจ ~ ์ค๊ท๋ชจ ๊ฒ์ |
| itch.io | ๊ฒ์ ์ ์ฉ ํ๋ซํผ, ์ปค๋ฎค๋ํฐ | ๋ฌด๋ฃ (์์ต ๊ณต์ ) | ์ธ๋ ๊ฒ์ ์ถ์ |
| Vercel | ์ฃ์ง ๋คํธ์ํฌ, ์๋ฒ๋ฆฌ์ค | ๋ฌด๋ฃ (๊ธฐ๋ณธ) | ๋ฉํฐํ๋ ์ด์ด ๊ฒ์ |
| Electron | ๋ฐ์คํฌํ ์ฑ์ผ๋ก ํจํค์ง | ๋ฌด๋ฃ | Steam ์ถ์ ๋ชฉํ |
| Capacitor | ๋ชจ๋ฐ์ผ ์ฑ์ผ๋ก ํจํค์ง | ๋ฌด๋ฃ | ์ฑ์คํ ์ด ์ถ์ |
๐บ๏ธ ํ์ต ๋ก๋๋งต - ์ด๋์๋ถํฐ ์์ํ ๊น?
JavaScript ๊ฒ์ ๊ฐ๋ฐ์ ์ฒ์ ์์ํ๋ ์ฌ๋๋ค์ ์ํ ๋จ๊ณ๋ณ ๋ก๋๋งต์ด์ผ.
์ด ์์๋๋ก ๋ฐ๋ผ๊ฐ๋ฉด 6๊ฐ์ ์์ ๊ฝค ๊ทธ๋ด๋ฏํ ๊ฒ์์ ๋ง๋ค ์ ์์ด! ๐ช
๐ฅ ์ค์ ํ & ์์ฃผ ํ๋ ์ค์๋ค
๊ฒ์ ๊ฐ๋ฐํ๋ฉด์ ์ง์ง ๋ง์ด ํ๋ ์ค์๋ค์ด ์์ด. ๋ฏธ๋ฆฌ ์์๋๋ฉด ์๊ฐ ๋ญ๋น๋ฅผ ์์ฒญ ์ค์ผ ์ ์์ด! ๐ค
โ ์์ฃผ ํ๋ ์ค์๋ค
setInterval์ ํญ์ด ๋ฐฑ๊ทธ๋ผ์ด๋์ ์์ด๋ ๊ณ์ ์คํ๋๊ณ , ํ๋ฉด ์ฃผ์ฌ์จ๊ณผ ๋๊ธฐํ๊ฐ ์ ๋ผ.๋ฐ๋์
requestAnimationFrame์ ์ฌ์ฉํด์ผ ํด!
new Vector2(), new Array() ๊ฐ์ ๊ฑธ ๋งค ํ๋ ์๋ง๋ค ๋ง๋ค๋ฉด GC(๊ฐ๋น์ง ์ปฌ๋ ํฐ)๊ฐ ์์ฃผ ์คํ๋์ด ํ๋ ์ ๋๋์ด ์๊ฒจ.์ค๋ธ์ ํธ ํ๋ง์ ์ฌ์ฉํ๊ฑฐ๋, ๋ฏธ๋ฆฌ ๋ง๋ค์ด๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฌ์ฉํด์ผ ํด!
new Image()๋ฅผ ๊ฒ์ ๋ฃจํ ์์์ ๋ง๋ค๋ฉด ์ ๋ผ. ๊ฒ์ ์์ ์ ์ ๋ชจ๋ ๋ฆฌ์์ค๋ฅผ ๋ฏธ๋ฆฌ ๋ก๋ํด๋๊ณ ์บ์ํด์ผ ํด!
โ ํ๋ก์ฒ๋ผ ์ฝ๋ฉํ๋ ํ
// ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ Vector2 ํด๋์ค
class Vector2 {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
add(v) { return new Vector2(this.x + v.x, this.y + v.y); }
sub(v) { return new Vector2(this.x - v.x, this.y - v.y); }
scale(s) { return new Vector2(this.x * s, this.y * s); }
dot(v) { return this.x * v.x + this.y * v.y; }
magnitude() { return Math.sqrt(this.x * this.x + this.y * this.y); }
normalize() {
const mag = this.magnitude();
return mag > 0 ? this.scale(1 / mag) : new Vector2();
}
distance(v) { return this.sub(v).magnitude(); }
angle() { return Math.atan2(this.y, this.x); }
rotate(angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return new Vector2(
this.x * cos - this.y * sin,
this.x * sin + this.y * cos
);
}
lerp(v, t) {
return new Vector2(
this.x + (v.x - this.x) * t,
this.y + (v.y - this.y) * t
);
}
clone() { return new Vector2(this.x, this.y); }
toString() { return `Vector2(${this.x.toFixed(2)}, ${this.y.toFixed(2)})`; }
}
// ์ฌ์ฉ ์์
const playerPos = new Vector2(100, 200);
const enemyPos = new Vector2(300, 400);
const direction = enemyPos.sub(playerPos).normalize();
const speed = 150;
const velocity = direction.scale(speed);
// ๋ถ๋๋ฌ์ด ์ด๋ (๋ณด๊ฐ)
player.position = player.position.lerp(targetPosition, 0.1);
// ์ ํ ์ํ ๋จธ์ (FSM)
class StateMachine {
constructor(initialState) {
this.states = {};
this.currentState = null;
this.previousState = null;
this.initialState = initialState;
}
addState(name, state) {
this.states[name] = state;
return this;
}
start() {
this.transition(this.initialState);
}
transition(stateName, data = {}) {
if (this.currentState && this.states[this.currentState].onExit) {
this.states[this.currentState].onExit(data);
}
this.previousState = this.currentState;
this.currentState = stateName;
if (this.states[this.currentState].onEnter) {
this.states[this.currentState].onEnter(data);
}
}
update(dt, input) {
const state = this.states[this.currentState];
if (state && state.update) {
const nextState = state.update(dt, input);
if (nextState) this.transition(nextState);
}
}
}
// ํ๋ ์ด์ด ์ํ ๋จธ์ ์์
const playerFSM = new StateMachine('idle');
playerFSM
.addState('idle', {
onEnter: () => player.playAnim('idle'),
update: (dt, input) => {
if (input.isKeyDown('ArrowLeft') || input.isKeyDown('ArrowRight')) return 'run';
if (input.isKeyDown('Space')) return 'jump';
if (input.isKeyDown('KeyZ')) return 'attack';
}
})
.addState('run', {
onEnter: () => player.playAnim('run'),
update: (dt, input) => {
if (!input.isKeyDown('ArrowLeft') && !input.isKeyDown('ArrowRight')) return 'idle';
if (input.isKeyDown('Space')) return 'jump';
// ์ด๋ ๋ก์ง
if (input.isKeyDown('ArrowLeft')) player.vx = -200;
if (input.isKeyDown('ArrowRight')) player.vx = 200;
}
})
.addState('jump', {
onEnter: () => {
player.playAnim('jump');
player.vy = -500;
},
update: (dt, input) => {
if (player.onGround) return 'idle';
}
});
playerFSM.start();
๐ ๋ง๋ฌด๋ฆฌ - ์ง๊ธ ๋น์ฅ ์์ํด!
์, ์ฌ๊ธฐ๊น์ง JavaScript ๊ฒ์ ๊ฐ๋ฐ์ ํต์ฌ์ ์ญ ํ์ด๋ดค์ด.
Canvas API๋ถํฐ WebGL, Three.js, Babylon.js, Phaser.js๊น์ง ์ ๋ง ๋ค์ํ ๋๊ตฌ๋ค์ด ์์ง?
๊ทผ๋ฐ ์์งํ ๋งํ ๊ฒ. ๊ฐ์ฅ ์ค์ํ ๊ฑด ์ง๊ธ ๋น์ฅ ๋ญ๊ฐ๋ฅผ ๋ง๋ค์ด๋ณด๋ ๊ฑฐ์ผ. ๐ฅ
์๋ฒฝํ ๊ฒ์์ ๋ง๋ค๋ ค๊ณ ํ์ง ๋ง๊ณ , ๊ณต์ด ํ๊ธฐ๋ ๊ฒ๋ถํฐ ์์ํด๋ด. ๊ทธ๊ฒ ์ง์ง ๊ฒ์ ๊ฐ๋ฐ์ ์์์ด์ผ.
- 1 Canvas API๋ก ๊ณต ํ๊ธฐ๊ธฐ ๊ฒ์ ๋ง๋ค๊ธฐ (30๋ถ)
- 2 Phaser.js ๊ณต์ ํํ ๋ฆฌ์ผ ๋ฐ๋ผํ๊ธฐ (2์๊ฐ)
- 3 Three.js Journey ๊ฐ์ ์์ํ๊ธฐ (๋ฌด๋ฃ ํํธ ์์)
- 4 itch.io์์ HTML5 ๊ฒ์ ์์ค ๋ถ์ํ๊ธฐ
- 5 ๋ค์ Ludum Dare ๊ฒ์ ์ผ ์ฐธ๊ฐ ์ ์ฒญํ๊ธฐ
๊ทธ๋ฆฌ๊ณ ๋ง์ฝ ๊ฒ์ ๊ฐ๋ฐ ์ค๋ ฅ์ ํค์์ ํ๋ฆฌ๋์๋ก ํ๋ํ๊ณ ์ถ๋ค๋ฉด, ์ฌ๋ฅ๋ท์์ ๊ฒ์ ๊ฐ๋ฐ ๊ด๋ จ ์ฌ๋ฅ์ ๋ฑ๋กํด๋ด. ๋ฏธ๋๊ฒ์ ์ ์, ๊ฒ์ UI ๊ฐ๋ฐ, ๊ฒ์ ๋ก์ง ๊ตฌํ ๋ฑ ์์๊ฐ ๊ฝค ์๊ฑฐ๋ ! ๐ฐ
JavaScript ๊ฒ์ ๊ฐ๋ฐ, ์๊ฐ๋ณด๋ค ํจ์ฌ ์ฌ๋ฐ๊ณ ๊ฐ๋ ฅํด. ๋ธ๋ผ์ฐ์ ํ๋๋ก ์ธ์์ ์ ๋ณตํ ์ ์๋ค๋ ๊ฑฐ, ์ด์ ๋๊ปด์ง์ง? ๐
์, ์ด์ ์ฝ๋ ์๋ํฐ ์ด๊ณ ๊ฒ์ ๋ง๋ค๋ฌ ๊ฐ์! ๐ฎโจ
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.