pbInfo.ro
Probleme
Probleme - clasa a IX-a
Probleme - clasa a X-a
Probleme - clasa a XI-a
Probleme din concursuri
Căutare problemă
Exerciții
Programează cu Blockly
Desenează cu Processing
Exersează SQL
Soluţii
Resurse
Resurse pentru clasa a IX-a
Resurse pentru clasa a X-a
Resurse pentru clasa a XI-a
Articole recomandate
Subiecte bacalaureat
Ajutor
Autentificare
Înregistrare
Autentificare
Utilizator sau email
Parola
Acest site foloseşte cookies. Navigând în continuare, vă exprimaţi acordul asupra folosirii cookie-urilor.
Îti place pbInfo? Atunci acceptă-l cu totul! Dezactivează modulul de blocare a reclamelor!
Script Nou - Processing
Lista scripturi
Script Nou
Ajutor
ID
Autor
Duplicat din
Ultima modificare
#7242
7B-Necsulescu Stefan Matei (Stefan_Matei_Necsulescu)
-
Luni, 18 mai 2026, 10:39
// ====================================================== // EXTREME OPTIMIZED MINECRAFT ENGINE - PRO ULTRA UPDATE // ====================================================== let cam; const BLOCK = 32; const CHUNK = 10; // Redus ușor pentru un echilibru perfect de performanță const RENDER = 2; const WORLD_SEED = 1337; let chunks = {}; let visibleBlocks = []; let particles = []; let mobs = []; let clouds = []; let hudGraphics; // Canvas separat 2D pentru HUD curat și randare rapidă const COLORS = { grass: [95, 159, 53], dirt: [121, 85, 58], stone: [120, 120, 125], wood: [102, 81, 51], leaf: [60, 120, 60, 200], // Transparență adăugată sand: [194, 178, 128], water: [50, 90, 220, 150], lava: [255, 90, 0], glass: [220, 240, 255, 100], flower: [255, 80, 120] }; const BLOCK_TYPES = ["grass", "dirt", "stone", "wood", "leaf", "sand", "glass", "water", "lava"]; let selectedIndex = 0; let selected = BLOCK_TYPES[selectedIndex]; let player = { pos: null, vel: null, yaw: 3.14, // Corecție orientare inițială pitch: 0, grounded: false, health: 20, maxHealth: 20, speed: 4, runSpeed: 7 }; let worldTime = 0; function setup() { createCanvas(windowWidth, windowHeight, WEBGL); hudGraphics = createGraphics(windowWidth, windowHeight); // Buffer 2D pentru interfață cam = createCamera(); noiseSeed(WORLD_SEED); randomSeed(WORLD_SEED); player.pos = createVector(0, -200, 0); player.vel = createVector(0, 0, 0); // Norii for (let i = 0; i < 20; i++) { clouds.push({ x: random(-3000, 3000), y: -600, z: random(-3000, 3000), size: random(150, 350) }); } // Mobs inteligenți for (let i = 0; i < 6; i++) { mobs.push({ x: random(-400, 400), y: -150, z: random(-400, 400), t: random(["pig", "zombie"]), seed: random(100) }); } requestPointerLock(); document.oncontextmenu = () => false; } function draw() { worldTime += 0.002; // Timpul curge mai natural updateEnvironment(); handleInput(); updatePhysics(); updateCamera(); generateWorld(); buildVisibleList(); // Randare elemente 3D renderWorld(); renderClouds(); renderMobs(); updateParticles(); drawOutline(); renderHand3D(); // Mână mișto randată corect în spațiul 3D // Randare HUD (Deasupra scenei 3D) drawHUD(); } // ====================================================== // MEDIU ȘI LUMINĂ ULTRA // ====================================================== function updateEnvironment() { let lightCycle = map(sin(worldTime), -1, 1, 0.1, 1.0); // Culori cer: tranzitie zi/noapte let skyR = lerp(10, 135, lightCycle); let skyG = lerp(15, 206, lightCycle); let skyB = lerp(45, 250, lightCycle); background(skyR, skyG, skyB); // Lumini dinamice ambientLight(40 + lightCycle * 140); directionalLight(255 * lightCycle, 240 * lightCycle, 220 * lightCycle, 0.5, 1, -0.3); } // ====================================================== // GENERARE LUME (Frustrum Culling & Chunks) // ====================================================== function generateWorld() { let pcx = floor(player.pos.x / (CHUNK * BLOCK)); let pcz = floor(player.pos.z / (CHUNK * BLOCK)); for (let cx = pcx - RENDER; cx <= pcx + RENDER; cx++) { for (let cz = pcz - RENDER; cz <= pcz + RENDER; cz++) { let key = cx + "," + cz; if (!chunks[key]) { chunks[key] = generateChunk(cx, cz); } } } } function generateChunk(cx, cz) { let arr = []; for (let x = 0; x < CHUNK; x++) { for (let z = 0; z < CHUNK; z++) { let wx = (cx * CHUNK + x) * BLOCK; let wz = (cz * CHUNK + z) * BLOCK; let biome = noise(wx * 0.0005, wz * 0.0005); let h = noise(wx * 0.003, wz * 0.003) * 0.7 + noise(wx * 0.01, wz * 0.01) * 0.3; h = floor(h * 12) * BLOCK; let top = "grass"; if (biome < 0.35) top = "sand"; if (biome > 0.75) top = "stone"; // Bloc suprafață arr.push({ x: wx, y: h, z: wz, t: top }); // Subsol for (let d = 1; d <= 4; d++) { arr.push({ x: wx, y: h + d * BLOCK, z: wz, t: d < 2 && top === "grass" ? "dirt" : "stone" }); } // Elemente decorative generate procedural if (top === "grass" && noise(wx, wz) > 0.82) { makeTree(arr, wx, h - BLOCK, wz); } else if (top === "grass" && noise(wx, wz) > 0.75) { arr.push({ x: wx, y: h - BLOCK, z: wz, t: "flower" }); } } } return arr; } function makeTree(arr, x, y, z) { let h = floor(random(4, 6)); for (let i = 0; i < h; i++) { arr.push({ x: x, y: y - i * BLOCK, z: z, t: "wood" }); } for (let lx = -1; lx <= 1; lx++) { for (let lz = -1; lz <= 1; lz++) { for (let ly = -2; ly <= 0; ly++) { if (lx !== 0 || lz !== 0 || ly < -1) { arr.push({ x: x + lx * BLOCK, y: y - h * BLOCK + ly * BLOCK, z: z + lz * BLOCK, t: "leaf" }); } } } } } // ====================================================== // RANDARE OPTIMIZATĂ ȘI CULLING (Anti-Lag) // ====================================================== function buildVisibleList() { visibleBlocks.length = 0; let px = player.pos.x; let pz = player.pos.z; let maxDistSq = 1400 * 1400; // Distanță optimă de randare (Frustrum Culling brut) for (let key in chunks) { let chunk = chunks[key]; for (let i = 0; i < chunk.length; i++) { let b = chunk[i]; let dx = px - b.x; let dz = pz - b.z; if (dx * dx + dz * dz < maxDistSq) { visibleBlocks.push(b); } } } } function renderWorld() { noStroke(); for (let b of visibleBlocks) { push(); translate(b.x, b.y, b.z); let c = COLORS[b.t]; if (c.length === 4) { fill(c[0], c[1], c[2], c[3]); // Suport pentru transparență (apă/frunze) } else { ambientMaterial(c[0], c[1], c[2]); } if (b.t === "flower") { translate(0, BLOCK/4, 0); box(8, BLOCK/2, 8); } else { box(BLOCK); } pop(); } } // ====================================================== // FIZICĂ ȘI CONTROALE REPARATE (Minecraft Style) // ====================================================== function handleInput() { let currentSpeed = keyIsDown(SHIFT) ? player.runSpeed : player.speed; let moveX = 0; let moveZ = 0; // Direcția fixată matematic conform axelor vectoriale din Minecraft if (keyIsDown(87)) { moveX += sin(player.yaw); moveZ += cos(player.yaw); } // W if (keyIsDown(83)) { moveX -= sin(player.yaw); moveZ -= cos(player.yaw); } // S if (keyIsDown(65)) { moveX += cos(player.yaw); moveZ -= sin(player.yaw); } // A if (keyIsDown(68)) { moveX -= cos(player.yaw); moveZ += sin(player.yaw); } // D if (moveX !== 0 || moveZ !== 0) { let moveVec = createVector(moveX, 0, moveZ).normalize().mult(currentSpeed); player.vel.x = lerp(player.vel.x, moveVec.x, 0.25); player.vel.z = lerp(player.vel.z, moveVec.z, 0.25); } else { player.vel.x = lerp(player.vel.x, 0, 0.3); player.vel.z = lerp(player.vel.z, 0, 0.3); } // Aplicare mișcare pe axe cu verificări individuale de coliziune player.pos.x += player.vel.x; if (collide()) player.pos.x -= player.vel.x; player.pos.z += player.vel.z; if (collide()) player.pos.z -= player.vel.z; } function updatePhysics() { player.vel.y += 0.55; // Gravitație stabilă player.pos.y += player.vel.y; if (collide()) { player.pos.y -= player.vel.y; player.vel.y = 0; player.grounded = true; } else { player.grounded = false; } // Limită de siguranță pentru cădere în gol if(player.pos.y > 1000) { player.pos.set(0, -200, 0); player.vel.set(0,0,0); } } function collide() { let px = player.pos.x; let py = player.pos.y; let pz = player.pos.z; for (let b of visibleBlocks) { if (b.t === "water" || b.t === "flower") continue; // Hitbox adaptat la dimensiunea jucătorului (2 blocks înălțime) if (abs(px - b.x) < 22 && (py - b.y) < 24 && (py - b.y) > -64 && abs(pz - b.z) < 22) { return true; } } return false; } function updateCamera() { // Efect de mișcare a capului (Bobbing) când mergi let isMoving = (abs(player.vel.x) > 0.2 || abs(player.vel.z) > 0.2) && player.grounded; let bob = isMoving ? sin(frameCount * 0.25) * 2.5 : 0; cam.setPosition(player.pos.x, player.pos.y - 45 + bob, player.pos.z); // Calculare vector direcție privire (Look Target) let lookX = player.pos.x + sin(player.yaw) * cos(player.pitch); let lookY = player.pos.y - 45 + sin(player.pitch); let lookZ = player.pos.z + cos(player.yaw) * cos(player.pitch); cam.lookAt(lookX, lookY, lookZ); } function mouseMoved() { if (document.pointerLockElement === canvas) { player.yaw -= movedX * 0.0025; player.pitch -= movedY * 0.0025; player.pitch = constrain(player.pitch, -HALF_PI + 0.05, HALF_PI - 0.05); } } // ====================================================== // INTERACȚIUNE BLOCURI (Raycasting Precis) // ====================================================== function getTarget() { let step = 6; let reach = 160; // Direcția exactă a camerei let dir = createVector(sin(player.yaw) * cos(player.pitch), sin(player.pitch), cos(player.yaw) * cos(player.pitch)); for (let d = 0; d < reach; d += step) { let checkX = player.pos.x + dir.x * d; let checkY = (player.pos.y - 45) + dir.y * d; let checkZ = player.pos.z + dir.z * d; for (let b of visibleBlocks) { if (abs(checkX - b.x) < BLOCK/2 && abs(checkY - b.y) < BLOCK/2 && abs(checkZ - b.z) < BLOCK/2) { return b; } } } return null; } function drawOutline() { let b = getTarget(); if (!b) return; push(); translate(b.x, b.y, b.z); noFill(); stroke(0, 150); // Contur negru subtil, tipic Minecraft strokeWeight(1.5); box(BLOCK + 1); pop(); } // ====================================================== // ELEMENTE AMBIENTALE ȘI ENTITĂȚI // ====================================================== function updateParticles() { for (let i = particles.length - 1; i >= 0; i--) { let p = particles[i]; p.x += p.vx; p.y += p.vy; p.z += p.vz; p.vy += 0.2; // Gravitație particule p.life -= 6; push(); translate(p.x, p.y, p.z); fill(p.c[0], p.c[1], p.c[2], p.life); noStroke(); box(random(2, 5)); pop(); if (p.life <= 0) particles.splice(i, 1); } } function renderClouds() { fill(255, 180); noStroke(); for (let c of clouds) { push(); translate(c.x, c.y, c.z); box(c.size, 15, c.size * 0.6); pop(); c.x += 0.15; if (c.x > 3000) c.x = -3000; } } function renderMobs() { for (let m of mobs) { m.x += sin(frameCount * 0.01 + m.seed) * 0.4; m.z += cos(frameCount * 0.01 + m.seed) * 0.4; push(); translate(m.x, m.y, m.z); if (m.t === "pig") fill(240, 140, 160); // Roz pentru porc/cow alternativ else fill(45, 110, 45); // Verde Zombie box(24, 36, 16); // Capul mob-ului translate(0, -22, 4); box(12, 12, 12); pop(); } } function renderHand3D() { // Mâna 3D legată rigid de ecran (Urmează camera perfect) push(); resetMatrix(); let bobHandX = (abs(player.vel.x) > 0.2) ? sin(frameCount * 0.15) * 1.5 : 0; let bobHandY = (abs(player.vel.x) > 0.2) ? abs(cos(frameCount * 0.15)) * 1.5 : 0; translate(width / 4 + 10 + bobHandX, height / 3 + 20 + bobHandY, -120); rotateX(-0.4); rotateY(-0.5); // Textură/Culoare mână skin Alex/Steve fill(215, 155, 125); box(22, 60, 22); pop(); } // ====================================================== // HUD CURAT 2D (Fără bug de distorsiune) // ====================================================== function drawHUD() { hudGraphics.clear(); // Tinta din centru (Crosshair) hudGraphics.stroke(255); hudGraphics.strokeWeight(2); hudGraphics.line(width/2 - 8, height/2, width/2 + 8, height/2); hudGraphics.line(width/2, height/2 - 8, width/2, height/2 + 8); // Bara Hotbar de jos let hbW = 400; let hbH = 45; let hbX = width/2 - hbW/2; let hbY = height - 70; hudGraphics.noStroke(); hudGraphics.fill(30, 30, 30, 200); hudGraphics.rect(hbX, hbY, hbW, hbH, 5); // Indicator selectat (Highlight slot) let slotW = hbW / BLOCK_TYPES.length; hudGraphics.stroke(255, 215, 0); hudGraphics.strokeWeight(3); hudGraphics.noFill(); hudGraphics.rect(hbX + selectedIndex * slotW, hbY, slotW, hbH, 5); // Text elemente din Hotbar hudGraphics.noStroke(); hudGraphics.fill(255); hudGraphics.textSize(11); hudGraphics.textAlign(CENTER, CENTER); for(let i = 0; i < BLOCK_TYPES.length; i++){ let txt = BLOCK_TYPES[i].substring(0,2).toUpperCase(); if(BLOCK_TYPES[i] === selected) hudGraphics.fill(255, 215, 0); else hudGraphics.fill(180); hudGraphics.text(txt, hbX + i * slotW + slotW/2, hbY + hbH/2); } // Text Stats (Stânga Sus) hudGraphics.textAlign(LEFT, TOP); hudGraphics.fill(255); hudGraphics.textSize(14); hudGraphics.text("⭐ BLOCK: " + selected.toUpperCase(), 20, 20); hudGraphics.text("❤️ HP: " + player.health + " / " + player.maxHealth, 20, 42); hudGraphics.fill(0, 255, 100); hudGraphics.text("⚡ FPS: " + floor(frameRate()), 20, 64); // Desenează tot bufferul 2D peste cel 3D image(hudGraphics, -width/2, -height/2); } // ====================================================== // EVENIMENTE REPARATE DE COORDONATE // ====================================================== function mousePressed() { if (document.pointerLockElement !== canvas) { requestPointerLock(); return; } let b = getTarget(); if (mouseButton === LEFT && b) { // Generare particule la spargere for (let i = 0; i < 12; i++) { particles.push({ x: b.x + random(-10,10), y: b.y + random(-10,10), z: b.z + random(-10,10), vx: random(-2.5, 2.5), vy: random(-4, -1), vz: random(-2.5, 2.5), c: COLORS[b.t], life: 255 }); } // Șterge blocul for (let key in chunks) { let idx = chunks[key].indexOf(b); if (idx !== -1) { chunks[key].splice(idx, 1); break; } } } if (mouseButton === RIGHT) { let dir = createVector(sin(player.yaw) * cos(player.pitch), sin(player.pitch), cos(player.yaw) * cos(player.pitch)); // Plasează blocul exact în fața ta calculat pe grilă standard let px = round((player.pos.x + dir.x * 55) / BLOCK) * BLOCK; let py = round(((player.pos.y - 45) + dir.y * 55) / BLOCK) * BLOCK; let pz = round((player.pos.z + dir.z * 55) / BLOCK) * BLOCK; let cx = floor(px / (CHUNK * BLOCK)); let cz = floor(pz / (CHUNK * BLOCK)); let key = cx + "," + cz; if (chunks[key]) { // Evită plasarea blocului în interiorul tău if (abs(player.pos.x - px) > 20 || abs((player.pos.y - 32) - py) > 32 || abs(player.pos.z - pz) > 20) { chunks[key].push({ x: px, y: py, z: pz, t: selected }); } } } } function keyPressed() { if (key === " " && player.grounded) { player.vel.y = -9.5; // Săritură fluidă, fără glitch-uri de tavan } // Selectare Hotbar prin tastele 1-9 if (key >= "1" && key <= "9") { selectedIndex = int(key) - 1; selected = BLOCK_TYPES[selectedIndex]; } } function windowResized() { resizeCanvas(windowWidth, windowHeight); hudGraphics = createGraphics(windowWidth, windowHeight); }
Duplicare
Executare
Cod
Cod HTML
<iframe sandbox="allow-scripts" src="/p5js/index.php?id=7242" style="width:408px; height:408px;border:solid 1px gray; overflow: scroll;"></iframe>
Duplicare script
Denumirea noului script
Du-te sus!