İçeriğe geç
Tüm kodlar

İstanbul'un Fethi

Yorumla oynanan TikTok LIVE strateji oyunu

261 satır · 9,8 KBHamİndir
// Theodosius surları: taş bloklar hasar aldıkça düşer, can bitince orta kısım çöker.
import * as THREE from 'three';
import { WALL_Z } from './world.js';

const COLS = 20;
const ROWS = 4;
const BW = 1.3;
const BH = 1.0;
const BD = 1.5;
const TOWERS = [-13.4, -6.5, 6.5, 13.4];

const stoneMats = [0xc9b48e, 0xbca582, 0xd3c19c, 0xb39b78, 0xc2ad88].map((c) => new THREE.MeshStandardMaterial({ color: c, roughness: 0.95 }));
const brickMat = new THREE.MeshStandardMaterial({ color: 0xa65a3c, roughness: 0.95 });
const blockGeo = new THREE.BoxGeometry(BW * 0.98, BH * 0.97, BD);
const merlonGeo = new THREE.BoxGeometry(BW * 0.5, 0.6, BD * 0.9);
const stripeGeo = new THREE.BoxGeometry(BW, 0.12, BD * 1.01);

function defender() {
  const g = new THREE.Group();
  const body = new THREE.Mesh(new THREE.CapsuleGeometry(0.2, 0.3, 4, 8), new THREE.MeshStandardMaterial({ color: 0x4b2a6a, roughness: 0.7 }));
  body.position.y = 0.45;
  const head = new THREE.Mesh(new THREE.SphereGeometry(0.2, 10, 8), new THREE.MeshStandardMaterial({ color: 0xe0ac82 }));
  head.position.y = 0.95;
  const helm = new THREE.Mesh(new THREE.ConeGeometry(0.22, 0.32, 10), new THREE.MeshStandardMaterial({ color: 0xc0c4cc, metalness: 0.8, roughness: 0.3 }));
  helm.position.y = 1.15;
  const shield = new THREE.Mesh(new THREE.CylinderGeometry(0.28, 0.28, 0.05, 14), new THREE.MeshStandardMaterial({ color: 0xc9a227, metalness: 0.6, roughness: 0.4 }));
  shield.rotation.x = Math.PI / 2;
  shield.position.set(-0.22, 0.5, 0.2);
  const spear = new THREE.Mesh(new THREE.CylinderGeometry(0.025, 0.025, 1.8, 6), new THREE.MeshStandardMaterial({ color: 0x6b4a2a }));
  spear.position.set(0.25, 0.9, 0.05);
  g.add(body, head, helm, shield, spear);
  return g;
}

