Rpg
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Realm of Eldoria - 3D Adventure</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
overflow: hidden;
background: #080d18;
color: white;
font-family: Arial, sans-serif;
}
#game {
width: 100vw;
height: 100vh;
display: block;
}
#hud {
position: absolute;
top: 15px;
left: 15px;
width: 230px;
background: rgba(0,0,0,.75);
padding: 14px;
border: 1px solid #b58b42;
border-radius: 10px;
}
.stat {
margin: 9px 0;
font-size: 13px;
}
.bar {
height: 12px;
background: #333;
border-radius: 8px;
overflow: hidden;
margin-top: 4px;
}
.fill {
height: 100%;
width: 100%;
transition: width .2s;
}
#healthFill { background: #e33d45; }
#manaFill { background: #328de5; }
#xpFill { background: #e8c34e; }
#quest {
position: absolute;
top: 15px;
right: 15px;
width: 230px;
padding: 14px;
background: rgba(0,0,0,.75);
border: 1px solid #b58b42;
border-radius: 10px;
font-size: 13px;
}
#message {
position: absolute;
bottom: 105px;
left: 50%;
transform: translateX(-50%);
padding: 12px 20px;
background: rgba(0,0,0,.8);
border-radius: 8px;
text-align: center;
display: none;
max-width: 85%;
}
#controls {
position: absolute;
bottom: 15px;
left: 50%;
transform: translateX(-50%);
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 8px;
width: 95%;
}
button {
padding: 12px 16px;
color: white;
background: #49351f;
border: 1px solid #d5a34e;
border-radius: 8px;
cursor: pointer;
font-weight: bold;
}
button:hover {
background: #755329;
}
#startScreen, #deathScreen {
position: absolute;
inset: 0;
background: linear-gradient(#101a2ddd,#050812f5);
display: flex;
justify-content: center;
align-items: center;
text-align: center;
z-index: 5;
}
.panel {
width: min(90%, 500px);
padding: 30px;
background: rgba(10,15,25,.95);
border: 2px solid #c49a4b;
border-radius: 15px;
}
h1 {
color: #f2cc75;
font-size: 32px;
}
#deathScreen {
display: none;
}
#hint {
position: absolute;
bottom: 75px;
right: 15px;
font-size: 12px;
color: #ddd;
text-align: right;
}
@media(max-width:600px) {
#hud { width: 165px; padding: 8px; }
#quest { width: 165px; padding: 8px; }
#hud, #quest { font-size: 11px; }
button { padding: 10px; font-size: 11px; }
h1 { font-size: 24px; }
}
</style>
</head>
<body>
<canvas id="game"></canvas>
<div id="hud">
<b id="playerName">Hero of Eldoria</b>
<div class="stat">Level: <span id="level">1</span></div>
<div class="stat">
Health
<div class="bar"><div class="fill" id="healthFill"></div></div>
<span id="healthText">100 / 100</span>
</div>
<div class="stat">
Mana
<div class="bar"><div class="fill" id="manaFill"></div></div>
<span id="manaText">50 / 50</span>
</div>
<div class="stat">
Experience
<div class="bar"><div class="fill" id="xpFill"></div></div>
<span id="xpText">0 / 100</span>
</div>
<div class="stat">Gold: <span id="gold">0</span></div>
<div class="stat">Crystals: <span id="crystals">0</span> / 5</div>
</div>
<div id="quest">
<b style="color:#f2cc75">QUEST: THE LOST CRYSTALS</b>
<p id="questText">Find the village elder and speak to him.</p>
<hr>
<b>World: Eldoria</b>
<p>Explore the land, defeat monsters, and collect treasure.</p>
</div>
<div id="message"></div>
<div id="hint">
WASD: Move<br>
Mouse: Look around<br>
SPACE: Sword attack<br>
Q: Magic spell<br>
E: Interact
</div>
<div id="controls">
<button onclick="attack()">⚔ ATTACK</button>
<button onclick="castSpell()">✨ MAGIC</button>
<button onclick="interact()">E INTERACT</button>
<button onclick="saveGame()">SAVE</button>
<button onclick="loadGame()">LOAD</button>
</div>
<div id="startScreen">
<div class="panel">
<h1>REALM OF ELDORIA</h1>
<h3 style="color:#d5b875">The Lost Kingdom</h3>
<p>A dangerous world awaits you, hero.</p>
<p>Explore the kingdom, defeat monsters, collect crystals, and complete your quest.</p>
<p><b>WASD</b> Move | <b>Mouse</b> Look</p>
<p><b>SPACE</b> Attack | <b>Q</b> Magic | <b>E</b> Interact</p>
<button onclick="startGame()">ENTER THE KINGDOM</button>
</div>
</div>
<div id="deathScreen">
<div class="panel">
<h1>You Have Fallen</h1>
<p>Your adventure is not over yet.</p>
<button onclick="restartGame()">RESPAWN</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/0.128.0/three.min.js"></script>
<script>
"use strict";
let scene, camera, renderer, clock;
let player, ground;
let enemies = [];
let crystals = [];
let trees = [];
let keys = {};
let health = 100;
let maxHealth = 100;
let mana = 50;
let maxMana = 50;
let xp = 0;
let level = 1;
let gold = 0;
let crystalCount = 0;
let started = false;
let dead = false;
let yaw = 0;
let pitch = 0.25;
let questStarted = false;
let questFinished = false;
let lastAttack = 0;
let lastSpell = 0;
let messageTimer = null;
const WORLD = 100;
function makeMaterial(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color: color,
roughness: 0.85,
...extra
});
}
function showMessage(text) {
const box = document.getElementById("message");
box.textContent = text;
box.style.display = "block";
clearTimeout(messageTimer);
messageTimer = setTimeout(() => {
box.style.display = "none";
}, 2500);
}
function updateHUD() {
document.getElementById("level").textContent = level;
document.getElementById("gold").textContent = gold;
document.getElementById("crystals").textContent = crystalCount;
document.getElementById("healthText").textContent =
Math.max(0, Math.floor(health)) + " / " + maxHealth;
document.getElementById("manaText").textContent =
Math.floor(mana) + " / " + maxMana;
document.getElementById("xpText").textContent =
xp + " / " + (level * 100);
document.getElementById("healthFill").style.width =
(health / maxHealth * 100) + "%";
document.getElementById("manaFill").style.width =
(mana / maxMana * 100) + "%";
document.getElementById("xpFill").style.width =
(xp / (level * 100) * 100) + "%";
}
function updateQuest() {
const text = document.getElementById("questText");
if (questFinished) {
text.textContent = "Quest completed! You restored the lost crystals.";
} else if (questStarted) {
text.textContent = "Collect 5 magical crystals. You have " +
crystalCount + " / 5.";
} else {
text.textContent = "Find the village elder and speak to him.";
}
}
function init() {
if (typeof THREE === "undefined") {
alert("The 3D library could not load. Please check your internet connection.");
return;
}
scene = new THREE.Scene();
scene.background = new THREE.Color(0x8ab9d5);
scene.fog = new THREE.Fog(0x8ab9d5, 45, 130);
camera = new THREE.PerspectiveCamera(
70,
window.innerWidth / window.innerHeight,
0.1,
300
);
renderer = new THREE.WebGLRenderer({
canvas: document.getElementById("game"),
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
clock = new THREE.Clock();
const ambient = new THREE.HemisphereLight(0xffffff, 0x46552d, 1.2);
scene.add(ambient);
const sunlight = new THREE.DirectionalLight(0xffe5b2, 1.5);
sunlight.position.set(-30, 50, 30);
sunlight.castShadow = true;
scene.add(sunlight);
createWorld();
createPlayer();
createNPC();
createEnemies();
createCrystals();
createChests();
window.addEventListener("resize", resize);
window.addEventListener("keydown", keyDown);
window.addEventListener("keyup", keyUp);
renderer.domElement.addEventListener("click", function() {
if (started && !dead && document.pointerLockElement !== renderer.domElement) {
renderer.domElement.requestPointerLock();
}
});
document.addEventListener("mousemove", function(event) {
if (document.pointerLockElement === renderer.domElement) {
yaw -= event.movementX * 0.0025;
pitch -= event.movementY * 0.002;
pitch = Math.max(-0.1, Math.min(0.8, pitch));
}
});
updateHUD();
animate();
}
function createWorld() {
ground = new THREE.Mesh(
new THREE.PlaneGeometry(WORLD * 2, WORLD * 2),
makeMaterial(0x4c873e)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.1;
ground.receiveShadow = true;
scene.add(ground);
// Paths
const path = new THREE.Mesh(
new THREE.PlaneGeometry(8, 100),
makeMaterial(0x9a8058)
);
path.rotation.x = -Math.PI / 2;
path.position.set(0, 0.01, 0);
scene.add(path);
// Village buildings
for (let i = 0; i < 5; i++) {
const x = i % 2 === 0 ? -12 : 12;
const z = -10 + Math.floor(i / 2) * 12;
createHouse(x, z);
}
// Trees
for (let i = 0; i < 100; i++) {
let x = (Math.random() - 0.5) * 170;
let z = (Math.random() - 0.5) * 170;
if (Math.abs(x) < 18 && Math.abs(z) < 35) continue;
createTree(x, z);
}
// Mountains
for (let i = 0; i < 16; i++) {
const mountain = new THREE.Mesh(
new THREE.ConeGeometry(8 + Math.random() * 7, 18 + Math.random() * 15, 5),
makeMaterial(0x65716d)
);
mountain.position.set(
(Math.random() - 0.5) * 180,
8,
(Math.random() - 0.5) * 180
);
scene.add(mountain);
}
// Decorative rocks
for (let i = 0; i < 40; i++) {
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.7 + Math.random()),
makeMaterial(0x777777)
);
rock.position.set(
(Math.random() - 0.5) * 150,
0.5,
(Math.random() - 0.5) * 150
);
rock.rotation.set(Math.random(), Math.random(), Math.random());
scene.add(rock);
}
}
function createTree(x, z) {
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.5, 3, 7),
makeMaterial(0x68401f)
);
trunk.position.set(x, 1.5, z);
trunk.castShadow = true;
scene.add(trunk);
const leaves = new THREE.Mesh(
new THREE.ConeGeometry(2.2, 5, 8),
makeMaterial(0x18552d)
);
leaves.position.set(x, 5, z);
leaves.castShadow = true;
scene.add(leaves);
trees.push({x:x, z:z});
}
function createHouse(x, z) {
const building = new THREE.Mesh(
new THREE.BoxGeometry(8, 5, 7),
makeMaterial(0xb68a59)
);
building.position.set(x, 2.5, z);
building.castShadow = true;
scene.add(building);
const roof = new THREE.Mesh(
new THREE.ConeGeometry(6, 4, 4),
makeMaterial(0x743b2b)
);
roof.position.set(x, 7, z);
roof.rotation.y = Math.PI / 4;
roof.castShadow = true;
scene.add(roof);
const door = new THREE.Mesh(
new THREE.BoxGeometry(1.5, 2.8, 0.2),
makeMaterial(0x392215)
);
door.position.set(x, 1.4, z - 3.6);
scene.add(door);
}
function createPlayer() {
player = new THREE.Group();
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.55, 0.65, 1.8, 10),
makeMaterial(0x2859a3)
);
body.position.y = 1.3;
body.castShadow = true;
player.add(body);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.48, 12, 12),
makeMaterial(0xe8b58c)
);
head.position.y = 2.5;
player.add(head);
const sword = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 1.4, 0.2),
makeMaterial(0xdce3e8, {
metalness: 0.7,
roughness: 0.25
})
);
sword.position.set(0.8, 1.5, 0);
sword.rotation.z = -0.3;
player.add(sword);
const handle = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.09, 0.5, 8),
makeMaterial(0x53351d)
);
handle.position.set(0.8, 0.7, 0);
player.add(handle);
player.position.set(0, 0, 12);
scene.add(player);
}
function createNPC() {
const npc = new THREE.Group();
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.55, 0.65, 1.8, 10),
makeMaterial(0xd2aa38)
);
body.position.y = 1.2;
npc.add(body);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.45, 12, 12),
makeMaterial(0xe6b48a)
);
head.position.y = 2.35;
npc.add(head);
npc.position.set(3, 0, 3);
scene.add(npc);
window.elder = npc;
const sign = new THREE.Mesh(
new THREE.BoxGeometry(2, 1.5, 0.2),
makeMaterial(0x55331b)
);
sign.position.set(3, 1, 0);
scene.add(sign);
}
function createEnemies() {
for (let i = 0; i < 12; i++) {
const enemy = new THREE.Group();
const body = new THREE.Mesh(
new THREE.BoxGeometry(1.2, 1.5, 1),
makeMaterial(0x8d2525)
);
body.position.y = 1;
enemy.add(body);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.55, 10, 10),
makeMaterial(0x4b1515)
);
head.position.y = 2.1;
enemy.add(head);
const x = (Math.random() - 0.5) * 90;
const z = (Math.random() - 0.5) * 90;
enemy.position.set(x, 0, z);
enemy.userData = {
hp: 50,
speed: 1.5 + Math.random(),
damageTimer: 0,
dead: false
};
scene.add(enemy);
enemies.push(enemy);
}
}
function createCrystals() {
for (let i = 0; i < 10; i++) {
const crystal = new THREE.Mesh(
new THREE.OctahedronGeometry(0.8),
new THREE.MeshStandardMaterial({
color: 0x35eaff,
emissive: 0x087b99,
metalness: 0.5,
roughness: 0.2
})
);
crystal.position.set(
(Math.random() - 0.5) * 80,
1,
(Math.random() - 0.5) * 80
);
crystal.userData.collected = false;
scene.add(crystal);
crystals.push(crystal);
}
}
function createChests() {
for (let i = 0; i < 5; i++) {
const chest = new THREE.Mesh(
new THREE.BoxGeometry(1.8, 1, 1.2),
makeMaterial(0x9c5d22)
);
chest.position.set(
(Math.random() - 0.5) * 70,
0.5,
(Math.random() - 0.5) * 70
);
chest.userData.opened = false;
scene.add(chest);
chests.push(chest);
}
}
let chests = [];
function keyDown(e) {
keys[e.code] = true;
if (e.code === "Space") {
e.preventDefault();
attack();
}
if (e.code === "KeyQ") castSpell();
if (e.code === "KeyE") interact();
}
function keyUp(e) {
keys[e.code] = false;
}
function startGame() {
if (!scene) init();
if (!scene) return;
started = true;
document.getElementById("startScreen").style.display = "none";
showMessage("Welcome to Eldoria, hero!");
}
function resize() {
if (!camera || !renderer) return;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function attack() {
if (!started || dead) return;
const now = performance.now();
if (now - lastAttack < 500) return;
lastAttack = now;
let hit = false;
enemies.forEach(enemy => {
if (enemy.userData.dead) return;
const distance = player.position.distanceTo(enemy.position);
if (distance < 4) {
enemy.userData.hp -= 25;
hit = true;
if (enemy.userData.hp <= 0) {
killEnemy(enemy);
}
}
});
showMessage(hit ? "You struck an enemy!" : "Your sword cuts through the air!");
}
function killEnemy(enemy) {
enemy.userData.dead = true;
scene.remove(enemy);
xp += 35;
gold += 10;
if (xp >= level * 100) {
xp -= level * 100;
level++;
maxHealth += 20;
maxMana += 10;
health = maxHealth;
mana = maxMana;
showMessage("LEVEL UP! You are now level " + level);
} else {
showMessage("Enemy defeated! +35 XP and 10 gold.");
}
updateHUD();
}
function castSpell() {
if (!started || dead) return;
const now = performance.now();
if (now - lastSpell < 900) return;
if (mana < 10) {
showMessage("Not enough mana!");
return;
}
lastSpell = now;
mana -= 10;
const direction = new THREE.Vector3(
-Math.sin(yaw),
0,
-Math.cos(yaw)
);
const spell = new THREE.Mesh(
new THREE.SphereGeometry(0.35, 12, 12),
new THREE.MeshBasicMaterial({
color: 0x42caff
})
);
spell.position.copy(player.position);
spell.position.y += 1.5;
scene.add(spell);
let distanceTravelled = 0;
const moveSpell = () => {
if (!spell.parent) return;
spell.position.addScaledVector(direction, 1.2);
distanceTravelled += 1.2;
let hit = false;
enemies.forEach(enemy => {
if (enemy.userData.dead) return;
if (spell.position.distanceTo(
enemy.position.clone().add(new THREE.Vector3(0,1,0))
) < 2) {
enemy.userData.hp -= 50;
hit = true;
if (enemy.userData.hp <= 0) {
killEnemy(enemy);
}
}
});
if (hit || distanceTravelled > 35) {
scene.remove(spell);
} else {
requestAnimationFrame(moveSpell);
}
};
moveSpell();
updateHUD();
showMessage("Magic spell cast!");
}
function interact() {
if (!started || dead) return;
if (player.position.distanceTo(window.elder.position) < 5) {
if (!questStarted) {
questStarted = true;
showMessage("Elder: Please find 5 magical crystals for our village!");
} else if (crystalCount >= 5 && !questFinished) {
questFinished = true;
gold += 100;
xp += 100;
showMessage("Elder: You saved our village! Reward: 100 gold and 100 XP!");
} else if (questFinished) {
showMessage("Elder: Thank you again, brave hero!");
} else {
showMessage("Elder: Please find the 5 magical crystals!");
}
updateQuest();
updateHUD();
return;
}
for (const chest of chests) {
if (!chest.userData.opened &&
player.position.distanceTo(chest.position) < 4) {
chest.userData.opened = true;
chest.material = makeMaterial(0x444444);
gold += 25;
showMessage("Treasure chest opened! +25 gold.");
updateHUD();
return;
}
}
showMessage("There is nothing nearby to interact with.");
}
function movePlayer(delta) {
const forward = new THREE.Vector3(
-Math.sin(yaw),
0,
-Math.cos(yaw)
);
const right = new THREE.Vector3(
Math.cos(yaw),
0,
-Math.sin(yaw)
);
const movement = new THREE.Vector3();
if (keys["KeyW"] || keys["ArrowUp"]) movement.add(forward);
if (keys["KeyS"] || keys["ArrowDown"]) movement.sub(forward);
if (keys["KeyD"] || keys["ArrowRight"]) movement.add(right);
if (keys["KeyA"] || keys["ArrowLeft"]) movement.sub(right);
if (movement.length() > 0) {
movement.normalize();
const nextX = player.position.x + movement.x * 9 * delta;
const nextZ = player.position.z + movement.z * 9 * delta;
player.position.x = Math.max(-WORLD + 3, Math.min(WORLD - 3, nextX));
player.position.z = Math.max(-WORLD + 3, Math.min(WORLD - 3, nextZ));
player.rotation.y = Math.atan2(movement.x, movement.z);
}
}
function updateEnemies(delta) {
enemies.forEach(enemy => {
if (enemy.userData.dead) return;
const distance = enemy.position.distanceTo(player.position);
if (distance < 22 && distance > 1.6) {
const direction = new THREE.Vector3()
.subVectors(player.position, enemy.position)
.normalize();
enemy.position.addScaledVector(
direction,
enemy.userData.speed * delta
);
enemy.lookAt(player.position.x, 0, player.position.z);
}
if (distance < 2.2) {
enemy.userData.damageTimer -= delta;
if (enemy.userData.damageTimer <= 0) {
health -= 8;
enemy.userData.damageTimer = 1;
updateHUD();
if (health <= 0) {
die();
}
}
}
});
}
function updateCollectibles() {
crystals.forEach(crystal => {
if (crystal.userData.collected) return;
crystal.rotation.y += 0.025;
crystal.position.y = 1 + Math.sin(performance.now() * 0.002) * 0.2;
if (player.position.distanceTo(crystal.position) < 2.5) {
crystal.userData.collected = true;
scene.remove(crystal);
crystalCount++;
gold += 5;
showMessage("Magical crystal collected! +5 gold.");
updateHUD();
updateQuest();
}
});
}
function updateCamera() {
const distance = 9;
const target = new THREE.Vector3(
player.position.x + Math.sin(yaw) * distance,
player.position.y + 4 + pitch * 3,
player.position.z + Math.cos(yaw) * distance
);
camera.position.lerp(target, 0.12);
const lookTarget = player.position.clone();
lookTarget.y += 1.5;
camera.lookAt(lookTarget);
}
function die() {
dead = true;
document.getElementById("deathScreen").style.display = "flex";
if (document.pointerLockElement) {
document.exitPointerLock();
}
}
function restartGame() {
health = maxHealth;
mana = maxMana;
player.position.set(0, 0, 12);
enemies.forEach(enemy => {
if (enemy.userData.dead) {
enemy.userData.dead = false;
enemy.userData.hp = 50;
scene.add(enemy);
}
});
dead = false;
document.getElementById("deathScreen").style.display = "none";
updateHUD();
showMessage("You have returned to the kingdom!");
}
function saveGame() {
if (!player) return;
const saveData = {
x: player.position.x,
z: player.position.z,
health: health,
mana: mana,
xp: xp,
level: level,
gold: gold,
crystals: crystalCount,
questStarted: questStarted,
questFinished: questFinished
};
try {
localStorage.setItem("eldoriaSave", JSON.stringify(saveData));
showMessage("Game saved successfully!");
} catch (error) {
showMessage("Saving is unavailable in this browser.");
}
}
function loadGame() {
try {
const data = localStorage.getItem("eldoriaSave");
if (!data) {
showMessage("No saved game found!");
return;
}
const save = JSON.parse(data);
if (!save || typeof save.x !== "number" ||
typeof save.z !== "number") {
showMessage("The save file is invalid.");
return;
}
player.position.set(save.x, 0, save.z);
health = Math.max(1, Math.min(maxHealth, save.health));
mana = Math.max(0, Math.min(maxMana, save.mana));
xp = Math.max(0, save.xp);
level = Math.max(1, save.level);
gold = Math.max(0, save.gold);
crystalCount = Math.max(0, save.crystals);
questStarted = !!save.questStarted;
questFinished = !!save.questFinished;
updateHUD();
updateQuest();
showMessage("Saved game loaded!");
} catch (error) {
showMessage("Unable to load the saved game.");
}
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (started && !dead) {
movePlayer(delta);
updateEnemies(delta);
updateCollectibles();
updateCamera();
mana = Math.min(maxMana, mana + delta * 1.5);
updateHUD();
}
renderer.render(scene, camera);
}
init();
</script>
</body>
</html>
Featured
Advertisement
Category
Action