New GameObject SpriteParticle
authorChristoph Sommer <mail@christoph-sommer.de>
Mon, 1 May 2006 19:26:44 +0000 (19:26 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Mon, 1 May 2006 19:26:44 +0000 (19:26 +0000)
SVN-Revision: 3482

12 files changed:
data/images/objects/particles/smoke-1.png [new file with mode: 0644]
data/images/objects/particles/smoke-2.png [new file with mode: 0644]
data/images/objects/particles/smoke-3.png [new file with mode: 0644]
data/images/objects/particles/smoke-4.png [new file with mode: 0644]
data/images/objects/particles/smoke-5.png [new file with mode: 0644]
data/images/objects/particles/smoke-6.png [new file with mode: 0644]
data/images/objects/particles/smoke.sprite [new file with mode: 0644]
src/badguy/flyingsnowball.cpp
src/badguy/flyingsnowball.hpp
src/object/sprite_particle.cpp [new file with mode: 0644]
src/object/sprite_particle.hpp [new file with mode: 0644]
src/sprite/sprite.hpp

diff --git a/data/images/objects/particles/smoke-1.png b/data/images/objects/particles/smoke-1.png
new file mode 100644 (file)
index 0000000..92526ad
Binary files /dev/null and b/data/images/objects/particles/smoke-1.png differ
diff --git a/data/images/objects/particles/smoke-2.png b/data/images/objects/particles/smoke-2.png
new file mode 100644 (file)
index 0000000..ea91786
Binary files /dev/null and b/data/images/objects/particles/smoke-2.png differ
diff --git a/data/images/objects/particles/smoke-3.png b/data/images/objects/particles/smoke-3.png
new file mode 100644 (file)
index 0000000..0c135ac
Binary files /dev/null and b/data/images/objects/particles/smoke-3.png differ
diff --git a/data/images/objects/particles/smoke-4.png b/data/images/objects/particles/smoke-4.png
new file mode 100644 (file)
index 0000000..471e8a2
Binary files /dev/null and b/data/images/objects/particles/smoke-4.png differ
diff --git a/data/images/objects/particles/smoke-5.png b/data/images/objects/particles/smoke-5.png
new file mode 100644 (file)
index 0000000..e85753b
Binary files /dev/null and b/data/images/objects/particles/smoke-5.png differ
diff --git a/data/images/objects/particles/smoke-6.png b/data/images/objects/particles/smoke-6.png
new file mode 100644 (file)
index 0000000..370506c
Binary files /dev/null and b/data/images/objects/particles/smoke-6.png differ
diff --git a/data/images/objects/particles/smoke.sprite b/data/images/objects/particles/smoke.sprite
new file mode 100644 (file)
index 0000000..30fe859
--- /dev/null
@@ -0,0 +1,12 @@
+(supertux-sprite
+ (action
+  (images 
+    "smoke-1.png"
+    "smoke-2.png"
+    "smoke-3.png"
+    "smoke-4.png"
+    "smoke-5.png"
+    "smoke-6.png"
+  )
+ )
+)
index a9ef465..e7bd28a 100644 (file)
 #include <stdio.h>
 
 #include "flyingsnowball.hpp"
+#include "random_generator.hpp"
+#include "object/sprite_particle.hpp"
 
 static const float FLYTIME = 1.0;
 static const float FLYSPEED = 100.0;
 
+namespace {
+  const float PUFF_PROBABILITY = 0.1; /**< chanche of puffs being spawned in the current cycle */
+  const float PUFF_INTERVAL_MIN = 0.1; /**< spawn new puff of smoke at most that often */
+  const float PUFF_INTERVAL_MAX = 1.1; /**< spawn new puff of smoke at least that often */
+}
+
 FlyingSnowBall::FlyingSnowBall(const lisp::Lisp& reader)
 {
   reader.get("x", start_position.x);
@@ -62,6 +70,7 @@ FlyingSnowBall::activate()
   mode = FLY_UP;
   physic.set_velocity_y(FLYSPEED);
   timer.start(FLYTIME/2);
+  puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
 }
 
 bool
@@ -89,9 +98,19 @@ FlyingSnowBall::active_update(float elapsed_time)
     if(mode == FLY_UP) {
       mode = FLY_DOWN;
       physic.set_velocity_y(-FLYSPEED);
+
+      // stop puffing
+      puff_timer.stop();
+      
     } else if(mode == FLY_DOWN) {
       mode = FLY_UP;
       physic.set_velocity_y(FLYSPEED);
+
+      // roll a dice whether to start puffing
+      if (systemRandom.randf(0, 1) < PUFF_PROBABILITY) {
+        puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
+      }
+
     }
     timer.start(FLYTIME);
   }
