/* check hit with all missiles in a missile list (linked list), * return 1 if finally destroyed by missiles; 0 otherwise */ int LazerEnemy::CheckHit(MissileList &missiles) { if (!alive) return -1; /* get the head of list */ MissileNode *node = missiles.getFront(); /* traverse the missiles in the list and check for hit */ while (node) { /* typeof(node->dat) is Missile* */ if (this->Plane::CheckHit(node->dat) || arm.CheckHit(node->dat)) { /* if hit by a missile, the enemy get hurt :( */ life -= node->dat->getPower(); /* the missile will disappear after collision, unless it's laser */ if (node->dat->getType() != LASER) node = missiles.Delete(node); else node = node->next; /* check for early termination, return 1 if destroyed */ if (life <= 0) return 1; } else { node = node->next; } } /* if we get here, then enemy is not destroyed */ return 0; }
int main(){ Vector2 startPosition(300, 500); Vector2 startDirection(0, -1); /* This list is used to store all user emmitted active missiles */ MissileList enemy2Missiles; MissileList playerMissiles; /* Create the thunder */ Thunder thunder(startPosition, startDirection); thunder.setVelocity(5); thunder.SwitchWeapon(BULLET, playerMissiles); Plane *player; player = &thunder; /* Create the boss */ LazerEnemy enemy2(Vector2(300, 0), Vector2(0,1)); enemy2.Init(enemy2Missiles); enemy2.setVelocity(15); Plane *enemy; enemy = &enemy2; FsOpenWindow(0,0,WINDOW_WID,WINDOW_HEI,1); glClearColor(0.1, 0.1, 0.1, 1); bool running = true; int i = 0; int cntDown = 0; while(running) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); FsPollDevice(); int key=FsInkey(); int lb, mb, rb, mx, my; int mouse = FsGetMouseEvent(lb, mb, rb, mx, my); if(FSKEY_ESC==key) { running=false; break; } else if (FSKEY_UP == key) { /* UP for power up */ ((Thunder *)player)->PowerUp(playerMissiles); } if (FSMOUSEEVENT_RBUTTONDOWN == mouse) { /* Right click for switch between 3 types of weapons */ i = (i+1) % 3; ((Thunder *)player)->SwitchWeapon((MissileType)i, playerMissiles); } /* Thunder: shoot and cool down weapon. */ player->Shoot(mouse, playerMissiles); player->CoolDown(); /* Thunder: move and draw. */ player->Move(1.0); player->Draw(); /* Draw the enemy */ enemy->Aim(player); printf("before move\n"); enemy->Move(0.4); printf("after move\n"); enemy->Draw(); /* Enemy fire */ ((LazerEnemy *)enemy)->Shoot(enemy2Missiles); enemy->CoolDown(); if (enemy->CheckHit(playerMissiles) == 1) { ((LazerEnemy *)enemy)->Disappear(enemy2Missiles); cntDown = 100; } /* Stay for a while after boss die */ if (cntDown > 0) { cntDown--; if (cntDown == 0) running = false; } /* traverse the missiles list, move missile */ MissileNode *node; node = enemy2Missiles.getFront(); while(node) { node->dat->Move(1.0); if (!node->dat->CheckInWindow()) { node = enemy2Missiles.Delete(node); } else { node = node->next; } } node = playerMissiles.getFront(); while(node) { node->dat->Move(1.0); if (!node->dat->CheckInWindow()) { node = playerMissiles.Delete(node); } else { node = node->next; } } FOR_EACH(node, enemy2Missiles) { node->dat->Draw(); } FOR_EACH(node, playerMissiles) { node->dat->Draw(); } PrintPower(((LazerEnemy *)enemy)->getLife()); FsSwapBuffers(); FsSleep(25); } return 0; }
int main(void) { Vector2 bulletV(0, -10); Vector2 cannonV(0, -20); Vector2 laserV(0, -1); Vector2 bulletPosition(300, 400); Vector2 laserPosition(100, 400); Vector2 cannonPosition(200, 400); Vector2 velocity(1, 1); MissileList missiles; Missile *missile; MissileNode *laser = NULL; int laserReload = 0; int cannonReload = 0; int bulletReload = 0; int firing = 0; srand(time(NULL)); FsOpenWindow(0,0,600, 800,1); glClearColor(0, 0, 0, 1); bool running = true; while(running) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); FsPollDevice(); int key=FsInkey(); int lb, mb, rb, mx, my; int mouse = FsGetMouseEvent(lb, mb, rb, mx, my); if(FSKEY_ESC==key) { running=false; } if(FsGetKeyState(FSKEY_UP)) { bulletV.rotate(0.05); cannonV.rotate(0.05); laserV.rotate(0.05); } if(FsGetKeyState(FSKEY_DOWN)) { bulletV.rotate(-0.05); cannonV.rotate(-0.05); laserV.rotate(-0.05); } if(FSMOUSEEVENT_LBUTTONDOWN == mouse) { firing = 1; } else if (FSMOUSEEVENT_LBUTTONUP == mouse) { firing = 0; } if (firing == 1) { if (laserReload <= 0) { laserReload = 100; missile = new Laser(gBlue, 100, laserPosition, laserV, 1); laser = missiles.InsertFront(missile); } if (bulletReload <= 0) { bulletReload = 100; missile = new Bullet(gRed, 100, bulletPosition, bulletV, 1); missiles.InsertFront(missile); } if (cannonReload <= 0) { cannonReload = 100; missile = new Cannon(gGreen, 100, cannonPosition, cannonV, 1); missiles.InsertFront(missile); } } else if (firing == 0 && laser) { missiles.Delete(laser); laser = NULL; laserReload = 0; } MissileNode *node; node = missiles.getFront(); while(node) { node->dat->Move(1.0); node->dat->Draw(); if (!node->dat->CheckInWindow()) { node = missiles.Delete(node); } else { node = node->next; } } if (bulletReload > 0) bulletReload -= 10; if (cannonReload > 0) cannonReload -= 10; /* if(key == FSKEY_UP) printf("UP!!!!\n"); if(key == FSKEY_SPACE) printf("SPACE!!!\n"); if (FsCheckKeyHeldDown()) printf("KEY DOWN!!!\n"); */ FsSwapBuffers(); FsSleep(25); } return 0; }