void Squad::computeActions() { if (!active) { if (isFull()) { active = true; } } if (active) { int noAlive = 0; int noDead = 0; for (int i = 0; i < (int)agents.size(); i++) { if (agents.at(i)->isAlive()) { noAlive++; } if (!agents.at(i)->isAlive()) { noDead++; } } if ((int)agents.size() > 0) { if (noAlive <= ((int)agents.size() / 10)) { noAlive = 0; } } if (noAlive == 0) { active = false; clearGoal(); return; } } if (active) { if (activePriority != priority) { priority = activePriority; } } checkAttack(); }
bool Entity::update( long Elapsed, float *XMove, float *YMove){ float MoveX, MoveY, Speed; float XDiff, YDiff, Dist, Radius; //float y1, y2; //long i, SpellNum; //bool SpellCast; // Entity *CharPtr; // Clear movements and action MoveX = MoveY = 0.0f; // Calculate movement speed Speed = (float)Elapsed / 1000.0f * getSpeed(this); // Move Character based on their type switch (this->AI) { case CHAR_STAND: break; case CHAR_WANDER: // Calculate new distance and direction if needed this->Distance -= Elapsed; if (this->Distance <= 0.0f) { // Calculate a new distance to travel this->Distance = (float)(rand() % 2000) + MAXIMUM_WALKING_DISTANCE_OFFSET; // Calculate a new direction calculateDirection(); } // Process walk or stand still if (this->Distance > MINIMUM_WALKING_DISTANCE) { MoveX = (float)sin(this->Direction) * Speed; MoveY = (float)cos(this->Direction) * Speed; this->Action = CHAR_MOVE; } else {//The padding in between zero and the min walking distance //With out this padding, the entity will never idle,eg. continously walk and changing directions // Stand still for random amount of time this->Action = CHAR_IDLE; } break; case CHAR_ROUTE: // Determine if character has reached point XDiff = (float)fabs(this->sprite.getPosition().x - this->routes[this->CurrentPoint].XPos); YDiff = (float)fabs(this->sprite.getPosition().y - this->routes[this->CurrentPoint].YPos); Dist = XDiff*XDiff + YDiff*YDiff; Radius = getXYRadius(this) * 0.25f; // Go to next point if reached if (Dist < (Radius*Radius)) { this->CurrentPoint++; if (this->CurrentPoint > this->NumPoints) this->CurrentPoint = 0; // Calculate new differences and distance XDiff = (float)fabs(this->sprite.getPosition().x - this->routes[this->CurrentPoint].XPos); YDiff = (float)fabs(this->sprite.getPosition().y - this->routes[this->CurrentPoint].YPos); Dist = XDiff*XDiff + YDiff*YDiff; } // Setup movement towards target Dist = (float)sqrt(Dist); if (Speed > Dist) Speed = Dist; MoveX = (this->routes[this->CurrentPoint].XPos - this->sprite.getPosition().x) / Dist * Speed; MoveY = (this->routes[this->CurrentPoint].YPos - this->sprite.getPosition().y) / Dist * Speed; // Set new direction this->Direction = (float)atan2(MoveX, MoveY); // Set new action this->Action = CHAR_MOVE; break; case CHAR_FOLLOW: if (this->TargetChar != NULL) { // Check distance between thiss XDiff = (float)fabs(this->sprite.getPosition().x - this->TargetChar->sprite.getPosition().x); YDiff = (float)fabs(this->sprite.getPosition().y - this->TargetChar->sprite.getPosition().y); Dist = XDiff*XDiff + YDiff*YDiff; // Update if further then distance if (Dist > (this->Distance*this->Distance)) { // Setup movement towards target Dist = (float)sqrt(Dist); if (Speed > Dist) Speed = Dist; MoveX = (this->TargetChar->sprite.getPosition().x - this->sprite.getPosition().x) / Dist * Speed; MoveY = (this->TargetChar->sprite.getPosition().y - this->sprite.getPosition().y) / Dist * Speed; // Set new direction this->Direction = (float)atan2(MoveX, MoveY); // Set new action this->Action = CHAR_MOVE; } } break; case CHAR_EVADE: if (this->TargetChar != NULL) { // Check distance between thiss XDiff = (float)fabs(this->sprite.getPosition().x - this->TargetChar->sprite.getPosition().x); YDiff = (float)fabs(this->sprite.getPosition().y - this->TargetChar->sprite.getPosition().y); Dist = XDiff*XDiff + YDiff*YDiff; // Update if closer then distance if (Dist < (this->Distance*this->Distance)) { // Setup movement away from target Dist = (float)sqrt(Dist); if (Speed > Dist) Speed = Dist; MoveX = -(this->TargetChar->sprite.getPosition().x - this->sprite.getPosition().x) / Dist * Speed; MoveY = -(this->TargetChar->sprite.getPosition().y - this->sprite.getPosition().y) / Dist * Speed; // Set new direction this->Direction = (float)atan2(MoveX, MoveY); // Set new action this->Action = CHAR_MOVE; } } break; }//End switch checkAttack(CHAR_MONSTER, CHAR_PC, Radius, Dist, XDiff, YDiff, MoveX, MoveY); *XMove = MoveX; *YMove = MoveY; return true; }