xxxxxxxxxx
114
let MAXIMUMBULLETS = 100;
class Bullet {
constructor({x = 0, y = 0, x_vel = 0, y_vel = 0}={}) {
this.x = x;
this.y = y;
this.x_vel = x_vel;
this.y_vel = y_vel;
this.flag_destroy = false;
}
update() {
this.x += this.x_vel;
this.y += this.y_vel;
if (this.x < -width/2-64 || this.x > width/2+64 ||
this.y < -height/2-64 || this.y > height/2+64) {
this.flag_destroy = true;
}
}
show() {
noStroke()
fill([0, 0, 0])
circle(this.x, this.y, 5);
//fill([255, 0, 0])
//circle(this.x, this.y, 24);
//fill([255, 255, 255])
//circle(this.x, this.y, 16);
}
}
let bullets = [];
let shaderTest;
function preload() {
shaderTest = loadShader("shaders/metaball.vert","shaders/metaball.frag");
}
function setup() {
createCanvas(512, 480, WEBGL);
noStroke();
for (let i = 0; i < 10; i++) {
bullets.push(new Bullet({
x : random(width), y : random(height),
x_vel : random(-2, 2), y_vel : random(-2, 2)
}))
}
}
let func_randomBulletCircle = (_posX, _posY) => {
if (bullets.length < 1000) {
let amount = floor(random(8, 16))
for (let i = 0; i < amount; i++) {
let d = (2 * 3.141) * (i / amount);
let xa = sin(d);
let ya = cos(d);
bullets.push(new Bullet({
x : _posX, y : _posY,
x_vel : xa, y_vel : ya
}))
}
}
}
function mousePressed() {
func_randomBulletCircle(mouseX - width/2, mouseY - height/2)
}
function draw() {
if (!keyIsDown(32)) {
if (random(1) < 0.02) {
func_randomBulletCircle(random(-width/2, width/2), random(-height/2, height/2))
}
for (let b of bullets) {
b.update()
//b.show()
}
}
for (let i = 0; i < bullets.length; i++) {
let b = bullets[i]
if (b.flag_destroy) {
bullets.splice(i, 1)
i--
}
}
let ballPositions = []
for (let b of bullets) {
ballPositions.push((b.x/width)+0.5)
ballPositions.push((b.y/height)+0.5)
}
while (ballPositions.length/2 < MAXIMUMBULLETS) {
ballPositions.push(-69, -69)
}
shader(shaderTest);
shaderTest.setUniform("iResolution", [width,height]);
shaderTest.setUniform("iTime", second());
shaderTest.setUniform("balls", ballPositions);
rect(0,0,width,height);
resetShader()
}