weak_block.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SuperTux - Weak Block
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "object/weak_block.hpp"
  18. #include <math.h>
  19. #include "audio/sound_manager.hpp"
  20. #include "badguy/badguy.hpp"
  21. #include "math/random.hpp"
  22. #include "object/bullet.hpp"
  23. #include "supertux/flip_level_transformer.hpp"
  24. #include "supertux/globals.hpp"
  25. #include "supertux/sector.hpp"
  26. #include "sprite/sprite.hpp"
  27. #include "sprite/sprite_manager.hpp"
  28. #include "util/log.hpp"
  29. #include "util/reader_mapping.hpp"
  30. #include "util/writer.hpp"
  31. WeakBlock::WeakBlock(const ReaderMapping& mapping) :
  32. MovingSprite(mapping, "images/objects/weak_block/meltbox.sprite", LAYER_TILES, COLGROUP_STATIC),
  33. state(STATE_NORMAL),
  34. lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite"))
  35. {
  36. // Older levels utilize hardcoded behaviour from the "linked" property.
  37. if (get_version() == 1)
  38. {
  39. bool linked = true;
  40. mapping.get("linked", linked);
  41. // The object was set to the "meltbox" (ICE) sprite, if it's not linked.
  42. // The default sprite was previously the HAY one, so we do the opposite check here.
  43. if (linked)
  44. m_type = HAY;
  45. on_type_change(TypeChange::INITIAL);
  46. }
  47. else
  48. {
  49. parse_type(mapping);
  50. }
  51. lightsprite->set_blend(Blend::ADD);
  52. lightsprite->set_color(Color(0.3f, 0.2f, 0.1f));
  53. if (m_type == HAY)
  54. SoundManager::current()->preload("sounds/fire.ogg"); // TODO: Use own sound?
  55. else
  56. SoundManager::current()->preload("sounds/sizzle.ogg");
  57. set_action("normal");
  58. }
  59. void
  60. WeakBlock::update_version()
  61. {
  62. // Use ICE as default, when migrating from version 1.
  63. if (get_version() == 1)
  64. {
  65. m_type = ICE;
  66. on_type_change(m_type);
  67. }
  68. GameObject::update_version();
  69. }
  70. void
  71. WeakBlock::save(Writer& writer)
  72. {
  73. // If the version is 1 and the type is ICE, save "linked" as false.
  74. // Used to properly initialize the object as ICE when reading the saved level.
  75. if (get_version() == 1 && m_type == ICE)
  76. writer.write("linked", false);
  77. GameObject::save(writer);
  78. }
  79. GameObjectTypes
  80. WeakBlock::get_types() const
  81. {
  82. return {
  83. { "ice", _("Ice") },
  84. { "hay", _("Hay") }
  85. };
  86. }
  87. std::string
  88. WeakBlock::get_default_sprite_name() const
  89. {
  90. switch (m_type)
  91. {
  92. case HAY:
  93. return "images/objects/weak_block/strawbox.sprite";
  94. default:
  95. return m_default_sprite_name;
  96. }
  97. }
  98. HitResponse
  99. WeakBlock::collision_bullet(Bullet& bullet, const CollisionHit& hit)
  100. {
  101. switch (state) {
  102. case STATE_NORMAL:
  103. //Ensure only fire destroys weakblock
  104. if (bullet.get_type() == FIRE_BONUS) {
  105. startBurning();
  106. bullet.remove_me();
  107. }
  108. //Other bullets ricochet
  109. else {
  110. bullet.ricochet(*this, hit);
  111. }
  112. break;
  113. case STATE_BURNING:
  114. case STATE_DISINTEGRATING:
  115. break;
  116. default:
  117. log_debug << "unhandled state" << std::endl;
  118. break;
  119. }
  120. return FORCE_MOVE;
  121. }
  122. HitResponse
  123. WeakBlock::collision(GameObject& other, const CollisionHit& hit)
  124. {
  125. switch (state) {
  126. case STATE_NORMAL:
  127. if (auto bullet = dynamic_cast<Bullet*> (&other)) {
  128. return collision_bullet(*bullet, hit);
  129. }
  130. break;
  131. case STATE_BURNING:
  132. if (m_type != HAY)
  133. break;
  134. if (auto badguy = dynamic_cast<BadGuy*> (&other)) {
  135. badguy->ignite();
  136. }
  137. break;
  138. case STATE_DISINTEGRATING:
  139. break;
  140. default:
  141. log_debug << "unhandled state" << std::endl;
  142. break;
  143. }
  144. return FORCE_MOVE;
  145. }
  146. void
  147. WeakBlock::update(float )
  148. {
  149. switch (state) {
  150. case STATE_NORMAL:
  151. break;
  152. case STATE_BURNING:
  153. // cause burn light to flicker randomly
  154. if (m_type == HAY) {
  155. if (graphicsRandom.rand(10) >= 7) {
  156. lightsprite->set_color(Color(0.2f + graphicsRandom.randf(20.0f) / 100.0f,
  157. 0.1f + graphicsRandom.randf(20.0f)/100.0f,
  158. 0.1f));
  159. } else
  160. lightsprite->set_color(Color(0.3f, 0.2f, 0.1f));
  161. }
  162. if (m_sprite->animation_done()) {
  163. state = STATE_DISINTEGRATING;
  164. set_action("disintegrating", 1);
  165. spreadHit();
  166. set_group(COLGROUP_DISABLED);
  167. lightsprite = SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-tiny.sprite");
  168. lightsprite->set_blend(Blend::ADD);
  169. lightsprite->set_color(Color(0.3f, 0.2f, 0.1f));
  170. }
  171. break;
  172. case STATE_DISINTEGRATING:
  173. if (m_sprite->animation_done()) {
  174. remove_me();
  175. return;
  176. }
  177. break;
  178. }
  179. }
  180. void
  181. WeakBlock::draw(DrawingContext& context)
  182. {
  183. //Draw the Sprite just in front of other objects
  184. m_sprite->draw(context.color(), get_pos(), LAYER_OBJECTS + 10, m_flip);
  185. if (m_type == HAY && (state != STATE_NORMAL))
  186. {
  187. lightsprite->draw(context.light(), m_col.m_bbox.get_middle(), 0);
  188. }
  189. }
  190. void
  191. WeakBlock::startBurning()
  192. {
  193. if (state != STATE_NORMAL) return;
  194. state = STATE_BURNING;
  195. set_action("burning", 1);
  196. if (m_type == HAY)
  197. SoundManager::current()->play("sounds/fire.ogg", get_pos()); // TODO: Use own sound?
  198. else
  199. SoundManager::current()->play("sounds/sizzle.ogg", get_pos());
  200. }
  201. void
  202. WeakBlock::spreadHit()
  203. {
  204. //Destroy adjacent weakblocks if applicable
  205. if (m_type == HAY) {
  206. for (auto& wb : Sector::get().get_objects_by_type<WeakBlock>()) {
  207. if (&wb != this && wb.state == STATE_NORMAL)
  208. {
  209. const float dx = fabsf(wb.get_pos().x - m_col.m_bbox.get_left());
  210. const float dy = fabsf(wb.get_pos().y - m_col.m_bbox.get_top());
  211. if ((dx <= 32.5f) && (dy <= 32.5f)) {
  212. wb.startBurning();
  213. }
  214. }
  215. }
  216. }
  217. }
  218. void
  219. WeakBlock::on_flip(float height)
  220. {
  221. MovingSprite::on_flip(height);
  222. FlipLevelTransformer::transform_flip(m_flip);
  223. }
  224. std::vector<std::string>
  225. WeakBlock::get_patches() const
  226. {
  227. return { _("Sprites no longer define the behaviour of the object.\nObject types are used instead.") };
  228. }
  229. /* EOF */