Permalink
Browse files

Make it possible to pick up dynamically created objects

  • Loading branch information...
1 parent e908649 commit 05febc5b59f1d7b97fe579f8a69510343d397903 @samuellb committed Jan 12, 2012
Showing with 18 additions and 4 deletions.
  1. +13 −3 controller.cpp
  2. +1 −0 controller.h
  3. +1 −0 old_man.cpp
  4. +3 −1 woods.cpp
View
@@ -247,7 +247,7 @@ Actor * Controller::get_actor(std::string actor_name)
*/
void Controller::add_object(Object & object, Place & place)
{
- objects.insert(std::pair<std::string, Object*>(object.name(), &object));
+ add_object(object);
place.add_object(object);
}
@@ -259,7 +259,7 @@ void Controller::add_object(Object & object, Place & place)
void Controller::add_object(Object & object, Container & container)
{
if (container.add(object)) {
- objects.insert(std::pair<std::string, Object*>(object.name(), &object));
+ add_object(object);
}
}
@@ -270,14 +270,24 @@ void Controller::add_object(Object & object, Container & container)
*/
void Controller::add_object(Object & object, Actor & actor)
{
- objects.insert(std::pair<std::string, Object*>(object.name(), &object));
+ add_object(object);
Container * container = dynamic_cast<Container*>(&object);
if (container != 0) {
actor.set_container(container);
}
}
/**
+ * Controller::add_object
+ *
+ * Add object that has already been added somewhere in the game.
+ */
+void Controller::add_object(Object & object)
+{
+ objects.insert(std::pair<std::string, Object*>(object.name(), &object));
+}
+
+/**
* Controller::get_object
*
* Get object from string name
View
@@ -30,6 +30,7 @@ class Controller {
void add_object(Object &, Place &);
void add_object(Object &, Container &);
void add_object(Object &, Actor &);
+ void add_object(Object & object);
Object * get_object(std::string);
Actor * get_actor(std::string);
View
@@ -42,6 +42,7 @@ void OldMan::talked_to(Actor & actor)
if (actor.is_player()) {
std::cout << "Thank you! Now I can make a fire! Here's a key." << std::endl;
}
+ controller.add_object(*key);
} else {
delete key;
}
View
@@ -50,7 +50,9 @@ void Woods::chop(Actor & actor)
// Give some firewood to the actor
Firewood *wood = new Firewood(controller, controller.make_identifier("firewood"));
- if (!human->pick_up(*wood)) {
+ if (human->pick_up(*wood)) {
+ controller.add_object(*wood);
+ } else {
delete wood;
}
}

0 comments on commit 05febc5

Please sign in to comment.