问题描述
我刚刚开始我的世界模组,我有点不确定如何做某事。我正在尝试将铂金添加到 Fabric 改装的我的世界游戏中,并且一切正常,但我不确定如何让我的铂金矿石像其他矿石一样随机生成。我看过很多视频,但没有一个视频对我很有帮助。
我最后的问题是:
如何在 y = 12 - 15 时随机生成我的铂矿,而无需手动放置?
解决方法
您需要创建一个 ConfiguredFeature。确保在 onInitialize 注册您的 ConfiguredFeature。随意更改值以适合您的模组。
let cubes = [];
function setup() {
/* max(...) here is just for rendering with minimum size in the snippet */
createCanvas(max(windowWidth,800),max(windowHeight,600),WEBGL);
backCol = color(243,243,243);
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
let xPos = map(i,9,50,width - 50);
let yPos = map(j,height - 50);
cubes.push(new Cubes(xPos,yPos));
}
}
}
function draw() {
background(backCol);
noFill();
for (let cube of cubes) {
cube.update();
}
attracting();
}
function attracting() {
/* changed to check for mouse pressed once for all cubes */
if (mouseIsPressed) {
/* generating mouse position vector once for all cubes */
const mousePosVect = new p5.Vector(mouseX,mouseY);
for (let a = 0; a < cubes.length; a++) {
cubes[a].attraction(mousePosVect);
}
}
}
class Cubes {
constructor(x,y) {
/* Removed useless and confusing this.x,this.y */
this.size = 30;
this.stroke = 70;
this.gap = 150;
this.shift1 = color(96);
this.shift2 = color(244);
//vector variables
this.pos = createVector(x,y);
this.vel = createVector();
this.acc = createVector();
}
update() {
this.test_Color();
//attraction values
this.vel.add(this.acc);
this.vel.limit(5);
this.pos.add(this.vel);
this.acc.mult(0);
this.shape();
}
shape() {
push();
stroke(this.stroke);
this.test_Color();
/* Used this.pos instead of this.x,this.y for positioning */
translate(this.pos.x - width / 2,this.pos.y - height / 2,0);
this.test_rotation();
box(this.size);
pop();
}
test_Color() {
fill(this.shift1);
}
test_rotation() {
rotateX(frameCount / 60);
rotateY(frameCount / 60);
}
attraction(targetVector) {
//all cubes supposed to attract towards the mouse when pressed
/* Set target argument to vector,* moved the `if (mouseIsPressed)` condition outside */
let force = p5.Vector.sub(targetVector,this.pos);
let d = force.mag();
d = constrain(d,1,25);
var G = 50;
var strength = G / (d * d);
force.setMag(strength);
if (d < 20) {
force.mult(10);
}
/* changed to add force to acceleration
* instead of velocity (physically accurate) */
this.acc.add(force);
}
}