void AGENT::update_rangedfight(GROUP* target_group) { // get target AGENT * t = find_target_random(target_group); if (!t) return; // didn't find one // fight move_mult(collision ? 3.0 : 1.0); set_dest(t->get_x(), t->get_y()); if (dist_dest() > unitData->range - group->get_radius()/2) { // move to target fight_timer--; if (!collision) move((int)get_dest_x(), (int)get_dest_y(), true); // move else collision_avoid(); // collision avoid state = AGENT::READY_FIGHT; setAnimation(is_stuckcounter() ? ANIMATION_DATA::STAND : ANIMATION_DATA::MOVE, true); } else { // close enough to target move_stop(); state = AGENT::FIGHTING; double angle = rotation * M_PI / 180.; double angle_target = angle_to(t->get_x() - unitData->r_horiz_offset*cos(angle + M_PI/2), t->get_y() - unitData->r_horiz_offset*sin(angle + M_PI/2)); if (is_near_angle(angle_target, 15) || group->is_building()) { // deal damage // create projectile if (frame_num() == unitData->rattack_frame) { if (is_first_frame_display()) { double t_angle = angle_target*M_PI/180; int plife = (int)(dist_to(t->get_x(), t->get_y()) - getRadius() - 2*t->getRadius()); part_sys.add(attack_type->particle, x + getRadius()*cos(t_angle) + unitData->r_horiz_offset*cos(t_angle + M_PI/2), y + getRadius()*sin(t_angle) + unitData->r_horiz_offset*sin(t_angle + M_PI/2), angle_target, (plife > 0) ? plife : t->getRadius(), attack_type, t->getGroup(), t->get_id(), group->get_id()); } } // unit stuff set_dest(x, y); setAnimation(ANIMATION_DATA::RANGED, true); } else { // face target fight_timer--; rotate_towards((int)angle_target); if (!is_near_angle(angle_target, 20)) setAnimation(ANIMATION_DATA::STAND, true); } } // single units sync group position with their own if (group->is_single_unit()) group->sync_to(this); // update gemeric stuff update(); }
float dist_to(const Posture& p) const { return dist_to(p.x(), p.y()); }