Added new "Explosion" object
authorChristoph Sommer <mail@christoph-sommer.de>
Wed, 13 Jun 2007 19:43:08 +0000 (19:43 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Wed, 13 Jun 2007 19:43:08 +0000 (19:43 +0000)
SVN-Revision: 5104

data/images/objects/explosion/explosion-0.png [new file with mode: 0644]
data/images/objects/explosion/explosion-1.png [new file with mode: 0644]
data/images/objects/explosion/explosion.sprite [new file with mode: 0644]
src/badguy/bomb.cpp
src/badguy/mrbomb.cpp
src/object/explosion.cpp [new file with mode: 0644]
src/object/explosion.hpp [new file with mode: 0644]

diff --git a/data/images/objects/explosion/explosion-0.png b/data/images/objects/explosion/explosion-0.png
new file mode 100644 (file)
index 0000000..f735278
Binary files /dev/null and b/data/images/objects/explosion/explosion-0.png differ
diff --git a/data/images/objects/explosion/explosion-1.png b/data/images/objects/explosion/explosion-1.png
new file mode 100644 (file)
index 0000000..51341b9
Binary files /dev/null and b/data/images/objects/explosion/explosion-1.png differ
diff --git a/data/images/objects/explosion/explosion.sprite b/data/images/objects/explosion/explosion.sprite
new file mode 100644 (file)
index 0000000..289c9e8
--- /dev/null
@@ -0,0 +1,18 @@
+(supertux-sprite
+  (action
+    (name "default")
+    (fps 15.0)
+    (hitbox 18 16 71 80)
+    (images 
+      "explosion-0.png"
+      "explosion-1.png"
+      "explosion-0.png"
+      "explosion-1.png"
+      "explosion-0.png"
+      "explosion-1.png"
+      "explosion-0.png"
+      "explosion-1.png"
+    )
+  )
+)
+
index 670baa5..38ec13b 100644 (file)
@@ -22,6 +22,7 @@
 #include "bomb.hpp"
 #include "random_generator.hpp"
 #include "object/sprite_particle.hpp"
+#include "object/explosion.hpp"
 
 Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "images/creatures/mr_bomb/mr_bomb.sprite"*/ )
        : BadGuy( pos, dir, custom_sprite )
@@ -103,24 +104,12 @@ void
 Bomb::explode()
 {
   ticking->stop();
-  state = STATE_EXPLODING;
-  set_group(COLGROUP_TOUCHABLE);
-  sound_manager->play("sounds/explosion.wav", get_pos());
-  set_action("explosion", 1, ANCHOR_BOTTOM);
 
-  // spawn some particles
-  // TODO: provide convenience function in MovingSprite or MovingObject?
-  for (int i = 0; i < 100; i++) {
-    Vector ppos = bbox.get_middle();
-    float angle = systemRandom.randf(-M_PI_2, M_PI_2);
-    float velocity = systemRandom.randf(450, 900);
-    float vx = sin(angle)*velocity;
-    float vy = -cos(angle)*velocity;
-    Vector pspeed = Vector(vx, vy);
-    Vector paccel = Vector(0, 1000);
-    Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
-  }
+  remove_me();
+  Explosion* explosion = new Explosion(get_bbox().get_middle());
+  Sector::current()->add_object(explosion);
 
+  run_dead_script();
 }
 
 void
index 74a0d7e..4ebe6fb 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "mrbomb.hpp"
 #include "bomb.hpp"
+#include "object/explosion.hpp"
 #include "sprite/sprite_manager.hpp"
 
 MrBomb::MrBomb(const lisp::Lisp& reader)
@@ -96,9 +97,8 @@ void
 MrBomb::kill_fall()
 {
   remove_me();
-  Bomb* bomb = new Bomb(get_pos(), dir, sprite_name );
-  Sector::current()->add_object(bomb);
-  bomb->explode();
+  Explosion* explosion = new Explosion(get_bbox().get_middle());
+  Sector::current()->add_object(explosion);
 
   run_dead_script();
 }
