1 | // SuperTux |
2 | // Copyright (C) 2006 Matthias Braun <matze@braunis.de> |
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/scripted_object.hpp" |
18 | |
19 | #include "editor/editor.hpp" |
20 | #include "math/random.hpp" |
21 | #include "object/player.hpp" |
22 | #include "sprite/sprite.hpp" |
23 | #include "supertux/sector.hpp" |
24 | #include "util/reader.hpp" |
25 | #include "util/reader_mapping.hpp" |
26 | |
27 | ScriptedObject::ScriptedObject(const ReaderMapping& mapping) : |
28 | MovingSprite(mapping, "images/objects/bonus_block/brick.sprite" , LAYER_OBJECTS, COLGROUP_MOVING_STATIC), |
29 | ExposedObject<ScriptedObject, scripting::ScriptedObject>(this), |
30 | physic(), |
31 | solid(), |
32 | physic_enabled(), |
33 | visible(), |
34 | hit_script(), |
35 | new_vel_set(false), |
36 | new_vel(), |
37 | new_size() |
38 | { |
39 | m_default_sprite_name = {}; |
40 | |
41 | if (!Editor::is_active()) { |
42 | if (m_name.empty()) { |
43 | m_name = "unnamed" + std::to_string(graphicsRandom.rand()); |
44 | log_warning << "Scripted object must have a name specified, setting to: " << m_name << std::endl; |
45 | } |
46 | } |
47 | |
48 | mapping.get("solid" , solid, true); |
49 | mapping.get("physic-enabled" , physic_enabled, true); |
50 | mapping.get("visible" , visible, true); |
51 | mapping.get("hit-script" , hit_script, "" ); |
52 | m_layer = reader_get_layer(mapping, LAYER_OBJECTS); |
53 | if ( solid ){ |
54 | set_group( COLGROUP_MOVING_STATIC ); |
55 | } else { |
56 | set_group( COLGROUP_DISABLED ); |
57 | } |
58 | } |
59 | ObjectSettings |
60 | ScriptedObject::get_settings() |
61 | { |
62 | new_size.x = m_col.m_bbox.get_width(); |
63 | new_size.y = m_col.m_bbox.get_height(); |
64 | |
65 | ObjectSettings result = MovingSprite::get_settings(); |
66 | |
67 | result.add_int(_("Z-pos" ), &m_layer, "z-pos" , LAYER_OBJECTS); |
68 | //result.add_float("width", &new_size.x, "width", OPTION_HIDDEN); |
69 | //result.add_float("height", &new_size.y, "height", OPTION_HIDDEN); |
70 | result.add_bool(_("Solid" ), &solid, "solid" , true); |
71 | result.add_bool(_("Physics enabled" ), &physic_enabled, "physic-enabled" , true); |
72 | result.add_bool(_("Visible" ), &visible, "visible" , true); |
73 | result.add_text(_("Hit script" ), &hit_script, "hit-script" ); |
74 | |
75 | result.reorder({"z-pos" , "visible" , "physic-enabled" , "solid" , "name" , "sprite" , "script" , "button" , "x" , "y" }); |
76 | |
77 | return result; |
78 | } |
79 | |
80 | void |
81 | ScriptedObject::move(float x, float y) |
82 | { |
83 | m_col.m_bbox.move(Vector(x, y)); |
84 | } |
85 | |
86 | float |
87 | ScriptedObject::get_pos_x() const |
88 | { |
89 | return get_pos().x; |
90 | } |
91 | |
92 | float |
93 | ScriptedObject::get_pos_y() const |
94 | { |
95 | return get_pos().y; |
96 | } |
97 | |
98 | void |
99 | ScriptedObject::set_velocity(float x, float y) |
100 | { |
101 | new_vel = Vector(x, y); |
102 | new_vel_set = true; |
103 | } |
104 | |
105 | float |
106 | ScriptedObject::get_velocity_x() const |
107 | { |
108 | return physic.get_velocity_x(); |
109 | } |
110 | |
111 | float |
112 | ScriptedObject::get_velocity_y() const |
113 | { |
114 | return physic.get_velocity_y(); |
115 | } |
116 | |
117 | void |
118 | ScriptedObject::set_visible(bool visible_) |
119 | { |
120 | visible = visible_; |
121 | } |
122 | |
123 | bool |
124 | ScriptedObject::is_visible() const |
125 | { |
126 | return visible; |
127 | } |
128 | |
129 | void |
130 | ScriptedObject::set_solid(bool solid_) |
131 | { |
132 | solid = solid_; |
133 | if ( solid ){ |
134 | set_group( COLGROUP_MOVING_STATIC ); |
135 | } else { |
136 | set_group( COLGROUP_DISABLED ); |
137 | } |
138 | } |
139 | |
140 | bool |
141 | ScriptedObject::is_solid() const |
142 | { |
143 | return solid; |
144 | } |
145 | |
146 | bool |
147 | ScriptedObject::gravity_enabled() const |
148 | { |
149 | return physic.gravity_enabled(); |
150 | } |
151 | |
152 | void |
153 | ScriptedObject::enable_gravity(bool f) |
154 | { |
155 | physic.enable_gravity(f); |
156 | } |
157 | |
158 | void |
159 | ScriptedObject::set_action(const std::string& animation) |
160 | { |
161 | m_sprite->set_action(animation); |
162 | } |
163 | |
164 | std::string |
165 | ScriptedObject::get_action() const |
166 | { |
167 | return m_sprite->get_action(); |
168 | } |
169 | |
170 | void |
171 | ScriptedObject::update(float dt_sec) |
172 | { |
173 | if (!physic_enabled) |
174 | return; |
175 | |
176 | if (new_vel_set) { |
177 | physic.set_velocity(new_vel.x, new_vel.y); |
178 | new_vel_set = false; |
179 | } |
180 | m_col.m_movement = physic.get_movement(dt_sec); |
181 | } |
182 | |
183 | void |
184 | ScriptedObject::draw(DrawingContext& context) |
185 | { |
186 | if (!visible) |
187 | return; |
188 | |
189 | m_sprite->draw(context.color(), get_pos(), m_layer); |
190 | } |
191 | |
192 | void |
193 | ScriptedObject::collision_solid(const CollisionHit& hit) |
194 | { |
195 | if (!physic_enabled) |
196 | return; |
197 | |
198 | if (hit.bottom) { |
199 | if (physic.get_velocity_y() > 0) |
200 | physic.set_velocity_y(0); |
201 | } else if (hit.top) { |
202 | physic.set_velocity_y(.1f); |
203 | } |
204 | |
205 | if (hit.left || hit.right) { |
206 | physic.set_velocity_x(0); |
207 | } |
208 | } |
209 | |
210 | HitResponse |
211 | ScriptedObject::collision(GameObject& other, const CollisionHit& ) |
212 | { |
213 | auto player = dynamic_cast<Player*> (&other); |
214 | if (player && !hit_script.empty()) { |
215 | Sector::get().run_script(hit_script, "hit-script" ); |
216 | } |
217 | |
218 | return FORCE_MOVE; |
219 | } |
220 | |
221 | /* EOF */ |
222 | |