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!
MINECRAFT - Processing
Lista scripturi
Script Nou
Ajutor
ID
Autor
Duplicat din
Ultima modificare
#7233
7B-Necsulescu Stefan Matei (Stefan_Matei_Necsulescu)
-
Duminica, 17 mai 2026, 11:01
// ========================= // SETTINGS & GLOBALS // ========================= let world = new Map(); let chunks = new Set(); let particles = []; let clouds = []; const chunkSize = 8; const renderDistance = 2; // În chunks const blockSize = 40; let eyeHeight = 50; const normalEyeHeight = 50; const crouchEyeHeight = 30; const AIR = 0, GRASS = 1, STONE = 2, WOOD = 3, LEAVES = 4, BRICK = 5, CHEST = 6, GLASS = 7; const breakTimes = { [GRASS]: 15, [STONE]: 40, [WOOD]: 30, [LEAVES]: 5, [BRICK]: 50, [CHEST]: 20, [GLASS]: 10 }; let breakingBlock = null; let breakProgress = 0; // ========================= // PLAYER // ========================= let pX = 0, pY = -300, pZ = 0; let rotX = 0, rotY = 0; let velocityY = 0; let isGrounded = false, wasGrounded = false; let isCrouching = false; let playerRadius = 12; let health = 100, maxHealth = 100; let fallStartY = 0; // ========================= // GAME & INVENTORY // ========================= let lootMessage = ""; let lootTimer = 0; let timeOfDay = 0; let showInventory = true; let selectedBlock = STONE; const hotbar = [GRASS, STONE, WOOD, LEAVES, BRICK, GLASS]; let inventory = { [GRASS]: 10, [STONE]: 0, [WOOD]: 0, [LEAVES]: 0, [BRICK]: 0, [GLASS]: 5 }; // ========================= // SETUP // ========================= function setup() { createCanvas(windowWidth, windowHeight, WEBGL); noiseSeed(99); document.body.style.overflow = "hidden"; // Fără scrollbar document.oncontextmenu = () => false; // Fără meniu click-dreapta // Generare nori inițiali for(let i=0; i<20; i++) { clouds.push({ x: random(-2000, 2000), y: random(-800, -500), z: random(-2000, 2000), w: random(100, 300) }); } } // ========================= // MAIN LOOP // ========================= function draw() { // Ciclu Zi/Noapte timeOfDay = (millis() / 40000) * TWO_PI; let skyBrightness = map(sin(timeOfDay), -1, 1, 20, 255); let ambient = map(sin(timeOfDay), -1, 1, 60, 200); background(skyBrightness * 0.5, skyBrightness * 0.8, skyBrightness); ambientLight(ambient); directionalLight(255, 255, 255, sin(timeOfDay), -1, cos(timeOfDay)); pointLight(255, 200, 150, pX, pY, pZ); // Lumina jucătorului (Lanterna) handleInput(); handlePhysics(); handleBlockBreaking(); updateCamera(); renderWorld(); drawClouds(); updateParticles(); drawCursor(); drawUI(); } // ========================= // CAMERA & RAYCAST // ========================= function updateCamera() { // Tranziție fină pentru crouch eyeHeight = lerp(eyeHeight, isCrouching ? crouchEyeHeight : normalEyeHeight, 0.2); let camY = pY - eyeHeight; let lookX = sin(rotY) * cos(rotX); let lookY = sin(rotX); let lookZ = -cos(rotY) * cos(rotX); camera(pX, camY, pZ, pX + lookX, camY + lookY, pZ + lookZ, 0, 1, 0); perspective(PI/3, width/height, 0.1, 8000); } function raycast() { let maxDist = 200; let camY = pY - eyeHeight; let dirX = sin(rotY) * cos(rotX); let dirY = sin(rotX); let dirZ = -cos(rotY) * cos(rotX); let lastX = floor(pX / blockSize); let lastY = floor(-camY / blockSize); let lastZ = floor(pZ / blockSize); for (let d = 0; d < maxDist; d += 0.5) { let rx = pX + dirX * d; let ry = camY + dirY * d; let rz = pZ + dirZ * d; let bx = floor(rx / blockSize); let by = floor(-ry / blockSize); let bz = floor(rz / blockSize); if (getBlock(bx, by, bz) !== AIR) { return { hit: { x: bx, y: by, z: bz }, prev: { x: lastX, y: lastY, z: lastZ } }; } lastX = bx; lastY = by; lastZ = bz; } return null; } // ========================= // STORAGE & WORLD GEN // ========================= function keyXYZ(x, y, z) { return `${x},${y},${z}`; } function getBlock(x, y, z) { return world.get(keyXYZ(x, y, z)) || 0; } function setBlock(x, y, z, type) { if (type === 0) world.delete(keyXYZ(x, y, z)); else world.set(keyXYZ(x, y, z), type); } function generateChunk(cx, cz) { const chunkID = `${cx},${cz}`; if (chunks.has(chunkID)) return; chunks.add(chunkID); for (let x = 0; x < chunkSize; x++) { for (let z = 0; z < chunkSize; z++) { let wx = cx * chunkSize + x; let wz = cz * chunkSize + z; let h = floor(noise(wx * 0.05, wz * 0.05) * 8) + 4; for (let y = 0; y < h; y++) { let isCave = noise(wx * 0.09, y * 0.09, wz * 0.09) > 0.65; if (isCave && y > 2) continue; if (y === h - 1) setBlock(wx, y, wz, GRASS); else setBlock(wx, y, wz, STONE); } // Spawning structuri let solidGround = getBlock(wx, h - 1, wz) === GRASS && getBlock(wx, h, wz) === AIR; if (solidGround) { if (random() < 0.01) spawnTree(wx, h, wz); else if (random() < 0.005) spawnHouse(wx, h, wz); } } } } function spawnTree(x, y, z) { let h = floor(random(3, 6)); for (let i = 0; i < h; i++) setBlock(x, y + i, z, WOOD); for (let lx = -2; lx <= 2; lx++) { for (let ly = -2; ly <= 2; ly++) { for (let lz = -2; lz <= 2; lz++) { if (abs(lx) + abs(ly) + abs(lz) < 4) setBlock(x + lx, y + h - 1 + ly, z + lz, LEAVES); } } } } function spawnHouse(x, y, z) { for (let xx = 0; xx < 5; xx++) { for (let zz = 0; zz < 5; zz++) { setBlock(x + xx, y, z + zz, BRICK); // Podea for (let yy = 1; yy < 4; yy++) { let edge = xx === 0 || xx === 4 || zz === 0 || zz === 4; if (edge) setBlock(x + xx, y + yy, z + zz, WOOD); // Pereți } setBlock(x + xx, y + 4, z + zz, BRICK); // Acoperiș } } setBlock(x + 2, y + 1, z, AIR); // Ușa setBlock(x + 2, y + 2, z, AIR); setBlock(x + 1, y + 2, z + 4, GLASS); // Fereastra setBlock(x + 3, y + 1, z + 3, CHEST); // Loot } // ========================= // RENDERING // ========================= function renderWorld() { let pcx = floor(pX / blockSize / chunkSize); let pcz = floor(pZ / blockSize / chunkSize); for (let cx = pcx - renderDistance; cx <= pcx + renderDistance; cx++) { for (let cz = pcz - renderDistance; cz <= pcz + renderDistance; cz++) { generateChunk(cx, cz); } } // Setare jucător la suprafață la primul cadru if (frameCount === 2) { for (let i = 20; i < 50; i++) { if (!checkCollision(0, -i * blockSize, 0)) { pY = -i * blockSize; break; } } } let maxDistSq = pow(renderDistance * chunkSize * blockSize * 1.5, 2); for (let [k, type] of [...world]) { if (type === AIR) continue; let [x, y, z] = k.split(",").map(Number); let wx = x * blockSize; let wy = -y * blockSize; let wz = z * blockSize; // Distance Culling if (distSq(wx, wz, pX, pZ) > maxDistSq) continue; // Block Culling (Randează doar exteriorul) let visible = getBlock(x+1,y,z)===AIR || getBlock(x-1,y,z)===AIR || getBlock(x,y+1,z)===AIR || getBlock(x,y-1,z)===AIR || getBlock(x,y,z+1)===AIR || getBlock(x,y,z-1)===AIR; if (!visible) continue; push(); translate(wx, wy, wz); let currentSize = blockSize; // Efect de spargere if (breakingBlock && breakingBlock.x === x && breakingBlock.y === y && breakingBlock.z === z) { let t = breakProgress / (breakTimes[type] || 30); currentSize = lerp(blockSize, blockSize * 0.4, t); } applyBlockMaterial(type); box(currentSize); pop(); } } function applyBlockMaterial(type) { noStroke(); if (type === GRASS) fill(50, 180, 50); else if (type === STONE) fill(120); else if (type === WOOD) fill(100, 60, 30); else if (type === LEAVES) fill(30, 130, 30); else if (type === BRICK) fill(180, 60, 60); else if (type === CHEST) fill(220, 160, 30); else if (type === GLASS) { fill(150, 200, 255, 120); stroke(255, 100); } } function distSq(x1, z1, x2, z2) { return (x1 - x2) * (x1 - x2) + (z1 - z2) * (z1 - z2); } // ========================= // INPUT & PHYSICS // ========================= function handleInput() { isCrouching = keyIsDown(16); // SHIFT let speed = isCrouching ? 2.5 : 5; let dx = 0, dz = 0; if (keyIsDown(87)) { dx += sin(rotY); dz -= cos(rotY); } // W if (keyIsDown(83)) { dx -= sin(rotY); dz += cos(rotY); } // S if (keyIsDown(65)) { dx -= cos(rotY); dz -= sin(rotY); } // A if (keyIsDown(68)) { dx += cos(rotY); dz += sin(rotY); } // D let len = Math.hypot(dx, dz); if (len > 0) { dx = (dx / len) * speed; dz = (dz / len) * speed; } // Step-Assist & Collision X pX += dx; if (checkCollision(pX, pY, pZ)) { pY -= blockSize; // Încearcă să urci if (checkCollision(pX, pY, pZ)) { pY += blockSize; pX -= dx; } // Lovit perete } // Step-Assist & Collision Z pZ += dz; if (checkCollision(pX, pY, pZ)) { pY -= blockSize; // Încearcă să urci if (checkCollision(pX, pY, pZ)) { pY += blockSize; pZ -= dz; } // Lovit perete } } function handlePhysics() { if (isGrounded && velocityY === 0) fallStartY = pY; velocityY += 0.4; // Gravity velocityY = constrain(velocityY, -25, 15); let steps = ceil(abs(velocityY)); let stepAmount = velocityY / steps; wasGrounded = isGrounded; for (let i = 0; i < steps; i++) { let ny = pY + stepAmount; if (checkCollision(pX, ny, pZ)) { if (velocityY > 0) { // Atinge solul isGrounded = true; let bY = floor((-ny + blockSize/2) / blockSize); pY = (-bY * blockSize) - (blockSize/2) - 0.01; // Fall Damage let fallDist = abs(pY - fallStartY) / blockSize; if (fallDist > 4) takeDamage(floor((fallDist - 4) * 10)); } velocityY = 0; break; } else { pY = ny; if (isGrounded) fallStartY = pY; isGrounded = false; } } } function checkCollision(x, y, z) { let margin = 2; let pMinX = x - playerRadius + margin, pMaxX = x + playerRadius - margin; let pH = isCrouching ? normalEyeHeight * 0.8 : normalEyeHeight * 1.5; let pMinY = y - pH + margin, pMaxY = y - margin; let pMinZ = z - playerRadius + margin, pMaxZ = z + playerRadius - margin; let sX = floor((pMinX + blockSize/2) / blockSize), eX = floor((pMaxX + blockSize/2) / blockSize); let sZ = floor((pMinZ + blockSize/2) / blockSize), eZ = floor((pMaxZ + blockSize/2) / blockSize); let sY = floor((-pMaxY + blockSize/2) / blockSize), eY = floor((-pMinY + blockSize/2) / blockSize); for (let bx = sX; bx <= eX; bx++) { for (let by = sY; by <= eY; by++) { for (let bz = sZ; bz <= eZ; bz++) { if (getBlock(bx, by, bz) !== AIR) return true; } } } return false; } function takeDamage(amount) { health -= amount; if (health <= 0) { health = maxHealth; pX = 0; pZ = 0; pY = -1500; velocityY = 0; lootMessage = "YOU DIED"; lootTimer = 180; } } // ========================= // MINING & PARTICLES // ========================= function handleBlockBreaking() { if (mouseIsPressed && mouseButton === LEFT) { let result = raycast(); if (!result || !result.hit) { resetMining(); return; } let hit = result.hit; if (!breakingBlock || hit.x !== breakingBlock.x || hit.y !== breakingBlock.y || hit.z !== breakingBlock.z) { breakingBlock = hit; breakProgress = 0; } let type = getBlock(hit.x, hit.y, hit.z); breakProgress++; // Spawn Particles if (frameCount % 3 === 0) { particles.push(new Particle(hit.x * blockSize, -hit.y * blockSize, hit.z * blockSize, type)); } if (breakProgress >= (breakTimes[type] || 30)) { if (type === CHEST) { let loot = ["Aur", "Diamante", "Măr", "Fier"]; lootMessage = "LOOT: " + random(loot); lootTimer = 180; } else if (type !== LEAVES && type !== GLASS) { inventory[type] = (inventory[type] || 0) + 1; } setBlock(hit.x, hit.y, hit.z, 0); resetMining(); } } else resetMining(); } function resetMining() { breakingBlock = null; breakProgress = 0; } function mousePressed() { if (mouseButton === RIGHT) { let result = raycast(); if (!result || inventory[selectedBlock] <= 0) return; let { prev } = result; if (getBlock(prev.x, prev.y, prev.z) === AIR) { setBlock(prev.x, prev.y, prev.z, selectedBlock); if (checkCollision(pX, pY, pZ)) setBlock(prev.x, prev.y, prev.z, AIR); // Prevent clipping else inventory[selectedBlock]--; } } } // ========================= // PARTICLES & CLOUDS // ========================= class Particle { constructor(x, y, z, type) { this.pos = createVector(x + random(-10, 10), y + random(-10, 10), z + random(-10, 10)); this.vel = createVector(random(-2, 2), random(-5, -2), random(-2, 2)); this.life = 255; this.type = type; } update() { this.vel.y += 0.5; this.pos.add(this.vel); this.life -= 15; } display() { push(); translate(this.pos.x, this.pos.y, this.pos.z); applyBlockMaterial(this.type); box(8); pop(); } } function updateParticles() { for (let i = particles.length - 1; i >= 0; i--) { particles[i].update(); particles[i].display(); if (particles[i].life <= 0) particles.splice(i, 1); } } function drawClouds() { noStroke(); fill(255, 200); for (let c of clouds) { push(); translate(c.x, c.y, c.z); box(c.w, 40, c.w * 0.8); pop(); c.x += 0.5; if (c.x > 2000) c.x = -2000; } } // ========================= // UI & CONTROLS // ========================= function drawUI() { push(); resetMatrix(); ortho(); noLights(); let gl = canvas.getContext('webgl'); gl.disable(gl.DEPTH_TEST); // Crosshair stroke(255); strokeWeight(2); line(-10, 0, 10, 0); line(0, -10, 0, 10); // Health Bar let barX = -width / 2 + 20, barY = height / 2 - 40; noStroke(); fill(50); rect(barX, barY, 200, 20); fill(255, 50, 50); rect(barX, barY, map(health, 0, maxHealth, 0, 200), 20); fill(255); textSize(14); textAlign(LEFT, TOP); text(`HP: ${health}/${maxHealth}`, barX + 5, barY + 2); // Inventory toggle if (showInventory) { fill(0, 0, 0, 150); rect(-width/2 + 10, -height/2 + 10, 150, 180, 10); fill(255); textSize(16); text("INVENTORY (E)", -width/2 + 20, -height/2 + 20); let yOff = 50; for (let id of hotbar) { if (id === selectedBlock) fill(255, 255, 0); else fill(200); text(`[${hotbar.indexOf(id)+1}] Block ${id}: ${inventory[id] || 0}`, -width/2 + 20, -height/2 + yOff); yOff += 25; } } // Loot Message if (lootTimer > 0) { fill(0, 255, 255); textSize(24); textAlign(CENTER, CENTER); text(lootMessage, 0, -height/4); lootTimer--; } gl.enable(gl.DEPTH_TEST); pop(); } function drawCursor() { let result = raycast(); if (result && result.hit) { let { x, y, z } = result.hit; push(); translate(x * blockSize, -y * blockSize, z * blockSize); noFill(); stroke(0); strokeWeight(2); box(blockSize + 1); pop(); } } function keyPressed() { if (key === " " && isGrounded) { velocityY = -7.5; isGrounded = false; } if (key.toLowerCase() === "e") showInventory = !showInventory; let num = parseInt(key); if (num >= 1 && num <= hotbar.length) selectedBlock = hotbar[num - 1]; } function mouseMoved() { rotY += movedX * 0.0025; rotX += movedY * 0.0025; rotX = constrain(rotX, -1.5, 1.5); } function windowResized() { resizeCanvas(windowWidth, windowHeight); }
Duplicare
Executare
Cod
Cod HTML
<iframe sandbox="allow-scripts" src="/p5js/index.php?id=7233" style="width:408px; height:408px;border:solid 1px gray; overflow: scroll;"></iframe>
Duplicare script
Denumirea noului script
Du-te sus!