class BugBase { Vector2D position; Vector2D velocity; float angle; float vangle; float size, speed; int state; //stand, turn, move, int stateCnt, stateLen, stateSubType; int type; //0 - random walker white // 1 afraider green int health = 100; static final int sSTAND = 0; static final int sROTATE = 1; static final int sWALKLINE = 2; static final int sBACKTRACK = 3; static final int sROTATETOWALKER = 4; static final int sROTATEFROMWALKER = 5; public BugBase(Vector2D position, float size, float speed, int type_) { this.position = position; this.velocity = new Vector2D(0, 0); this.size = size; this.speed = speed; type = type_; state = 0; } void update(int myIdx) { angle = getPositiveAngle(angle); vangle = 0; //velocity.zero(); doStates(); doBasicCollides(myIdx); doExtraCollides(myIdx); angle += vangle; position = position.plus(velocity); stateCnt ++; } void doStates() { if (state == sSTAND) dostate_stand(); else if (state == sROTATE) dostate_rotate(); else if (state == sWALKLINE) dostate_walkLine(); else if (state == sBACKTRACK) dostate_backtrack(); else if (state == sROTATETOWALKER) dostate_rotateToWalker(); else if (state == sROTATEFROMWALKER) dostate_rotateFromWalker(); } void doBasicCollides(int myIdx) { collideWithBugs(myIdx); collideWithWalls(); collideWithScreenEdges(); collideWithBullets(); collideWithWalker(); } void doExtraCollides(int myIdx) { } void collideWithBugs(int myIdx) { } void collideWithWalls() { /*if (state == sWALKLINE) { if (!position.isInside(new Vector2D(width, height))) { state = sBACKTRACK; stateLen = int(random(40, 80)); stateCnt = 0; velocity.reverse(); velocity.divideBy(3); } }*/ } void collideWithScreenEdges() { float x = position.getX(); float y = position.getY(); if (x > 500) position.setX(0); if (y > 400) position.setY(0); if (x < 0) position.setX(500); if (y < 0) position.setY(400); } void collideWithWalker() { if (position.isCircleCollision(size/2, new Vector2D(hero.x, hero.y), 15)) { hero.health -= 0.05 * size; } } void collideWithBullets() { //check for collisions with bullets and react on it for (int ib=0; ib 4) { //smooth(); line(cos(radians(50))*size/2, sin(radians(50))*size/2, cos(radians(-50))*size/2, sin(radians(-50))*size/2); float deg = 60; if (state != 0) deg += random(-10,10); line(cos(radians(deg))*size/2, sin(radians(deg))*size/2, cos(radians(deg))*size*0.7, sin(radians(deg))*size*0.7); deg = -60; if (state != 0) deg += random(-10,10); line(cos(radians(deg))*size/2, sin(radians(deg))*size/2, cos(radians(deg))*size*0.7, sin(radians(deg))*size*0.7); deg = 90; if (state != 0) deg += random(-10,10); line(cos(radians(deg))*size/2, sin(radians(deg))*size/2, cos(radians(deg))*size*0.7, sin(radians(deg))*size*0.7); deg = -90; if (state != 0) deg += random(-10,10); line(cos(radians(deg))*size/2, sin(radians(deg))*size/2, cos(radians(deg))*size*0.7, sin(radians(deg))*size*0.7); deg = 120; if (state != 0) deg += random(-10,10); line(cos(radians(deg))*size/2, sin(radians(deg))*size/2, cos(radians(deg))*size*0.7, sin(radians(deg))*size*0.7); deg = -120; if (state != 0) deg += random(-10,10); line(cos(radians(deg))*size/2, sin(radians(deg))*size/2, cos(radians(deg))*size*0.7, sin(radians(deg))*size*0.7); } popMatrix(); } } void setStrokeAndFill() //meant to be owerrided { noFill(); stroke(color(129, 140, 111)); } //DO STATES void dostate_stand() { if (stateCnt > stateLen) { state = 1; //int(random(0,12)/10); //(move or rotate) stateLen = int(random(10, 60)); stateCnt = 0; stateSubType = round(random(0,1)); //(left or right) if (stateSubType == 0) stateSubType = -1; } } void dostate_rotate() { angle += 0.06 * float(stateSubType); if (stateCnt > stateLen) { state = 2; stateLen = int(random(10, 40)); stateCnt = 0; } } void dostate_walkLine() { velocity = new Vector2D(speed, 0); velocity.rotateTo(angle); if (stateCnt > stateLen) { state = 0; stateLen = int(random(7, 18)); stateCnt = 0; velocity.zero(); } } void dostate_backtrack() { if (stateCnt > stateLen) { state = 0; stateCnt = 0; velocity.zero(); } } void dostate_rotateToWalker() { Vector2D walkPos = new Vector2D(hero.x, hero.y); float wangle = getPositiveAngle(position.getAngle(walkPos)) - angle; if (abs(wangle) < 0.3) { state = sWALKLINE; stateLen = int(random(10, 40)); stateCnt = 0; } else { float speed = 0.08; if ( wangle < 0 ) speed = -0.08; vangle += speed; // * (angle - PI); } } void dostate_rotateFromWalker() { Vector2D walkPos = new Vector2D(hero.x, hero.y); float wangle = getPositiveAngle(position.getAngle(walkPos) - PI) - angle; if (abs(wangle) < 0.3) { state = sWALKLINE; stateLen = int(random(10, 40)); stateCnt = 0; } else { float speed = 0.18; if ( wangle < 0 ) speed = -0.18; vangle += speed; // * (angle - PI); } } int clean(int i) { if (health < 0) { //warn this is global variable - array of all BugBase-s bugs = removeArrayItem(i, bugs); return 1; } return 0; } } public BugBase[] removeArrayItem(int index, BugBase[] items) { if(index < bugsCount) { items[index] = null; for(int i = index + 1;i < bugsCount; i++) { items[i - 1] = items[i]; } bugsCount--; } return items; }