Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / supertux / object_factory.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include <sstream>
19 #include <stdexcept>
20
21 #include "lisp/lisp.hpp"
22 #include "lisp/parser.hpp"
23 #include "math/vector.hpp"
24 #include "supertux/object_factory.hpp"
25
26 GameObject* create_object(const std::string& name, const Reader& reader)
27 {
28   Factory::Factories::iterator i = Factory::get_factories().find(name);
29   if(i == Factory::get_factories().end()) {
30     std::stringstream msg;
31     msg << "No factory for object '" << name << "' found.";
32     throw std::runtime_error(msg.str());
33   }
34
35   return i->second->create_object(reader);
36 }
37
38 GameObject* create_object(const std::string& name, const Vector& pos, const Direction dir)
39 {
40   std::stringstream lisptext;
41   lisptext << "((x " << pos.x << ")"
42            << " (y " << pos.y << ")";
43   if(dir != AUTO)
44     lisptext << " (direction " << dir << "))";
45
46   lisp::Parser parser;
47   const lisp::Lisp* lisp = parser.parse(lisptext, "create_object");
48   GameObject* object = create_object(name, *(lisp->get_car()));
49
50   return object;
51 }
52
53 /* EOF */