bullet.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/bullet.hpp"
  17. #include "math/random.hpp"
  18. #include "object/camera.hpp"
  19. #include "sprite/sprite.hpp"
  20. #include "sprite/sprite_manager.hpp"
  21. #include "supertux/direction.hpp"
  22. #include "supertux/sector.hpp"
  23. #include "video/video_system.hpp"
  24. #include "video/viewport.hpp"
  25. Bullet::Bullet(const Vector& pos, const Vector& xm, Direction dir, BonusType type_, Player& player) :
  26. m_player(player),
  27. physic(),
  28. life_count(3),
  29. sprite(),
  30. lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite")),
  31. type(type_)
  32. {
  33. physic.set_velocity(xm);
  34. switch (type) {
  35. case BONUS_FIRE:
  36. sprite = SpriteManager::current()->create("images/objects/bullets/firebullet.sprite");
  37. lightsprite->set_blend(Blend::ADD);
  38. lightsprite->set_color(Color(0.3f, 0.1f, 0.0f));
  39. break;
  40. case BONUS_ICE:
  41. sprite = SpriteManager::current()->create("images/objects/bullets/icebullet.sprite");
  42. break;
  43. default:
  44. log_warning << "Bullet::Bullet called with unknown BonusType" << std::endl;
  45. life_count = 10;
  46. sprite = SpriteManager::current()->create("images/objects/bullets/firebullet.sprite");
  47. break;
  48. }
  49. m_col.m_bbox.set_pos(pos);
  50. m_col.m_bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
  51. }
  52. void
  53. Bullet::update(float dt_sec)
  54. {
  55. // Cause fireball color to flicker randomly.
  56. if (graphicsRandom.rand(5) != 0) {
  57. lightsprite->set_color(Color(0.3f + graphicsRandom.randf(10) / 100.0f,
  58. 0.1f + graphicsRandom.randf(20.0f) / 100.0f,
  59. graphicsRandom.randf(10.0f) / 100.0f));
  60. } else
  61. lightsprite->set_color(Color(0.3f, 0.1f, 0.0f));
  62. if (life_count <= 0)
  63. {
  64. remove_me();
  65. return;
  66. }
  67. float scroll_x = Sector::get().get_camera().get_translation().x;
  68. float scroll_y = Sector::get().get_camera().get_translation().y;
  69. float scale = Sector::get().get_camera().get_current_scale();
  70. if (get_pos().x < scroll_x ||
  71. get_pos().x > scroll_x + static_cast<float>(SCREEN_WIDTH) / scale ||
  72. // get_pos().y < scroll_y ||
  73. get_pos().y > scroll_y + static_cast<float>(SCREEN_HEIGHT) / scale) {
  74. remove_me();
  75. return;
  76. }
  77. bool in_water = !Sector::get().is_free_of_tiles(get_bbox(), true, Tile::WATER);
  78. physic.set_gravity_modifier(in_water ? 0.3f : 1.f);
  79. m_col.set_movement(physic.get_movement(dt_sec) * (in_water ? 0.5f : 1.f));
  80. }
  81. void
  82. Bullet::draw(DrawingContext& context)
  83. {
  84. sprite->draw(context.color(), get_pos(), LAYER_OBJECTS);
  85. if (type == BONUS_FIRE){
  86. lightsprite->draw(context.light(), m_col.m_bbox.get_middle(), 0);
  87. }
  88. }
  89. void
  90. Bullet::collision_solid(const CollisionHit& hit)
  91. {
  92. if (hit.top || hit.bottom) {
  93. physic.set_velocity_y(-physic.get_velocity_y());
  94. life_count--;
  95. } else if (hit.left || hit.right) {
  96. if (type == BONUS_ICE) {
  97. physic.set_velocity_x(-physic.get_velocity_x());
  98. life_count--;
  99. } else
  100. remove_me();
  101. }
  102. }
  103. void
  104. Bullet::ricochet(GameObject& , const CollisionHit& hit)
  105. {
  106. collision_solid(hit);
  107. }
  108. HitResponse
  109. Bullet::collision(MovingObject& , const CollisionHit& )
  110. {
  111. return FORCE_MOVE;
  112. }
  113. /* EOF */