I am struggling to convert this code into p5.js format and looking for assistance with the conversion process. I understand that there may be differences in syntax between regular Java and p5.js, especially with arrays. Any help on getting this code to function correctly in p5.js would be greatly appreciated.
Myself myself;
ArrayList<Enemy> enemies;
`enter code here`ArrayList<Enemy> enemies;
ArrayList<Bullet> myBullets;
ArrayList<Bullet> eneBullets;
function setup(){
createCanvas(640, 640);
rectMode(CENTER);
myself = new Myself();
enemies = new ArrayList<Enemy>();
myBullets = new ArrayList<Bullet>();
eneBullets = new ArrayList<Bullet>();
}
function draw(){
background(0);
myself.display();
for(let i = 0; i < enemies.length; i++){
enemy[i].display();
}
for(let i = 0; i < myBullets.length; i++){
bullet[i].display();
}
for(let i = 0; i < eneBullets.length; i++){
bullet[i].display();
}
myself.update();
let nextEnemies = [];
for(let i = 0; i < enemies.length; i++){
enemy[i].update();
if(!enemy[i].isDead){
nextEnemies.push(enemy[i]);
}
}
enemies = nextEnemies;
let nextMyBullets = [];
for(let i = 0; i < myBullets.length; i++){
bullet[i].update();
if(!bullet[i].isDead){
nextMyBullets.push(bullet[i]);
}
}
myBullets = nextMyBullets;
let nextEneBullets = [];
for(let i = 0; i < eneBullets.length; i++){
bullet[i].update();
if(!bullet[i].isDead){
nextEneBullets.push(bullet[i]);
}
}
eneBullets = nextEneBullets;
if(random(1) < 0.02){
enemies.push(new Enemy());
}
}
class Myself{
let loc;
let size;
let coolingTime;
let isDead;
Myself(){
size = 25;
loc = createVector(width / 2, height - size / 2 - 10);
coolingTime = 0;
isDead = false;
}
display(){
if(isDead){
fill(255, 255, 0);
stroke(0, 255, 0);
} else {
noFill();
stroke(0, 255, 0);
}
rect(loc.x, loc.y, size, size);
}
update(){
isDead = false;
let dmx = mouseX - loc.x;
dmx = constrain(dmx, -5, 5);
loc.x += dmx;
coolingTime++;
if(mouseIsPressed && coolingTime >= 10){
myBullets.push(new Bullet());
coolingTime = 0;
}
for(let j = 0; j < eneBullets.length; j++){
if((loc.x - size / 2 <= b.loc.x && b.loc.x <= loc.x + size / 2)
&& (loc.y - size / 2 <= b.loc.y && b.loc.y <= loc.y + size / 2)){
isDead = true;
b.isDead = true;
break;
}
}
for(let k = 0; k < enemies.length; k++){
if(abs(loc.x - e.loc.x) < size / 2 + e.size / 2 && abs(loc.y - e.loc.y) < size / 2 + e.size / 2){
isDead = true;
e.isDead = true;
break;
}
}
}
}
If you could provide guidance on what changes need to be made or fixed within the code, it would greatly assist me. Thank you.