// Dünya: şafak gökyüzü, toprak, Haliç denizi, surların arkasındaki şehir, ordugâh, toplar ve sancaklar.
import * as THREE from 'three';
export const WALL_Z = -10;
export const CANNON_Z = -3.6;
export const CANNONS_X = [-4.6, 0, 4.6];
const std = (color, o = {}) => new THREE.MeshStandardMaterial({ color, roughness: 0.85, ...o });
/** Al sancak: kırmızı zemin, beyaz hilal ve yıldız */
export function flagTexture(w = 512, h = 340) {
const c = document.createElement('canvas');
c.width = w;
c.height = h;
const g = c.getContext('2d');
g.fillStyle = '#d0101e';
g.fillRect(0, 0, w, h);
const cx = w * 0.42;
const cy = h / 2;
const r = h * 0.27;
g.fillStyle = '#fff';
g.beginPath();
g.arc(cx, cy, r, 0, Math.PI * 2);
g.fill();
g.fillStyle = '#d0101e';
g.beginPath();
g.arc(cx + r * 0.25, cy, r * 0.8, 0, Math.PI * 2);
g.fill();
// yıldız
const sx = cx + r * 0.72;
const sr = r * 0.42;
g.fillStyle = '#fff';
g.beginPath();
for (let i = 0; i < 10; i++) {
const a = Math.PI + (i * Math.PI) / 5; // bir ucu hilale bakar
const rr = i % 2 ? sr * 0.4 : sr;
g.lineTo(sx + Math.cos(a) * rr, cy + Math.sin(a) * rr);
}
g.closePath();
g.fill();
const tex = new THREE.CanvasTexture(c);
tex.colorSpace = THREE.SRGBColorSpace;
tex.anisotropy = 4;
return tex;
}
/** Dalgalanan bayrak (direk dahil). update(t) ile dalgalanır. */
export function makeFlag({ w = 1.6, h = 1.06, pole = 3.6, tex = flagTexture() } = {}) {
const g = new THREE.Group();
const p = new THREE.Mesh(new THREE.CylinderGeometry(0.045, 0.06, pole, 8), std(0x5a3a1e));
p.position.y = pole / 2;
const ball = new THREE.Mesh(new THREE.SphereGeometry(0.1, 10, 8), std(0xffcc33, { metalness: 0.8, roughness: 0.3 }));
ball.position.y = pole + 0.06;
const geo = new THREE.PlaneGeometry(w, h, 16, 4);
geo.translate(w / 2, 0, 0);
const base = geo.attributes.position.array.slice();
const cloth = new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ map: tex, side: THREE.DoubleSide, roughness: 0.8 }));
cloth.position.set(0.04, pole - h / 2 - 0.08, 0);
g.add(p, ball, cloth);
const seed = Math.random() * 10;
g.userData.update = (t) => {
const a = geo.attributes.position.array;
for (let i = 0; i < a.length; i += 3) {
const x = base[i];
const k = x / w;
a[i + 2] = Math.sin(x * 3.2 - t * 6 + seed) * 0.16 * k + Math.sin(base[i + 1] * 2 + t * 3) * 0.04 * k;
}
geo.attributes.position.needsUpdate = true;
geo.computeVertexNormals();
};
return g;
}
function sky(scene) {
const geo = new THREE.SphereGeometry(150, 32, 16);
const mat = new THREE.ShaderMaterial({
side: THREE.BackSide,
depthWrite: false,
uniforms: {},
vertexShader: 'varying vec3 vP; void main(){ vP = normalize(position); gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }',
fragmentShader: `varying vec3 vP;
void main(){
float h = vP.y;
vec3 top = vec3(0.08,0.10,0.28);
vec3 mid = vec3(0.55,0.32,0.42);
vec3 hor = vec3(1.0,0.62,0.30);
vec3 c = h > 0.12 ? mix(mid, top, smoothstep(0.12, 0.6, h)) : mix(hor, mid, smoothstep(-0.02, 0.12, h));
// doğan güneşin parıltısı (arka sağda)
float sun = max(0.0, dot(vP, normalize(vec3(0.35, 0.08, -1.0))));
c += vec3(1.0,0.75,0.4) * pow(sun, 60.0) * 1.2 + vec3(1.0,0.5,0.2) * pow(sun, 6.0) * 0.25;
gl_FragColor = vec4(c, 1.0);
}`,
});
scene.add(new THREE.Mesh(geo, mat));
}
function city(scene) {
const g = new THREE.Group();
const walls = [0xe8d9c0, 0xd9c2a0, 0xcfb08a, 0xe6cfa8, 0xbfa58a];
const roofs = [0xa8442a, 0x8f3a24, 0xb85a32];
const rnd = (a, b) => a + Math.random() * (b - a);
for (let i = 0; i < 70; i++) {
const x = rnd(-22, 22);
const z = rnd(-15, -34);
if (Math.abs(x - 2) < 5 && z < -19 && z > -29) continue; // büyük kubbenin yeri
const w = rnd(1.2, 2.6);
const d = rnd(1.2, 2.4);
const h = rnd(1.2, 3.4) + (z < -22 ? 1.5 : 0);
const house = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), std(walls[i % walls.length]));
house.position.set(x, h / 2, z);
const roof = new THREE.Mesh(new THREE.ConeGeometry(Math.max(w, d) * 0.75, 0.9, 4), std(roofs[i % roofs.length]));
roof.position.set(x, h + 0.45, z);
roof.rotation.y = Math.PI / 4;
g.add(house, roof);
}
// küçük kubbeli yapılar
for (const [x, z, r] of [[-12, -22, 1.6], [13, -20, 1.4], [-7, -30, 1.8], [16, -30, 2]]) {
const b = new THREE.Mesh(new THREE.CylinderGeometry(r, r, r * 1.6, 12), std(0xd8c4a4));
b.position.set(x, r * 0.8, z);
const dome = new THREE.Mesh(new THREE.SphereGeometry(r, 16, 8, 0, Math.PI * 2, 0, Math.PI / 2), std(0x8c9aa8, { roughness: 0.5, metalness: 0.3 }));
dome.position.set(x, r * 1.6, z);
g.add(b, dome);
}
// büyük kubbeli bazilika
const bx = 2;
const bz = -24;
const body = new THREE.Mesh(new THREE.BoxGeometry(9, 5, 8), std(0xd9a98a));
body.position.set(bx, 2.5, bz);
const drum = new THREE.Mesh(new THREE.CylinderGeometry(3.3, 3.3, 1.2, 24), std(0xd9a98a));
drum.position.set(bx, 5.6, bz);
const big = new THREE.Mesh(new THREE.SphereGeometry(3.3, 24, 12, 0, Math.PI * 2, 0, Math.PI / 2), std(0x9aa7b2, { roughness: 0.45, metalness: 0.35 }));
big.position.set(bx, 6.2, bz);
g.add(body, drum, big);
for (const s of [-1, 1]) {
const semi = new THREE.Mesh(new THREE.SphereGeometry(2.4, 16, 8, 0, Math.PI, 0, Math.PI / 2), std(0x9aa7b2, { roughness: 0.45, metalness: 0.35 }));
semi.position.set(bx, 5, bz + s * 3.6);
semi.rotation.y = s > 0 ? 0 : Math.PI;
g.add(semi);
}
// serviler
const cyp = std(0x1f3b26);
for (let i = 0; i < 26; i++) {
const t = new THREE.Mesh(new THREE.ConeGeometry(0.45, rnd(3, 5), 7), cyp);
t.position.set(rnd(-20, 20), 2, rnd(-13.5, -32));
g.add(t);
}
scene.add(g);
}
function camp(scene, flags) {
// çadırlar (yanlarda, orduyu kapatmasın)
const tentMat = [std(0xf2ead8), std(0xc8202c)];
for (const [x, z, s] of [[-8.6, 3, 1.2], [8.6, 2.5, 1.1], [-8, 7.5, 1], [8.2, 7.8, 1], [-11, -1, 1.3], [11, -1.5, 1.3]]) {
const tent = new THREE.Group();
const cone = new THREE.Mesh(new THREE.ConeGeometry(1.4 * s, 2.2 * s, 10), tentMat[0]);
cone.position.y = 1.1 * s;
const band = new THREE.Mesh(new THREE.CylinderGeometry(1.05 * s, 1.18 * s, 0.35 * s, 10), tentMat[1]);
band.position.y = 0.55 * s;
tent.add(cone, band);
tent.position.set(x, 0, z);
scene.add(tent);
}
// sancaklar
for (const [x, z, h] of [[-6.6, -2.4, 3.6], [6.6, -2.4, 3.6], [-7.2, 5, 3], [7.2, 5, 3]]) {
const f = makeFlag({ pole: h, w: 1.3, h: 0.86 });
f.position.set(x, 0, z);
f.rotation.y = 0.35 * Math.sign(x);
scene.add(f);
flags.push(f);
}
}
/** Şahi topu: tekerlekli kızak üstünde tunç namlu. fire() geri tepme yapar, namlu ağzını döner. */
function makeCannon(x) {
const g = new THREE.Group();
const wood = std(0x5b3a1f);
const bronze = std(0xb08a3e, { metalness: 0.75, roughness: 0.35 });
const bed = new THREE.Mesh(new THREE.BoxGeometry(1.1, 0.35, 2.6), wood);
bed.position.y = 0.45;
g.add(bed);
for (const sx of [-1, 1]) for (const sz of [-0.8, 0.8]) {
const wheel = new THREE.Mesh(new THREE.CylinderGeometry(0.42, 0.42, 0.16, 14), wood);
wheel.rotation.z = Math.PI / 2;
wheel.position.set(sx * 0.62, 0.42, sz);
g.add(wheel);
}
const barrel = new THREE.Group();
barrel.position.set(0, 0.95, 0.3);
barrel.rotation.x = 0.16; // namlu hafif yukarı
const tube = new THREE.Mesh(new THREE.CylinderGeometry(0.34, 0.46, 3.2, 16), bronze);
tube.rotation.x = Math.PI / 2;
tube.position.z = -1.1;
const ring1 = new THREE.Mesh(new THREE.TorusGeometry(0.4, 0.06, 6, 16), bronze);
ring1.position.z = -2.65;
const ring2 = ring1.clone();
ring2.position.z = 0.2;
const back = new THREE.Mesh(new THREE.SphereGeometry(0.46, 14, 10), bronze);
back.position.z = 0.45;
barrel.add(tube, ring1, ring2, back);
g.add(barrel);
const flash = new THREE.Mesh(new THREE.SphereGeometry(0.6, 12, 8), new THREE.MeshBasicMaterial({ color: 0xffb040, transparent: true, opacity: 0, toneMapped: false }));
flash.position.set(0, 0, -2.9);
barrel.add(flash);
const light = new THREE.PointLight(0xff8a2a, 0, 12, 1.5);
light.position.set(0, 1.4, -2.6);
g.add(light);
g.position.set(x, 0, CANNON_Z);
let recoil = 0;
let cool = 0;
const muzzle = new THREE.Vector3();
return {
group: g,
x,
busy: false,
get ready() { return cool <= 0; },
gunnerSpot: { x: x + 0.95, z: CANNON_Z + 1.6 },
fire() {
recoil = 1;
cool = 1.3;
flash.getWorldPosition(muzzle);
return muzzle.clone();
},
update(dt) {
cool -= dt;
recoil = Math.max(0, recoil - dt * 2.2);
barrel.position.z = 0.3 + recoil * recoil * 0.7;
flash.material.opacity = recoil > 0.75 ? (recoil - 0.75) * 4 : 0;
flash.scale.setScalar(1 + (1 - recoil) * 2);
light.intensity = recoil > 0.6 ? recoil * 40 : 0;
},
};
}
/** Kızakla karadan yürütülen gemi (Haliç'e indirilir). */
export function makeShip() {
const g = new THREE.Group();
const wood = std(0x6b4423);
const hull = new THREE.Mesh(new THREE.BoxGeometry(4.4, 1, 1.4), wood);
hull.position.y = 0.9;
const bow = new THREE.Mesh(new THREE.ConeGeometry(0.7, 1.4, 4), wood);
bow.rotation.z = Math.PI / 2;
bow.rotation.x = Math.PI / 4;
bow.position.set(-2.8, 1.0, 0);
const mast = new THREE.Mesh(new THREE.CylinderGeometry(0.07, 0.09, 4, 8), wood);
mast.position.y = 3;
// al yelken: üstünde hilal ve yıldız, uzaktan da görünsün
const sailGeo = new THREE.PlaneGeometry(3.0, 2.4, 8, 4);
const sailBase = sailGeo.attributes.position.array.slice();
const sail = new THREE.Mesh(sailGeo, new THREE.MeshStandardMaterial({ map: flagTexture(), side: THREE.DoubleSide, roughness: 0.9 }));
sail.position.set(0, 3.1, 0.05);
const fl = makeFlag({ pole: 0.01, w: 0.9, h: 0.6 });
fl.position.set(0, 4.95, 0);
g.add(hull, bow, mast, sail, fl);
for (let i = -2; i <= 2; i++) {
const log = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.16, 1.8, 8), std(0x7a5a35));
log.rotation.x = Math.PI / 2;
log.position.set(i * 1.1, 0.16, 0);
g.add(log);
}
// kürekler
const oarGeo = new THREE.BoxGeometry(0.08, 0.08, 1.6);
for (let i = -3; i <= 3; i++) {
const oar = new THREE.Mesh(oarGeo, wood);
oar.position.set(i * 0.55, 0.7, 1.1);
oar.rotation.x = -0.6;
g.add(oar);
}
g.userData.update = (t) => {
fl.userData.update(t);
const a = sailGeo.attributes.position.array;
for (let i = 0; i < a.length; i += 3) a[i + 2] = sailBase[i + 2] + Math.sin(t * 2 + sailBase[i] * 1.5) * 0.12 + (1 - Math.abs(sailBase[i + 1]) / 1.2) * 0.25;
sailGeo.attributes.position.needsUpdate = true;
};
return g;
}
export function buildWorld(scene) {
scene.background = new THREE.Color(0x2a1f3a);
scene.fog = new THREE.Fog(0xd08a62, 40, 110);
sky(scene);
// toprak
const ground = new THREE.Mesh(new THREE.PlaneGeometry(140, 120), std(0x7d6a45));
ground.rotation.x = -Math.PI / 2;
ground.position.z = -10;
scene.add(ground);
// ordugâh çimeni
const grass = new THREE.Mesh(new THREE.CircleGeometry(14, 32), std(0x5f7a3a));
grass.rotation.x = -Math.PI / 2;
grass.scale.set(1, 0.7, 1);
grass.position.set(0, 0.01, 6);
scene.add(grass);
// hendek (surların önü)
const moat = new THREE.Mesh(new THREE.PlaneGeometry(30, 1.3), std(0x3d4f5a, { roughness: 0.2, metalness: 0.4 }));
moat.rotation.x = -Math.PI / 2;
moat.position.set(0, 0.02, WALL_Z + 2.6);
scene.add(moat);
// Haliç (solda deniz)
const sea = new THREE.Mesh(new THREE.PlaneGeometry(60, 70), new THREE.MeshStandardMaterial({ color: 0x2b5d7a, roughness: 0.15, metalness: 0.5 }));
sea.rotation.x = -Math.PI / 2;
sea.position.set(-46, 0.03, -18);
scene.add(sea);
// ışıklar: şafak
scene.add(new THREE.HemisphereLight(0xffd9b0, 0x4a3a2a, 1.4));
const sun = new THREE.DirectionalLight(0xffc08a, 2.2);
sun.position.set(-10, 18, 14);
scene.add(sun);
const back = new THREE.DirectionalLight(0xff9a5a, 1.0);
back.position.set(8, 6, -30);
scene.add(back);
city(scene);
const flags = [];
camp(scene, flags);
const cannons = CANNONS_X.map((x) => {
const c = makeCannon(x);
scene.add(c.group);
return c;
});
return {
cannons,
flags,
update(dt, t) {
for (const c of cannons) c.update(dt);
for (const f of flags) f.userData.update(t);
},
};
}
Lisans bilgisi projenin LICENSE dosyasında.