1// SuperTux
2// Copyright (C) 2015 Hume2 <teratux.mail@gmail.com>
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
17#include "object/water_drop.hpp"
18
19#include "audio/sound_manager.hpp"
20#include "math/random.hpp"
21#include "object/sprite_particle.hpp"
22#include "sprite/sprite.hpp"
23#include "supertux/sector.hpp"
24
25WaterDrop::WaterDrop(const Vector& pos, const std::string& sprite_path_, const Vector& velocity) :
26 MovingSprite(pos, sprite_path_, LAYER_OBJECTS - 1, COLGROUP_MOVING_ONLY_STATIC),
27 physic(),
28 wd_state(WDS_FALLING),
29 sprite_path(sprite_path_)
30{
31 physic.enable_gravity(true);
32 physic.set_velocity(velocity);
33}
34
35void
36WaterDrop::update(float dt_sec)
37{
38 m_col.m_movement = physic.get_movement(dt_sec);
39
40 if ( m_sprite->animation_done() ) {
41 remove_me();
42 }
43}
44
45void
46WaterDrop::collision_solid(const CollisionHit& hit)
47{
48 if (hit.bottom && wd_state == WDS_FALLING) {
49 wd_state = WDS_SPLASH;
50 physic.enable_gravity(false);
51 SoundManager::current()->play("sounds/splash.ogg", get_pos());
52 m_sprite->set_action("splash", 1);
53
54 // spawn water particles
55 for (int i = 50; i; i--) {
56 int pa = graphicsRandom.rand(0,3);
57 float px = graphicsRandom.randf(m_col.m_bbox.get_left(), m_col.m_bbox.get_right());
58 float py = graphicsRandom.randf(m_col.m_bbox.get_top(), m_col.m_bbox.get_bottom());
59 Vector ppos = Vector(px, py);
60 Vector pspeed = ppos - m_col.m_bbox.get_middle();
61 pspeed.x *= 12;
62 pspeed.y *= 12;
63 Sector::get().add<SpriteParticle>(sprite_path, "particle_" + std::to_string(pa),
64 ppos, ANCHOR_MIDDLE,
65 pspeed, Vector(0, 100 * Sector::get().get_gravity()),
66 LAYER_OBJECTS+1);
67 }
68 }
69}
70
71HitResponse
72WaterDrop::collision(GameObject&, const CollisionHit& )
73{
74 return FORCE_MOVE;
75}
76
77/* EOF */
78