js实现三维古堡内骑士打斗游戏代码

代码语言:html

所属分类:游戏

代码描述:js实现三维古堡内骑士打斗游戏代码

代码标签: js 三维 古堡 骑士 打斗 游戏 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>烬堡 EMBERHOLD</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
html,body{width:100%;height:100%;background:#000;overflow:hidden;color:#e6dfd2;
  font-family:"Noto Serif SC","Source Han Serif SC","Songti SC","STSong","SimSun",Georgia,"Times New Roman",serif;
  -webkit-font-smoothing:antialiased;user-select:none;-webkit-user-select:none}
#c{position:fixed;inset:0;width:100%;height:100%;display:block}
.layer{position:fixed;inset:0;pointer-events:none}
#hud{opacity:0;transition:opacity .6s}
#hud.on{opacity:1}
#bars{position:absolute;left:44px;top:36px}
.bar{height:11px;background:rgba(8,7,6,.72);border:1px solid rgba(190,170,130,.42);position:relative;margin-bottom:6px;box-shadow:0 1px 6px rgba(0,0,0,.6)}
.bar b,.bar i{position:absolute;left:0;top:0;bottom:0;display:block}
.bar i{background:#b99a4a;transition:width .5s .35s}
#hpb{background:linear-gradient(#a32a1e,#6e1710)}
#stb{background:linear-gradient(#5d8f3e,#33591f)}
#st.bar{height:8px}
#flask{position:absolute;left:44px;bottom:40px;display:flex;align-items:flex-end;gap:12px;text-shadow:0 1px 6px #000}
#flask .ic{width:26px;height:38px;border:2px solid rgba(220,200,160,.8);border-radius:5px 5px 12px 12px;position:relative;overflow:hidden}
#flask .ic:before{content:"";position:absolute;left:0;right:0;bottom:0;height:var(--f,70%);background:linear-gradient(#ffb04a,#c9541a)}
#flask .n{font-size:30px;line-height:1}
#flask .k{font-size:13px;opacity:.6;letter-spacing:2px;margin-left:4px}
#ash{position:absolute;right:44px;bottom:40px;min-width:150px;text-align:right;padding:7px 14px 6px;background:rgba(8,7,6,.6);border:1px solid rgba(190,170,130,.35);font-size:22px;letter-spacing:1px;text-shadow:0 1px 6px #000}
#ash small{font-size:12px;opacity:.6;letter-spacing:4px;display:block}
#boss{position:absolute;left:20%;right:20%;bottom:112px;opacity:0;transition:opacity .8s}
#boss.on{opacity:1}
#boss .nm{font-size:19px;letter-spacing:5px;margin-bottom:6px;text-shadow:0 1px 8px #000}
#boss .bar{height:13px}
#bsb{background:linear-gradient(#b0301f,#6a1510)}
#banner{position:absolute;top:36%;left:0;right:0;text-align:center;opacity:0;transition:opacity 1.2s;padding:26px 0;
  background:linear-gradient(90deg,transparent,rgba(0,0,0,.72) 25%,rgba(0,0,0,.72) 75%,transparent)}
