İçeriğe geç
Tüm kodlar

İstanbul'un Fethi

Yorumla oynanan TikTok LIVE strateji oyunu

179 satır · 7,1 KBHamİndir
// Efektler: gülleler, oklar, toz/duman bulutları, kıvılcım, kırmızı-beyaz konfeti, ekran sarsıntısı.
import * as THREE from 'three';

const MAXP = 900;
const MAXC = 900;

export function createFx(scene, sfx) {
  // ---------- parçacıklar ----------
  const puffMesh = new THREE.InstancedMesh(
    new THREE.IcosahedronGeometry(0.5, 1),
    new THREE.MeshStandardMaterial({ roughness: 1, flatShading: true, transparent: true, opacity: 0.6, depthWrite: false }),
    MAXP,
  );
  puffMesh.frustumCulled = false;
  puffMesh.count = 0;
  scene.add(puffMesh);
  const chipMesh = new THREE.InstancedMesh(
    new THREE.PlaneGeometry(0.16, 0.1),
    new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, toneMapped: false }),
    MAXC,
  );
  chipMesh.frustumCulled = false;
  chipMesh.count = 0;
  scene.add(chipMesh);

  const puffs = [];
  const chips = [];
  const col = new THREE.Color();
  const DUST = [0xe6d8bc, 0xd8c8a8, 0xeee4d0, 0xc8b898];
  const SMOKE = [0xe8e4dc, 0xd0ccc4, 0xbab6ae];
  const FIRE = [0xffb030, 0xff7a1a, 0xffe08a];

  function puff(pos, { colors = DUST, n = 10, spread = 1, up = 2, size = 1, life = 1.6, drag = 2, rise = 0 } = {}) {
    for (let i = 0; i < n; i++) {
      const p = {
        pos: pos.clone().add(new THREE.Vector3((Math.random() - 0.5) * spread, Math.random() * spread * 0.5, (Math.random() - 0.5) * spread)),
        vel: new THREE.Vector3((Math.random() - 0.5) * 4, Math.random() * up, (Math.random() - 0.3) * 4),
        size: size * (0.5 + Math.random() * 0.8),
        life, age: 0, drag, rise,
      };
      const idx = puffs.length < MAXP ? puffs.length : Math.floor(Math.random() * MAXP);
      puffs[idx] = p;
      puffMesh.setColorAt(idx, col.setHex(colors[(Math.random() * colors.length) | 0]));
    }
    if (puffMesh.instanceColor) puffMesh.instanceColor.needsUpdate = true;
  }

  const CONF = [0xe0102f, 0xffffff, 0xffcc33, 0xe0102f, 0xffffff];
  function confetti(pos, n = 200, spread = 6) {
    for (let i = 0; i < n; i++) {
      const c = {
        pos: pos.clone().add(new THREE.Vector3((Math.random() - 0.5) * spread, Math.random() * 2, (Math.random() - 0.5) * spread)),
        vel: new THREE.Vector3((Math.random() - 0.5) * 5, 3 + Math.random() * 6, (Math.random() - 0.5) * 5),
        rot: new THREE.Vector3(Math.random() * 6, Math.random() * 6, Math.random() * 6),
        spin: new THREE.Vector3(Math.random() * 9, Math.random() * 9, Math.random() * 9),
        life: 5 + Math.random() * 2, age: 0,
      };
      const idx = chips.length < MAXC ? chips.length : Math.floor(Math.random() * MAXC);
      chips[idx] = c;
      chipMesh.setColorAt(idx, col.setHex(CONF[(Math.random() * CONF.length) | 0]));
    }
    if (chipMesh.instanceColor) chipMesh.instanceColor.needsUpdate = true;
  }

  // ---------- mermiler ----------
  const ballGeo = new THREE.SphereGeometry(0.22, 12, 8);
  const ballMat = new THREE.MeshStandardMaterial({ color: 0x2a2a2a, roughness: 0.6, metalness: 0.4 });
  const bigMat = new THREE.MeshStandardMaterial({ color: 0x3a2a1a, emissive: 0xff5a10, emissiveIntensity: 0.6 });
  const arrowGeo = new THREE.CylinderGeometry(0.018, 0.018, 0.8, 5);
  arrowGeo.rotateX(Math.PI / 2);
  const arrowMat = new THREE.MeshStandardMaterial({ color: 0x5a3a1e });
  const shots = [];

  /** from -> to arasında kavisli uçuş; varınca onHit */
  function shoot(kind, from, to, onHit) {
    let mesh;
    let dur;
    let arc;
    if (kind === 'arrow' || kind === 'enemyArrow') {
      mesh = new THREE.Mesh(arrowGeo, arrowMat);
      dur = 0.9 + from.distanceTo(to) * 0.02;
      arc = 2.5;
      sfx?.arrow();
    } else {
      const big = kind === 'big';
      mesh = new THREE.Mesh(ballGeo, big ? bigMat : ballMat);
      if (big) mesh.scale.setScalar(1.8);
      dur = 0.75 + from.distanceTo(to) * 0.012;
      arc = big ? 3.5 : 2.2;
    }
    mesh.position.copy(from);
    scene.add(mesh);
    shots.push({ kind, mesh, from: from.clone(), to: to.clone(), t: 0, dur, arc, onHit, prev: from.clone() });
  }

  let shake = 0;
  const tmp = new THREE.Vector3();

  return {
    shoot,
    confetti,
    dust(pos, n = 10) { puff(pos, { n: Math.ceil(n * 0.7), spread: 1.2, up: 2.5, size: 0.55, life: 1.5 }); },
    smoke(pos, n = 8) { puff(pos, { colors: SMOKE, n, spread: 0.6, up: 1.5, size: 0.7, life: 2.0, rise: 1.2 }); },
    fire(pos, n = 8) { puff(pos, { colors: FIRE, n, spread: 0.5, up: 3, size: 0.5, life: 0.5 }); },
    defenderArrow(from) {
      const to = new THREE.Vector3((Math.random() - 0.5) * 10, 0, -6 + Math.random() * 6);
      shoot('enemyArrow', from, to, () => {});
    },
    shake(amount) { shake = Math.max(shake, amount); },
    get shakeAmount() { return shake; },
    update(dt) {
      shake = Math.max(0, shake - dt * 1.8);
      // mermiler
      for (let i = shots.length - 1; i >= 0; i--) {
        const s = shots[i];
        s.t += dt / s.dur;
        const k = Math.min(1, s.t);
        s.prev.copy(s.mesh.position);
        s.mesh.position.lerpVectors(s.from, s.to, k);
        s.mesh.position.y += Math.sin(k * Math.PI) * s.arc;
        if (s.kind.endsWith('rrow')) {
          tmp.copy(s.mesh.position).sub(s.prev);
          if (tmp.lengthSq() > 0) s.mesh.lookAt(tmp.add(s.mesh.position));
        } else if (Math.random() < 0.5) {
          puff(s.mesh.position, { colors: s.kind === 'big' ? FIRE : SMOKE, n: 1, spread: 0.1, up: 0.3, size: 0.35, life: 0.6 });
        }
        if (k >= 1) {
          scene.remove(s.mesh);
          shots.splice(i, 1);
          s.onHit?.(s.to);
        }
      }
      // puf
      const m = new THREE.Matrix4();
      const q = new THREE.Quaternion();
      const sc = new THREE.Vector3();
      let n = 0;
      for (let i = 0; i < puffs.length; i++) {
        const p = puffs[i];
        if (!p) continue;
        p.age += dt;
        if (p.age >= p.life) { puffs[i] = null; continue; }
        p.vel.multiplyScalar(Math.max(0, 1 - p.drag * dt));
        p.vel.y += p.rise * dt;
        p.pos.addScaledVector(p.vel, dt);
        const k = p.age / p.life;
        const s = p.size * (0.4 + k * 1.2) * (1 - k * k);
        m.compose(p.pos, q, sc.set(s, s, s));
        puffMesh.setMatrixAt(i, m);
        n = i + 1;
      }
      for (let i = 0; i < n; i++) if (!puffs[i]) puffMesh.setMatrixAt(i, m.makeScale(0, 0, 0));
      puffMesh.count = n;
      puffMesh.instanceMatrix.needsUpdate = true;
      // konfeti
      const e = new THREE.Euler();
      let c = 0;
      for (let i = 0; i < chips.length; i++) {
        const p = chips[i];
        if (!p) continue;
        p.age += dt;
        if (p.age >= p.life) { chips[i] = null; continue; }
        p.vel.y -= 6 * dt;
        p.vel.multiplyScalar(1 - 1.4 * dt);
        p.pos.addScaledVector(p.vel, dt);
        if (p.pos.y < 0.03) { p.pos.y = 0.03; p.vel.set(0, 0, 0); }
        else p.rot.addScaledVector(p.spin, dt);
        q.setFromEuler(e.set(p.rot.x, p.rot.y, p.rot.z));
        const s = p.age > p.life - 0.5 ? (p.life - p.age) * 2 : 1;
        m.compose(p.pos, q, sc.set(s, s, s));
        chipMesh.setMatrixAt(i, m);
        c = i + 1;
      }
      for (let i = 0; i < c; i++) if (!chips[i]) chipMesh.setMatrixAt(i, m.makeScale(0, 0, 0));
      chipMesh.count = c;
      chipMesh.instanceMatrix.needsUpdate = true;
    },
  };
}

Lisans bilgisi projenin LICENSE dosyasında.