How can i implement enemy shooting in this game?

Hi, I’ve been wondering how can I implement shooting to objects in my game named “obsticles”?
Here is the code; (sry for like 100 tabs of it)

int maxBCount = 1000;
int obsticleCount = 10;
Bullet b[] = new Bullet[maxBCount];
Obsticle o[] = new Obsticle[obsticleCount];
float x,y,r, pSize = 50, pS = 2; //ps - player speed

float PTNF = 0; //program time next fire
int bFired = 0;
int cGun = 0;


boolean canShoot = true;
boolean reloading = false;
float reloadTime = 1300;
float PTRE = 0; //program time reload end

char gameKeys[] = {'w','s','a','d'};
boolean keysPressed[] = new boolean[gameKeys.length];

void setup() {
  size(600,600);
  giveStats();
  x = 300;
  y = 300;
  for(int i = 0; i < maxBCount; i++) {
    b[i] = new Bullet();
  }
  for(int i = 0; i < obsticleCount; i++) {
    o[i] = new Obsticle();
    o[i].giveStats(random(0,width*2),random(0,height*2),10,10);
  }
  rectMode(3);
}

void draw() {
  background(0);
  r = getRotation(width/2,height/2,mouseX,mouseY);
  if(mousePressed) {
    mp();
  }
  updateAllBullets();
  updateAllObsticals();
  checkReload();
  useKeys();
  drawPlayer();
}

float getRotation(float p1x, float p1y, float p2x, float p2y) {
  float temp = atan2(p2y-p1y,p2x-p1x);
  return(temp);
}

void mp() { //mouse pressed
  if(millis() >= PTNF) {
    shoot();
  }
}

next tab

class Bullet {
  float bX, bY, r, s, bS, t, a;
  boolean gotStats = false;
  void giveStats(float x_, float y_, float r_, float s_, float bS_, float t_, float a_) {
    bX = x_;
    bY = y_;
    a = a_;
    r = r_;
    s = s_;
    bS = bS_;
    t = t_;
    gotStats = true;
    move(40);
    r += radians(random(-a,a));
  }
  void clearStats() {
    gotStats = false;
  }
  void move(float amount) {
    bX += amount * cos(r);
    bY += amount * sin(r);
  }
  void update() {
    if (gotStats) {
      move(s);
      t--;
      if (t < 0) {
        clearStats();
      }
    }
  }
  void show() {
    fill(0);
    stroke(255);
    strokeWeight(1);
    ellipse(bX - x,bY - y,bS,bS);
  }
}

next tab

float gunStats[][] = new float[2][7]; //[X][] - gun | 0-machine gun, 1-sniper| [][X] - stats | 0 - fire rate, 1 speed, 2 - accuracy, 3 - bullet size, 4-clip size, 5-reload time, 6-time

void giveStats() {
  gunStats[0][0] = 20;
  gunStats[0][1] = 10;
  gunStats[0][2] = 10;
  gunStats[0][3] = 3;
  gunStats[0][4] = 60;
  gunStats[0][5] = 2500;
  gunStats[0][6] = 65;
  
  gunStats[1][0] = 1;
  gunStats[1][1] = 20;
  gunStats[1][2] = 1;
  gunStats[1][3] = 5;
  gunStats[1][4] = 8;
  gunStats[1][5] = 2000;
  gunStats[1][6] = 400;
}

next tab

void keyPressed() {
  for(int i = 0; i < gameKeys.length; i++) {
    if(key == gameKeys[i]) {
      keysPressed[i] = true;
    }
  }
}

void keyReleased() {
  for(int i = 0; i < gameKeys.length; i++) {
    if(key == gameKeys[i]) {
      keysPressed[i] = false;
    }
  }
}

void useKeys() {
  if(keysPressed[0]) {
    y -= pS;
  }
  if(keysPressed[1]) {
    y += pS;
  }
  
  if(keysPressed[2]) {
    x -= pS;
  }
  
  if(keysPressed[3]) {
    x += pS;
  }
  
}

