unstable_tile.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SuperTux - Unstable Tile
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  4. // Copyright (C) 2010 Florian Forster <supertux at octo.it>
  5. // Copyright (C) 2021 A. Semphris <semphris@protonmail.com>
  6. // 2023 Vankata453
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #include "object/unstable_tile.hpp"
  21. #include "math/random.hpp"
  22. #include "object/explosion.hpp"
  23. #include "object/player.hpp"
  24. #include "sprite/sprite.hpp"
  25. #include "supertux/constants.hpp"
  26. #include "supertux/flip_level_transformer.hpp"
  27. #include "supertux/sector.hpp"
  28. static const float CRACK_TIME = 0.3f;
  29. static const float FALL_TIME = 0.8f;
  30. static const float RESPAWN_TIME = 5.f;
  31. static const float FADE_OUT_TIME = 1.f;
  32. static const float FADE_IN_TIME = .5f;
  33. static const float DELAY_IF_TUX = 0.001f;
  34. UnstableTile::UnstableTile(const ReaderMapping& mapping, int type) :
  35. MovingSprite(mapping, "images/objects/unstable_tile/snow.sprite", LAYER_TILES, COLGROUP_STATIC),
  36. physic(),
  37. state(STATE_NORMAL),
  38. slowfall_timer(),
  39. m_revive_timer(),
  40. m_respawn(),
  41. m_alpha(1.f),
  42. m_original_pos(m_col.get_pos()),
  43. m_fall_timer(),
  44. m_player_hit(false)
  45. {
  46. if (type >= 0)
  47. {
  48. m_type = type;
  49. on_type_change(TypeChange::INITIAL);
  50. }
  51. else
  52. {
  53. parse_type(mapping);
  54. }
  55. set_action("normal");
  56. physic.set_gravity_modifier(.98f);
  57. physic.enable_gravity(false);
  58. }
  59. GameObjectTypes
  60. UnstableTile::get_types() const
  61. {
  62. return {
  63. { "ice", _("Ice") },
  64. { "brick", _("Brick") },
  65. { "delayed", _("Delayed") }
  66. };
  67. }
  68. std::string
  69. UnstableTile::get_default_sprite_name() const
  70. {
  71. switch (m_type)
  72. {
  73. case BRICK:
  74. return "images/objects/unstable_tile/brick.sprite";
  75. case DELAYED:
  76. return "images/objects/skull_tile/skull_tile.sprite";
  77. default:
  78. return m_default_sprite_name;
  79. }
  80. }
  81. HitResponse
  82. UnstableTile::collision(GameObject& other, const CollisionHit& )
  83. {
  84. if (state == STATE_NORMAL)
  85. {
  86. Player* player = dynamic_cast<Player*>(&other);
  87. if (player != nullptr &&
  88. (player->get_bbox().get_bottom() < m_col.m_bbox.get_top() + SHIFT_DELTA ||
  89. player->get_bbox().get_top() < m_col.m_bbox.get_bottom() + SHIFT_DELTA))
  90. {
  91. if (m_type == DELAYED)
  92. m_player_hit = true;
  93. else
  94. shake();
  95. }
  96. if (m_type != DELAYED && dynamic_cast<Explosion*>(&other))
  97. {
  98. shake();
  99. }
  100. }
  101. return FORCE_MOVE;
  102. }
  103. void
  104. UnstableTile::shake()
  105. {
  106. if (state != STATE_NORMAL)
  107. return;
  108. if (m_sprite->has_action("shake"))
  109. {
  110. state = STATE_SHAKE;
  111. set_action("shake", /* loops = */ 1);
  112. }
  113. else
  114. {
  115. dissolve();
  116. }
  117. }
  118. void
  119. UnstableTile::dissolve()
  120. {
  121. if ((state != STATE_NORMAL) && (state != STATE_SHAKE))
  122. return;
  123. if (m_sprite->has_action("dissolve"))
  124. {
  125. state = STATE_DISSOLVE;
  126. set_action("dissolve", /* loops = */ 1);
  127. }
  128. else
  129. {
  130. slow_fall();
  131. }
  132. }
  133. void
  134. UnstableTile::slow_fall()
  135. {
  136. /* Only enter slow-fall if neither shake nor dissolve is available. */
  137. if (state != STATE_NORMAL)
  138. {
  139. fall_down();
  140. return;
  141. }
  142. if (m_sprite->has_action("fall-down"))
  143. {
  144. state = STATE_SLOWFALL;
  145. set_action("fall-down", /* loops = */ 1);
  146. physic.set_gravity_modifier(.10f);
  147. physic.enable_gravity(true);
  148. m_original_pos = m_col.get_pos();
  149. slowfall_timer = 0.5f; /* Fall slowly for half a second. */
  150. }
  151. else
  152. {
  153. state = STATE_FALL;
  154. }
  155. }
  156. void
  157. UnstableTile::fall_down()
  158. {
  159. if (state == STATE_FALL)
  160. return;
  161. state = STATE_FALL;
  162. const bool has_action = m_sprite->has_action("fall-down");
  163. if (m_type == DELAYED || has_action)
  164. {
  165. if (has_action)
  166. set_action("fall-down", /* loops = */ 1);
  167. physic.set_gravity_modifier(.98f);
  168. physic.enable_gravity(true);
  169. }
  170. }
  171. void
  172. UnstableTile::revive()
  173. {
  174. state = STATE_NORMAL;
  175. set_group(COLGROUP_STATIC);
  176. physic.enable_gravity(false);
  177. physic.set_velocity(Vector(0.0f, 0.0f));
  178. m_col.set_pos(m_original_pos);
  179. m_col.set_movement(Vector(0.0f, 0.0f));
  180. m_revive_timer.stop();
  181. m_respawn.reset(new FadeHelper(&m_alpha, FADE_IN_TIME, 1.f));
  182. set_action("normal");
  183. }
  184. void
  185. UnstableTile::update(float dt_sec)
  186. {
  187. if (m_respawn)
  188. {
  189. m_respawn->update(dt_sec);
  190. if (m_respawn->completed())
  191. m_respawn.reset();
  192. }
  193. switch (state)
  194. {
  195. case STATE_NORMAL:
  196. if (m_type != DELAYED)
  197. break;
  198. /** Manage DELAYED type (behaviour of the former SkullTile object). */
  199. if (m_player_hit)
  200. {
  201. set_action("mad");
  202. if (m_fall_timer.check())
  203. fall_down();
  204. else if (!m_fall_timer.started())
  205. m_fall_timer.start(FALL_TIME);
  206. else if (m_fall_timer.get_timegone() > CRACK_TIME) // Should perform shake animation.
  207. m_col.set_pos(m_original_pos + Vector(static_cast<float>(graphicsRandom.rand(-3, 3)), 0.f));
  208. }
  209. else
  210. {
  211. set_action("normal");
  212. m_fall_timer.stop();
  213. m_col.set_pos(m_original_pos);
  214. }
  215. m_player_hit = false; // To be updated next frame in collision().
  216. break;
  217. case STATE_SHAKE:
  218. if (m_sprite->animation_done())
  219. dissolve();
  220. break;
  221. case STATE_DISSOLVE:
  222. if (m_sprite->animation_done()) {
  223. /* dissolving is done. Set to non-solid. */
  224. set_group(COLGROUP_DISABLED);
  225. fall_down();
  226. }
  227. break;
  228. case STATE_SLOWFALL:
  229. if (slowfall_timer >= dt_sec)
  230. slowfall_timer -= dt_sec;
  231. else /* Switch to normal falling procedure */
  232. fall_down();
  233. m_col.set_movement(physic.get_movement(dt_sec));
  234. break;
  235. case STATE_FALL:
  236. // TODO: A state enum for when the tile is "dead"?
  237. m_alpha = std::max(m_alpha - dt_sec / FADE_OUT_TIME, 0.f);
  238. if (!m_revive_timer.started())
  239. {
  240. if (m_revive_timer.check())
  241. {
  242. if (Sector::get().is_free_of_movingstatics(Rectf(m_original_pos, get_bbox().get_size()).grown(-1.f)))
  243. {
  244. revive();
  245. }
  246. else
  247. {
  248. m_revive_timer.start(DELAY_IF_TUX);
  249. }
  250. }
  251. else
  252. {
  253. m_revive_timer.start(RESPAWN_TIME);
  254. }
  255. }
  256. else if (m_alpha > 0.f)
  257. {
  258. m_col.set_movement(physic.get_movement(dt_sec));
  259. }
  260. else
  261. {
  262. set_group(COLGROUP_DISABLED);
  263. }
  264. break;
  265. }
  266. }
  267. void
  268. UnstableTile::draw(DrawingContext& context)
  269. {
  270. // FIXME: This method is more future-proof, but more ugly than simply copying
  271. // the draw() function from MovingSprite
  272. context.push_transform();
  273. context.transform().alpha *= m_alpha;
  274. MovingSprite::draw(context);
  275. context.pop_transform();
  276. }
  277. void
  278. UnstableTile::on_flip(float height)
  279. {
  280. MovingObject::on_flip(height);
  281. m_original_pos.y = height - m_original_pos.y - get_bbox().get_height();
  282. FlipLevelTransformer::transform_flip(m_flip);
  283. }
  284. /* EOF */