2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "object/camera.hpp"
19 #include "object/sprite_particle.hpp"
20 #include "supertux/main.hpp"
21 #include "supertux/sector.hpp"
23 SpriteParticle::SpriteParticle(std::string sprite_name, std::string action,
24 Vector position, AnchorPoint anchor, Vector velocity, Vector acceleration,
28 acceleration(acceleration),
29 drawing_layer(drawing_layer)
31 sprite = sprite_manager->create(sprite_name);
32 if (!sprite.get()) throw std::runtime_error("Could not load sprite "+sprite_name);
33 sprite->set_action(action, 1);
34 sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
36 this->position -= get_anchor_pos(sprite->get_current_hitbox(), anchor);
39 SpriteParticle::~SpriteParticle()
45 SpriteParticle::hit(Player& )
50 SpriteParticle::update(float elapsed_time)
52 // die when animation is complete
53 if (sprite->animation_done()) {
58 // calculate new position and velocity
59 position.x += velocity.x * elapsed_time;
60 position.y += velocity.y * elapsed_time;
61 velocity.x += acceleration.x * elapsed_time;
62 velocity.y += acceleration.y * elapsed_time;
64 // die when too far offscreen
65 Vector camera = Sector::current()->camera->get_translation();
66 if ((position.x < camera.x - 128) || (position.x > SCREEN_WIDTH + camera.x + 128) ||
67 (position.y < camera.y - 128) || (position.y > SCREEN_HEIGHT + camera.y + 128)) {
74 SpriteParticle::draw(DrawingContext& context)
76 sprite->draw(context, position, drawing_layer);