#banner h2{font-size:58px;letter-spacing:22px;font-weight:400;margin-left:22px;color:#d9c27a}
#banner.dead h2{color:#9c1f16}
#banner p{font-size:15px;letter-spacing:8px;opacity:.7;margin-top:8px}
#zone{position:absolute;top:19%;left:0;right:0;text-align:center;font-size:30px;letter-spacing:14px;opacity:0;transition:opacity 1.4s;text-shadow:0 2px 14px #000}
#zone:after{content:"";display:block;width:260px;height:1px;margin:10px auto 0;background:linear-gradient(90deg,transparent,rgba(220,200,160,.7),transparent)}
#prompt{position:absolute;bottom:150px;left:0;right:0;text-align:center;font-size:18px;letter-spacing:4px;opacity:0;transition:opacity .25s;text-shadow:0 1px 8px #000}
#prompt b{display:inline-block;border:1px solid rgba(230,223,210,.7);padding:1px 9px;margin-right:10px;font-weight:400;font-family:Georgia,serif}
#lock{position:absolute;width:14px;height:14px;margin:-7px 0 0 -7px;border:2px solid #f2ead8;border-radius:50%;opacity:0;box-shadow:0 0 6px #000}
#lock:after{content:"";position:absolute;left:3px;top:3px;width:4px;height:4px;background:#f2ead8;border-radius:50%}
.ebar{position:absolute;width:86px;height:5px;margin-left:-43px;background:rgba(8,7,6,.75);border:1px solid rgba(190,170,130,.4);display:none}
.ebar b{display:block;height:100%;background:#a32a1e}
#feed{position:absolute;left:44px;top:96px;font-size:15px;letter-spacing:2px}
#feed div{opacity:0;animation:fd 3.2s ease-out forwards;text-shadow:0 1px 6px #000;margin-bottom:3px}
@keyframes fd{0%{opacity:0;transform:translateX(-10px)}10%{opacity:.9;transform:none}80%{opacity:.9}100%{opacity:0}}
#q{position:absolute;right:44px;top:30px;font-size:12px;letter-spacing:3px;opacity:.4;font-family:Georgia,serif}
#lvl{position:fixed;inset:0;display:flex;align-items:center;justify-content:center;background:rgba(2,2,2,.55);opacity:0;pointer-events:none;transition:opacity .4s}
#lvl.on{opacity:1}
#lvl .box{width:460px;padding:30px 36px;background:rgba(10,9,8,.9);border:1px solid rgba(190,170,130,.45)}
#lvl h3{font-size:26px;letter-spacing:10px;font-weight:400;color:#d9c27a;text-align:center;margin-bottom:6px}
#lvl .sub{text-align:center;font-size:13px;letter-spacing:3px;opacity:.6;margin-bottom:20px}
#lvl .row{display:flex;justify-content:space-between;align-items:center;padding:9px 0;border-top:1px solid rgba(190,170,130,.16);font-size:18px;letter-spacing:2px}
#lvl .row b{display:inline-block;border:1px solid rgba(230,223,210,.6);padding:0 8px;margin-right:12px;font-weight:400;font-family:Georgia,serif;font-size:15px}
#lvl .row span.v{opacity:.75;font-size:15px}
#lvl .row.no{opacity:.4}
#lvl .foot{margin-top:18px;text-align:center;font-size:13px;letter-spacing:3px;opacity:.6}
#menu,#load{position:fixed;inset:0;display:flex;flex-direction:column;align-items:center;justify-content:center;text-align:center;
  background:radial-gradient(ellipse at center,rgba(6,5,4,.3) 0%,rgba(2,2,2,.92) 100%);transition:opacity .6s}
#load{background:#040303;z-index:5}
#load p{letter-spacing:8px;font-size:16px;opacity:.7}
#load .lb{width:240px;height:2px;background:#221c14;margin-top:16px;overflow:hidden}
#load .lb b{display:block;height:100%;width:40%;background:#d9c27a;animation:ld 1.1s infinite ease-in-out}
@keyframes ld{0%{transform:translateX(-100%)}100%{transform:translateX(260%)}}
#menu h1{font-size:120px;letter-spacing:40px;font-weight:400;line-height:1;margin-left:40px;color:#e6dfd2;text-shadow:0 4px 40px rgba(0,0,0,.9)}
#menu h1 span{color:#e0793a}
#menu .sub{font-size:16px;letter-spacing:12px;opacity:.65;margin:16px 0 46px;font-family:Georgia,serif}
#menu .go{pointer-events:auto;cursor:pointer;font:inherit;font-size:22px;letter-spacing:12px;color:#e6dfd2;background:rgba(20,16,12,.7);border:1px solid rgba(217,194,122,.7);padding:13px 46px 13px 58px;transition:background .15s,color .15s}
#menu .go:hover{background:#d9c27a;color:#0a0806}
#menu .keys{margin-top:44px;font-size:14px;letter-spacing:2px;opacity:.66;line-height:2.3}
#menu .keys b{display:inline-block;border:1px solid rgba(230,223,210,.5);padding:0 8px;margin:0 6px 0 16px;font-weight:400;font-family:Georgia,serif}
#menu .stat{font-size:18px;letter-spacing:5px;margin-bottom:30px;opacity:.9;white-space:pre-wrap;line-height:1.9}
.hide{opacity:0!important;pointer-events:none!important}
.hide *{pointer-events:none!important}
</style>
<script type="importmap">{"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/"}}</script>
</head>
<body>
<canvas id="c"></canvas>
<div id="hud" class="layer">
  <div id="q"></div>
  <div id="bars">
    <div class="bar" id="hp"><i id="hpg"></i><b id="hpb"></b></div>
    <div class="bar" id="st"><b id="stb"></b></div>
  </div>
  <div id="feed"></div>
  <div id="zone"></div>
  <div id="banner"><h2></h2><p></p></div>
  <div id="ebars"></div>
  <div id="lock"></div>
  <div id="prompt"></div>
  <div id="boss"><div class="nm" id="bsn"></div><div class="bar"><i id="bsg"></i><b id="bsb"></b></div></div>
  <div id="flask"><div class="ic" id="fli"></div><div class="n" id="fln">3</div><div class="k">余烬瓶 R</div></div>
  <div id="ash"><small>灰烬</small><span id="ashn">0</span></div>
