1// SuperTux
2// Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "object/path_object.hpp"
18
19#include <boost/optional.hpp>
20
21#include "object/path_gameobject.hpp"
22#include "supertux/d_scope.hpp"
23#include "supertux/sector.hpp"
24#include "util/log.hpp"
25#include "util/reader_mapping.hpp"
26#include "util/writer.hpp"
27
28PathObject::PathObject() :
29 m_path_uid(),
30 m_walker()
31{
32}
33
34PathObject::~PathObject()
35{
36}
37
38void
39PathObject::init_path(const ReaderMapping& mapping, bool running_default)
40{
41 bool running = running_default;
42 mapping.get("running", running);
43
44 std::string path_ref;
45 boost::optional<ReaderMapping> path_mapping;
46 if (mapping.get("path", path_mapping))
47 {
48 auto& path_gameobject = d_gameobject_manager->add<PathGameObject>(*path_mapping, true);
49 m_path_uid = path_gameobject.get_uid();
50 m_walker.reset(new PathWalker(m_path_uid, running));
51 }
52 else if (mapping.get("path-ref", path_ref))
53 {
54 d_gameobject_manager->request_name_resolve(path_ref, [this, running](UID uid){
55 m_path_uid = uid;
56 m_walker.reset(new PathWalker(uid, running));
57 });
58 }
59}
60
61void
62PathObject::init_path_pos(const Vector& pos, bool running)
63{
64 auto& path_gameobject = d_gameobject_manager->add<PathGameObject>(pos);
65 m_path_uid = path_gameobject.get_uid();
66 m_walker.reset(new PathWalker(path_gameobject.get_uid(), running));
67}
68
69Path*
70PathObject::get_path() const
71{
72 if(!d_gameobject_manager)
73 return nullptr;
74
75 if (auto* path_gameobject = d_gameobject_manager->get_object_by_uid<PathGameObject>(m_path_uid)) {
76 return &path_gameobject->get_path();
77 } else {
78 return nullptr;
79 }
80}
81
82std::string
83PathObject::get_path_ref() const
84{
85 if(!d_gameobject_manager)
86 return nullptr;
87
88 if (auto* path_gameobject = d_gameobject_manager->get_object_by_uid<PathGameObject>(m_path_uid)) {
89 return path_gameobject->get_name();
90 } else {
91 return {};
92 }
93}
94
95/* EOF */
96