xxxxxxxxxx
103
/*
Minecraft ore spawner made with help of chatgpt
*/
let diamond, emerald;
let blocks = [];
let numBlocks = 32; // number of blocks
let disappearDist = 358; // the distance at which blocks disappear
let button;
function preload() {
diamond = loadImage('diamond.png');
emerald = loadImage('emerald.png');
}
function setup() {
createCanvas(400, 400, WEBGL);
noStroke()
for (let i = 0; i < numBlocks; i++) {
blocks[i] = new Block(random(-100, 100), random(-100, 100), random(-100, 100),
random()>0.5 ? diamond : emerald);
}
button = createButton('save image and data');
button.mousePressed(saveData);
}
function draw() {
clear()
rotateX(frameCount * 0.01);
rotateY(frameCount * 0.01);
rotateZ(frameCount * 0.01)
for (let i = 0; i < numBlocks; i++) {
blocks[i].update();
blocks[i].display();
}
}
class Block {
constructor(x, y, z, textureimage) {
this.pos = createVector(x, y, z);
this.vel = createVector(random(-1, 1), random(-1, 1), random(-1, 1));
this._texture = textureimage;
}
update() {
this.pos.add(this.vel);
if (this.pos.mag() > disappearDist) {
this.pos = createVector(random(-100, 100), random(-100, 100), random(-100, 100));
}
}
display() {
push();
translate(this.pos.x, this.pos.y, this.pos.z);
for (let i = -1; i <= 1; i += 2) {
push();
translate(0, 0, i * 25);
texture(this._texture);
plane(50);
pop();
push();
rotateY(HALF_PI);
translate(0, 0, i * 25);
texture(this._texture);
plane(50);
pop();
push();
rotateX(HALF_PI);
translate(0, 0, i * 25);
texture(this._texture);
plane(50);
pop();
}
pop();
}
}
function saveData() {
saveCanvas("image", "png");
let data = {
"cubes": blocks.map((block, i) => {
return {
"name": "cube" + (i + 1),
"x": block.pos.x,
"y": block.pos.y,
"z": block.pos.z,
};
})
};
saveJSON(data, 'cubes.json');
}