bullet.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 FIRE_BONUS:
  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 ICE_BONUS:
  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. m_col.set_movement(physic.get_movement(dt_sec));
  78. }
  79. void
  80. Bullet::draw(DrawingContext& context)
  81. {
  82. sprite->draw(context.color(), get_pos(), LAYER_OBJECTS);
  83. if (type == FIRE_BONUS){
  84. lightsprite->draw(context.light(), m_col.m_bbox.get_middle(), 0);
  85. }
  86. }
  87. void
  88. Bullet::collision_solid(const CollisionHit& hit)
  89. {
  90. if (hit.top || hit.bottom) {
  91. physic.set_velocity_y(-physic.get_velocity_y());
  92. life_count--;
  93. } else if (hit.left || hit.right) {
  94. if (type == ICE_BONUS) {
  95. physic.set_velocity_x(-physic.get_velocity_x());
  96. life_count--;
  97. } else
  98. remove_me();
  99. }
  100. }
  101. void
  102. Bullet::ricochet(GameObject& , const CollisionHit& hit)
  103. {
  104. collision_solid(hit);
  105. }
  106. HitResponse
  107. Bullet::collision(GameObject& , const CollisionHit& )
  108. {
  109. return FORCE_MOVE;
  110. }
  111. /* EOF */