export function buildWall(scene, { fx, onFall } = {}) {
  const root = new THREE.Group();
  root.position.z = WALL_Z;
  scene.add(root);

  // kırılabilir parçalar (iki kule arasındaki orta bölüm), diğerleri sağlam
  const pieces = [];
  const breakable = [];
  for (let c = 0; c < COLS; c++) {
    const x = -COLS * BW / 2 + BW / 2 + c * BW;
    const brk = Math.abs(x) < 5.6;
    for (let r = 0; r < ROWS; r++) {
      const m = new THREE.Mesh(blockGeo, stoneMats[(c * 7 + r * 3) % stoneMats.length]);
      const home = new THREE.Vector3(x, BH / 2 + r * BH, 0);
      m.position.copy(home);
      root.add(m);
      if (r === 1 || r === 3) {
        const s = new THREE.Mesh(stripeGeo, brickMat); // tuğla bantlar
        s.position.y = BH / 2 - 0.06;
        m.add(s);
      }
      const p = { mesh: m, home, col: c, row: r, x, state: 'up', vel: new THREE.Vector3(), spin: new THREE.Vector3() };
      pieces.push(p);
      if (brk) breakable.push(p);
    }
    for (const dx of [-0.3, 0.3]) {
      const m = new THREE.Mesh(merlonGeo, stoneMats[(c + 2) % stoneMats.length]);
      const home = new THREE.Vector3(x + dx, ROWS * BH + 0.3, 0);
      m.position.copy(home);
      root.add(m);
      const p = { mesh: m, home, col: c, row: ROWS, x: x + dx, state: 'up', vel: new THREE.Vector3(), spin: new THREE.Vector3(), merlon: true };
      pieces.push(p);
      if (brk) breakable.push(p);
    }
  }

  // kuleler (yıkılmaz)
  for (const tx of TOWERS) {
    const t = new THREE.Group();
    const h = 6.2;
    const body = new THREE.Mesh(new THREE.BoxGeometry(2.6, h, 2.6), stoneMats[1]);
    body.position.y = h / 2;
    t.add(body);
    for (const y of [1.6, 3.4, 5.2]) {
      const band = new THREE.Mesh(new THREE.BoxGeometry(2.64, 0.14, 2.64), brickMat);
      band.position.y = y;
      t.add(band);
    }
    for (let i = 0; i < 4; i++) for (const s of [-1, 1]) {
      const m = new THREE.Mesh(new THREE.BoxGeometry(0.5, 0.6, 0.5), stoneMats[2]);
      const off = -0.95 + i * 0.63;
      if (i % 2) m.position.set(s * 1.05, h + 0.3, off); else m.position.set(off, h + 0.3, s * 1.05);
      t.add(m);
    }
    const win = new THREE.Mesh(new THREE.PlaneGeometry(0.4, 0.8), new THREE.MeshBasicMaterial({ color: 0x1a1008 }));
    win.position.set(0, 4.2, 1.31);
    t.add(win);
    t.position.set(tx, 0, 0.45);
    root.add(t);
  }

  // surdaki savunucular
  const defenders = [];
  for (const c of [6, 8, 10, 11, 13]) {
    const d = defender();
    const x = -COLS * BW / 2 + BW / 2 + c * BW;
    d.position.set(x, ROWS * BH, -0.35);
    root.add(d);
    defenders.push({ g: d, col: c, x, state: 'up', vy: 0, t: Math.random() * 5, shoot: 2 + Math.random() * 6 });
  }

  let maxHp = 1500;
  let hp = maxHp;
  let order = [];
  function makeOrder() {
    // önce mazgallar, sonra üst sıralardan aşağı; her katta rastgele
    const byRow = (r) => breakable.filter((p) => p.row === r).sort(() => Math.random() - 0.5);
    order = [...byRow(ROWS), ...byRow(3), ...byRow(2), ...byRow(1)];
  }
  makeOrder();
  const keep = 0.12; // son darbeye kalan pay

  function knock(p, power = 1) {
    if (p.state !== 'up') return;
    p.state = 'falling';
    p.vel.set((Math.random() - 0.5) * 3, 1 + Math.random() * 2.5 * power, 0.8 + Math.random() * 2 * power);
    p.spin.set((Math.random() - 0.5) * 8, (Math.random() - 0.5) * 6, (Math.random() - 0.5) * 8);
    const wp = p.mesh.getWorldPosition(new THREE.Vector3());
    fx?.dust(wp, power > 1 ? 18 : 10);
    // üstündeki savunucu düşer
    for (const d of defenders) if (d.state === 'up' && d.col === p.col && p.row >= ROWS - 1) dropDefender(d);
    // üstte kalan bloklar havada asılı kalmasın
    for (const q of pieces) if (q.state === 'up' && q.col === p.col && q.row > p.row && !q.merlon) knock(q, power);
    for (const q of pieces) if (q.state === 'up' && q.merlon && Math.abs(q.x - p.x) < 0.7 && p.row < ROWS) knock(q, power);
  }
  function dropDefender(d) {
    d.state = 'fall';
    d.vy = 2;
    onFall?.();
  }

  function sync() {
    const lost = 1 - hp / maxHp;
    const want = Math.floor(Math.min(1, lost / (1 - keep)) * order.length);
    let down = order.filter((p) => p.state !== 'up').length;
    for (const p of order) {
      if (down >= want) break;
      if (p.state === 'up') {
        knock(p);
        down = order.filter((q) => q.state !== 'up').length;
      }
    }
  }

  const tmp = new THREE.Vector3();
  let rebuildT = -1;

  return {
    get hp() { return hp; },
    get maxHp() { return maxHp; },
    get down() { return hp <= 0; },
    breach: new THREE.Vector3(0, 0, WALL_Z),
    setHp(value, max) {
      maxHp = max;
      hp = Math.max(0, Math.min(max, value));
      if (hp <= 0) { for (const p of breakable) knock(p); }
      else sync();
    },
    /** Sıradaki yıkılacak taşın dünya konumu (mermi oraya gider) */
    targetPoint() {
      const p = order.find((q) => q.state === 'up') || breakable[Math.floor(Math.random() * breakable.length)];
      p.mesh.getWorldPosition(tmp);
      if (p.state !== 'up') tmp.set(p.home.x + (Math.random() - 0.5) * 3, 1 + Math.random() * 2.5, WALL_Z);
      return tmp.clone().add(new THREE.Vector3(0, 0, BD / 2));
    },
    randomPoint() {
      return new THREE.Vector3((Math.random() - 0.5) * 10, 0.8 + Math.random() * 3.2, WALL_Z + BD / 2);
    },
    defenderTop() {
      const up = defenders.filter((d) => d.state === 'up');
      return up.length ? up[Math.floor(Math.random() * up.length)] : null;
    },
    /** Hasar uygula; sur düştüyse true */
    damage(n) {
      if (hp <= 0) return false;
      hp = Math.max(0, hp - n);
      if (hp <= 0) return true;
      sync();
      return false;
    },
    /** Son darbe: ortadaki her şey yıkılır */
    collapse() {
      breakable.forEach((p, i) => setTimeout(() => knock(p, 1.6), (p.row === ROWS ? 0 : (ROWS - p.row) * 180) + Math.random() * 400 + i));
      for (const d of defenders) if (d.state === 'up') dropDefender(d);
    },
    rebuild(newMax) {
      maxHp = newMax;
      hp = newMax;
      for (const p of pieces) {
        p.from = p.mesh.position.clone();
        p.fromRot = p.mesh.rotation.clone();
      }
      rebuildT = 0;
      for (const d of defenders) {
        d.state = 'up';
        d.g.visible = true;
        d.g.position.set(d.x, ROWS * BH, -0.35);
        d.g.rotation.set(0, 0, 0);
      }
      makeOrder();
    },
    update(dt, t, beat) {
      for (const p of pieces) {
        if (p.state !== 'falling') continue;
        p.vel.y -= 18 * dt;
        p.mesh.position.addScaledVector(p.vel, dt);
        p.mesh.rotation.x += p.spin.x * dt;
        p.mesh.rotation.z += p.spin.z * dt;
        const floor = 0.35;
        if (p.mesh.position.y < floor) {
          p.mesh.position.y = floor;
          if (Math.abs(p.vel.y) > 3) {
            p.vel.y *= -0.25;
            p.vel.x *= 0.5;
            p.vel.z *= 0.4;
            p.spin.multiplyScalar(0.4);
          } else {
            p.state = 'rubble';
          }
        }
      }
      if (rebuildT >= 0) {
        rebuildT += dt / 2.5;
        const k = Math.min(1, rebuildT);
        const e = k * k * (3 - 2 * k);
        for (const p of pieces) {
          p.mesh.position.lerpVectors(p.from, p.home, e);
          p.mesh.rotation.set(p.fromRot.x * (1 - e), p.fromRot.y * (1 - e), p.fromRot.z * (1 - e));
          if (k >= 1) p.state = 'up';
        }
        if (k >= 1) rebuildT = -1;
      }
      for (const d of defenders) {
        d.t += dt;
        if (d.state === 'up') {
          d.g.position.y = ROWS * BH + Math.abs(Math.sin(d.t * 3)) * 0.05 + beat.pulse * 0.03;
          d.g.rotation.y = Math.sin(d.t * 0.8) * 0.4;
          d.shoot -= dt;
          if (d.shoot <= 0) {
            d.shoot = 4 + Math.random() * 7;
            fx?.defenderArrow(d.g.getWorldPosition(new THREE.Vector3()).add(new THREE.Vector3(0, 1, 0.4)));
          }
        } else if (d.state === 'fall') {
          d.vy -= 14 * dt;
          d.g.position.y += d.vy * dt;
          d.g.position.z += 2.5 * dt;
          d.g.rotation.x -= 4 * dt;
          if (d.g.position.y < 0.2) {
            d.state = 'gone';
            d.g.visible = false;
            fx?.dust(d.g.getWorldPosition(new THREE.Vector3()), 6);
          }
        }
      }
    },
  };
}

Lisans bilgisi projenin LICENSE dosyasında.