67 lines
6.0 KiB
JavaScript
67 lines
6.0 KiB
JavaScript
const http = require('node:http');
|
||
const fs = require('node:fs');
|
||
const path = require('node:path');
|
||
const ts = require('typescript');
|
||
const root = path.resolve(__dirname, '..');
|
||
const html = `<!doctype html><html><head><meta charset="utf-8"><title>矩形出块门验证</title>
|
||
<style>body{margin:0;background:#20242a;color:white;font:15px sans-serif}header{padding:12px;display:flex;gap:12px;flex-wrap:wrap}label{display:flex;gap:6px;align-items:center}select,button{font:inherit}#stage{height:640px;width:100%;position:relative}canvas{display:block}#status{padding:12px;white-space:pre-wrap}</style></head>
|
||
<body><header><label>宽<select id="width"></select></label><label>高<select id="height"></select></label><button id="apply">切换尺寸</button><label>打开程度<input id="opening" type="range" min="0" max="100" value="0"></label><button id="animate">运行一批</button></header><div id="stage"><canvas id="GameCanvas"></canvas></div><div id="status">正在加载 Cocos 引擎</div>
|
||
<script>function failure(e){document.getElementById('status').textContent=String(e.reason?.stack||e.error?.stack||e.message||e);if(window.cc&&cc.game)cc.game.pause()}window.addEventListener('error',failure);window.addEventListener('unhandledrejection',failure);</script><script src="/engine.js"></script><script src="/gate.js"></script><script>
|
||
const names={leftTileFrame:'2left',rightTileFrame:'2right',middleFrame:'zhong',counterFrame:'yuan',innerEdgeFrame:'neiKuang1',innerCornerFrame:'neiKuang2',middleEdgeFrame:'zhongKuang',outerEdgeFrame:'waiKuang1',outerCornerFrame:'waiKuang2'};
|
||
for(const id of ['width','height'])document.getElementById(id).innerHTML=Array.from({length:10},(_,i)=>'<option>'+ (i+1) +'</option>').join('');
|
||
document.getElementById('width').value=3;document.getElementById('height').value=3;
|
||
let gate,scene,frames={},map;
|
||
function status(){document.getElementById('status').textContent=JSON.stringify({size:gate.config.width+' × '+gate.config.height,count:gate.countLabel.string,opening:document.getElementById('opening').value,children:gate.node.children.map(n=>n.name),left:gate.leftDoor.x,right:gate.rightDoor.x},null,2)}
|
||
function apply(){
|
||
if(gate){gate.node.destroy();gate=null;}
|
||
const w=+document.getElementById('width').value,h=+document.getElementById('height').value;
|
||
const node=new cc.Node('openDoor');scene.addChild(node);
|
||
for(const name of ['bg','kuang','left','right','count','line']){const n=new cc.Node(name);node.addChild(n);if(name==='count'){const label=n.addComponent(cc.Label);label.fontSize=45;label.lineHeight=45;label.string='5';label.isBold=true;}}
|
||
gate=node.addComponent(SpawnGate);Object.assign(gate,frames);gate.enabled=false;
|
||
map={mapBlocksWall:Array.from({length:w+2},()=>Array.from({length:h+2},()=>({x:0,y:0}))),isSpawnGateAreaEmpty:()=>true,createSpawnGateBatch:()=>({nodes:[],opacities:[]}),unlockSpawnGateBatch:()=>{},iceTrue:()=>false};
|
||
gate.init(map,{x:1,y:1,width:w,height:h,spawnQueue:Array.from({length:5},()=>({members:[{}]}))});
|
||
node.setPosition(360,320);node.scale=Math.min(600/(w*120),560/(h*120),1.5);
|
||
document.getElementById('opening').value=0;status();
|
||
}
|
||
document.getElementById('apply').onclick=apply;
|
||
document.getElementById('opening').oninput=e=>{gate.setClosedAmount(1-e.target.value/100);gate.countLabel.node.active=e.target.value==='0';status()};
|
||
document.getElementById('animate').onclick=()=>{gate.update(0);let prev=performance.now();const tick=now=>{gate.update((now-prev)/1000);prev=now;status();if(gate.state!==0)requestAnimationFrame(tick)};requestAnimationFrame(tick)};
|
||
cc.assetManager.init({importBase:'assets/others/import',nativeBase:'assets/others/native'});
|
||
cc.assetManager.loadBundle('internal',error=>{if(error)throw error;cc.game.run({id:'GameCanvas',debugMode:1,frameRate:30,renderMode:2},async()=>{
|
||
cc.view.setDesignResolutionSize(720,640,cc.ResolutionPolicy.SHOW_ALL);cc.view.resizeWithBrowserSize(true);
|
||
scene=new cc.Scene();const cameraNode=new cc.Node('Camera');scene.addChild(cameraNode);cameraNode.setPosition(360,320,1000);const camera=cameraNode.addComponent(cc.Camera);camera.ortho=true;camera.orthoSize=320;camera.backgroundColor=cc.color(32,36,42);cc.director.runScene(scene);
|
||
for(const [field,name] of Object.entries(names)){
|
||
const image=await new Promise((resolve,reject)=>{const im=new Image();im.onload=()=>resolve(im);im.onerror=reject;im.src='/art/'+name+'.png'});
|
||
const texture=new cc.Texture2D();texture.initWithElement(image);texture.handleLoadedTexture();frames[field]=new cc.SpriteFrame(texture);
|
||
}
|
||
apply();
|
||
});});
|
||
</script></body></html>`;
|
||
http.createServer((req, res) => {
|
||
if (req.url.startsWith('/assets/')) {
|
||
http.get('http://localhost:7456'+req.url, upstream => {
|
||
res.writeHead(upstream.statusCode,upstream.headers);upstream.pipe(res);
|
||
}).on('error', e => {res.writeHead(502);res.end(e.message);});
|
||
return;
|
||
}
|
||
if (req.url === '/engine.js') {
|
||
http.get('http://localhost:7456/app/engine/bin/cocos2d-js-for-preview.js', upstream => {
|
||
res.writeHead(upstream.statusCode, {'Content-Type':'application/javascript'});
|
||
upstream.pipe(res);
|
||
}).on('error', e => {res.writeHead(502);res.end(e.message);});
|
||
return;
|
||
}
|
||
if (req.url === '/gate.js') {
|
||
const source=fs.readFileSync(path.join(root,'assets/Script/SpawnGate.ts'),'utf8');
|
||
const compiled=ts.transpileModule(source,{compilerOptions:{target:ts.ScriptTarget.ES2017,module:ts.ModuleKind.CommonJS,experimentalDecorators:true}}).outputText;
|
||
res.setHeader('Content-Type','application/javascript');
|
||
res.end('(function(){const exports={};'+compiled+';window.SpawnGate=exports.default;})();');return;
|
||
}
|
||
if (req.url.startsWith('/art/')) {
|
||
const name=path.basename(req.url);
|
||
res.setHeader('Content-Type','image/png');
|
||
res.end(fs.readFileSync(path.join(root,'assets/UI/prop/doorCreateBlock',name)));return;
|
||
}
|
||
res.setHeader('Content-Type','text/html; charset=utf-8');res.end(html);
|
||
}).listen(63367,'127.0.0.1',()=>console.log('http://127.0.0.1:63367'));
|