From: Christoph Sommer Date: Mon, 1 May 2006 19:26:44 +0000 (+0000) Subject: New GameObject SpriteParticle X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=16262ff3cac7fdd1de8dc46a967f93cce902ed7f;p=supertux.git New GameObject SpriteParticle SVN-Revision: 3482 --- diff --git a/data/images/objects/particles/smoke-1.png b/data/images/objects/particles/smoke-1.png new file mode 100644 index 000000000..92526adc5 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 index 000000000..ea91786a6 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 index 000000000..0c135ac32 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 index 000000000..471e8a23b 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 index 000000000..e85753b66 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 index 000000000..370506cc8 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 index 000000000..30fe8597b --- /dev/null +++ b/data/images/objects/particles/smoke.sprite @@ -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" + ) + ) +) diff --git a/src/badguy/flyingsnowball.cpp b/src/badguy/flyingsnowball.cpp index a9ef465dc..e7bd28a80 100644 --- a/src/badguy/flyingsnowball.cpp +++ b/src/badguy/flyingsnowball.cpp @@ -22,10 +22,18 @@ #include #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") diff --git a/src/badguy/flyingsnowball.hpp b/src/badguy/flyingsnowball.hpp index 7762c966d..c9fa3fca3 100644 --- a/src/badguy/flyingsnowball.hpp +++ b/src/badguy/flyingsnowball.hpp @@ -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 index 000000000..c166253d4 --- /dev/null +++ b/src/object/sprite_particle.cpp @@ -0,0 +1,72 @@ +// $Id: rainsplash.cpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $ +// +// SuperTux +// Copyright (C) 2006 Matthias Braun +// Copyright (C) 2006 Christoph Sommer +// +// 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 index 000000000..160cdc060 --- /dev/null +++ b/src/object/sprite_particle.hpp @@ -0,0 +1,50 @@ +// $Id: rainsplash.hpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $ +// +// SuperTux +// Copyright (C) 2006 Matthias Braun +// Copyright (C) 2006 Christoph Sommer +// +// 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 + diff --git a/src/sprite/sprite.hpp b/src/sprite/sprite.hpp index 9c138e200..b558cd1fc 100644 --- a/src/sprite/sprite.hpp +++ b/src/sprite/sprite.hpp @@ -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);