File tree Expand file tree Collapse file tree 3 files changed +14
-10
lines changed
Expand file tree Collapse file tree 3 files changed +14
-10
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,8 @@ class EntityManager {
2828 void clear_except_player ();
2929 std::optional<std::reference_wrapper<Entity>> get_blocking_entity_at (
3030 Vector2D position);
31- Entity* get_non_blocking_entity_at (Vector2D position);
31+ std::optional<std::reference_wrapper<Entity>> get_non_blocking_entity_at (
32+ Vector2D position);
3233 Entity* get_closest_living_monster (Vector2D position, float range) const ;
3334 std::vector<Entity*> get_entities_at (Vector2D position);
3435 void place_entities (
Original file line number Diff line number Diff line change @@ -124,14 +124,15 @@ EntityManager::get_blocking_entity_at(Vector2D position) {
124124 return std::nullopt ;
125125}
126126
127- Entity* EntityManager::get_non_blocking_entity_at (Vector2D position) {
127+ std::optional<std::reference_wrapper<Entity>>
128+ EntityManager::get_non_blocking_entity_at (Vector2D position) {
128129 for (const auto & entity : entities_) {
129130 if (!entity->is_blocking () &&
130131 entity->get_transform_component ().get_position () == position) {
131- return entity;
132+ return std::reference_wrapper<Entity>(* entity) ;
132133 }
133134 }
134- return nullptr ;
135+ return std:: nullopt ;
135136}
136137
137138void EntityManager::remove (Entity* entity) {
Original file line number Diff line number Diff line change 1212namespace cpprl {
1313
1414StateResult PickupCommand::execute () {
15- Entity* item = world_.get_entities ().get_non_blocking_entity_at (
16- entity_->get_transform_component ().get_position ());
17- if (item) {
15+ std::optional<std::reference_wrapper<Entity>> optional_item_ref =
16+ world_.get_entities ().get_non_blocking_entity_at (
17+ entity_->get_transform_component ().get_position ());
18+ if (optional_item_ref.has_value ()) {
19+ Entity& item = optional_item_ref.value ().get ();
1820 world_.get_message_log ().add_message (
19- " You pick up the " + item-> get_name () + " ." , WHITE);
20- entity_->get_container ().add (item);
21- world_.get_entities ().remove (item);
21+ " You pick up the " + item. get_name () + " ." , WHITE);
22+ entity_->get_container ().add (& item);
23+ world_.get_entities ().remove (& item);
2224 } else {
2325 return NoOp{" There is nothing here to pick up." };
2426 }
You can’t perform that action at this time.
0 commit comments