1 /****************************************************************************** 2 * 3 * SmashPcEnemy() - Creates a player, and places him in the space 4 * 5 ******************************************************************************/ 6 SmashPcEnemy::SmashPcEnemy(SmashPcData &GameData, 7 sf::RenderWindow *pApp, cpSpace *pSpace, 8 cpVect Location, U32 u32Level) : 9 PhysicalObject(pApp, pSpace, "Gfx/blueenemy.bmp", "", 10 Location, PI/2.0f, 0.0f, TRUE), 11 mu32Health(20*u32Level), 12 mu32LastFire(timeGetTime()), 13 mu32LastMoveUpdate(0), 14 mGameData(GameData) 15 { 16 printf("Enemy created!\n"); 17 // Set the Velocity limit to our fake movement 18 mpBody->v_limit = ENEMY_SPEED; 19 20 mpShape->collision_type = ENEMY_COL_TYPE; 21 mpShape->layers = ENEMY_PLAYER_LAYER; 22 mpShape->data = (void *)this; 23 24 // Create Collision handler here 25 cpSpaceAddCollisionHandler(mpSpace, ENEMY_COL_TYPE, BULLET_COL_TYPE, SmashPcEnemy::BulletCollision, NULL, NULL, NULL, NULL); 26 27 // Sound notifying a player has spawned 28 GameSound::Play("EnemySpawn"); 29 30 } 31 32 /****************************************************************************** 33 * 34 * ~SmashPcEnemy() - Removes a player 35 * 36 ******************************************************************************/ 37 SmashPcEnemy::~SmashPcEnemy() 38 { 39 40 } 41 42 /****************************************************************************** 43 * 44 * Update() - Do enemy things 45 * 46 ******************************************************************************/ 47 void SmashPcEnemy::Update(cpBody *pPlayerBody) 48 { 49 U32 u32Time = timeGetTime(); 50 51 // check if we need to update the movement 52 if (mu32LastMoveUpdate + ENEMY_UPDATE_MOVE < u32Time) 53 { 54 cpFloat Angle = atan2(pPlayerBody->p.y - mpBody->p.y, 55 pPlayerBody->p.x - mpBody->p.x); 56 57 // add randomization to angle 58 cpFloat RandomDegrees = (cpFloat)(((S32)rand() % 20) - 10); 59 Angle += (RandomDegrees*PI)/180.0f; 60 61 cpBodySetAngle(mpBody, Angle); 62 63 // Change force towards player 64 mForce = cpv(ENEMY_FORCE*cos(Angle), ENEMY_FORCE*sin(Angle)); 65 //mpBody->f = cpv(ENEMY_FORCE*cos(Angle), ENEMY_FORCE*sin(Angle)); 66 67 mu32LastMoveUpdate = u32Time; 68 } 69 70 mpBody->f = mForce; 71 72 if (mu32LastFire + ENEMY_REFIRE_TIME < u32Time) 73 { 74 SmashPcData::tBulletList BulletList; 75 mGameData.GetBulletList(BulletList); 76 SmashPcBullet *pBullet = new SmashPcBullet(mpApp, BulletList[0], 77 mpBody, mpSpace, FALSE); 78 79 mGameData.AddActiveBullet(pBullet); 80 mu32LastFire = u32Time; 81 } 82 } 83 84 /****************************************************************************** 85 * 86 * NotifyHit() - Notify the enemy it has been hit by a bullet 87 * 88 ******************************************************************************/ 89 void SmashPcEnemy::NotifyHit(SmashPcBullet *pBullet) 90 { 91 92 if (mu32Health < pBullet->GetDamage()) 93 { 94 // Enemy Dead 95 mu32Health = 0; 96 97 // Play dead sound 98 } 99 else 100 { 101 GameSound::Play("PlayerHit"); 102 mu32Health -= pBullet->GetDamage(); 103 } 104 105 pBullet->SetDead(); 106 } 107 108 /****************************************************************************** 109 * 110 * BulletCollision() - Callback for when Enemy hits a bullet 111 * 112 ******************************************************************************/ 113 int SmashPcEnemy::BulletCollision(cpArbiter *arb, struct cpSpace *space, void *data) 114 { 115 SmashPcBullet *pBullet; 116 SmashPcEnemy *pEnemy; 117 cpShape *pBulletShape, *pEnemyShape; 118 cpArbiterGetShapes(arb, &pEnemyShape, &pBulletShape); 119 120 pEnemy = reinterpret_cast<SmashPcEnemy*>(pEnemyShape->data); 121 pBullet = reinterpret_cast<SmashPcBullet*>(pBulletShape->data); 122 123 // We have the enemy that hit, and the bullet he hit 124 pEnemy->NotifyHit(pBullet); 125 126 return 0; 127 128 }