1// SuperTux
2// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 "badguy/flyingsnowball.hpp"
18
19#include "math/random.hpp"
20#include "object/sprite_particle.hpp"
21#include "object/player.hpp"
22#include "sprite/sprite.hpp"
23#include "supertux/sector.hpp"
24
25namespace {
26const float PUFF_INTERVAL_MIN = 4.0f; /**< spawn new puff of smoke at most that often */
27const float PUFF_INTERVAL_MAX = 8.0f; /**< spawn new puff of smoke at least that often */
28}
29
30FlyingSnowBall::FlyingSnowBall(const ReaderMapping& reader) :
31 BadGuy(reader, "images/creatures/flying_snowball/flying_snowball.sprite"),
32 normal_propeller_speed(),
33 puff_timer()
34{
35 m_physic.enable_gravity(true);
36}
37
38void
39FlyingSnowBall::initialize()
40{
41 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
42}
43
44void
45FlyingSnowBall::activate()
46{
47 puff_timer.start(static_cast<float>(gameRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX)));
48 normal_propeller_speed = gameRandom.randf(0.95f, 1.05f);
49}
50
51bool
52FlyingSnowBall::collision_squished(GameObject& object)
53{
54 m_sprite->set_action(m_dir == Direction::LEFT ? "squished-left" : "squished-right");
55 m_physic.set_acceleration_y(0);
56 m_physic.set_velocity_y(0);
57 kill_squished(object);
58 return true;
59}
60
61void
62FlyingSnowBall::collision_solid(const CollisionHit& hit)
63{
64 if (hit.top || hit.bottom) {
65 m_physic.set_velocity_y(0);
66 }
67}
68
69void
70FlyingSnowBall::active_update(float dt_sec)
71{
72
73 const float grav = Sector::get().get_gravity() * 100.0f;
74 if (get_pos().y > m_start_position.y + 2*32) {
75
76 // Flying too low - increased propeller speed
77 m_physic.set_acceleration_y(-grav*1.2f);
78
79 m_physic.set_velocity_y(m_physic.get_velocity_y() * 0.99f);
80
81 } else if (get_pos().y < m_start_position.y - 2*32) {
82
83 // Flying too high - decreased propeller speed
84 m_physic.set_acceleration_y(-grav*0.8f);
85
86 m_physic.set_velocity_y(m_physic.get_velocity_y() * 0.99f);
87
88 } else {
89
90 // Flying at acceptable altitude - normal propeller speed
91 m_physic.set_acceleration_y(-grav*normal_propeller_speed);
92
93 }
94
95 m_col.m_movement=m_physic.get_movement(dt_sec);
96
97 auto player = get_nearest_player();
98 if (player) {
99 m_dir = (player->get_pos().x > get_pos().x) ? Direction::RIGHT : Direction::LEFT;
100 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
101 }
102
103 // spawn smoke puffs
104 if (puff_timer.check()) {
105 Vector ppos = m_col.m_bbox.get_middle();
106 Vector pspeed = Vector(gameRandom.randf(-10, 10), 150);
107 Vector paccel = Vector(0,0);
108 Sector::get().add<SpriteParticle>("images/objects/particles/smoke.sprite",
109 "default",
110 ppos, ANCHOR_MIDDLE, pspeed, paccel,
111 LAYER_OBJECTS-1);
112 puff_timer.start(gameRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
113
114 normal_propeller_speed = gameRandom.randf(0.95f, 1.05f);
115 m_physic.set_velocity_y(m_physic.get_velocity_y() - 50);
116 }
117}
118
119/* EOF */
120