小游戏
by SilverViper87968 lines40.3 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>小游戏</title>
<style>
body {
background-color: #111;
margin: 0;
overflow: hidden;
user-select: none;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
perspective: 1200px;
font-family: "Microsoft YaHei", sans-serif;
}
#floor {
position: absolute;
top: 50%;
left: 50%;
width: 4000px;
height: 4000px;
background-image:
linear-gradient(rgba(255,255,255,0.05) 2px, transparent 2px),
linear-gradient(90deg, rgba(255,255,255,0.05) 2px, transparent 2px);
background-size: 100px 100px;
transform-origin: 0 0;
transform: translate(-50%, 500px) rotateX(90deg) translateZ(-2000px);
pointer-events: none;
z-index: -1;
}
#wrapper {
position: relative;
width: 0;
height: 0;
transform-style: preserve-3d;
}
#scene {
position: relative;
width: 0;
height: 0;
transform-style: preserve-3d;
}
.box {
position: absolute;
transform-style: preserve-3d;
}
.face {
position: absolute;
left: 50%;
top: 50%;
border: 1px solid rgba(0,0,0,0.3);
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
font-size: 24px;
color: #000;
outline: 1px solid transparent;
overflow: hidden;
backface-visibility: hidden;
}
.hit-area {
position: absolute;
width: 100%;
height: 100%;
transform: translateZ(2px);
cursor: pointer;
pointer-events: auto;
}
@keyframes pulse {
0% { transform: translate(-50%, -50%) scale(1); }
50% { transform: translate(-50%, -50%) scale(1.08); }
100% { transform: translate(-50%, -50%) scale(1); }
}
.breathing {
animation: pulse 2.5s infinite ease-in-out;
}
#modal-overlay {
position: absolute;
top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0, 0, 0, 0.7);
display: none;
justify-content: center;
align-items: center;
z-index: 1000;
backdrop-filter: blur(4px);
}
#modal-content {
background: rgba(30, 30, 30, 0.95);
border: 2px solid #555;
border-radius: 20px;
padding: 30px;
color: #fff;
text-align: center;
box-shadow: 0 10px 30px rgba(0,0,0,0.8), inset 0 0 20px rgba(255,255,255,0.05);
min-width: 320px;
transform: scale(0.9);
transition: transform 0.2s;
}
#modal-overlay.active #modal-content {
transform: scale(1);
}
.modal-title {
font-size: 24px;
margin-bottom: 20px;
color: #55ff55;
font-weight: bold;
letter-spacing: 2px;
}
.modal-btn {
background: linear-gradient(to bottom, #555, #333);
border: 1px solid #666;
color: #fff;
padding: 10px 30px;
border-radius: 10px;
font-size: 16px;
cursor: pointer;
margin-top: 20px;
transition: 0.2s;
}
.modal-btn:hover {
background: linear-gradient(to bottom, #666, #444);
transform: translateY(-2px);
}
.setting-row {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
font-size: 16px;
}
input[type="color"] {
width: 50px;
height: 30px;
border: none;
border-radius: 5px;
cursor: pointer;
background: transparent;
}
input[type="range"] {
width: 150px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="floor"></div>
<div id="wrapper">
<div id="scene"></div>
</div>
<div id="modal-overlay">
<div id="modal-content">
<div class="modal-title" id="modal-title">提示</div>
<div class="modal-body" id="modal-body" style="line-height: 1.6;"></div>
<button class="modal-btn" onclick="closeModal()">确 定</button>
</div>
</div>
<script id="worker-script" type="javascript/worker">
self.onmessage = function(e) {
var chunk = e.data.data;
var stepMultiplier = e.data.stepMultiplier || 1;
// 为了避免大步长下摩擦力计算错误,使用幂函数计算衰减
var friction = Math.pow(0.99, stepMultiplier);
for (var i = 0; i < chunk.length; i++) {
var b = chunk[i];
if (b.isMoving && !b.isDragging) {
// 【核心修复】:按照准确的物理公式,直接计算 5 帧、10 帧之后的位置
b.x += b.vx * stepMultiplier;
b.y += b.vy * stepMultiplier;
b.z += b.vz * stepMultiplier;
b.vy += 0.6 * stepMultiplier;
b.vx *= friction;
b.vz *= friction;
}
}
self.postMessage({ data: chunk, stepMultiplier: stepMultiplier });
};
</script>
<script>
const wrapper = document.getElementById('wrapper');
const scene = document.getElementById('scene');
let isPhysicsMode = false;
let boxes = [];
let cameraRX = 0, cameraRY = 0;
let camPanX = 0, camPanY = 0, camPanZ = 0;
let mouseLeft = false;
let mouseRight = false;
let lastMouseX = 0, lastMouseY = 0;
let mouseDownX = 0, mouseDownY = 0;
const floorY = 450;
const wallZ = -200;
const RENDER_DIST = 10000;
// ================= 【自适应时间累加器】 =================
let physicsStepMultiplier = 1; // 动态步长倍率
let accumulatedTime = 0; // 累积时间
const BASE_TIME_STEP = 16.67; // 基准时间步长(60fps 对应的时间)
let lastLoopTime = performance.now();
function checkEnvironment() {
physicsStepMultiplier = 1;
let isOldSystem = false;
let isOldBrowser = false;
const ua = navigator.userAgent;
if (/Windows NT 6\./.test(ua)) isOldSystem = true;
const cpuCores = navigator.hardwareConcurrency || 2;
if (cpuCores <= 4) isOldBrowser = true;
if (isOldSystem || isOldBrowser) {
physicsStepMultiplier = 3; // 默认跳过 3 帧的计算,降低卡顿
console.log("静态检测:老旧环境,已启用降级步长 3x");
} else {
physicsStepMultiplier = 1;
console.log("静态检测:现代环境,默认步长 1x");
}
}
// =========================================================
const workerScriptText = document.getElementById('worker-script').textContent;
const blob = new Blob([workerScriptText], { type: 'application/javascript' });
const workerUrl = URL.createObjectURL(blob);
const workers = [];
const WORKER_COUNT = 6;
for (let i = 0; i < WORKER_COUNT; i++) {
workers.push(new Worker(workerUrl));
}
function showModal(title, htmlContent) {
document.getElementById('modal-title').innerText = title;
document.getElementById('modal-body').innerHTML = htmlContent;
const overlay = document.getElementById('modal-overlay');
overlay.style.display = 'flex';
setTimeout(() => { overlay.classList.add('active'); }, 10);
}
function closeModal() {
const overlay = document.getElementById('modal-overlay');
overlay.classList.remove('active');
setTimeout(() => { overlay.style.display = 'none'; }, 200);
}
function createBox(x, y, z, w, h, d, colorFront, colorSide, textHTML, isDebris, isBg, clipStr) {
const boxDiv = document.createElement('div');
boxDiv.className = 'box';
scene.appendChild(boxDiv);
const facesData = [
{ w: w, h: h, t: `translate(-50%, -50%) translateZ(${d/2}px)`, c: colorFront, txt: textHTML, isFront: true },
{ w: w, h: h, t: `translate(-50%, -50%) rotateY(180deg) translateZ(${d/2}px)`, c: colorSide, txt: "" },
{ w: d, h: h, t: `translate(-50%, -50%) rotateY(-90deg) translateZ(${w/2}px)`, c: colorSide, txt: "" },
{ w: d, h: h, t: `translate(-50%, -50%) rotateY(90deg) translateZ(${w/2}px)`, c: colorSide, txt: "" },
{ w: w, h: d, t: `translate(-50%, -50%) rotateX(90deg) translateZ(${h/2}px)`, c: colorSide, txt: "" },
{ w: w, h: d, t: `translate(-50%, -50%) rotateX(-90deg) translateZ(${h/2}px)`, c: colorSide, txt: "" }
];
for (let i = 0; i < facesData.length; i++) {
const f = facesData[i];
const faceDiv = document.createElement('div');
faceDiv.className = 'face';
faceDiv.style.width = f.w + 'px';
faceDiv.style.height = f.h + 'px';
faceDiv.style.transform = f.t;
faceDiv.style.backgroundColor = f.c;
if (clipStr && f.isFront) {
faceDiv.style.webkitClipPath = clipStr;
faceDiv.style.clipPath = clipStr;
}
if (f.txt) {
faceDiv.innerHTML = f.txt;
} else if (isBg && f.isFront) {
let decoHTML = '';
const icons = ['🌻', '🌿', '🌸', '🌱', '🍀', '🌳', '🌺', '🌵', '🍄'];
for (let k = 0; k < 80; k++) {
const randX = Math.random() * 90 + 5;
const randY = Math.random() * 90 + 5;
const randIcon = icons[Math.floor(Math.random() * icons.length)];
const randSize = Math.floor(Math.random() * 50) + 40;
const randRot = Math.floor(Math.random() * 60) - 30;
decoHTML += `<div style="position:absolute; left:${randX}%; top:${randY}%; font-size:${randSize}px; transform:rotate(${randRot}deg); filter:hue-rotate(90deg); opacity:0.7; pointer-events:none;">${randIcon}</div>`;
}
faceDiv.innerHTML = decoHTML;
}
if (f.isFront) {
if (textHTML && textHTML.indexOf("开始") !== -1 && !isDebris) {
faceDiv.classList.add('breathing');
}
const hit = document.createElement('div');
hit.className = 'hit-area';
hit.ondragstart = () => false;
faceDiv.appendChild(hit);
}
boxDiv.appendChild(faceDiv);
}
const boxObj = {
dom: boxDiv,
x, y, z, w, h, d,
rx: 0, ry: 0, rz: 0,
vx: 0, vy: 0, vz: 0,
vrx: 0, vry: 0, vrz: 0,
isBroken: false,
isDebris: isDebris || false,
isBg: isBg || false,
isDragging: false,
isMoving: false,
isFlat: false,
text: textHTML
};
boxDiv.onmousedown = (e) => {
if (e.button === 0) mouseLeft = true;
if (e.button === 2) mouseRight = true;
if (mouseLeft && mouseRight) {
e.preventDefault();
e.stopPropagation();
if (boxObj.isBroken) return;
if (boxObj.isBg) {
if (isPhysicsMode) {
const rect = e.target.getBoundingClientRect();
const clickX = e.clientX - rect.left;
const clickY = e.clientY - rect.top;
breakEntity(boxObj, 0, 0, 0, clickX, clickY, rect.width, rect.height);
}
} else {
if (!isPhysicsMode) return;
boxObj.isDragging = true;
boxObj.dragStartX = e.clientX;
boxObj.dragStartY = e.clientY;
boxObj.isMoving = true;
boxObj.isFlat = false;
}
}
};
boxes.push(boxObj);
updateBoxDOM(boxObj);
return boxObj;
}
function updateBoxDOM(b) {
if (Math.abs(b.x) > RENDER_DIST || Math.abs(b.y) > RENDER_DIST || Math.abs(b.z) > RENDER_DIST) {
if (b.dom.style.display !== 'none') {
b.dom.style.display = 'none';
}
return;
} else {
if (b.dom.style.display === 'none') {
b.dom.style.display = 'block';
}
}
b.dom.style.transform = `translate3d(${b.x}px, ${b.y}px, ${b.z}px) rotateX(${b.rx}deg) rotateY(${b.ry}deg) rotateZ(${b.rz}deg)`;
}
// ================= 初始化场景 =================
const bgWall = createBox(0, 0, wallZ, 3000, 2000, 20, '#2a2a2a', '#111111', '', false, true, null);
const uiPanel = createBox(0, -55, 0, 650, 600, 10, 'rgba(255, 255, 255, 0.05)', 'rgba(255, 255, 255, 0.02)', '', false, false, null);
for (let i = 0; i < uiPanel.dom.children.length; i++) {
uiPanel.dom.children[i].style.borderRadius = '20px';
uiPanel.dom.children[i].style.border = '2px solid rgba(255, 255, 255, 0.6)';
uiPanel.dom.children[i].style.backdropFilter = 'blur(5px)';
}
const titleHTML = `<span style="position:absolute; left:20px; font-size:40px;">🎲</span>
<span style="color:blue; font-weight:900;">“猜猜”</span>小游戏
<span style="position:absolute; right:20px; font-size:40px;">🎮</span>
<span style="position:absolute; right:80px; bottom:5px; font-size:24px;">✨</span>
<span style="position:absolute; left:80px; top:5px; font-size:24px;">🌟</span>`;
const title = createBox(0, -295, 30, 600, 120, 30, '#ffffff', '#aaaaaa', titleHTML, false, false, null);
const btn1 = createBox(0, -145, 30, 280, 70, 30, '#55ff55', '#33aa33', '▶️ 开始游戏', false, false, null);
const btn2 = createBox(0, -35, 30, 280, 70, 30, '#aaaaaa', '#666666', '⏸️ 暂停', false, false, null);
const btn3 = createBox(0, 75, 30, 280, 70, 30, '#aaaaaa', '#666666', '🏠 主菜单', false, false, null);
const btn4 = createBox(0, 185, 30, 280, 70, 30, '#aaaaaa', '#666666', '⚙️ 设置', false, false, null);
btn1.dom.onmousedown = (e) => {
if (e.button !== 0) return;
e.stopPropagation();
if (btn1.isBroken || isPhysicsMode) return;
checkEnvironment();
isPhysicsMode = true;
const radRX = cameraRX * Math.PI / 180;
const radRY = cameraRY * Math.PI / 180;
const force = 15;
const fx = Math.sin(radRY) * Math.cos(radRX) * force;
const fy = Math.sin(radRX) * force;
const fz = -Math.cos(radRY) * Math.cos(radRX) * force;
const rect = e.target.getBoundingClientRect();
const clickX = e.clientX - rect.left;
const clickY = e.clientY - rect.top;
breakEntity(btn1, fx, fy, fz, clickX, clickY, rect.width, rect.height);
};
btn2.dom.onmousedown = (e) => {
if (e.button !== 0 || isPhysicsMode) return;
e.stopPropagation();
showModal("系统提示", "请先开始游戏哦~");
};
btn3.dom.onmousedown = (e) => {
if (e.button !== 0 || isPhysicsMode) return;
e.stopPropagation();
showModal("系统提示", "当前已经是主菜单啦~");
};
btn4.dom.onmousedown = (e) => {
if (e.button !== 0 || isPhysicsMode) return;
e.stopPropagation();
let html = `
<div class="setting-row">
<span>背景颜色</span>
<input type="color" id="color-bg" value="#2a2a2a">
</div>
<div class="setting-row">
<span>标题颜色</span>
<input type="color" id="color-title" value="#ffffff">
</div>
<div class="setting-row">
<span>普通按钮颜色</span>
<input type="color" id="color-btn" value="#aaaaaa">
</div>
<div class="setting-row">
<span>全局刷新率 (FPS)</span>
<input type="range" id="fps-range" min="10" max="144" step="1" value="60">
<span id="fps-val">60</span>
</div>
`;
showModal("游戏设置", html);
setTimeout(() => {
document.getElementById('color-bg').oninput = function() {
bgWall.dom.children[0].style.backgroundColor = this.value;
};
document.getElementById('color-title').oninput = function() {
title.dom.children[0].style.backgroundColor = this.value;
};
document.getElementById('color-btn').oninput = function() {
btn2.dom.children[0].style.backgroundColor = this.value;
btn3.dom.children[0].style.backgroundColor = this.value;
btn4.dom.children[0].style.backgroundColor = this.value;
};
document.getElementById('fps-range').oninput = function() {
targetFPS = parseInt(this.value);
document.getElementById('fps-val').innerText = this.value;
};
}, 50);
};
// ================= 物理循环(累加器控制) =================
function physicsLoop(now) {
if (isPhysicsMode) {
let deltaTime = now - lastLoopTime;
lastLoopTime = now;
// 防止切后台恢复瞬间的计算爆炸
if (deltaTime > 100) deltaTime = BASE_TIME_STEP;
accumulatedTime += deltaTime;
// 计算出当前需要的物理间隔
let targetInterval = BASE_TIME_STEP * physicsStepMultiplier;
// 只有当累积时间达到了目标间隔,才执行一次计算
if (accumulatedTime >= targetInterval) {
let startTime = performance.now();
// 消费掉累积的时间,防止无限叠加
accumulatedTime -= targetInterval;
if (accumulatedTime > targetInterval) accumulatedTime = 0; // 如果落后太多,直接清零
let chunks = Array.from({ length: WORKER_COUNT }, () => []);
for (let i = 0; i < boxes.length; i++) {
let b = boxes[i];
if (b.isBroken || !b.isMoving) continue;
let chunkIdx = i % WORKER_COUNT;
chunks[chunkIdx].push({
index: i,
x: b.x, y: b.y, z: b.z,
vx: b.vx, vy: b.vy, vz: b.vz,
isMoving: b.isMoving,
isDragging: b.isDragging
});
}
let completedWorkers = 0;
for (let w = 0; w < WORKER_COUNT; w++) {
workers[w].onmessage = function(ev) {
let results = ev.data.data;
for (let j = 0; j < results.length; j++) {
let res = results[j];
let b = boxes[res.index];
if (b.isBroken) continue;
b.x = res.x; b.y = res.y; b.z = res.z;
b.vx = res.vx; b.vy = res.vy; b.vz = res.vz;
}
completedWorkers++;
if (completedWorkers === WORKER_COUNT) {
mainThreadPhysics();
// 计算主线程耗时
let cost = performance.now() - startTime;
// 根据耗时动态调节跳帧倍率
if (cost > 8) {
physicsStepMultiplier = Math.min(10, physicsStepMultiplier + 1);
} else if (cost < 2) {
physicsStepMultiplier = Math.max(1, physicsStepMultiplier - 1);
}
}
};
// 将当前步长传给 Worker
workers[w].postMessage({ data: chunks[w], stepMultiplier: physicsStepMultiplier });
}
}
} else {
lastLoopTime = now;
}
requestAnimationFrame(physicsLoop);
}
function mainThreadPhysics() {
for (let i = 0; i < boxes.length; i++) {
const b = boxes[i];
if (b.isBroken || !b.isMoving) continue;
if (!b.isDragging) {
if (b.y + b.h/2 > floorY) {
b.y = floorY - b.h/2;
b.vy *= -0.1;
b.vx *= 0.8; b.vz *= 0.8;
if (Math.abs(b.vy) < 1 && Math.abs(b.vx) < 1 && Math.abs(b.vz) < 1) {
b.isFlat = true;
b.vrx = 0; b.vry = 0; b.vrz = 0;
let diffX = 90 - b.rx;
diffX = ((diffX + 180) % 360 + 360) % 360 - 180;
b.rx += diffX * 0.05;
let diffY = 0 - b.ry;
diffY = ((diffY + 180) % 360 + 360) % 360 - 180;
b.ry += diffY * 0.05;
let diffZ = 0 - b.rz;
diffZ = ((diffZ + 180) % 360 + 360) % 360 - 180;
b.rz += diffZ * 0.05;
const targetY = floorY - b.d / 2;
b.y += (targetY - b.y) * 0.05;
if (Math.abs(diffX) < 0.5 && Math.abs(diffY) < 0.5) {
b.rx = 90; b.ry = 0; b.rz = 0;
b.y = floorY - b.d / 2;
}
} else {
b.rx += b.vrx;
b.ry += b.vry;
b.rz += b.vrz;
b.vrx *= 0.98; b.vry *= 0.98; b.vrz *= 0.98;
}
} else {
b.rx += b.vrx;
b.ry += b.vry;
b.rz += b.vrz;
b.vrx *= 0.98; b.vry *= 0.98; b.vrz *= 0.98;
}
if (!b.isBg && !bgWall.isBroken) {
if (b.z - b.d/2 < wallZ + 10) {
b.z = wallZ + b.d/2 + 11;
if (b.w > 80 && b.h > 80) {
breakEntity(b, b.vx * 0.5, b.vy * 0.5, b.vz * 0.5, 0, 0, 0, 0);
} else {
b.vz = Math.abs(b.vz) * 0.5;
}
}
}
}
}
for (let i = 0; i < boxes.length; i++) {
if (boxes[i].isBroken || boxes[i].isDragging || boxes[i].isBg || !boxes[i].isMoving) continue;
for (let j = i + 1; j < boxes.length; j++) {
if (boxes[j].isBroken || boxes[j].isDragging || boxes[j].isBg || !boxes[j].isMoving) continue;
const b1 = boxes[i];
const b2 = boxes[j];
const h1 = b1.isFlat ? b1.d : b1.h;
const h2 = b2.isFlat ? b2.d : b2.h;
const overlapX = (b1.w + b2.w)/2 - Math.abs(b1.x - b2.x);
const overlapY = (h1 + h2)/2 - Math.abs(b1.y - b2.y);
const overlapZ = (b1.d + b2.d)/2 - Math.abs(b1.z - b2.z);
if (overlapX > 0 && overlapY > 0 && overlapZ > 0) {
if (overlapX < overlapY && overlapX < overlapZ) {
const dir = b1.x > b2.x ? 1 : -1;
b1.x += dir * overlapX * 0.5; b2.x -= dir * overlapX * 0.5;
b1.vx += dir * 2; b2.vx -= dir * 2;
} else if (overlapY < overlapX && overlapY < overlapZ) {
const dir = b1.y > b2.y ? 1 : -1;
b1.y += dir * overlapY * 0.5; b2.y -= dir * overlapY * 0.5;
b1.vy += dir * 1; b2.vy -= dir * 1;
b1.y = Math.min(b1.y, floorY - h1/2);
b2.y = Math.min(b2.y, floorY - h2/2);
} else {
const dir = b1.z > b2.z ? 1 : -1;
b1.z += dir * overlapZ * 0.5; b2.z -= dir * overlapZ * 0.5;
b1.vz += dir * 2; b2.vz -= dir * 2;
}
b1.isMoving = true;
b2.isMoving = true;
}
}
}
for (let i = 0; i < boxes.length; i++) {
if (!boxes[i].isBroken) updateBoxDOM(boxes[i]);
}
}
function breakEntity(b, initVx, initVy, initVz, clickX, clickY, elemW, elemH) {
b.isBroken = true;
b.dom.style.display = 'none';
const cFront = b.dom.children[0].style.backgroundColor;
const cSide = b.dom.children[1].style.backgroundColor;
let fragments = [];
const localClickX = clickX - elemW / 2;
const localClickY = clickY - elemH / 2;
if (b.isBg) {
function splitBg(rx, ry, rw, rh) {
const dist = Math.sqrt(Math.pow(rx + rw/2 - localClickX, 2) + Math.pow(ry + rh/2 - localClickY, 2));
let maxArea = 1000000;
if (dist < 200) maxArea = 2000;
else if (dist < 400) maxArea = 8000;
else if (dist < 700) maxArea = 18000;
else if (dist < 1100) maxArea = 35000;
else maxArea = 80000;
let area = rw * rh;
if (area > maxArea || rw > 300 || rh > 300) {
let isVertical = true;
if (rw > rh * 1.5) isVertical = true;
else if (rh > rw * 1.5) isVertical = false;
else isVertical = Math.random() > 0.5;
if (isVertical) {
const splitX = rw * (0.3 + Math.random() * 0.4);
splitBg(rx, ry, splitX, rh);
splitBg(rx + splitX, ry, rw - splitX, rh);
} else {
const splitY = rh * (0.3 + Math.random() * 0.4);
splitBg(rx, ry, rw, splitY);
splitBg(rx, ry + splitY, rw, rh - splitY);
}
return;
}
let clip = "polygon(0 0, 100% 0, 100% 100%, 0 100%)";
if (rw > 15 && rh > 15) {
let cut1 = Math.random() * 30;
let cut2 = Math.random() * 30;
clip = `polygon(${cut1}% 0%, 100% ${cut2}%, ${100 - cut1}% 100%, 0% ${100 - cut2}%)`;
}
fragments.push({ x: rx, y: ry, w: rw, h: rh, clip: clip });
}
splitBg(-b.w/2, -b.h/2, b.w, b.h);
} else {
const maxDist = Math.sqrt(b.w*b.w + b.h*b.h) / 2;
function splitButton(rx, ry, rw, rh, depth) {
const dist = Math.sqrt(Math.pow(rx + rw/2 - localClickX, 2) + Math.pow(ry + rh/2 - localClickY, 2));
let distFactor = dist / maxDist;
if ((rw < 45 || rh < 30) && depth > 1) {
let clip = "polygon(0 0, 100% 0, 100% 100%, 0 100%)";
if (rw > 20 && rh > 15) {
let cut1 = Math.random() * 25;
let cut2 = Math.random() * 25;
clip = `polygon(${cut1}% 0%, 100% ${cut2}%, ${100 - cut1}% 100%, 0% ${100 - cut2}%)`;
}
fragments.push({ x: rx, y: ry, w: rw, h: rh, clip: clip });
return;
}
if (depth > 4) {
let clip = "polygon(0 0, 100% 0, 100% 100%, 0 100%)";
fragments.push({ x: rx, y: ry, w: rw, h: rh, clip: clip });
return;
}
let splitProb = 1.0 - (distFactor * 0.7);
if (Math.random() > splitProb && depth > 1) {
let clip = "polygon(0 0, 100% 0, 100% 100%, 0 100%)";
if (rw > 20 && rh > 15) {
let cut1 = Math.random() * 25;
let cut2 = Math.random() * 25;
clip = `polygon(${cut1}% 0%, 100% ${cut2}%, ${100 - cut1}% 100%, 0% ${100 - cut2}%)`;
}
fragments.push({ x: rx, y: ry, w: rw, h: rh, clip: clip });
return;
}
let isVertical = true;
if (rw > rh * 1.5) isVertical = true;
else if (rh > rw * 1.5) isVertical = false;
else isVertical = Math.random() > 0.5;
if (isVertical) {
const splitX = rw * (0.4 + Math.random() * 0.2);
splitButton(rx, ry, splitX, rh, depth + 1);
splitButton(rx + splitX, ry, rw - splitX, rh, depth + 1);
} else {
const splitY = rh * (0.4 + Math.random() * 0.2);
splitButton(rx, ry, rw, splitY, depth + 1);
splitButton(rx, ry + splitY, rw, rh - splitY, depth + 1);
}
}
splitButton(-b.w/2, -b.h/2, b.w, b.h, 0);
}
let rawText = b.text || "";
rawText = rawText.replace(/<[^>]*>/g, '');
let filteredText = "";
for (let k = 0; k < rawText.length; k++) {
let char = rawText[k];
if (/[\u4e00-\u9fa5]/.test(char)) {
filteredText += char;
} else if (char === " ") {
filteredText += " ";
} else {
filteredText += " ";
}
}
const textArr = filteredText.split("");
for (let i = 0; i < fragments.length; i++) {
const f = fragments[i];
let pieceText = "";
if (textArr.length > 0 && !b.isBg) {
if (i < textArr.length) pieceText = textArr[i];
}
const piece = createBox(
b.x + f.x, b.y + f.y, b.z,
f.w, f.h, b.d,
cFront, cSide, pieceText, true, false, f.clip
);
piece.rx = (Math.random() - 0.5) * 180;
piece.ry = (Math.random() - 0.5) * 180;
piece.rz = (Math.random() - 0.5) * 180;
let fragCX = f.x;
let fragCY = f.y;
let distToClick = Math.sqrt(Math.pow(fragCX - localClickX, 2) + Math.pow(fragCY - localClickY, 2));
let angle = Math.atan2(fragCY - localClickY, fragCX - localClickX);
let blastForce = Math.max(0, 1 - (distToClick / (b.isBg ? 1000 : 200)));
let speedMultiplier = 18 * blastForce;
let bx = Math.cos(angle) * speedMultiplier;
let by = Math.sin(angle) * speedMultiplier;
piece.vx = initVx * blastForce + bx + (Math.random() - 0.5) * 5;
piece.vy = initVy * blastForce + by - (Math.random() * 8) - 2;
piece.vz = initVz * blastForce - (Math.random() * 8);
piece.vrx = (Math.random() - 0.5) * 20 * blastForce;
piece.vry = (Math.random() - 0.5) * 20 * blastForce;
piece.vrz = (Math.random() - 0.5) * 20 * blastForce;
piece.isMoving = true;
}
}
function triggerShockwave() {
for (let i = 0; i < boxes.length; i++) {
const b = boxes[i];
if (b.isBroken) continue;
const isOnFloor = (b.y + b.h/2 >= floorY - 15) || b.isFlat;
if (isOnFloor) {
if (!b.isDebris && b !== bgWall && !b.isBg) {
b.vx = 0; b.vz = 0; b.vy = -12;
breakEntity(b, 0, -12, 0, 0, 0, 0, 0);
}
else if (b.isDebris) {
if (b.w > 80 && b.h > 80) {
breakEntity(b, 0, -12, 0, 0, 0, 0, 0);
} else {
b.isMoving = true;
b.isFlat = false;
b.vy = -12 - Math.random() * 8;
b.vx = (Math.random() - 0.5) * 12;
b.vz = (Math.random() - 0.5) * 12;
b.vrx = (Math.random() - 0.5) * 15;
b.vry = (Math.random() - 0.5) * 15;
b.vrz = (Math.random() - 0.5) * 15;
}
}
}
}
}
document.onmousedown = (e) => {
if (!isPhysicsMode) return;
if (e.button === 0) mouseLeft = true;
if (e.button === 2) {
mouseRight = true;
e.preventDefault();
}
lastMouseX = e.clientX;
lastMouseY = e.clientY;
mouseDownX = e.clientX;
mouseDownY = e.clientY;
};
document.onmousemove = (e) => {
if (!isPhysicsMode) return;
const dx = e.clientX - lastMouseX;
const dy = e.clientY - lastMouseY;
if (mouseLeft && mouseRight) {
for (let i = 0; i < boxes.length; i++) {
const b = boxes[i];
if (b.isDragging) {
const radRY = cameraRY * Math.PI / 180;
const dx_world = dx * Math.cos(radRY);
const dz_world = -dx * Math.sin(radRY);
b.x += dx_world * 2;
b.z += dz_world * 2;
b.y += dy * 2;
b.z -= dy * 1.5;
if (b.y + b.h/2 > floorY) {
b.y = floorY - b.h/2;
}
updateBoxDOM(b);
}
}
}
else if (mouseLeft) {
cameraRY += dx * 0.2;
cameraRX -= dy * 0.2;
cameraRX = Math.max(-80, Math.min(80, cameraRX));
scene.style.transform = `rotateX(${cameraRX}deg) rotateY(${cameraRY}deg)`;
}
else if (mouseRight) {
if (e.shiftKey) {
camPanZ -= dy * 3;
} else {
camPanX += dx;
camPanY += dy;
}
wrapper.style.transform = `translate3d(${camPanX}px, ${camPanY}px, ${camPanZ}px)`;
}
lastMouseX = e.clientX;
lastMouseY = e.clientY;
};
document.onmouseup = (e) => {
if (e.button === 0) mouseLeft = false;
if (e.button === 2) mouseRight = false;
if (e.button === 0 && !mouseRight) {
const moveDist = Math.abs(e.clientX - mouseDownX) + Math.abs(e.clientY - mouseDownY);
if (moveDist < 2) {
let hitAnyBox = false;
for (let i = 0; i < boxes.length; i++) {
const b = boxes[i];
if (!b.isBroken && b.dom.contains(e.target)) {
hitAnyBox = true;
if (!b.isDragging) {
const radRX = cameraRX * Math.PI / 180;
const radRY = cameraRY * Math.PI / 180;
const force = 15;
const fx = Math.sin(radRY) * Math.cos(radRX) * force;
const fy = Math.sin(radRX) * force;
const fz = -Math.cos(radRY) * Math.cos(radRX) * force;
const rect = e.target.getBoundingClientRect();
const clickX = e.clientX - rect.left;
const clickY = e.clientY - rect.top;
breakEntity(b, fx, fy, fz, clickX, clickY, rect.width, rect.height);
}
break;
}
}
if (!hitAnyBox) {
triggerShockwave();
}
}
}
for (let i = 0; i < boxes.length; i++) {
if (boxes[i].isDragging) boxes[i].isDragging = false;
}
};
document.oncontextmenu = (e) => e.preventDefault();
requestAnimationFrame(physicsLoop);
</script>
</body>
</html>Game Source: 小游戏
Creator: SilverViper87
Libraries: none
Complexity: complex (968 lines, 40.3 KB)
The full source code is displayed above on this page.
Remix Instructions
To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: game-silverviper87" to link back to the original. Then publish at arcadelab.ai/publish.