Revert "Proposed fix for coverity #29372" because of causing Segmentation
[supertux.git] / src / badguy / skydive.cpp
1 //  SuperTux
2 //  Copyright (C) 2010 Florian Forster <supertux at octo.it>
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/skydive.hpp"
18
19 #include "supertux/constants.hpp"
20 #include "supertux/sector.hpp"
21 #include "object/anchor_point.hpp"
22 #include "object/player.hpp"
23 #include "object/explosion.hpp"
24
25 SkyDive::SkyDive(const Reader& reader) :
26   BadGuy(reader, "images/creatures/skydive/skydive.sprite"),
27   is_grabbed(false)
28 {
29 }
30
31 SkyDive::SkyDive(const Vector& pos, Direction d) :
32   BadGuy(pos, d, "images/creatures/skydive/skydive.sprite"),
33   is_grabbed(false)
34 {
35 }
36
37 void
38 SkyDive::collision_solid(const CollisionHit& hit)
39 {
40   if (hit.bottom) {
41     explode ();
42     return;
43   }
44
45   if (hit.left || hit.right)
46     physic.set_velocity_x (0.0);
47 } /* void collision_solid */
48
49 HitResponse
50 SkyDive::collision_badguy(BadGuy&, const CollisionHit& hit)
51 {
52   if (hit.bottom) {
53     explode ();
54     return (ABORT_MOVE);
55   }
56
57   return (FORCE_MOVE);
58 } /* HitResponse collision_badguy */
59
60 void
61 SkyDive::grab (MovingObject&, const Vector& pos, Direction dir_)
62 {
63   movement = pos - get_pos();
64   this->dir = dir_;
65
66   is_grabbed = true;
67
68   physic.set_velocity_x (movement.x * LOGICAL_FPS);
69   physic.set_velocity_y (0.0);
70   physic.set_acceleration_y (0.0);
71   physic.enable_gravity (false);
72   set_colgroup_active (COLGROUP_DISABLED);
73 }
74
75 void
76 SkyDive::ungrab (MovingObject& , Direction)
77 {
78   is_grabbed = false;
79
80   physic.set_velocity_y (0);
81   physic.set_acceleration_y (0);
82   physic.enable_gravity (true);
83   set_colgroup_active (COLGROUP_MOVING);
84 }
85
86 HitResponse
87 SkyDive::collision_player(Player&, const CollisionHit& hit)
88 {
89   if (hit.bottom) {
90     explode ();
91     return (ABORT_MOVE);
92   }
93
94   return FORCE_MOVE;
95 } /* HitResponse collision_player */
96
97 bool
98 SkyDive::collision_squished (GameObject& obj)
99 {
100   Player *player = dynamic_cast<Player *> (&obj);
101   if (player) {
102     player->bounce (*this);
103     return (false);
104   }
105
106   explode ();
107   return (false);
108 } /* bool collision_squished */
109
110 void
111 SkyDive::active_update (float elapsed_time)
112 {
113   if (!is_grabbed)
114     movement = physic.get_movement(elapsed_time);
115 } /* void active_update */
116
117 void
118 SkyDive::explode (void)
119 {
120   if (!is_valid())
121     return;
122
123   auto explosion = std::make_shared<Explosion>(get_anchor_pos (bbox, ANCHOR_BOTTOM));
124
125   explosion->hurts(true);
126   explosion->pushes(false);
127   Sector::current()->add_object(explosion);
128
129   remove_me ();
130 } /* void explode */
131
132 /* vim: set sw=2 sts=2 et fdm=marker : */
133 /* EOF */