I changed a few thing up in my code trying to make it look cleaner.
here setup:
Path path; // the path the enemy follows
SpaceShip ship;// the spaceship
int s = 20;// the size of each block to create the maps easier
ArrayList<SpawnPoint> spawnPoint = new ArrayList<SpawnPoint>();// the points that create my enemies
ArrayList<Wall> wall = new ArrayList<Wall>();// the walls that i use to create my paths
ArrayList<Enemy> enemy = new ArrayList<Enemy>();// my enemies
ArrayList<Bullet> bullet = new ArrayList<Bullet>();// the bullets
Table pathTable, startTable;// the tables I use to create paths
TableRow tr;// table row
int tn = 1;// the number I get from the table
int room = 0;// the room for when i create map or play
int map = 0;// the map aka the path the enemy follows
int wave = 5;// number of enemies
void setup(){
size(600,600);// here I set the size
pathTable = loadTable("table/path.csv");// the path table
startTable = loadTable("table/start.csv");// where the enemies start at
mapCreator();// creates the maps using the tables
}
void draw(){
background(0);// here I color the background
path.display();// see the path
for (SpawnPoint sp: spawnPoint){// spawn the enemies
sp.show();
sp.update();
}
for (int i = enemy.size()-1; i >= 0; i--){// the enemies
Enemy e = enemy.get(i);
e.show();
e.update();
e.seek(path);
}
ship.show();// see the ship
ship.move();// move the ship
for (int i = bullet.size()-1; i >= 0; i--){// the bullets
Bullet b = bullet.get(i);
b.show();
b.move();
if (b.alive == false){bullet.remove(i);}
}
}
void mapCreator(){// create the path
path = new Path();// make the path
tr = startTable.getRow(map);// get the start point using the table
int n = tr.getInt(0);// figure out how many start points there is
for (int i = 1; i < n; i++){// loop through all the points
tn = tr.getInt(i);
spawnPoint.add(new SpawnPoint(tn));
}
ship = new SpaceShip();// create the spaceship
}
void keyPressed(){
switch(key){
case 'n':// go to the next map
for (int i = spawnPoint.size()-1; i >= 0; i--){
spawnPoint.remove(i);
}
for (int i = enemy.size()-1; i >= 0; i--){
enemy.remove(i);
}
if (map + 1 < pathTable.getRowCount()){map += 1;}
mapCreator();
break;
case 'b':// go back a map
for (int i = spawnPoint.size()-1; i >= 0; i--){
spawnPoint.remove(i);
}
for (int i = enemy.size()-1; i >= 0; i--){
enemy.remove(i);
}
if (map - 1 >= 0){map -= 1;}
mapCreator();
break;
// ship movement
case'w':// move ship frontwards
ship.spd = -2;
break;
case's':// move ship backwards
ship.spd = 2;
break;
case 'a':// rotate ship left
ship.r -= 2;
break;
case 'd':// rotate ship right
ship.r += 2;
break;
case ' ':// fires a bullet
ship.fire();
break;
}
}
void keyReleased(){
switch(key){
case 'w':
ship.spd = 0;
break;
}
}
bullet
class Bullet{
PVector pos;
float spd = -10;
float type = 0;
boolean alive = true;
int r = 0;
Bullet(PVector p, int r){
pos = new PVector(p.x,p.y);
this.r = r;
}
void show(){
fill(255);
noStroke();
ellipse(pos.x,pos.y,8,8);
}
void move(){
pos.x += cos(radians(r)) * spd;
pos.y += sin(radians(r)) * spd;
if (pos.y < 0 || pos.y > height || pos.x < 0 || pos.x > width){
alive = false;
}
}
}
Enemy
class Enemy{
PVector pos,acc,vel;
int n = 0;
float maxS = 2;
float minS = 0.5;
float maxF = random(0.01,0.3);
float life = 1;
int an = 0;
int r = 16;
Enemy(int n){
int jj = 0;
while (n > 29){
n -= 30;
jj += 1;
}
pos = new PVector(n * s, jj * s);
acc = new PVector(0,0);
vel = new PVector(maxS,0);
maxS = round(maxS);
}
void show(){
if (maxS == 2){fill(255,0,0);}
if (maxS == 3){fill(0,0,255);}
ellipse(pos.x,pos.y,r * 2,r * 2);
fill(0);
text(an,300,20);
}
void update(){
vel.add(acc);
pos.add(vel);
acc.mult(0);
}
void seek(Path p){
PVector t = p.points.get(an);
PVector desired = PVector.sub(t,pos);
if (desired.mag() == 0) return;
desired.normalize();
float d = PVector.dist(pos,t);
if (d >= 5){desired.mult(maxS);}
if (d < 5){desired.mult(minS);}
PVector steer = PVector.sub(desired, vel);
steer.limit(maxF);
acc.add(steer);
if (d < 1){
if (an + 1 < p.mn-1){
an += 1;
}
else if (an + 1 == p.mn-1){
an = 0;
}
}
}
}
Path
class Path{
ArrayList<PVector> points;
float radius;
int mn;
Path(){
radius = 20;
points = new ArrayList<PVector>();
addPoint();
}
PVector getStart(){
return points.get(0);
}
PVector getEnd(){
return points.get(points.size()-1);
}
void addPoint(){
tr = pathTable.getRow(map);
mn = tr.getInt(0);
for (int i = 1; i < mn; i++){
int tn = tr.getInt(i);
int h = 0;
while(tn > 29){
h += 1;
tn -= 30;
}
PVector point = new PVector(tn * s, (h * s)+(s/2));
points.add(point);
}
}
void display(){
strokeWeight(1);
stroke(255);
for (int i = 0; i < points.size()-1; i++){
PVector v1 = points.get(i);
PVector v2 = points.get(i + 1);
line(v1.x,v1.y,v2.x,v2.y);
}
}
}
SpaceShip
class SpaceShip{// I create a class
PVector pos; // I make a PVector to hold the position
int spd = 0;// I made a Int to say how fast I'm going might change to float
int r = 90; // this int says the rotation of the image
int timer = 0;
int timeBetween = 32;
SpaceShip(){
pos = new PVector(width/2,height - 60);// I set the position of the ship
}
void show(){
if (timer > -1){timer -= 1;}
pushMatrix();// I push it out
translate(pos.x,pos.y);// I translate the ship to its position
rotate(radians(r));// I rotate the ship
fill(192,192,192);
noStroke();
triangle(-25,0,25,-30,25,30);// I drew the ship so you can clearly tell when end is up
fill(255,0,0);
ellipse(-25,0,8,8); // here is where I want the bullet
popMatrix();// I pop everything back in
}
void fire(){
if (timer < 0 && bullet.size() < 5){
bullet.add(new Bullet(pos,r));
timer = timeBetween;
}
}
void move(){// I move the ship by the cos and sin
pos.x += cos(radians(r)) * spd;
pos.y += sin(radians(r)) * spd;
if (pos.x > width){pos.x = 0;}
if (pos.x < 0){pos.x = width;}
if (pos.y > height){pos.y = 0;}
if (pos.y < 0){pos.y = height;}
}
}
SpawnPoint
class SpawnPoint{
PVector pos;
int n = 0;
int timer = 0;
int timeBetween = 64;
SpawnPoint(int n){
int jj = 0;
this.n = n;
while (n > 29){
n -= 30;
jj += 1;
}
pos = new PVector(n * s, jj * s);
}
void show(){
fill(255,0,0);
rect(pos.x,pos.y,s,s);
}
void update(){
if (timer > -1){timer -= 1;}
if (timer < 0 && enemy.size() < wave){
enemy.add(new Enemy(n));
timer = timeBetween;
}
}// update
}
wall
class Wall{
PVector pos;
int n = 0;
boolean checked = false;
int type = 0;
Wall(int n){
int jj = 0;
this.n = n;
while (n > 29){
n -= 30;
jj += 1;
}
pos = new PVector(n * s, jj * s);
}
void show(){
fill(255,100);
if (checked == true){fill(255,255,0);}
if (type == 1){fill(255,0,0);}
stroke(0);
rect(pos.x,pos.y,s,s);
fill(0);
textSize(8);
text(n,pos.x + 2, pos.y + 15);
}
void mouseCheck(){
checked = false;
if (mouseX > pos.x && mouseX < pos.x + s &&
mouseY > pos.y && mouseY < pos.y + s){
checked = true;
}
}
}