next tab

class Obsticle {
  float oX;
  float oY;
  float r;
  float rot;
  float mt; //minus time (when bullet hits object, it decreases time (surviving) by __
  boolean alive;
  void giveStats(float x_, float y_, float r_, float mt_) {
    oX = x_;
    oY = y_;
    r = r_;
    mt = mt_;
    alive = true;
  }
  void update() {
    if (alive) {
      rot = getRotation(oX,oY,x + width/2,y + height/2);
      stroke(255);
      line(oX - x,oY - y,oX - x + 20*cos(rot),oY - y + 20*sin(rot));
      //if(oX > width/2*3) {
      //  oX = width/2;
      //}
      
      for (int i = 0; i < bFired; i++) {
        if (dist(oX, oY, b[i].bX, b[i].bY) < r + b[i].r && b[i].gotStats) {
          alive = false;
          b[i].t -= mt;
        }
      }
    }
  }
  void display() {
    if (alive) {
      stroke(255);
      strokeWeight(2);
      fill(200, 0, 0);
      ellipse(oX -x,oY - y, r*2, r*2);
    }
  }
}
void updateAllObsticals() {
  for (int i = 0; i < obsticleCount; i++) {
    o[i].update();
    o[i].display();
  }
}

next tab

void drawPlayer() {
  fill(20);
  stroke(255);
  strokeWeight(2);
  
  pushMatrix();
  translate(width/2,height/2);
  rotate(r);
  rect(30,0,50,15);
  ellipse(0,0,pSize,pSize);
  popMatrix();
  
  //rect(500,500,50,50);
}

next tab

boolean ReloadingTemp = false;

void checkReload() {
  if (bFired >= gunStats[cGun][4]) {
    if(!ReloadingTemp) {
      reload();
    }
    ReloadingTemp = true;
  }
  if (millis() >= PTRE && reloading) {
    ReloadingTemp = false;
    reloading = false;
    canShoot = true;
    bFired = 0;
  }
}

void reload() {
  println("RELOADING");
  reloading = true;
  canShoot = false;
  PTRE = millis() + gunStats[cGun][5];
}

next tab

void shoot() {
  if (canShoot) {
    b[bFired].giveStats(x + width/2, y + height/2, r, gunStats[cGun][1], gunStats[cGun][3], gunStats[cGun][6], gunStats[cGun][2]);
    PTNF = millis() + 1000/gunStats[cGun][0];
    bFired ++;
  }
}

void updateAllBullets() {
  for (int i = 0; i < bFired; i++) {
    if (b[i].gotStats) {
      //println("B " + i + " rotation is " + b[i].r);
      b[i].update();
      b[i].show();
    }
  }
}

So should I add shooting buy creating 2D array of enemy bullets? [X] [] - wich object [][X] - wich clone?
or is there any way to create sub class inside of class?

I am rlly sry for posting all this nonsence, and i hope i dont waist too much of ur time :stuck_out_tongue:

I am not able to understand the question. If you can, try to explain what you intend to do exactly and what the problem is.

I am attempting to give “obsticles” abitlity to shoot at me, but i dont know how to do it optimal. If possible i would like to have class Bullets inside class Obsticles

Why don’t you declare another field for class Obsticle which would refer to an ArrayList of Bullet instances? :bulb:

class Obsticle {
  final ArrayList<Bullet> bullets = new ArrayList<Bullet>();

  float oX, oY, r, rot, mt;
  boolean alive;

  // ...
}

Do you want “obsticles” to be spelled that way (not “obstacles”) ?

You don’t need the bullets to be stored in the Obsticles in order to make them able to shoot. You could still have one global Bullets ArrayList and just let the Obstacles append a bullet to the list each time they shoot.

Having the Bullet listed inside its Obstacle is only an advantage is the Obstacle can control the bullet in some way, and needs to know which bullets belong to it. Otherwise it is simpler to fire and forget by adding to the global list.