void MetalineEnemy::moveMetalineEnemy() { updateActionState(); if (actionState == DEFAULT_ACTION_STATE) { } else if (actionState == PLAYER_DETECTED_ACTION_STATE) { if (!hasCollided) { GameState *gs = GameState::GetInstance(); Vector3f playerPos = gs->GetPlayerPosition(); Vertex newDirection; newDirection.x = playerPos(0) - this->center.x; newDirection.y = playerPos(1) - this->center.y; newDirection.z = playerPos(2) - this->center.z; normalize(newDirection); this->velocity = Vec3f(newDirection.x * this->speed, newDirection.y * this->speed, newDirection.z * this->speed); } } else if (actionState == SOUND_DETECTED_ACTION_STATE) { //Move Toward Sound TODO } this->center.x = this->center.x + (this->velocity)[0]; this->center.y = this->center.y + (this->velocity)[1]; this->center.z = this->center.z + (this->velocity)[2]; }
bool MetalineEnemy::isPlayerVisible() { GameState *gs = GameState::GetInstance(); GameRoom *gr = gs->GetRoom(); vector<GameObject *> gobjs = gr->GetGameObjects(); vector<GamePlayer *> players = gr->GetPlayers(); int objSize = gobjs.size(); int playerSize = players.size(); Vector3f playerPos = gs->GetPlayerPosition(); Vertex playerDir; playerDir.x = playerPos(0) - this->center.x; playerDir.y = playerPos(1) - this->center.y; playerDir.z = playerPos(2) - this->center.z; float length = (playerDir.x * playerDir.x) + (playerDir.y * playerDir.y) + (playerDir.z * playerDir.z); float xIntervalFraction = playerDir.x / NUM_RAY_TRACE_INTERVALS; float yIntervalFraction = playerDir.y / NUM_RAY_TRACE_INTERVALS; float zIntervalFraction = playerDir.z / NUM_RAY_TRACE_INTERVALS; float radiusSquared = this->radius * this->radius; for (int i = 0; i < NUM_RAY_TRACE_INTERVALS; i++) { Vec3f v = Vec3f(this->center.x + playerDir.x * xIntervalFraction, this->center.y + playerDir.y * yIntervalFraction, this->center.z + playerDir.z * zIntervalFraction); for (int j; j < playerSize; j++) { GamePlayer *player = players[j]; if (player->IsInBoundingBox(v)) { return true; } } if ((i * length / NUM_RAY_TRACE_INTERVALS) > radiusSquared) { for (int k = 0; k < objSize; k++){ GameObject *obj = gobjs[k]; if (obj->IsInBoundingBox(v)) { return false; } } } } return true; }
void MetalineEnemy::checkToFire() { if (fireCounter >= FIRE_COUNTER_THRESHOLD) { GameState *gs = GameState::GetInstance(); Vector3f playerPos = gs->GetPlayerPosition(); Vector3f pCenter(this->center.x, this->center.y, this->center.z); Vector3f projectileDirection = (playerPos - pCenter).normalized(); Vector3f velocity(projectileDirection(0) * PROJECTILE_SPEED, projectileDirection(1) * PROJECTILE_SPEED, projectileDirection(2) * PROJECTILE_SPEED); Projectile *p = new Ball(pCenter, velocity, PROJECTILE_RADIUS); gs->GetParticleSystems()->AddBullet(p); fireCounter = 0; } else if (fireCounter < FIRE_COUNTER_THRESHOLD) { fireCounter++; } }