scripted_object.cpp 4.7 KB

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