Hey!
I have been using Processing recently to create small games just for fun since I am stuck at home a lot due to the Covid 19 Situation. So this is my first time posting on the forum because I ran into a problem this time which I cant solve by looking up similar stuff on the internet. I apologize in advance for the badly organised code and I think that most of it you dont have to look at, it’s just in case you want to run everything on your on that I included it here. So basically my problem is the following: In the game you have a stage were two players can spawn units and fight against each other. I haven’t gotten far but right now you can spawn 1 type(ArrayList baseInf for basic Infantery) and they shoot at the enemy and kill them eventually once they have done enough damage. But this only works for the first spawned Unit, after that if a unit with INdex 1 dies , the game crashes. I thank anyone who has the patience to look at this mess in advance and am very happy about anyy kind of response!
int playerQuantity = 2;
int towerQuantity = 7;
Stage stage = new Stage();
InnerCircle innerCircle = new InnerCircle(960, 540, 300);
Player myPlayer = new Player[playerQuantity];
Playerbase playerbase = new Playerbase();
ArrayList myBaseInf = new ArrayList[playerQuantity];
Estate estate = new Estate[playerQuantity];
Tower tower = new Tower[playerQuantity][towerQuantity];
Health health = new Health(playerQuantity,towerQuantity);
PFont mono;
String gamePhase = “Start”;
String gameMap = “Naboo”;
float rnd= new float[width+1];
float mX;
float mY;
int baseInfCreatedCount = {0,0};
int towerCount = {0,0};
void setup(){
fullScreen();
//size(1920,1080);
println(width);
println(height);
mono = loadFont(“FootlightMTLight-48.vlw”);
textFont(mono);
gamePhase = “NewGameStarted”;
estate[0] = new Estate();
estate[1] = new Estate();
myPlayer[0] = new Player(0);
myPlayer[1] = new Player(1);
myBaseInf[0] = new ArrayList();
myBaseInf[1] = new ArrayList();
//ONLY DURING DEVELOPEMENT; DELETE AFTER FINISH
//ONLY DURING DEVELOPEMENT; DELETE AFTER FINISH
//ONLY DURING DEVELOPEMENT; DELETE AFTER FINISH
int tY = 270;
tower[0][2] = new Tower(400,tY2,0,2);
tower[1][2] = new Tower(1520,tY2,1,2);
for(int i = 0; i < 2; i++){
tower[0][i] = new Tower(300,tY,0,i);
tower[1][i] = new Tower(1620,tY,1,i);
tY += 540;
}
towerCount[0] = 3;
towerCount[1] = 3;
for(int i = 0; i<3; i++){
println( "1: " + tower[1][i].xTurr + " + " + tower[1][i].yTurr);
println( "0 :" + tower[0][i].xTurr + " + " + tower[0][i].yTurr);
}
}
void draw(){
//GAME PHASES
if(gamePhase == “Start”){
stage.started();
}
else if(gamePhase == “Menu”){
stage.menu();
//println(mouseX);
}
else if(gamePhase == “NewGameStarted”){
stage.newGameStarted(gameMap);
}
//GAME MECHANICS
estate[0].updateMoney();
estate[1].updateMoney();
//DRAW OBJECTS
if(towerCount[0] > 0){
for(int i = 0; i < towerCount[0]; i++){
if(tower[0][i].manned == false){
tower[0][i].displayBasic();
}
else if(tower[0][i].manned == true){
tower[0][i].displayManned();
}
}
}
if(towerCount[1] > 0){
for(int i = 0; i < towerCount[1]; i++){
if(tower[1][i].manned == false){
tower[1][i].displayBasic();
}
else if(tower[1][i].manned == true){
tower[1][i].displayManned();
}
}
}
//draw BaseInf()
for (int i = myBaseInf[0].size()-1; i >= 0; i–) {
BaseInf baseInf0 = myBaseInf[0].get(i);
baseInf0.display();
}
for (int i = myBaseInf[1].size()-1; i >= 0; i–) {
BaseInf baseInf1 = myBaseInf[1].get(i);
baseInf1.display();
}
}
void mousePressed(){
// if stage.start() is clicked, start game…
if ((width/2 -mouseX) <= 150 && (width/2-mouseX) >= -150 && (height/2 -mouseY) <= 100 && (height/2 -mouseY) >= -100 && gamePhase == “Start”){
gamePhase = “Menu”;
}
// if in stage.menu() “new game” is clicked, create new game…
if(mouseX >= width/2-125 && mouseX <=width/2+125 && mouseY>= height/2 -150 && mouseY <= height/2-50 && gamePhase == “Menu”){
int tY = 270;
tower[0][2] = new Tower(400,tY*2,0,2);
tower[1][2] = new Tower(1520,tY*2,1,2);
for(int i = 0; i < 2; i++){
tower[0][i] = new Tower(300,tY,0,i);
tower[1][i] = new Tower(1620,tY,1,i);
tY += 540;
}
towerCount[0] = 3;
towerCount[1] = 3;
gamePhase = "NewGameStarted";
}
//AI-Playstyle
if(mouseX >= 1720 && mouseX <= 1870 && mouseY >= 20 && mouseY <=50 && myPlayer[0].mode != -1){
myPlayer[0].mode = -1;
}
else if(mouseX >= 1720 && mouseX <= 1870 && mouseY >= 60 && mouseY <=90 && myPlayer[0].mode != 0){
myPlayer[0].mode = 0;
}
else if(mouseX >= 1720 && mouseX <= 1870 && mouseY >= 100 && mouseY <=130 && myPlayer[0].mode != 1){
myPlayer[0].mode = 1;
}
if(mouseX >= 1720 && mouseX <= 1870 && mouseY >= height-130 && mouseY <= height-100 && myPlayer[1].mode != -1){
myPlayer[1].mode = -1;
}
else if(mouseX >= 1720 && mouseX <= 1870 && mouseY >= height-90 && mouseY <=height-60 && myPlayer[1].mode != 0){
myPlayer[1].mode = 0;
}
else if(mouseX >= 1720 && mouseX <= 1870 && mouseY >= height-50 && mouseY <=height-20 && myPlayer[1].mode != 1){
myPlayer[1].mode = 1;
}
}
void keyPressed(){
if (key == ‘q’ || key == ‘Q’){
myBaseInf[1].add(baseInfCreatedCount[1],new BaseInf(myPlayer[1].spawnpoint.x,myPlayer[1].spawnpoint.y, 10.0, 1,baseInfCreatedCount[1]));
baseInfCreatedCount[1]++;
}
if (key == ‘1’ ){
myBaseInf[0].add(baseInfCreatedCount[0],new BaseInf(myPlayer[0].spawnpoint.x,myPlayer[0].spawnpoint.y, 10, 0,baseInfCreatedCount[0]));
baseInfCreatedCount[0]++;
}
}
class BaseInf{
ArrayList myBullet;
PVector pos = new PVector (100,0);
PVector newPos = new PVector (100,0);
float speed = 10;
int fireSpeed = 5;
float softAttack = 6;
float hardAttack = 6;
float softDefense = 5;
float hardDefense = 10;
float range = 300;
float size;
float rotation;
float rAdd = 0;
float xTower;
float newStructureX;
float yTower;
float enemyTowerX;
float enemyTowerY;
float dmgDone = 0;
int player;
int enemy;
int ident;
int towerNR;
int colr = new int[2][3];
boolean shoot;
boolean obstacleOvercome = true;
boolean shrink = false;
boolean dead = false;
boolean reverse = false;
Float myBaseInfHealth;
Float enemyBaseInfHealth;
BaseInf(float x, float y, float s, int p,int id){
colr[0][0] = 50;
colr[0][1] = 100;
colr[0][2] = 206;
colr[1][0] = 8;
colr[1][1] = 155;
colr[1][2] = 2;
pos.x = x;
pos.y = y;
size = s;
player = p;
ident = id;
myBullet = new ArrayList<Bullet>();
health.BaseInf[player].append(health.baseInfStartHP);
health.BaseInf[player].set(ident,health.baseInfStartHP);
if(player == 0){
enemy = 1;
}
else if(player == 1){
enemy = 0;
}
xTower = tower[player][tower[player][0].distanceCheck(pos.x, pos.y,player)].xTurr;
yTower = tower[player][tower[player][0].distanceCheck(pos.x, pos.y,player)].yTurr;
enemyTowerX = tower[enemy][tower[enemy][0].distanceCheck(pos.x,pos.y,enemy)].xTurr;
enemyTowerY = tower[enemy][tower[enemy][0].distanceCheck(pos.x,pos.y,enemy)].yTurr;
for(int i = 0; i<towerCount[player]; i++){
if(yTower - tower[player][i].yTurr == 0 && xTower - tower[player][i].xTurr == 0){
towerNR = i;
}
}
}
void display (){
translate(0,0);
move();
death();
}
void move (){
//defensiveMode
if(playerbase.defensiveNeed[player] > 0 && tower[player][towerNR].manned == false && myPlayer[player].mode == -1){
tower[player][towerNR].headed = true;
rotation = atan2(yTower-pos.y, xTower- pos.x);
pushMatrix();
translate(pos.x,pos.y-1.5*size);
rotate(.5*PI);
rotate(rotation);
picture();
newPos.x = cos(rotation)*speed + pos.x;
newPos.y = sin(rotation)*speed + pos.y;
translate(0,0);
pos.set(newPos.x, newPos.y, 0.);
popMatrix();
mannedCheck();
estateGain();
//println( "Spieler 0: " + estate[0].holding + "Spieler 1: " +estate[1].holding);
} //normal mode
else if(myPlayer[player].mode == 0){
if(collisionCheckStructures().y == 0 && obstacleOvercome == true){
rotation = atan2(enemyTowerY -pos.y, enemyTowerX- pos.x);
pushMatrix();
translate(pos.x,pos.y-1.5*size);
rotate(.5*PI);
rotate(rotation);
picture();
newPos.x = cos(rotation)*speed + pos.x;
newPos.y = sin(rotation)*speed + pos.y;
translate(0,0);
pos.set(newPos.x, newPos.y, 0.);
popMatrix();
estateGain();
}
else if(collisionCheckStructures().y == 1 && collisionCheckStructures().x == player && obstacleOvercome == false){
translate(0,0);
int nb1 = int(collisionCheckStructures().x);
int nb2 = int(collisionCheckStructures().z);
if(pos.x > tower[nb1][nb2].xTurr - tower[nb1][nb2].sizeTurret && player == 0){
obstacleOvercome = true;
}
else if(pos.x < tower[nb1][nb2].xTurr + tower[nb1][nb2].sizeTurret && player == 1 ){
obstacleOvercome = true;
}
float newStructureY = tower[nb1][nb2].yTurr;
if(pos.y > tower[nb1][nb2].yTurr){
newStructureY += 1.15*tower[nb1][nb2].sizeTurret;
}
else if(pos.y <= tower[nb1][nb2].yTurr){
newStructureY -= 1.15*tower[nb1][nb2].sizeTurret;
}
if(player == 0){
newStructureX = tower[nb1][nb2].xTurr- tower[nb1][nb2].sizeTurret;
}
else if(player == 1){
newStructureX = tower[nb1][nb2].xTurr+ tower[nb1][nb2].sizeTurret;
}
float newR = atan2(newStructureY-pos.y,newStructureX-pos.x);
pushMatrix();
translate(pos.x,pos.y-1.5*size);
rotate(0.5*PI);
rotate(newR);
picture();
newPos.x = cos(newR)*speed + pos.x;
newPos.y = sin(newR)*speed + pos.y;
translate(0,0);
pos.set(newPos.x, newPos.y, 0.);
popMatrix();
estateGain();
}
else if(collisionCheckStructures().y > 0 && collisionCheckStructures().x == enemy && obstacleOvercome == true){
collisionCheckStructures();
shooting(int(collisionCheckStructures().x),int(collisionCheckStructures().y),int(collisionCheckStructures().z));
}
}
}
void mannedCheck(){
if(dist(pos.x,pos.y,xTower,yTower) <= tower[player][tower[player][0].distanceCheck(pos.x, pos.y,player)].sizeTurret ){
tower[player][towerNR].manned = true;
playerbase.defensiveNeed[player] --;
}
}
void estateGain(){
if(pos.x - estate[player].holding > 0 && player == 0){
estate[player].holding = pos.x;
}
else if((width - estate[player].holding) - pos.x > 0 && player == 1){
estate[player].holding = width - pos.x;
}
}
PVector collisionCheckStructures(){
//EXPLANATION collisionData:
// PVector.x = player
// PVector.y = object type( 1 = tower, 2 = playerbase)
// PVector.z = object number
PVector collisionData = new PVector(0,0,0);
//friendly tower
for(int i = 0; i< towerCount[player]; i++){
if(dist(pos.x,pos.y, tower[player][i].xTurr,tower[player][i].yTurr) <= tower[player][i].sizeTurret * 3 ){
if(player == 1){
if(pos.x- tower[player][i].xTurr> 80&& player == 1){
collisionData.set(player,1,i);
obstacleOvercome = false;
}
else{
collisionData.set(0,0,0);
obstacleOvercome = true;
}
}
else if(player == 0){
if(pos.x- tower[player][i].xTurr< -80&& player == 0){
collisionData.set(player,1,i);
obstacleOvercome = false;
}
else{
collisionData.set(0,0,0);
obstacleOvercome = true;
}
}
}
}
for(int i = 0; i< towerCount[enemy]; i++){
if(dist(pos.x,pos.y, tower[enemy][i].xTurr,tower[enemy][i].yTurr) <= range){
collisionData.set(enemy,1,i);
}
}
for(int i = myBaseInf[enemy].size()-1; i >= 0; i--){
BaseInf baseInf = myBaseInf[enemy].get(i);
enemyBaseInfHealth = health.BaseInf[enemy].get(i);
if(dist(pos.x,pos.y, baseInf.pos.x,baseInf.pos.y) <= range && enemyBaseInfHealth >1){
collisionData.set(enemy,3,i);
//println(collisionData.x+ " " + collisionData.y + " " + collisionData.z);
}
}
if(dist(pos.x,pos.y, playerbase.xBase[enemy], playerbase.yBase) <= playerbase.sizeBase[enemy]+10){
collisionData.set(enemy,2,-1);
}
return collisionData;
}
void picture(){
noStroke();
myBaseInfHealth = health.BaseInf[player].get(ident);
if(ident>0){
//print(myBaseInfHealth);
}
if( shrink == true){
size -= dmgDone/2;
shrink =false;
}
fill(colr[player][0],colr[player][1],colr[player][2]);
triangle( -size, 1.5*size , 0,-1.5*size, size, 0.5*size);
triangle(-size, 0.5*size, 0,-1.5*size, size, 1.5*size);
strokeWeight(1);
stroke(0);
line(-size,0.5*size,0,-1.5*size);
line(size,0.5*size,0,-1.5*size);
line(-size,1.5*size,0,0.75*size);
line(size,1.5*size,0,0.75*size);
line(-size,1.5*size,-0.6*size,0.3*size);
line(size,1.5*size,0.6*size,0.3*size);
}
void shooting(int p,int c, int id){
int idShooting = id;
int pShooting = p;
int cShooting = c;
//println("Player :" + pShooting + "Kind: "+ cShooting+ "Index: " +idShooting);
PVector targetPos = new PVector(0,0);
float targetSize = 0;
float targetSoftDefense = 0;
float targetHardDefense = 0;
if(cShooting == 1){
targetPos.x = tower[pShooting][idShooting].xTurr;
targetPos.y = tower[pShooting][idShooting].yTurr;
targetSize = tower[pShooting][idShooting].sizeTurret;
targetSoftDefense = tower[pShooting][idShooting].softDefense;
targetHardDefense = tower[pShooting][idShooting].hardDefense;
}
else if(cShooting == 3){
BaseInf baseInfc = myBaseInf[enemy].get(idShooting);
targetPos.x = baseInfc.pos.x;
targetPos.y = baseInfc.pos.y;
targetSize = baseInfc.size;
targetSoftDefense = baseInfc.softDefense;
targetHardDefense = baseInfc.hardDefense;
}
float newR = atan2(targetPos.y-pos.y,targetPos.x-pos.x);
if(fireSpeed == 0){
shoot = true;
fireSpeed = 100;
}
else{
shoot = false;
}
pushMatrix();
translate(pos.x,pos.y-1.5*size);
rotate(0.5*PI);
rotate(newR);
picture();
popMatrix();
fireSpeed --;
if(shoot == true){
myBullet.add(new Bullet(pos.x,pos.y,targetPos.x, targetPos.y,targetSize,"BaseInf",player,idShooting));
}
for (int i = 0; i < myBullet.size(); i++) {
Bullet bullet = myBullet.get(i);
bullet.mainFunction();
boolean done= false;
if(bullet.mainFunction()[0] == true && bullet.mainFunction()[1] == false ){
myBullet.remove(i);
done = true;
}
else if(bullet.mainFunction()[1] == true&& done == false){
done = true;
dmgDone = 0;
if(softAttack- targetSoftDefense > 0){
dmgDone+= softAttack- targetSoftDefense;
}
if(this.hardAttack- targetHardDefense > 0){
dmgDone+= hardAttack- targetHardDefense;
}
if(cShooting == 1){
health.tower[pShooting][idShooting] -= dmgDone;
tower[pShooting][idShooting].shrink = true;
myBullet.remove(i);
}
else if(cShooting == 3){
println(idShooting + " Spieler: " + player + " Ident: " + ident);
enemyBaseInfHealth = health.BaseInf[enemy].get(idShooting);
enemyBaseInfHealth -= dmgDone;
BaseInf baseInfc = myBaseInf[enemy].get(idShooting);
baseInfc.shrink = true;
health.BaseInf[enemy].set(idShooting,enemyBaseInfHealth);
//printArray(health.BaseInf[enemy]);
myBullet.remove(i);
reverse = true;
}
}
}
}
void death(){
//println("Health " + player + " & " +ident+": " +myBaseInfHealth);
if(myBaseInfHealth < 2){
println("hey " +ident);
dead = true;
myBaseInf[player].remove(ident);
}
}
}
class Bullet{
String type;
PVector pos = new PVector(0,0);
PVector newPos = new PVector(0,0);
PVector targetPos = new PVector(0,0);
float targetSize;
float speed = 10;
int player;
int enemy = 0;
int explosionTime = 0;
PVector colr = new PVector(0,0,0);
PVector part = new PVector(0,0);
PVector targetIdent = new PVector(0,0);
boolean twoIdents = false;
Bullet(float x, float y,float tX, float tY,float tSize, String t, int p,int Ident){
pos.x = x;
pos.y = y;
targetPos.x = tX;
targetPos.y = tY;
targetSize = tSize;
type = t;
player = p;
if(player == 0){
enemy =1 ;
}
if( Ident > -1){
twoIdents = true;
}
targetIdent.x = enemy;
targetIdent.y = Ident;
if(player ==1){
colr.x = 100;
colr.y = 250;
colr.z = 10;
}
else if(player == 0){
colr.x = 10;
colr.y = 150;
colr.z = 250;
}
}
boolean mainFunction(){
boolean[] hit = new boolean[2];
hit[0]= false;
hit[1]= false;
if(dist(targetPos.x,targetPos.y,pos.x,pos.y) > targetSize){
move();
hit[0] = false;
hit[1] = false;
return hit;
}
else if(dist(targetPos.x,targetPos.y,pos.x,pos.y) <= targetSize){
//explode();
hit[0] = true;
hit[1] = true;
return hit;
}
else {
hit[0] = true;
hit[1] = false;
return hit;
}
}
void display(){
part.x = random(-5,5);
part.y = random(-1,1);
noStroke();
rectMode(CENTER);
fill(colr.x,colr.y,colr.z);
rect(0,0,10,4);
fill(255);
rect(part.x,part.y,1,1);
rect(-part.x,-part.y,1,1);
rect(-part.x/2,-part.y/2,1,1);
}
void move(){
float rotation = atan2(targetPos.y-pos.y, targetPos.x-pos.x);
pushMatrix();
translate(pos.x,pos.y);
rotate(rotation);
this.display();
newPos.x = cos(rotation)*speed + pos.x;
newPos.y = sin(rotation)*speed + pos.y;
translate(0,0);
pos.set(newPos.x, newPos.y, 0.);
popMatrix();
}
void explode(){
if(explosionTime < 3){
part.x = random(-5,5);
part.y = random(-1,1);
noStroke();
rectMode(CENTER);
ellipseMode(CENTER);
fill(colr.x,colr.y,colr.z);
ellipse(pos.x,pos.y,8+2*explosionTime,8+2*explosionTime);
explosionTime ++;
}
}
}
class Health{
float tower;
float towerStartHP = 100;
float base;
float baseStartHP = 1000;
FloatList BaseInf= new FloatList[playerQuantity];
float baseInfStartHP = 10;
int playrN;
Health(int playerN, int towerN){
tower = new float[playerN][towerN];
base = new float[playerN];
BaseInf[0] = new FloatList();
BaseInf[1] = new FloatList();
playrN = playerN;
for(int i = 0; i<towerN; i++){
tower[0][i] = towerStartHP;
tower[1][i] = towerStartHP;
}
base[0]= baseStartHP;
base[1] = baseStartHP;
}
}
class Estate{
float holding = 149;
int money;
void updateMoney(){
money = int(holding/10);
}
}
class InnerCircle{
float x;
float y;
float size;
int playerCaptured;
InnerCircle(float X, float Y, float s){
x = X;
y = Y;
size = s;
}
void display(){
translate(0,0);
ellipseMode(CENTER);
fill(177, 102, 128);
ellipse(x,y,size,size*1.5);
}
}
class Player {
PVector spawnpoint;
int player;
int mode;
Player(int p){
player = p;
if(player == 0){
spawnpoint = new PVector (150, 540);
}
else if(player == 1){
spawnpoint = new PVector (1770, 540);
}
}
}
class Player {
PVector spawnpoint;
int player;
int mode;
Player(int p){
player = p;
if(player == 0){
spawnpoint = new PVector (150, 540);
}
else if(player == 1){
spawnpoint = new PVector (1770, 540);
}
}
}
class Player {
PVector spawnpoint;
int player;
int mode;
Player(int p){
player = p;
if(player == 0){
spawnpoint = new PVector (150, 540);
}
else if(player == 1){
spawnpoint = new PVector (1770, 540);
}
}
}
class Tower{
float sizeTurret = 50;
float xTurr;
float yTurr;
float softDefense = 20;
float hardDefense = 5;
int colorTurr = new int[2][3];
boolean manned = false;
int player;
int ident;
boolean headed = false;
boolean shrink = false;
Tower(float x, float y, int p, int i){
player = p;
ident = i;
xTurr = x;
yTurr = y;
colorTurr[0][0] = 50;
colorTurr[0][1] = 100;
colorTurr[0][2] = 206;
colorTurr[1][0] = 8;
colorTurr[1][1] = 155;
colorTurr[1][2] = 2;
}
void displayBasic(){
if( shrink == true){
sizeTurret = sizeTurret * (health.tower[player][ident]/100);
shrink =false;
}
translate(0,0);
rectMode(CENTER);
strokeWeight(6);
stroke(240);
fill(colorTurr[player][0], colorTurr[player][1],colorTurr[player][2]);
rect(xTurr, yTurr, sizeTurret, sizeTurret);
}
void displayManned(){
translate(0,0);
rectMode(CENTER);
if(player == 0){
fill(0);
noStroke();
rect(xTurr+30,yTurr-20,20,9);
rect(xTurr+30,yTurr+20,20,9);
rect(xTurr+40,yTurr-20,10,6);
rect(xTurr+40,yTurr+20,10,6);
}
else if(player ==1){
fill(0);
noStroke();
rect(xTurr-30,yTurr-20,20,9);
rect(xTurr-30,yTurr+20,20,9);
rect(xTurr-40,yTurr-20,10,6);
rect(xTurr-40,yTurr+20,10,6);
}
strokeWeight(6);
stroke(0);
fill(colorTurr[player][0], colorTurr[player][1],colorTurr[player][2]);
rect(xTurr, yTurr, sizeTurret, sizeTurret);
}
int distanceCheck(float xObj, float yObj, int p){
float[] distance = {0,0,0,0,0,0,0};
int nearestTower = 0;
//printArray(headed);
//printArray(manned);
for(int i = 0; i<towerCount[p]; i++){
if(tower[p][i].manned == false && tower[p][i].headed != true){
distance[i] = dist(tower[p][i].xTurr,tower[p][i].yTurr, xObj, yObj);
}
}
for(int i= 0; i<towerCount[p]; i++){
if(distance[i]-max(distance) == 0){
nearestTower = i;
}
}
return nearestTower;
}
}