Skip to content

Commit

Permalink
use RTTI to switch classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sid-parikh committed Mar 6, 2024
1 parent 873803d commit aa5a0cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,25 +311,17 @@ void RobotFactoryPosition::set_default_positions(WorldState& world_state,
// Offensive mode
// Closest 2 robots on defense, rest on offense
if (i <= 1) {
if (current_position_->get_name() != "Defense") {
current_position_ = std::make_unique<Defense>(*current_position_);
}
set_current_position<Defense>();
} else {
if (current_position_->get_name() != "Offense") {
current_position_ = std::make_unique<Offense>(robot_id_);
}
set_current_position<Offense>();
}
} else {
// Defensive mode
// Closest 4 robots on defense, rest on offense
if (i <= 3) {
if (current_position_->get_name() != "Defense") {
current_position_ = std::make_unique<Defense>(robot_id_);
}
set_current_position<Defense>();
} else {
if (current_position_->get_name() != "Offense") {
current_position_ = std::make_unique<Offense>(*current_position_);
}
set_current_position<Offense>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class RobotFactoryPosition : public Position {
void revive() override;

void update_play_state(const PlayState& play_state) override {
Position::update_play_state(play_state);
current_position_->update_play_state(play_state);
}

Expand Down Expand Up @@ -125,6 +126,22 @@ class RobotFactoryPosition : public Position {
State update_state();

void state_to_task();

void perform_free_kick();

/**
* @brief Sets the current position to the parameterized type.
* Requires the type to have a constructor that takes a reference to the old Position
* (and to be a subclass of Position)
*/
template <class Pos>
void set_current_position() {
// If we are not currently playing Pos
if (dynamic_cast<Pos*>(current_position_.get()) == nullptr) {
// This line requires Pos to implement the constructor Pos(const Position&)
current_position_ = std::make_unique<Pos>(*current_position_);
}
}
};

} // namespace strategy

0 comments on commit aa5a0cf

Please sign in to comment.