This is the AI for one of the enemies in my game created for 2D Games programming. The first function updates the animation of the enemy each loop and checks for collision with characters. The enemy is placed on a platform and rotates in a circle. The second function animates the fireball delivered by the enemy and checks for collision each loop.
void updateReznor(SPRITE* whichReznor, SPRITE* whichPlatform) { //while reznor has health if (whichReznor->health > 0) { //has animation delay reached threshold? if (++whichReznor->animcount > whichReznor->animdelay) { //reset counter whichReznor->animcount = 0; //animate the sprite if (++whichReznor->curframe > 1) whichReznor->curframe = 0; //if reznor curframe is equal to 1, release a fireball if (whichReznor->curframe == 1) { if (Mario.x > 3800) { PlaySound(reznor); lState = bossFight; ReznorFireball.setValues(whichReznor->x + 45, whichReznor->y + 50, 0, 3, 40, 40, 0, 2, 0, 2, 2, 0, 1, nohealth, 0); reznorfireballvector.push_back(ReznorFireball); } } } //make the reznor move in a circle on the platform angle += 0.0005f; double rez1x = 0; double rez1y = 0; rez1x = cos(angle) * radius; rez1y = sin(angle) * radius; rez1x += centerpointX; rez1y += centerpointY; reznorvector[0].x = rez1x; reznorvector[0].y = rez1y; reznorplatformvector[0].x = rez1x + 19; reznorplatformvector[0].y = rez1y + 99; //if there is a collision between mario and reznor if (CollisionD(Mario, reznorvector[0])) { Mario.health -= reznorvector[0].damage; Mario.x -= 150; } } //if reznor has no health, make it fall off screen if (whichReznor->health <= 0) { if (whichReznor->state == nohealth) { whichReznor->x += 0.02f; whichReznor->y += 0.3f; if (whichReznor->y >= 620) whichReznor->y = 700; } else { whichReznor->state = nohealth; Mario.score += whichReznor->score; //give mario points for killing reznor } } } void updateReznorFireballs() { //only start throwing fireballs once mario reaches the boss part of the stage if (Mario.x >= 4000) { for (int i = 0; i < reznorfireballvector.size(); ++i) { //move and animate sprite //has animation delay reached threshold? if (++reznorfireballvector[i].animcount > reznorfireballvector[i].animdelay) { //reset counter reznorfireballvector[i].animcount = 0; //animate the sprite if (++reznorfireballvector[i].curframe > 1) reznorfireballvector[i].curframe = 0; } reznorfireballvector[i].x -= 0.4f; //if collision between mario and fireball, remove health if (CollisionD(Mario, reznorfireballvector[i])) { Mario.health -= reznorfireballvector[i].damage; } //if fireball goes offscreen, remove from vector if (reznorfireballvector[i].x < 3000) { reznorfireballvector.erase(reznorfireballvector.begin() + i); } } } }