diff --git a/src/object/explosion.cpp b/src/object/explosion.cpp
new file mode 100644 (file)
index 0000000..5d64ca2
--- /dev/null
@@ -0,0 +1,112 @@
+//  $Id$
+//
+//  SuperTux -- Explosion object
+//  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#include <config.h>
+
+#include "explosion.hpp"
+#include "badguy/badguy.hpp"
+#include "object/sprite_particle.hpp"
+
+#include "resources.hpp"
+#include "video/drawing_context.hpp"
+#include "sprite/sprite_manager.hpp"
+#include "player.hpp"
+#include "sector.hpp"
+#include "player_status.hpp"
+#include "gameobjs.hpp"
+#include "statistics.hpp"
+#include "object_factory.hpp"
+#include "level.hpp"
+#include "random_generator.hpp"
+#include "audio/sound_source.hpp"
+#include "audio/sound_manager.hpp"
+#include "timer.hpp"
+
+Explosion::Explosion(const Vector& pos)
+       : MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_BACKGROUNDTILES+10, COLGROUP_TOUCHABLE), state(STATE_WAITING)
+{
+  sound_manager->preload("sounds/explosion.wav");
+  set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
+}
+
+Explosion::Explosion(const lisp::Lisp& reader)
+       : MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_BACKGROUNDTILES+10, COLGROUP_TOUCHABLE), state(STATE_WAITING)
+{
+  sound_manager->preload("sounds/explosion.wav");
+}
+
+void
+Explosion::explode()
+{
+  if (state != STATE_WAITING) return;
+  state = STATE_EXPLODING;
+
+  set_action("default", 1);
+  sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
+  sound_manager->play("sounds/explosion.wav", get_pos());
+
+  // spawn some particles
+  // TODO: provide convenience function in MovingSprite or MovingObject?
+  for (int i = 0; i < 100; i++) {
+    Vector ppos = bbox.get_middle();
+    float angle = systemRandom.randf(-M_PI_2, M_PI_2);
+    float velocity = systemRandom.randf(450, 900);
+    float vx = sin(angle)*velocity;
+    float vy = -cos(angle)*velocity;
+    Vector pspeed = Vector(vx, vy);
+    Vector paccel = Vector(0, 1000);
+    Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+  }
+}
+
+void 
+Explosion::update(float )
+{
+  switch(state) {
+    case STATE_WAITING:
+      explode();
+      break;
+    case STATE_EXPLODING:
+      if(sprite->animation_done()) {
+       remove_me();
+      }
+      break;
+  }
+}
+
+HitResponse
+Explosion::collision(GameObject& other, const CollisionHit& )
+{
+  if(state != STATE_EXPLODING) return ABORT_MOVE;
+
+  Player* player = dynamic_cast<Player*>(&other);
+  if(player != 0) {
+    player->kill(false);
+  }
+
+  BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
+  if(badguy != 0) {
+    badguy->kill_fall();
+  }
+
+  return ABORT_MOVE;
+}
+
+IMPLEMENT_FACTORY(Explosion, "explosion");
+
diff --git a/src/object/explosion.hpp b/src/object/explosion.hpp
new file mode 100644 (file)
index 0000000..4ae40e4
--- /dev/null
@@ -0,0 +1,57 @@
+//  $Id$
+//
+//  SuperTux -- Explosion object
+//  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#ifndef __EXPLOSION_H__
+#define __EXPLOSION_H__
+
+#include "moving_sprite.hpp"
+#include "lisp/lisp.hpp"
+
+/**
+ * Just your average explosion - goes boom, hurts Tux
+ */
+class Explosion : public MovingSprite
+{
+public:
+  /**
+   * Create new Explosion centered(!) at @c pos
+   */
+  Explosion(const Vector& pos);
+  Explosion(const lisp::Lisp& reader);
+
+  void update(float elapsed_time);
+  HitResponse collision(GameObject& other, const CollisionHit& hit);
+
+protected:
+  /**
+   * plays sound, starts animation
+   */
+  void explode();
+
+private:
+  enum State {
+    STATE_WAITING,
+    STATE_EXPLODING
+  };
+  State state;
+
+};
+
+#endif
+