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 | #ifndef HEADER_SUPERTUX_SCRIPTING_GAME_OBJECT_HPP |
18 | #define |
19 | |
20 | #include "supertux/game_object_manager.hpp" |
21 | #include "util/log.hpp" |
22 | #include "util/uid.hpp" |
23 | |
24 | #ifndef SCRIPTING_API |
25 | |
26 | #define SCRIPT_GUARD_VOID \ |
27 | auto object_ptr = get_object_ptr(); \ |
28 | if (object_ptr == nullptr) { \ |
29 | log_fatal << "error: script is accessing a dead object: " \ |
30 | << m_uid << std::endl; \ |
31 | return; \ |
32 | } \ |
33 | auto& object = *object_ptr |
34 | |
35 | #define SCRIPT_GUARD_DEFAULT \ |
36 | auto object_ptr = get_object_ptr(); \ |
37 | if (object_ptr == nullptr) { \ |
38 | log_fatal << "error: script is accessing a dead object: " \ |
39 | << m_uid << std::endl; \ |
40 | return {}; \ |
41 | } \ |
42 | auto& object = *object_ptr |
43 | |
44 | #define SCRIPT_GUARD_RETURN(x) \ |
45 | auto object_ptr = get_object_ptr(); \ |
46 | if (object_ptr == nullptr) { \ |
47 | log_fatal << "error: script is accessing a dead object: " \ |
48 | << m_uid << std::endl; \ |
49 | return x; \ |
50 | } \ |
51 | auto& object __attribute__((unused)) = *object_ptr |
52 | |
53 | class GameObjectManager; |
54 | |
55 | namespace scripting { |
56 | |
57 | GameObjectManager& get_game_object_manager(); |
58 | |
59 | template<class T> |
60 | class GameObject |
61 | { |
62 | public: |
63 | GameObject() : |
64 | m_uid() |
65 | {} |
66 | |
67 | GameObject(UID uid) : |
68 | m_uid(uid) |
69 | {} |
70 | |
71 | T* get_object_ptr() const |
72 | { |
73 | return get_game_object_manager().get_object_by_uid<T>(m_uid); |
74 | } |
75 | |
76 | protected: |
77 | UID m_uid; |
78 | }; |
79 | |
80 | } // namespace scripting |
81 | |
82 | #endif |
83 | |
84 | #endif |
85 | |
86 | /* EOF */ |
87 | |