🎮ArcadeLab

Shadow Fighters 3D

by FrostFlare80
558 lines16.2 KB🛠️ Three.js (3D graphics)
▶ Play

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Shadow Fighters 3D</title>
<style>
*{box-sizing:border-box}
body{margin:0;overflow:hidden;background:#050817;color:white;font-family:Arial;touch-action:none}
#game{position:fixed;inset:0}
canvas{display:block;width:100%;height:100%}
#hud{position:absolute;top:12px;left:3%;width:94%;display:grid;grid-template-columns:1fr auto 1fr;gap:12px;align-items:center;pointer-events:none}
.name{font-size:12px;font-weight:bold;letter-spacing:2px;margin-bottom:5px}
.enemy{text-align:right}
.bar{height:17px;background:#202b42;border:2px solid white;border-radius:10px;overflow:hidden}
.fill{height:100%;width:100%;background:linear-gradient(90deg,#00ff88,#c6ff00);transition:width .12s}
#ehp{background:linear-gradient(90deg,#ffbd35,#ff1744)}
#timer{font-size:32px;font-weight:bold;color:#ffe45c}
#logo{position:absolute;top:75px;width:100%;text-align:center;font-size:22px;font-weight:1000;letter-spacing:4px;color:#00eaff;text-shadow:0 0 18px #00bfff;pointer-events:none}
#message{position:absolute;top:19%;width:100%;text-align:center;font-size:clamp(24px,5vw,44px);font-weight:1000;color:#ffe34d;text-shadow:0 0 20px #ff8500;pointer-events:none}
#sub{font-size:12px;margin-top:5px}
#restart{position:absolute;right:12px;top:120px}
#controls{position:absolute;bottom:15px;left:0;right:0;display:flex;justify-content:space-between;padding:0 3%;gap:8px}
.group{display:flex;gap:7px;align-items:end;flex-wrap:wrap}
button{background:linear-gradient(145deg,#173d61,#09162b);border:2px solid #00cfff;border-radius:12px;color:white;font-weight:bold;padding:13px 12px;min-height:48px;touch-action:none;user-select:none;box-shadow:0 0 12px #008cff55}
button:active,.held{background:#007cba;transform:scale(.94)}
#restart{border-color:#ffdf40;color:#ffdf40;min-height:36px;padding:8px}
@media(max-width:600px){
 #controls{padding:0 2%;gap:4px}
 .group{gap:4px}
 button{font-size:10px;padding:10px 7px;min-height:44px}
 #logo{top:58px;font-size:17px}
 #restart{top:95px}
 #timer{font-size:25px}
 .name{font-size:10px}
}
</style>
</head>
<body>
<div id="game"></div>
<div id="hud">
 <div><div class="name">PLAYER</div><div class="bar"><div class="fill" id="php"></div></div></div>
 <div id="timer">90</div>
 <div class="enemy"><div class="name">SHADOW RIVAL</div><div class="bar"><div class="fill" id="ehp"></div></div></div>
</div>
<div id="logo">SHADOW FIGHTERS 3D</div>
<div id="message">ROUND 1<div id="sub">GET READY!</div></div>
<button id="restart">↻ RESTART</button>
<div id="controls">
 <div class="group">
  <button data-hold="left">◀</button>
  <button data-hold="right">▶</button>
  <button data-hold="jump">JUMP</button>
 </div>
 <div class="group">
  <button data-hold="block">🛡</button>
  <button data-tap="punch">👊</button>
  <button data-tap="kick">🦵</button>
  <button data-tap="special">⚡</button>
 </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"></script>
<script>
(()=>{
'use strict';
const game=document.getElementById('game');
const message=document.getElementById('message');
if(!window.THREE){
 message.innerHTML='3D ENGINE FAILED<div id="sub">Check internet connection and reload</div>';
 return;
}

const scene=new THREE.Scene();
scene.background=new THREE.Color(0x080e21);
scene.fog=new THREE.Fog(0x080e21,17,40);

const camera=new THREE.PerspectiveCamera(45,innerWidth/innerHeight,.1,100);
camera.position.set(0,6,14);
camera.lookAt(0,1,0);

const renderer=new THREE.WebGLRenderer({antialias:true});
renderer.setPixelRatio(Math.min(devicePixelRatio,2));
renderer.setSize(innerWidth,innerHeight);
renderer.shadowMap.enabled=true;
renderer.shadowMap.type=THREE.PCFSoftShadowMap;
renderer.outputColorSpace=THREE.SRGBColorSpace;
renderer.toneMapping=THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure=1.25;
game.appendChild(renderer.domElement);

scene.add(new THREE.HemisphereLight(0xaad9ff,0x191225,2));
const sun=new THREE.DirectionalLight(0xffe8d0,3);
sun.position.set(-5,10,7);
sun.castShadow=true;
sun.shadow.mapSize.set(2048,2048);
scene.add(sun);

const blue=new THREE.PointLight(0x00cfff,40,20);
blue.position.set(-6,4,-3);
scene.add(blue);
const red=new THREE.PointLight(0xff2465,40,20);
red.position.set(6,4,-3);
scene.add(red);

const material=(c,metal=0,rough=.5,glow=0)=>
 new THREE.MeshStandardMaterial({
 color:c,metalness:metal,roughness:rough,
 emissive:glow?c:0,emissiveIntensity:glow
 });

function cube(parent,w,h,d,mat,x,y,z){
 const o=new THREE.Mesh(new THREE.BoxGeometry(w,h,d),mat);
 o.position.set(x,y,z);
 o.castShadow=true;o.receiveShadow=true;
 parent.add(o);return o;
}
function ball(parent,r,mat,x,y,z,sx=1,sy=1,sz=1){
 const o=new THREE.Mesh(new THREE.SphereGeometry(r,20,16),mat);
 o.position.set(x,y,z);o.scale.set(sx,sy,sz);
 o.castShadow=true;parent.add(o);return o;
}
function rod(parent,a,b,r,mat){
 const A=new THREE.Vector3(...a),B=new THREE.Vector3(...b);
 const d=B.clone().sub(A);
 const o=new THREE.Mesh(new THREE.CylinderGeometry(r*.8,r,d.length(),12),mat);
 o.position.copy(A).add(B).multiplyScalar(.5);
 o.quaternion.setFromUnitVectors(new THREE.Vector3(0,1,0),d.normalize());
 o.castShadow=true;parent.add(o);return o;
}

// Arena
const floor=new THREE.Mesh(
 new THREE.PlaneGeometry(35,26),
 material(0x111a2d,.45,.35)
);
floor.rotation.x=-Math.PI/2;
floor.receiveShadow=true;
scene.add(floor);

const platform=new THREE.Mesh(
 new THREE.CylinderGeometry(6.3,6.3,.13,64),
 material(0x192a40,.7,.3)
);
platform.position.y=.07;
platform.receiveShadow=true;
scene.add(platform);

const circle=new THREE.Mesh(
 new THREE.TorusGeometry(6,.035,8,100),
 new THREE.MeshBasicMaterial({color:0x00dfff})
);
circle.rotation.x=Math.PI/2;
circle.position.y=.15;
scene.add(circle);

cube(scene,34,9,.5,material(0x11172c),0,4,-10);

for(let i=0;i<10;i++){
 let x=-15+i*3.3;
 cube(scene,.13,7,.2,material(i%2?0x00bfff:0xff286d,0,.4,1),x,3.5,-8.9);
}
for(let i=0;i<8;i++){
 let x=-13+i*3.7;
 let h=2+(i%3)*.8;
 cube(scene,1.8,h,.8,material(0x172139),x,h/2,-7.5);
 for(let j=0;j<3;j++){
  cube(scene,.12,.18,.04,material(0x16bddd,0,.3,1),x-.4+j*.4,1,-7.05);
 }
}
const moon=ball(scene,1.2,material(0xd5f4ff,0,.3,1),7,7,-9,1,1,1);

// Fighter builder
function fighter(x,color,accent,isPlayer){
 const root=new THREE.Group();
 root.position.x=x;
 scene.add(root);
 const body=new THREE.Group();
 root.add(body);

 const cloth=material(color,.25,.4,.12);
 const dark=material(0x111522,.5,.3);
 const metal=material(accent,.7,.25,.5);
 const skin=material(0xc58c70,0,.7);
 const glow=material(accent,.2,.2,2);

 cube(body,.65,.4,.4,cloth,0,1,0);
 const torso=new THREE.Mesh(
  new THREE.CapsuleGeometry(.38,.65,6,12),cloth
 );
 torso.position.y=1.62;
 torso.castShadow=true;
 body.add(torso);

 cube(body,.5,.18,.45,dark,0,1.3,0);
 cube(body,.56,.23,.12,metal,0,1.75,.32);

 ball(body,.25,skin,0,2.3,0,.85,1.1,.85);
 ball(body,.26,dark,0,2.49,-.02,1,.48,.9);
 cube(body,.43,.07,.05,metal,0,2.39,.2);
 ball(body,.035,glow,-.08,2.32,.22);
 ball(body,.035,glow,.08,2.32,.22);

 for(const s of [-1,1]){
  ball(body,.19,metal,s*.48,1.85,0,1,.8,1);
 }
 const limbs={};

 function arm(side){
  const p=new THREE.Group();
  p.position.set(side*.49,1.85,0);
  body.add(p);
  rod(p,[0,0,0],[side*.12,-.4,.02],.11,skin);
  ball(p,.105,metal,side*.12,-.42,.02);
  rod(p,[side*.12,-.42,.02],[side*.15,-.78,.12],.09,skin);
  ball(p,.11,cloth,side*.15,-.82,.13,1,.8,.8);
  cube(p,.2,.12,.2,metal,side*.15,-.82,.17);
  return p;
 }
 function leg(side){
  const p=new THREE.Group();
  p.position.set(side*.22,1,0);
  body.add(p);
  rod(p,[0,0,0],[side*.03,-.43,0],.15,cloth);
  rod(p,[side*.03,-.43,0],[side*.07,-.86,.05],.105,skin);
  cube(p,.24,.16,.38,dark,side*.07,-.92,.12);
  cube(p,.22,.05,.3,metal,side*.07,-.83,.12);
  return p;
 }
 limbs.la=arm(-1);limbs.ra=arm(1);
 limbs.ll=leg(-1);limbs.rl=leg(1);

 const shadow=new THREE.Mesh(
  new THREE.CircleGeometry(.6,24),
  new THREE.MeshBasicMaterial({color:0x000000,transparent:true,opacity:.4})
 );
 shadow.rotation.x=-Math.PI/2;
 shadow.position.y=.025;
 root.add(shadow);

 return {
  root,body,limbs,shadow,hp:100,face:isPlayer?1:-1,
  isPlayer,accent,action:'idle',actionTime:0,cooldown:0,
  stun:0,block:false,onGround:true,jumpV:0,
  special:3,walk:0,hitDone:false
 };
}

let player,enemy,projectiles=[],particles=[];
let over=false,time=90,last=0,elapsed=0,shake=0;
const keys={};

function burst(pos,color,n=15){
 for(let i=0;i<n;i++){
  const mesh=new THREE.Mesh(
   new THREE.SphereGeometry(.04,7,6),
   new THREE.MeshBasicMaterial({color,transparent:true})
  );
  mesh.position.copy(pos);
  scene.add(mesh);
  particles.push({
   mesh,
   v:new THREE.Vector3(
    (Math.random()-.5)*4,
    Math.random()*4,
    (Math.random()-.5)*3
   ),
   life:.7+Math.random()*.3
  });
 }
}

function hud(){
 document.getElementById('php').style.width=player.hp+'%';
 document.getElementById('ehp').style.width=enemy.hp+'%';
 document.getElementById('timer').textContent=Math.ceil(time);
}
function restart(){
 if(player)scene.remove(player.root);
 if(enemy)scene.remove(enemy.root);
 projectiles.forEach(p=>scene.remove(p.mesh));
 particles.forEach(p=>scene.remove(p.mesh));
 projectiles=[];particles=[];
 player=fighter(-2.6,0x087da5,0x00eaff,true);
 enemy=fighter(2.6,0x811e43,0xff3975,false);
 enemy.root.rotation.y=Math.PI;
 time=90;over=false;elapsed=0;shake=0;
 Object.keys(keys).forEach(k=>keys[k]=false);
 message.innerHTML='ROUND 1<div id="sub">FIGHT!</div>';
 hud();
}

function attack(f,type){
 if(over||f.cooldown>0||f.stun>0)return;
 if(type==='special'){
  if(f.special<=0)return;
  f.special--;
  f.cooldown=1.1;
  const mesh=new THREE.Mesh(
   new THREE.IcosahedronGeometry(.24,2),
   new THREE.MeshStandardMaterial({
    color:f.accent,emissive:f.accent,
    emissiveIntensity:3,metalness:.5
   })
  );
  mesh.position.set(f.root.position.x+f.face*.7,1.7,0);
  scene.add(mesh);
  projectiles.push({mesh,owner:f,vx:f.face*10,life:2});
  burst(mesh.position,f.accent,14);
  return;
 }
 f.action=type;
 f.actionTime=type==='punch'?.35:.5;
 f.cooldown=type==='punch'?.4:.65;
 f.hitDone=false;
}

function hit(a,b,damage,range){
 if(a.actionTime<=0||a.hitDone)return;
 const dx=b.root.position.x-a.root.position.x;
 if(Math.abs(dx)>range||Math.sign(dx)!==a.face)return;
 a.hitDone=true;
 const blocked=b.block;
 b.hp=Math.max(0,b.hp-(blocked?damage*.15:damage));
 if(!blocked){
  b.stun=.3;
  b.root.position.x+=a.face*.2;
 }
 burst(new THREE.Vector3(b.root.position.x,1.5,0),
  blocked?0x00eaff:0xffdf4a,blocked?8:18);
 shake=blocked?.1:.22;
}

function updateFighter(f,o,dt,ai){
 f.cooldown=Math.max(0,f.cooldown-dt);
 f.stun=Math.max(0,f.stun-dt);
 f.actionTime=Math.max(0,f.actionTime-dt);
 if(f.actionTime<=0){f.action='idle';f.hitDone=false;}

 const dx=o.root.position.x-f.root.position.x;
 f.face=Math.sign(dx)||f.face;
 f.root.rotation.y=f.face===1?0:Math.PI;
 f.block=false;
 let move=0;

 if(f.stun<=0){
  if(!ai){
   if(keys.left)move--;
   if(keys.right)move++;
   f.block=!!keys.block;
   if(keys.jump&&f.onGround){
    f.jumpV=7;f.onGround=false;
   }
   if(keys.punch){attack(f,'punch');keys.punch=false;}
   if(keys.kick){attack(f,'kick');keys.kick=false;}
   if(keys.special){attack(f,'special');keys.special=false;}
  }else{
   const d=Math.abs(dx);
   if(d>1.5)move=Math.sign(dx);
   if(d<1.8&&Math.random()<.014)f.block=true;
   if(d<1.9&&Math.random()<.019)
    attack(f,Math.random()<.55?'punch':'kick');
   if(d>3.4&&f.special>0&&Math.random()<.003)
    attack(f,'special');
   if(d<2.1&&f.onGround&&Math.random()<.002){
    f.jumpV=6.5;f.onGround=false;
   }
  }
 }

 const speed=ai?2.6:4;
 f.root.position.x=THREE.MathUtils.clamp(
  f.root.position.x+move*speed*dt,-5.2,5.2
 );

 if(!f.onGround){
  f.root.position.y+=f.jumpV*dt;
  f.jumpV-=19*dt;
  if(f.root.position.y<=0){
   f.root.position.y=0;f.jumpV=0;f.onGround=true;
  }
 }

 f.walk+=dt*(Math.abs(move)>0?10:3);
 const swing=Math.sin(f.walk)*.45*(Math.abs(move)>0?1:.1);
 f.limbs.ll.rotation.x=swing;
 f.limbs.rl.rotation.x=-swing;
 f.limbs.la.rotation.x=-swing*.5;
 f.limbs.ra.rotation.x=swing*.5;

 if(f.action==='punch'){
  f.limbs.ra.rotation.x=-1.8*(f.actionTime/.35);
 }else if(f.action==='kick'){
  f.limbs.rl.rotation.x=-1.5*(f.actionTime/.5);
 }
 if(f.block){
  f.limbs.la.rotation.x=-1.6;
  f.limbs.ra.rotation.x=-1.6;
 }
 f.body.position.y=Math.abs(Math.sin(f.walk*2))*
  (Math.abs(move)>.1?.04:.01);
 f.shadow.scale.setScalar(1-f.root.position.y*.05);

 if(f.action!=='idle'&&f.action!=='special')
  hit(f,o,f.action==='kick'?13:9,f.action==='kick'?1.9:1.4);
}

function updateProjectiles(dt){
 for(const p of projectiles){
  p.mesh.position.x+=p.vx*dt;
  p.life-=dt;
  p.mesh.rotation.x+=dt*7;
  p.mesh.rotation.y+=dt*8;
  const target=p.owner===player?enemy:player;
  if(Math.abs(p.mesh.position.x-target.root.position.x)<.5&&
     Math.abs(p.mesh.position.y-1.5)<1){
   target.hp=Math.max(0,target.hp-(target.block?5:20));
   if(!target.block)target.stun=.4;
   burst(p.mesh.position,p.owner.accent,24);
   p.life=0;shake=.3;
  }
  if(p.life<=0)scene.remove(p.mesh);
 }
 projectiles=projectiles.filter(p=>p.life>0);
}

function updateParticles(dt){
 for(const p of particles){
  p.life-=dt;
  p.mesh.position.addScaledVector(p.v,dt);
  p.v.y-=5*dt;
  p.mesh.material.opacity=Math.max(0,p.life);
  p.mesh.scale.setScalar(Math.max(.01,p.life));
 }
 particles=particles.filter(p=>{
  if(p.life<=0){scene.remove(p.mesh);return false;}
  return true;
 });
}

function finish(msg){
 over=true;
 message.innerHTML=msg+'<div id="sub">PRESS RESTART TO PLAY AGAIN</div>';
}

function animate(now){
 requestAnimationFrame(animate);
 const dt=Math.min((now-last)/1000||0,.035);
 last=now;

 if(!over){
  elapsed+=dt;
  time=Math.max(0,time-dt);
  updateFighter(player,enemy,dt,false);
  updateFighter(enemy,player,dt,true);

  if(Math.abs(player.root.position.x-enemy.root.position.x)<.8){
   const s=Math.sign(player.root.position.x-enemy.root.position.x)||1;
   player.root.position.x-=s*.01;
   enemy.root.position.x+=s*.01;
  }

  updateProjectiles(dt);
  updateParticles(dt);

  if(player.hp<=0)finish('💀 YOU LOST!');
  else if(enemy.hp<=0)finish('🏆 YOU WIN!');
  else if(time<=0)
   finish(player.hp>enemy.hp?'🏆 YOU WIN!':
    player.hp<enemy.hp?'💀 YOU LOSE!':'🤝 DRAW!');

  hud();
 }

 const mid=(player.root.position.x+enemy.root.position.x)/2;
 camera.position.x+=(mid*.2-camera.position.x)*.04;
 camera.lookAt(mid*.2,1.3,0);

 if(shake>0){
  camera.position.x+=(Math.random()-.5)*shake;
  camera.position.y+=(Math.random()-.5)*shake;
  shake=Math.max(0,shake-dt);
 }
 renderer.render(scene,camera);
}

// Input: pointer IDs prevent one finger releasing another finger's key.
const held={};
document.querySelectorAll('[data-hold]').forEach(btn=>{
 const key=btn.dataset.hold;
 btn.addEventListener('pointerdown',e=>{
  e.preventDefault();
  btn.setPointerCapture(e.pointerId);
  if(!held[key])held[key]=new Set();
  held[key].add(e.pointerId);
  keys[key]=true;
  btn.classList.add('held');
 });
 const release=e=>{
  if(held[key])held[key].delete(e.pointerId);
  keys[key]=!!(held[key]&&held[key].size);
  if(!keys[key])btn.classList.remove('held');
 };
 btn.addEventListener('pointerup',release);
 btn.addEventListener('pointercancel',release);
 btn.addEventListener('lostpointercapture',release);
});

document.querySelectorAll('[data-tap]').forEach(btn=>{
 btn.addEventListener('pointerdown',e=>{
  e.preventDefault();
  keys[btn.dataset.tap]=true;
  btn.classList.add('held');
 });
 btn.addEventListener('pointerup',()=>{
  keys[btn.dataset.tap]=false;
  btn.classList.remove('held');
 });
 btn.addEventListener('pointercancel',()=>{
  keys[btn.dataset.tap]=false;
  btn.classList.remove('held');
 });
});

const map={
 ArrowLeft:'left',a:'left',
 ArrowRight:'right',d:'right',
 ArrowUp:'jump',w:'jump',' ':'jump',
 s:'block',j:'punch',k:'kick',l:'special'
};
addEventListener('keydown',e=>{
 const key=map[e.key];
 if(key){e.preventDefault();keys[key]=true;}
});
addEventListener('keyup',e=>{
 const key=map[e.key];
 if(key)keys[key]=false;
});
addEventListener('blur',()=>{
 Object.keys(keys).forEach(k=>keys[k]=false);
});
document.getElementById('restart').addEventListener('click',restart);

addEventListener('resize',()=>{
 camera.aspect=innerWidth/innerHeight;
 camera.updateProjectionMatrix();
 renderer.setSize(innerWidth,innerHeight);
 renderer.setPixelRatio(Math.min(devicePixelRatio,2));
});

restart();
requestAnimationFrame(animate);
})();
</script>
</body>
</html>

Game Source: Shadow Fighters 3D

Creator: FrostFlare80

Libraries: three

Complexity: complex (558 lines, 16.2 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: shadow-fighters-3d-frostflare80" to link back to the original. Then publish at arcadelab.ai/publish.