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!
M1 - Processing
Lista scripturi
Script Nou
Ajutor
ID
Autor
Duplicat din
Ultima modificare
#7241
7B-Necsulescu Stefan Matei (Stefan_Matei_Necsulescu)
-
Luni, 18 mai 2026, 10:43
let cam; const BLOCK = 32; const CHUNK = 8; const RENDER = 0.6; const WORLD_SEED = 1337; let chunks = {}; let visibleBlocks = []; let particles = []; let mobs = []; let clouds = []; let frameFog = 0; const COLORS = { grass: [95, 159, 53], dirt: [121, 85, 58], stone: [120, 120, 125], wood: [102, 81, 51], leaf: [60, 120, 60], sand: [194, 178, 128], water: [50, 90, 220, 130], lava: [255, 90, 0], glass: [220, 240, 255, 70], flower: [255, 80, 120] }; const BLOCK_TYPES = [ "grass", "dirt", "stone", "wood", "leaf", "sand", "glass", "water", "lava" ]; let selected = "grass"; let player = { pos: null, vel: null, yaw: 0, pitch: 0, grounded: false, health: 20, hunger: 20, xp: 0, level: 1 }; let worldTime = 0; function setup() { createCanvas(windowWidth, windowHeight, WEBGL); cam = createCamera(); noiseSeed(WORLD_SEED); player.pos = createVector(0, -300, 0); player.vel = createVector(); // CLOUDS for (let i = 0; i < 25; i++) { clouds.push({ x: random(-5000, 5000), y: random(-1000, -700), z: random(-5000, 5000), s: random(100, 300) }); } // SIMPLE MOBS for (let i = 0; i < 8; i++) { mobs.push({ x: random(-1000, 1000), y: -200, z: random(-1000, 1000), t: random(["cow", "zombie"]) }); } requestPointerLock(); document.oncontextmenu = () => false; } function draw() { updateTime(); updateSky(); updateLighting(); handleInput(); updatePhysics(); updateCamera(); generateWorld(); buildVisibleList(); renderWorld(); renderClouds(); renderMobs(); updateParticles(); drawOutline(); drawHUD(); } // ====================================================== // SKY // ====================================================== function updateTime() { worldTime += 0.0005; } function updateSky() { let d = map(sin(worldTime), -1, 1, 0, 1); background( lerp(10, 120, d), lerp(15, 190, d), lerp(40, 255, d) ); // SUN push(); translate( sin(worldTime) * 4000, cos(worldTime) * 2000, -4000 ); emissiveMaterial(255, 220, 120); sphere(100); pop(); // MOON push(); translate( -sin(worldTime) * 4000, -cos(worldTime) * 2000, -4000 ); emissiveMaterial(180); sphere(60); pop(); } function updateLighting() { let d = map(sin(worldTime), -1, 1, 0, 1); ambientLight(30 + d * 160); directionalLight( 255 * d, 240 * d, 220 * d, 0.4, 1, 0.2 ); } // ====================================================== // WORLD // ====================================================== 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; // BIOME let biome = noise(wx * 0.0003, wz * 0.0003); // HEIGHT let h = noise(wx * 0.002, wz * 0.002) * 0.7 + noise(wx * 0.01, wz * 0.01) * 0.3; h = floor(h * 16) * BLOCK; let top = "grass"; if (biome < 0.3) top = "sand"; if (biome > 0.7) top = "stone"; // TOP BLOCK arr.push({ x: wx, y: h, z: wz, t: top }); // UNDERGROUND for (let d = 1; d < 6; d++) { // CAVES let cave = noise(wx * 0.02, wz * 0.02, d * 0.1); if (cave > 0.6) continue; arr.push({ x: wx, y: h + d * BLOCK, z: wz, t: d < 3 ? "dirt" : "stone" }); } // TREES if (top === "grass" && random() < 0.02) { makeTree(arr, wx, h - BLOCK, wz); } // WATER if (h > 200) { arr.push({ x: wx, y: 240, z: wz, t: "water" }); } // FLOWERS if (random() < 0.01) { arr.push({ x: wx, y: h - BLOCK, z: wz, t: "flower" }); } } } return arr; } function makeTree(arr, x, y, z) { let h = floor(random(3, 6)); for (let i = 0; i < h; i++) { arr.push({ x: x, y: y - i * BLOCK, z: z, t: "wood" }); } for (let lx = -2; lx <= 2; lx++) { for (let lz = -2; lz <= 2; lz++) { for (let ly = -2; ly <= 1; ly++) { if (lx*lx + lz*lz + ly*ly < 7) { arr.push({ x: x + lx * BLOCK, y: y - h * BLOCK + ly * BLOCK, z: z + lz * BLOCK, t: "leaf" }); } } } } } // ====================================================== // FAST RENDER LIST // ====================================================== function buildVisibleList() { visibleBlocks.length = 0; let px = player.pos.x; let pz = player.pos.z; 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; // FAST DISTANCE CHECK if (dx*dx + dz*dz > 1800*1800) continue; visibleBlocks.push(b); } } } // ====================================================== // RENDER // ====================================================== function renderWorld() { noStroke(); for (let i = 0; i < visibleBlocks.length; i++) { let b = visibleBlocks[i]; drawBlockFast(b); } } function drawBlockFast(b) { push(); translate(b.x, b.y, b.z); let c = COLORS[b.t]; // SIMPLE FOG let dx = player.pos.x - b.x; let dz = player.pos.z - b.z; let d = sqrt(dx*dx + dz*dz); let a = map(d, 0, 1800, 255, 40); if ( b.t === "water" || b.t === "glass" ) { fill(c[0], c[1], c[2], a); } else { ambientMaterial( c[0], c[1], c[2], a ); } // FLOWER if (b.t === "flower") { sphere(6); } else { box(BLOCK); } pop(); } // ====================================================== // PLAYER // ====================================================== function handleInput() { let speed = keyIsDown(SHIFT) ? 8 : 5; let mx = 0; let mz = 0; if (keyIsDown(87)) { mx += sin(player.yaw); mz += cos(player.yaw); } if (keyIsDown(83)) { mx -= sin(player.yaw); mz -= cos(player.yaw); } if (keyIsDown(65)) { mx += cos(player.yaw); mz -= sin(player.yaw); } if (keyIsDown(68)) { mx -= cos(player.yaw); mz += sin(player.yaw); } player.vel.x = lerp( player.vel.x, mx * speed, 0.2 ); player.vel.z = lerp( player.vel.z, mz * speed, 0.2 ); 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.5; 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; } } function collide() { let px = player.pos.x; let py = player.pos.y; let pz = player.pos.z; for (let i = 0; i < visibleBlocks.length; i++) { let b = visibleBlocks[i]; if ( abs(px - b.x) < 20 && abs(py - b.y) < 60 && abs(pz - b.z) < 20 ) { return true; } } return false; } // ====================================================== // CAMERA // ====================================================== function updateCamera() { let bob = sin(frameCount * 0.15) * 2; cam.setPosition( player.pos.x, player.pos.y - 40 + bob, player.pos.z ); cam.lookAt( player.pos.x + sin(player.yaw), player.pos.y - 40 + player.pitch * 100, player.pos.z + cos(player.yaw) ); } function mouseMoved() { player.yaw += movedX * 0.002; player.pitch += movedY * 0.002; player.pitch = constrain( player.pitch, -1.5, 1.5 ); } // ====================================================== // BLOCK TARGET // ====================================================== function getTarget() { let dir = createVector( sin(player.yaw), player.pitch, cos(player.yaw) ); for (let d = 0; d < 200; d += 8) { let px = player.pos.x + dir.x * d; let py = player.pos.y + dir.y * d; let pz = player.pos.z + dir.z * d; for (let i = 0; i < visibleBlocks.length; i++) { let b = visibleBlocks[i]; if ( abs(px - b.x) < 20 && abs(py - b.y) < 20 && abs(pz - b.z) < 20 ) { return b; } } } return null; } function drawOutline() { let b = getTarget(); if (!b) return; push(); translate(b.x, b.y, b.z); noFill(); stroke(255); box(BLOCK + 2); pop(); } // ====================================================== // PARTICLES // ====================================================== 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.1; p.life -= 8; push(); translate(p.x, p.y, p.z); fill(p.c[0], p.c[1], p.c[2], p.life); box(4); pop(); if (p.life <= 0) { particles.splice(i, 1); } } } // ====================================================== // CLOUDS // ====================================================== function renderClouds() { noStroke(); fill(255, 220); for (let c of clouds) { push(); translate(c.x, c.y, c.z); ellipsoid(c.s, 40, c.s * 0.5); pop(); c.x += 0.2; if (c.x > 5000) { c.x = -5000; } } } // ====================================================== // MOBS // ====================================================== function renderMobs() { for (let m of mobs) { push(); translate(m.x, m.y, m.z); fill( m.t === "cow" ? color(120,70,50) : color(60,130,60) ); box(30, 50, 20); pop(); m.x += random(-0.5, 0.5); m.z += random(-0.5, 0.5); } } // ====================================================== // HUD // ====================================================== function drawHUD() { resetMatrix(); // CROSSHAIR stroke(255); line(-10,0,10,0); line(0,-10,0,10); // HOTBAR translate(-width/2 + 20, height/2 - 80); fill(0,150); noStroke(); rect(0,0,420,60,10); fill(255); textSize(16); text( "[1]G [2]D [3]S [4]W [5]L [6]Sa [7]Gl [8]Wa [9]Lv", 20, 30 ); text( "Selected: " + selected, 20, 50 ); text( "HP: " + player.health, 250, 30 ); text( "FPS: " + floor(frameRate()), 250, 50 ); // HAND push(); translate(width/2 + 240, height/2 + 140); rotate(-0.3); fill(220,180,150); box(40,120,40); pop(); } // ====================================================== // BLOCK INTERACTION // ====================================================== function mousePressed() { // BREAK if (mouseButton === LEFT) { let b = getTarget(); if (b) { // PARTICLES for (let i = 0; i < 10; i++) { particles.push({ x: b.x, y: b.y, z: b.z, vx: random(-3,3), vy: random(-5,0), vz: random(-3,3), c: COLORS[b.t], life: 255 }); } // REMOVE BLOCK for (let key in chunks) { let arr = chunks[key]; let idx = arr.indexOf(b); if (idx !== -1) { arr.splice(idx,1); break; } } } } // PLACE if (mouseButton === RIGHT) { let dir = createVector( sin(player.yaw), player.pitch, cos(player.yaw) ); let px = round((player.pos.x + dir.x * 120)/BLOCK)*BLOCK; let py = round((player.pos.y + dir.y * 120)/BLOCK)*BLOCK; let pz = round((player.pos.z + dir.z * 120)/BLOCK)*BLOCK; let key = floor(px/(CHUNK*BLOCK)) + "," + floor(pz/(CHUNK*BLOCK)); if (!chunks[key]) return; chunks[key].push({ x:px, y:py, z:pz, t:selected }); } } // ====================================================== // INPUT // ====================================================== function keyPressed() { if ( key === " " && player.grounded ) { player.vel.y = -11; } if (key >= "1" && key <= "9") { selected = BLOCK_TYPES[int(key)-1]; } } function windowResized() { resizeCanvas( windowWidth, windowHeight ); }
Duplicare
Executare
Cod
Cod HTML
<iframe sandbox="allow-scripts" src="/p5js/index.php?id=7241" style="width:408px; height:408px;border:solid 1px gray; overflow: scroll;"></iframe>
Duplicare script
Denumirea noului script
Du-te sus!