</div>
<div id="lvl"><div class="box">
  <h3>灯火旁</h3><div class="sub" id="lvs"></div>
  <div class="row" id="lv1"><span><b>1</b>体魄</span><span class="v"></span></div>
  <div class="row" id="lv2"><span><b>2</b>耐力</span><span class="v"></span></div>
  <div class="row" id="lv3"><span><b>3</b>力量</span><span class="v"></span></div>
  <div class="foot">按 <b style="border:1px solid rgba(230,223,210,.6);padding:0 8px;font-weight:400;font-family:Georgia,serif">E</b> 起身</div>
</div></div>
<div id="menu" class="hide">
  <h1 id="mt">烬<span>堡</span></h1>
  <div class="sub" id="ms">EMBERHOLD &nbsp;·&nbsp; 第一关 无名囚室</div>
  <div class="stat" id="mstat" style="display:none"></div>
  <button class="go" id="go">开始</button>
  <div class="keys"><b>W A S D</b>移动<b>鼠标</b>视角<b>左键</b>轻击<b>右键</b>重击<b>空格</b>翻滚<b>Shift</b>奔跑<br><b>Q</b>锁定<b>R</b>余烬瓶<b>E</b>互动<b>G</b>画质<b>V</b>震动<b>M</b>静音<b>Esc</b>暂停</div>