@@ -102,6 +121,15 @@ FlyingSnowBall::active_update(float elapsed_time)
     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
     sprite->set_action(dir == LEFT ? "left" : "right");
   }
+
+  // spawn smoke puffs
+  if (puff_timer.check()) {
+    Vector ppos = bbox.get_middle();
+    Vector pspeed = Vector(systemRandom.randf(-10, 10), 150);
+    Vector paccel = Vector(0,0);
+    Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", ppos, pspeed, paccel, LAYER_OBJECTS-1));
+    puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
+  }
 }
 
 IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball")
index 7762c96..c9fa3fc 100644 (file)
@@ -41,6 +41,7 @@ protected:
   bool collision_squished(Player& player);
 private:
   Timer timer;
+  Timer puff_timer; /**< time until the next smoke puff is spawned */
 };
 
 #endif
diff --git a/src/object/sprite_particle.cpp b/src/object/sprite_particle.cpp
new file mode 100644 (file)
index 0000000..c166253
--- /dev/null
@@ -0,0 +1,72 @@
+//  $Id: rainsplash.cpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $
+//
+//  SuperTux
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
+//  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.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 "sprite_particle.hpp"
+#include "sector.hpp"
+#include "camera.hpp"
+#include "main.hpp"
+
+SpriteParticle::SpriteParticle(std::string sprite_name, Vector position, Vector velocity, Vector acceleration, int drawing_layer) 
+       : position(position), velocity(velocity), acceleration(acceleration), drawing_layer(drawing_layer)
+{
+  sprite = sprite_manager->create(sprite_name);
+  if (!sprite) throw std::runtime_error("Could not load sprite "+sprite_name);
+  sprite->set_animation_loops(1);
+}
+  
+SpriteParticle::~SpriteParticle() 
+{
+  remove_me();
+}
+
+void
+SpriteParticle::hit(Player& )
+{
+}
+
+void
+SpriteParticle::update(float elapsed_time) 
+{
+  // die when animation is complete
+  if (sprite->animation_done()) {
+    remove_me();
+    return;
+  }
+
+  // calculate new position and velocity
+  position.x += velocity.x * elapsed_time;
+  position.y += velocity.y * elapsed_time;
+  velocity.x += acceleration.x * elapsed_time;
+  velocity.y += acceleration.y * elapsed_time;
+
+  // die when too far offscreen
+  Vector camera = Sector::current()->camera->get_translation();
+  if ((position.x < camera.x - 128) || (position.x > SCREEN_WIDTH + camera.x + 128) || 
+      (position.y < camera.y - 128) || (position.y > SCREEN_HEIGHT + camera.y + 128)) {
+    remove_me();
+    return;
+  }
+}
+
+void
+SpriteParticle::draw(DrawingContext& context) 
+{
+   sprite->draw(context, position, drawing_layer);
+}
diff --git a/src/object/sprite_particle.hpp b/src/object/sprite_particle.hpp
new file mode 100644 (file)
index 0000000..160cdc0
--- /dev/null
@@ -0,0 +1,50 @@
+//  $Id: rainsplash.hpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $
+//
+//  SuperTux
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
+//  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.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 __SPRITE_PARTICLE_H__
+#define __SPRITE_PARTICLE_H__
+
+
+#include "game_object.hpp"
+#include "resources.hpp"
+#include "player.hpp"
+#include "sprite/sprite.hpp"
+#include "sprite/sprite_manager.hpp"
+#include "video/drawing_context.hpp"
+
+class SpriteParticle : public GameObject
+{
+public:
+  SpriteParticle(std::string sprite_name, Vector position, Vector velocity, Vector acceleration, int drawing_layer = LAYER_OBJECTS-1);
+  ~SpriteParticle();
+protected:  
+  virtual void hit(Player& player);
+  virtual void update(float elapsed_time);
+  virtual void draw(DrawingContext& context);
+private:
+  Sprite* sprite;
+  Vector position;
+  Vector velocity;
+  Vector acceleration;
+  int drawing_layer;
+};
+
+#endif
+
index 9c138e2..b558cd1 100644 (file)
@@ -46,6 +46,10 @@ public:
   /** Set action (or state) */
   void set_action(const std::string& act, int loops = -1);
 
+  /** Set number of animation cycles until animation stops */
+  void set_animation_loops(int loops = -1)
+  { animation_loops = loops; }
+
   /** Set framerate */
   void set_fps(float new_fps);