İçeriğe geç
Tüm kodlar

İstanbul'un Fethi

Yorumla oynanan TikTok LIVE strateji oyunu

438 satır · 14,2 KBHamİndir
// Osmanlı askeri: yeniçeri (börk), sipahi (sarık + mızrak), okçu (yay). İsim etiketi, balon, iş sırası (yürü, tırman, vuruş...).
import * as THREE from 'three';
import { CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';

const SKIN = [0xf2c9a0, 0xe0ac82, 0xc68863, 0xd9a47a, 0xf5d5b8];
const cache = new Map();
const mat = (c, o = {}) => {
  const k = c + JSON.stringify(o);
  if (!cache.has(k)) cache.set(k, new THREE.MeshStandardMaterial({ color: c, roughness: 0.7, ...o }));
  return cache.get(k);
};
const G = {
  kaftan: new THREE.CylinderGeometry(0.19, 0.3, 0.62, 10),
  chest: new THREE.CapsuleGeometry(0.19, 0.18, 4, 10),
  sash: new THREE.TorusGeometry(0.2, 0.05, 6, 14),
  head: new THREE.IcosahedronGeometry(0.25, 2),
  eye: new THREE.SphereGeometry(0.03, 6, 5),
  must: new THREE.CapsuleGeometry(0.025, 0.12, 3, 6),
  arm: new THREE.CapsuleGeometry(0.06, 0.24, 3, 6),
  hand: new THREE.SphereGeometry(0.06, 6, 5),
  leg: new THREE.CapsuleGeometry(0.07, 0.2, 3, 6),
  borkBody: new THREE.CylinderGeometry(0.2, 0.17, 0.5, 12),
  borkFlap: new THREE.BoxGeometry(0.26, 0.55, 0.05),
  borkGold: new THREE.BoxGeometry(0.08, 0.2, 0.04),
  turban: new THREE.SphereGeometry(0.29, 12, 8),
  turbanTop: new THREE.ConeGeometry(0.1, 0.22, 8),
  cap: new THREE.CylinderGeometry(0.17, 0.22, 0.2, 10),
  blade: new THREE.BoxGeometry(0.04, 0.62, 0.09),
  hilt: new THREE.BoxGeometry(0.16, 0.04, 0.05),
  spear: new THREE.CylinderGeometry(0.02, 0.02, 2.0, 6),
  spearTip: new THREE.ConeGeometry(0.05, 0.18, 6),
  bow: new THREE.TorusGeometry(0.42, 0.022, 4, 16, Math.PI * 0.9),
  torch: new THREE.CylinderGeometry(0.03, 0.03, 0.4, 6),
  flame: new THREE.SphereGeometry(0.08, 8, 6),
};
G.arm.translate(0, -0.16, 0);
G.leg.translate(0, -0.16, 0);
G.borkFlap.translate(0, -0.25, 0);
G.blade.translate(0, 0.36, 0);
G.spear.translate(0, 0.6, 0);
const FLAME = new THREE.MeshBasicMaterial({ color: 0xffa030, toneMapped: false });
const STEEL = mat(0xd8dde4, { metalness: 0.85, roughness: 0.25 });
const GOLD = mat(0xffcc33, { metalness: 0.8, roughness: 0.3 });
const WOOD = mat(0x6b4423);

function seeded(str) {
  let h = 2166136261;
  for (let i = 0; i < str.length; i++) h = Math.imul(h ^ str.charCodeAt(i), 16777619);
  return () => {
    h = Math.imul(h ^ (h >>> 15), 2246822507);
    h = Math.imul(h ^ (h >>> 13), 3266489909);
    return ((h ^= h >>> 16) >>> 0) / 4294967296;
  };
}
const pick = (a, r) => a[Math.floor(r * a.length) % a.length];

const LADDER = (() => {
  const g = new THREE.Group();
  for (const s of [-0.28, 0.28]) {
    const rail = new THREE.Mesh(new THREE.BoxGeometry(0.07, 4.8, 0.07), WOOD);
    rail.position.set(s, 2.4, 0);
    g.add(rail);
  }
  for (let i = 0; i < 11; i++) {
    const r = new THREE.Mesh(new THREE.BoxGeometry(0.56, 0.05, 0.05), WOOD);
    r.position.y = 0.3 + i * 0.42;
    g.add(r);
  }
  return g;
})();
export const makeLadder = () => LADDER.clone();

export class Soldier {
  constructor(name) {
    const rnd = seeded(name);
    this.name = name;
    const r = rnd();
    this.kind = r < 0.55 ? 'yeniceri' : r < 0.8 ? 'sipahi' : 'okcu';
    this.root = new THREE.Group();
    this.hips = new THREE.Group();
    this.hips.position.y = 0.4;
    this.root.add(this.hips);

    const kaftanColor = this.kind === 'sipahi' ? pick([0xc0262e, 0xa0182a], rnd())
      : this.kind === 'okcu' ? pick([0x3c6b2e, 0x2f5a3a], rnd())
        : pick([0x1e4f9c, 0x8e1b22, 0x23408a, 0x6a1a5a], rnd());
    const kaftan = mat(kaftanColor);
    const skin = mat(pick(SKIN, rnd()));
    const skirt = new THREE.Mesh(G.kaftan, kaftan);
    skirt.position.y = -0.02;
    const chest = new THREE.Mesh(G.chest, kaftan);
    chest.position.y = 0.36;
    const sash = new THREE.Mesh(G.sash, mat(pick([0xffcc33, 0xf2ead8, 0xe0102f], rnd())));
    sash.rotation.x = Math.PI / 2;
    sash.position.y = 0.24;
    this.hips.add(skirt, chest, sash);

    this.head = new THREE.Group();
    this.head.position.y = 0.86;
    this.hips.add(this.head);
    this.head.add(new THREE.Mesh(G.head, skin));
    const black = mat(0x1a1410);
    for (const s of [-1, 1]) {
      const e = new THREE.Mesh(G.eye, black);
      e.position.set(0.085 * s, 0.04, 0.225);
      const m = new THREE.Mesh(G.must, black); // bıyık
      m.rotation.z = Math.PI / 2 + s * 0.35;
      m.position.set(0.07 * s, -0.07, 0.235);
      this.head.add(e, m);
    }
    if (this.kind === 'yeniceri') {
      const white = mat(0xf4efe2);
      const bork = new THREE.Mesh(G.borkBody, white);
      bork.position.set(0, 0.3, -0.02);
      bork.rotation.x = -0.25;
      const flap = new THREE.Mesh(G.borkFlap, white);
      flap.position.set(0, 0.52, -0.12);
      flap.rotation.x = 0.35;
      const gold = new THREE.Mesh(G.borkGold, GOLD);
      gold.position.set(0, 0.26, 0.19);
      this.head.add(bork, flap, gold);
    } else if (this.kind === 'sipahi') {
      const t = new THREE.Mesh(G.turban, mat(0xf4efe2));
      t.scale.set(1, 0.62, 1);
      t.position.y = 0.17;
      const top = new THREE.Mesh(G.turbanTop, mat(0xc0262e));
      top.position.y = 0.37;
      this.head.add(t, top);
    } else {
      const cap = new THREE.Mesh(G.cap, mat(0xb3202a));
      cap.position.y = 0.24;
      this.head.add(cap);
    }

    const mkArm = (x) => {
      const a = new THREE.Group();
      a.position.set(x, 0.5, 0);
      a.add(new THREE.Mesh(G.arm, kaftan));
      const h = new THREE.Mesh(G.hand, skin);
      h.position.y = -0.36;
      a.add(h);
      this.hips.add(a);
      return a;
    };
    this.armL = mkArm(-0.25);
    this.armR = mkArm(0.25);
    const boots = mat(pick([0xb3202a, 0xd9a520, 0x3a2414], rnd()));
    const mkLeg = (x) => {
      const l = new THREE.Group();
      l.position.set(x, 0.4, 0);
      l.add(new THREE.Mesh(G.leg, boots));
      this.root.add(l);
      return l;
    };
    this.legL = mkLeg(-0.1);
    this.legR = mkLeg(0.1);

    // silah (sağ elde)
    this.weapon = new THREE.Group();
    this.weapon.position.y = -0.36;
    this.armR.add(this.weapon);
    if (this.kind === 'sipahi') {
      const sp = new THREE.Mesh(G.spear, WOOD);
      const tip = new THREE.Mesh(G.spearTip, STEEL);
      tip.position.y = 1.7;
      this.weapon.add(sp, tip);
      this.weapon.rotation.x = Math.PI / 2;
    } else if (this.kind === 'okcu') {
      const bow = new THREE.Mesh(G.bow, WOOD);
      bow.rotation.z = Math.PI / 2 + Math.PI * 0.05;
      this.armL.add(bow);
      bow.position.y = -0.36;
      this.bow = bow;
      const blade = new THREE.Mesh(G.blade, STEEL);
      blade.scale.set(1, 0.6, 1);
      this.weapon.add(blade);
      this.weapon.rotation.x = Math.PI / 2;
    } else {
      const blade = new THREE.Mesh(G.blade, STEEL);
      blade.rotation.z = -0.12; // pala kavisi hissi
      const hilt = new THREE.Mesh(G.hilt, GOLD);
      this.weapon.add(blade, hilt);
      this.weapon.rotation.x = Math.PI / 2;
    }
    this.torch = new THREE.Group();
    const tStick = new THREE.Mesh(G.torch, WOOD);
    const tFlame = new THREE.Mesh(G.flame, FLAME);
    tFlame.position.y = 0.24;
    this.torch.add(tStick, tFlame);
    this.torch.position.y = -0.38;
    this.torch.visible = false;
    this.armL.add(this.torch);

    // etiket
    this.labelEl = document.createElement('div');
    this.labelEl.className = 'tag';
    this.bubbleEl = document.createElement('div');
    this.bubbleEl.className = 'bubble';
    this.nameEl = document.createElement('div');
    this.nameEl.className = 'name';
    this.nameEl.textContent = name;
    this.labelEl.append(this.bubbleEl, this.nameEl);
    this.label = new CSS2DObject(this.labelEl);
    this.label.position.y = 1.75;
    this.root.add(this.label);

    this.t = rnd() * 10;
    this.facing = Math.PI;
    this.targetFacing = 0;
    this.home = { x: 0, z: 0 };
    this.queue = [];
    this.step = null;
    this.pose = 'stand';
    this.moving = false;
    this.ladder = null;
    this.y = 0;
  }

  get busy() { return !!this.step || this.queue.length > 0; }

  setLabelClass(cls) { this.nameEl.className = 'name ' + (cls || ''); }

  say(text, seconds = 5) {
    this.bubbleEl.textContent = text.length > 40 ? text.slice(0, 38) + '…' : text;
    this.bubbleEl.classList.add('show');
    clearTimeout(this.bubbleTimer);
    this.bubbleTimer = setTimeout(() => this.bubbleEl.classList.remove('show'), seconds * 1000);
  }

  pop(emoji) {
    const el = document.createElement('div');
    el.className = 'pop';
    el.textContent = emoji;
    this.labelEl.appendChild(el);
    setTimeout(() => el.remove(), 1600);
  }

  /** İş sırası: [{go:{x,z}, run}, {wait, pose, face}, {climb: h}, {drop:true}, {call: fn}] */
  run(steps, replace = true) {
    if (replace) {
      this.queue = [];
      this.step = null;
      this.dropLadder();
    }
    this.queue.push(...steps);
  }

  goHome() {
    this.run([{ go: this.home }, { call: () => { this.targetFacing = 0; } }], false);
  }

  carryLadder() {
    if (this.ladder) return;
    this.ladder = makeLadder();
    this.ladder.rotation.set(0, 0, Math.PI / 2);
    this.ladder.position.set(-2.2, 0.55, 0);
    this.armR.add(this.ladder);
  }
  dropLadder() {
    if (this.ladder) {
      this.ladder.removeFromParent();
      this.ladder = null;
    }
  }

  dispose() {
    clearTimeout(this.bubbleTimer);
    this.labelEl.remove();
    this.root.removeFromParent();
    this.dropLadder();
  }

  next() {
    this.step = this.queue.shift() || null;
    if (this.step) this.step.t = 0;
  }

  update(dt, beat) {
    this.t += dt;
    if (!this.step && this.queue.length) this.next();
    const s = this.step;
    const p = this.root.position;
    this.moving = false;
    this.pose = 'stand';
    if (s) {
      s.t += dt;
      if (s.go) {
        const dx = s.go.x - p.x;
        const dz = s.go.z - p.z;
        const d = Math.hypot(dx, dz);
        const v = (s.run ? 6 : 3.2) * dt;
        if (d <= v) {
          p.x = s.go.x;
          p.z = s.go.z;
          this.next();
        } else {
          p.x += (dx / d) * v;
          p.z += (dz / d) * v;
          this.targetFacing = Math.atan2(dx, dz);
          this.moving = s.run ? 'run' : 'walk';
        }
        this.pose = this.ladder ? 'carry' : s.run ? 'charge' : 'walk';
      } else if (s.climb !== undefined) {
        this.pose = 'climb';
        this.targetFacing = Math.PI;
        this.y = Math.min(s.climb, this.y + dt * 2.6);
        if (this.y >= s.climb) this.next();
      } else if (s.drop) {
        this.pose = 'fall';
        s.vy = (s.vy ?? 2) - 16 * dt;
        this.y += s.vy * dt;
        p.z += 1.8 * dt;
        if (this.y <= 0) {
          this.y = 0;
          this.next();
        }
      } else if (s.call) {
        s.call(this);
        this.next();
      } else if (s.wait !== undefined) {
        this.pose = s.pose || 'stand';
        if (s.face !== undefined) this.targetFacing = s.face;
        if (s.t >= s.wait) this.next();
      } else {
        this.next();
      }
    } else if (this.cheering) {
      this.pose = 'cheer';
    } else {
      this.pose = 'march';
    }
    p.y = this.y;
    this.animate(dt, beat);

    let diff = this.targetFacing - this.facing;
    diff = Math.atan2(Math.sin(diff), Math.cos(diff));
    this.facing += diff * Math.min(1, dt * 10);
    this.root.rotation.y = this.facing;
  }

  animate(dt, beat) {
    const { armL, armR, legL, legR, hips, head, weapon } = this;
    const ph = beat.phase * Math.PI; // iki vuruşta bir adım
    hips.rotation.set(0, 0, 0);
    hips.position.y = 0.4;
    head.rotation.set(0, 0, 0);
    legL.rotation.set(0, 0, 0);
    legR.rotation.set(0, 0, 0);
    armL.rotation.set(0, 0, 0.12);
    armR.rotation.set(0, 0, -0.12);
    this.torch.visible = false;
    const t = this.t;
    switch (this.pose) {
      case 'march': {
        // mehter adımı: vuruşta bir diz kalkar, gövde sallanır
        const k = Math.sin(ph);
        legL.rotation.x = -Math.max(0, k) * 0.9;
        legR.rotation.x = -Math.max(0, -k) * 0.9;
        hips.position.y = 0.4 + Math.abs(k) * 0.04;
        hips.rotation.z = k * 0.06;
        armL.rotation.x = k * 0.4;
        armR.rotation.x = -0.5 - beat.pulse * 0.3;
        head.rotation.z = -k * 0.08;
        break;
      }
      case 'walk':
      case 'charge':
      case 'carry': {
        const f = this.pose === 'walk' ? 10 : 16;
        const sw = Math.sin(t * f);
        legL.rotation.x = sw * 0.75;
        legR.rotation.x = -sw * 0.75;
        hips.position.y = 0.4 + Math.abs(Math.cos(t * f)) * 0.06;
        if (this.pose === 'charge') {
          hips.rotation.x = 0.25;
          armR.rotation.set(-2.6, 0, -0.2); // kılıç havada
          armL.rotation.x = sw * 0.8;
        } else if (this.pose === 'carry') {
          armR.rotation.set(-Math.PI, 0, 0);
          armL.rotation.set(-Math.PI, 0, 0);
        } else {
          armL.rotation.x = -sw * 0.5;
          armR.rotation.x = sw * 0.5;
        }
        break;
      }
      case 'climb': {
        const c = Math.sin(t * 12);
        armL.rotation.x = -2.6 + c * 0.4;
        armR.rotation.x = -2.6 - c * 0.4;
        legL.rotation.x = -0.6 - c * 0.5;
        legR.rotation.x = -0.6 + c * 0.5;
        break;
      }
      case 'swing': {
        const c = (t * 5) % 1;
        armR.rotation.set(-2.8 + c * 2.6, 0, -0.3);
        hips.rotation.y = -0.3 + c * 0.6;
        legL.rotation.x = -0.3;
        legR.rotation.x = 0.3;
        break;
      }
      case 'aim': {
        armL.rotation.set(-Math.PI / 2, 0, 0.1);
        armR.rotation.set(-Math.PI / 2, 0, -0.5);
        head.rotation.y = 0.1;
        legL.rotation.x = -0.25;
        legR.rotation.x = 0.25;
        break;
      }
      case 'torch': {
        this.torch.visible = true;
        armL.rotation.set(-1.4, 0, 0.3);
        hips.rotation.x = 0.2;
        break;
      }
      case 'fall': {
        armL.rotation.set(-2.6, 0, 0.6 + Math.sin(t * 20) * 0.3);
        armR.rotation.set(-2.6, 0, -0.6 - Math.sin(t * 20) * 0.3);
        legL.rotation.x = -0.5;
        break;
      }
      case 'cheer': {
        const k = Math.max(0, Math.sin(beat.phase * Math.PI * 2));
        hips.position.y = 0.4 + k * 0.25;
        armL.rotation.set(-2.7 + Math.sin(t * 6) * 0.2, 0, 0.3);
        armR.rotation.set(-2.9, 0, -0.3);
        legL.rotation.x = legR.rotation.x = -k * 0.3;
        head.rotation.x = -0.25;
        break;
      }
      default:
        hips.position.y = 0.4 + Math.sin(t * 2) * 0.01;
    }
    if (this.bow) this.bow.visible = this.pose !== 'carry';
    weapon.visible = this.pose !== 'carry' && this.pose !== 'climb';
  }
}

Lisans bilgisi projenin LICENSE dosyasında.