</div>
<div id="load"><p id="lt">正在砌石</p><div class="lb"><b></b></div></div>
<script type="module">
/* ══════ 10-core.js ══════ */
import * as THREE from 'three';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
import { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js';
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
import { Pass, FullScreenQuad } from 'three/addons/postprocessing/Pass.js';
import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js';

const $ = id => document.getElementById(id);
const URLP = new URLSearchParams(location.search);
const stage = t => { $('lt').textContent = t; return new Promise(r => setTimeout(r, 30)); };
await stage('正在砌石');

/* ───────────────────────── 数学、随机、噪声 ───────────────────────── */
const V3 = THREE.Vector3, PI = Math.PI, TAU = PI * 2;
const clamp = (x, a = 0, b = 1) => x < a ? a : x > b ? b : x;
const lerp = (a, b, t) => a + (b - a) * t;
const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a), 0, 1); return t * t * (3 - 2 * t); };
const damp = (a, b, k, dt) => lerp(a, b, 1 - Math.exp(-k * dt));
const angDiff = (a, b) => { const d = b - a; return Math.atan2(Math.sin(d), Math.cos(d)); };
const dampAng = (a, b, k, dt) => a + angDiff(a, b) * (1 - Math.exp(-k * dt));
function mulberry(a) { return () => { a |= 0; a = a + 0x6D2B79F5 | 0; let t = Math.imul(a ^ a >>> 15, 1 | a); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; }
const R = mulberry(20261113);
const rr = (a, b) => a + (b - a) * R();
const pick = arr => arr[(R() * arr.length) | 0];

const PERM = new Uint8Array(512), VAL = new Float32Array(256);
{ const r = mulberry(7); for (let i = 0; i < 256; i++) { PERM[i] = i; VAL[i] = r(); }
  for (let i = 255; i > 0; i--) { const j = (r() * (i + 1)) | 0; const t = PERM[i]; PERM[i] = PERM[j]; PERM[j] = t; }
  for (let i = 0; i < 256; i++) PERM[i + 256] = PERM[i]; }
function vn(x, y, px, py, s) {
  const xi = Math.floor(x), yi = Math.floor(y), fx = x - xi, fy = y - yi;
  const x0 = ((xi % px) + px) % px, x1 = (x0 + 1) % px, y0 = ((yi % py) + py) % py, y1 = (y0 + 1) % py;
  const u = fx * fx * (3 - 2 * fx), v = fy * fy * (3 - 2 * fy);
  const a = VAL[PERM[(PERM[x0 & 255] + y0 + s) & 255]], b = VAL[PERM[(PERM[x1 & 255] + y0 + s) & 255]];
  const c = VAL[PERM[(PERM[x0 & 255] + y1 + s) & 255]], d = VAL[PERM[(PERM[x1 & 255] + y1 + s) & 255]];
  return a + (b - a) * u + (c - a) * v + (a - b - c + d) * u * v;
}
function fbm(u, v, oct, px, py, s = 0) {
  let a = .5, sum = 0, n = 0;
  for (let i = 0; i < oct; i++) { sum += a * vn(u * px, v * py, px, py, s + i * 19); n += a; a *= .5; px *= 2; py *= 2; if (px > 256 || py > 256) break; }
  return sum / n;
}
const h2 = (ix, iy, s = 0) => { let h = Math.imul(ix * 374761393 + iy * 668265263 + s * 974711 | 0, 1274126177); h ^= h >>> 15; h = Math.imul(h, 2246822519); return ((h >>> 8) & 0xffff) / 65535; };
const wob = (t, s = 0) => vn(t, s * 3.7, 256, 256, s) * 2 - 1;          // 一维平滑噪声,-1..1

/* ───────────────────────── 渲染器 ───────────────────────── */
const canvas = $('c');
const renderer = new THREE.WebGLRenderer({ canvas, antialias: false, powerPreference: 'high-performance', stencil: false });
const gl = renderer.getContext();
const dbgInfo = gl.getExtension('WEBGL_debug_renderer_info');
const GPU = dbgInfo ? gl.getParameter(dbgInfo.UNMASKED_RENDERER_WEBGL) : gl.getParameter(gl.RENDERER);
const SOFT = /swiftshader|llvmpipe|software/i.test(GPU);
const prFor = () => URLP.has('pr') ? parseFloat(URLP.get('pr')) : Math.min(window.devicePixelRatio || 1, 1.5, Math.max(.75, Math.sqrt(2.9e6 / (innerWidth * innerHeight))));
let PR = prFor();
renderer.setPixelRatio(PR);
renderer.setSize(innerWidth, innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.2;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
renderer.shadowMap.autoUpdate = false;
const MAXA = Math.min(8, renderer.capabilities.getMaxAnisotropy());

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x030405);
scene.fog = new THREE.FogExp2(0x0b0e13, 0.021);
const camera = new THREE.PerspectiveCamera(58, innerWidth / innerHeight, 0.08, 130);
scene.add(camera);

/* ───────────────────────── 程序化贴图 ───────────────────────── */
function mkCanvas(w, h) { const c = document.createElement('canvas'); c.width = w; c.height = h; return c; }
function texFrom(c, srgb, repeat = true) {
  const t = new THREE.CanvasTexture(c);
  if (srgb) t.colorSpace = THREE.SRGBColorSpace;
  if (repeat) t.wrapS = t.wrapT = THREE.RepeatWrapping;
  t.anisotropy = MAXA; return t;
}
// fn(u, v, o, x, y) 填 o.h 高度 / o.r o.g o.b 颜色 / o.rough / o.metal / o.ao;法线由高度差算出
function pbr(N, fn, ns = 2) {
  const H = new Float32Array(N * N), ca = mkCanvas(N, N), co = mkCanvas(N, N), cn = mkCanvas(N, N);
  const xa = ca.getContext('2d'), xo = co.getContext('2d'), xn = cn.getContext('2d');
  const ia = xa.createImageData(N, N), io = xo.createImageData(N, N), im = xn.createImageData(N, N);
  const o = { h: .5, r: .5, g: .5, b: .5, rough: .5, metal: 0, ao: 1 };
  for (let y = 0, i = 0; y < N; y++) for (let x = 0; x < N; x++, i++) {
    o.ao = 1; o.metal = 0; fn(x / N, y / N, o, x, y); H[i] = o.h; const j = i * 4;
    ia.data[j] = o.r * 255; ia.data[j + 1] = o.g * 255; ia.data[j + 2] = o.b * 255; ia.data[j + 3] = 255;
    io.data[j] = o.ao * 255; io.data[j + 1] = o.rough * 255; io.data[j + 2] = o.metal * 255; io.data[j + 3] = 255;
  }
  const k = ns * N / 256;
  for (let y = 0, i = 0; y < N; y++) for (let x = 0; x < N; x++, i++) {
    const xl = (x + N - 1) % N, xr = (x + 1) % N, yu = (y + N - 1) % N, yd = (y + 1) % N;
    const nx = -(H[y * N + xr] - H[y * N + xl]) * k, ny = (H[yd * N + x] - H[yu * N + x]) * k;
    const l = 1 / Math.hypot(nx, ny, 1), j = i * 4;
    im.data[j] = (nx * l * .5 + .5) * 255; im.data[j + 1] = (ny * l * .5 + .5) * 255; im.data[j + 2] = (l * .5 + .5) * 255; im.data[j + 3] = 255;
  }
  xa.putImageData(ia, 0, 0); xo.putImageData(io, 0, 0); xn.putImageData(im, 0, 0);
  return { map: texFrom(ca, true), normalMap: texFrom(cn, false), orm: texFrom(co, false) };
}
function layer(N, oct, px, py = px, s = 0) { const f = new Float32Array(N * N); for (let y = 0, i = 0; y < N; y++) for (let x = 0; x < N; x++, i++) f[i] = fbm(x / N, y / N, oct, px, py, s); return f; }
function maskOf(N, draw) {
  const c = mkCanvas(N, N), x = c.getContext('2d'); x.fillStyle = '#000'; x.fillRect(0, 0, N, N); draw(x, N);
  const d = x.getImageData(0, 0, N, N).data, f = new Float32Array(N * N);
  for (let i = 0; i < f.length; i++) f[i] = d[i * 4] / 255; return f;
}
// 所有贴图材质都带顶点色:同一个材质可以染成很多种颜色
function stdMat(t, o = {}) {
  const m = new THREE.MeshStandardMaterial({ map: t.map, normalMap: t.normalMap, roughnessMap: t.orm, metalnessMap: t.orm, aoMap: t.orm, roughness: 1, metalness: 1, vertexColors: true });
  m.userData.tile = o.tile || 1; if (o.ns) m.normalScale.set(o.ns, o.ns);
  if (o.env !== undefined) m.envMapIntensity = o.env; if (o.side) m.side = o.side; return m;
}
const flatMat = (color, rough = .6, metal = 0) => { const m = new THREE.MeshStandardMaterial({ color, roughness: rough, metalness: metal, vertexColors: true }); m.userData.tile = 1; return m; };
const emis = (c, i) => { const m = new THREE.MeshStandardMaterial({ color: 0, emissive: new THREE.Color(...c), emissiveIntensity: i, roughness: .4 }); m.userData.tile = 1; return m; };

const T = {};
// 石墙:1.0 × 0.5 m 的条石错缝砌,3 m 一块贴图;灰缝凹进去,水渍往下流,有气孔、裂纹、潮斑
{ const N = 1024, n1 = layer(N, 5, 3, 3, 61), fine = layer(N, 3, 96, 96, 62), n2 = layer(N, 4, 14, 14, 63), streak = layer(N, 4, 40, 2, 64), cr = layer(N, 5, 5, 5, 65);
  const PORE = maskOf(N, (x) => { for (let i = 0; i < 2000; i++) { const s = rr(.7, 2.6); x.fillStyle = `rgba(255,255,255,${rr(.3, 1)})`; x.beginPath(); x.ellipse(rr(4, N - 4), rr(4, N - 4), s, s * rr(.5, 1), rr(0, 3), 0, TAU); x.fill(); } });
  T.block = pbr(N, (u, v, o, x, y) => { const i = y * N + x;
    const ty = v * 6, iy = Math.floor(ty), fy = ty - iy, tx = u * 3 + (iy & 1) * .5, ixf = Math.floor(tx), fx = tx - ixf, ix = ((ixf % 3) + 3) % 3;
    const e = Math.min(Math.min(fx, 1 - fx), Math.min(fy, 1 - fy) * .5) + (n2[i] - .5) * .014;
    const mortar = 1 - sstep(.006, .018, e), bev = sstep(.012, .07, e);
    const t = h2(ix, iy, 64), t2 = h2(ix, iy, 65);
    const st = sstep(.5, .8, streak[i]) * sstep(.35, .7, n1[i]);
    const c = Math.abs(cr[i] - .5) * 2, crack = (1 - sstep(.002, .007, c)) * sstep(.55, .68, n1[i]);
    const moss = sstep(.58, .8, n2[i]) * sstep(.5, .75, 1 - n1[i]);
    let a = .34 + (t - .5) * .15 + (n1[i] - .5) * .14 + (fine[i] - .5) * .07 + (n2[i] - .5) * .06; a *= 1 - st * .32; a *= 1 - PORE[i] * .3; a *= 1 - crack * .4;
    let r = a * (1.03 + (t2 - .5) * .06), g = a, b = a * (.95 - (t2 - .........完整代码请登录后点击上方下载按钮下载查看

网友评论0