//Towards player, shoot if close, run if low hp void Enemy::classicAI() { Shape* player = getPlayer_(); if (player) { b2Vec2 playerPos = player->getPosition(); b2Vec2 ePos = getPosition(); b2Vec2 between = playerPos - ePos; //In long distance, look if (between.Length() < 40 || between.Length() < 4.f) { orient(between); } //In med distance, move towards, shoot at if (between.Length() < 25 && (getHP() >= verticesMIN_ * hpScale_)) { if (getArmed()) { move(between); orient(between); if (getWeaponReady()) { float rotation = atan2f(between.y, between.x); float adjust = randFloat(-0.4, 0.4); b2Vec2 newDir(cosf(rotation + adjust), sinf(rotation + adjust)); trigger(between); } else release(); } } else if (getArmed() && between.Length() < verticesMIN_ * hpScale_) { orient(-between); move(-between); } else if (!getArmed() && between.Length() < size_ * 1.5f) { orient(-between); } else move(b2Vec2_zero); } }
//Flocking together void Enemy::flockTest() { b2Vec2 sum(0, 0); b2Vec2 fire(0, 0); int count = 0; for (Enemy* v : swarm_) { if (v != this && v->getVertices() == vertices_) { b2Vec2 between = v->getPosition() - getPosition(); float dist = between.Length(); if (dist <= 5.f) { sum += LenardJonesPotential(v, count); } } } Shape* player = getPlayer_(); if (player) { if ((player->getPosition() - getPosition()).Length() < visRange_[3]) { //swarmTarget_ = player; //sum += LenardJonesPotential(player, count); //fire = FiringPotential(player); } } //get average if (count > 0) { sum.x /= static_cast<float>(count); sum.y /= static_cast<float>(count); orient(sum); move(sum); if (getArmed()) { if (getWeaponReady()) { trigger(fire); } else release(); if ((weapon_->getBar() / weapon_->getBarMAX()) <= 0.1f) { reup(); } } } }
void Button::mouseExited(MouseEventDetails* const e) { if(getEnabled()) { if(getArmed()) { this->setActive(false); } } Component::mouseExited(e); }
//Fire forward and brake void Enemy::blindFire() { move(b2Vec2_zero); if (getArmed()) { if (getWeaponReady()) { trigger(b2Vec2(0, 0)); } else { release(); } } }
void Button::mouseReleased(MouseEventDetails* const e) { if(getEnabled()) { if(e->getButton() == MouseEventDetails::BUTTON1 && getArmed()) { this->setActive(false); produceActionPerformed(); setArmed(false); //Consume the event e->consume(); return; } } Component::mouseReleased(e); }
//Gets current weapon's range float Enemy::getRange() const { return (getArmed() ? weapon_->getRange() : 0.f); }