Replaced Ref and RefCounter with std::shared_ptr<>
[supertux.git] / src / badguy / stumpy.cpp
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/stumpy.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "math/random_generator.hpp"
21 #include "object/player.hpp"
22 #include "object/sprite_particle.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25
26 #include <math.h>
27
28 static const float STUMPY_SPEED = 120;
29 static const float INVINCIBLE_TIME = 1;
30
31 Stumpy::Stumpy(const Reader& reader) :
32   WalkingBadguy(reader, "images/creatures/mr_tree/stumpy.sprite","left","right"),
33   mystate(STATE_NORMAL),
34   invincible_timer()
35 {
36   walk_speed = STUMPY_SPEED;
37   max_drop_height = 16;
38   SoundManager::current()->preload("sounds/mr_treehit.ogg");
39 }
40
41 Stumpy::Stumpy(const Vector& pos, Direction d) :
42   WalkingBadguy(pos, d, "images/creatures/mr_tree/stumpy.sprite","left","right"),
43   mystate(STATE_INVINCIBLE),
44   invincible_timer()
45 {
46   walk_speed = STUMPY_SPEED;
47   max_drop_height = 16;
48   SoundManager::current()->preload("sounds/mr_treehit.ogg");
49   invincible_timer.start(INVINCIBLE_TIME);
50 }
51
52 void
53 Stumpy::initialize()
54 {
55   switch (mystate) {
56     case STATE_INVINCIBLE:
57       sprite->set_action(dir == LEFT ? "dizzy-left" : "dizzy-right");
58       bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
59       physic.set_velocity_x(0);
60       break;
61     case STATE_NORMAL:
62       WalkingBadguy::initialize();
63       break;
64   }
65 }
66
67 void
68 Stumpy::active_update(float elapsed_time)
69 {
70   switch (mystate) {
71     case STATE_INVINCIBLE:
72       if (invincible_timer.check()) {
73         mystate = STATE_NORMAL;
74         WalkingBadguy::initialize();
75       }
76       BadGuy::active_update(elapsed_time);
77       break;
78     case STATE_NORMAL:
79       WalkingBadguy::active_update(elapsed_time);
80       break;
81   }
82 }
83
84 bool
85 Stumpy::collision_squished(GameObject& object)
86 {
87
88   // if we're still invincible, we ignore the hit
89   if (mystate == STATE_INVINCIBLE) {
90     SoundManager::current()->play("sounds/mr_treehit.ogg", get_pos());
91     Player* player = dynamic_cast<Player*>(&object);
92     if (player) player->bounce(*this);
93     return true;
94   }
95
96   // if we can die, we do
97   if (mystate == STATE_NORMAL) {
98     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
99     set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
100     kill_squished(object);
101     // spawn some particles
102     // TODO: provide convenience function in MovingSprite or MovingObject?
103     for (int i = 0; i < 25; i++) {
104       Vector ppos = bbox.get_middle();
105       float angle = graphicsRandom.randf(-M_PI_2, M_PI_2);
106       float velocity = graphicsRandom.randf(45, 90);
107       float vx = sin(angle)*velocity;
108       float vy = -cos(angle)*velocity;
109       Vector pspeed = Vector(vx, vy);
110       Vector paccel = Vector(0, 100);
111       Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/bark.sprite",
112                                                                      "default",
113                                                                      ppos, ANCHOR_MIDDLE,
114                                                                      pspeed, paccel,
115                                                                      LAYER_OBJECTS-1));
116     }
117
118     return true;
119
120   }
121
122   //TODO: exception?
123   return true;
124 }
125
126 void
127 Stumpy::collision_solid(const CollisionHit& hit)
128 {
129   update_on_ground_flag(hit);
130
131   switch (mystate) {
132     case STATE_INVINCIBLE:
133       if(hit.top || hit.bottom) {
134         physic.set_velocity_y(0);
135       }
136       if(hit.left || hit.right) {
137         physic.set_velocity_x(0);
138       }
139       break;
140     case STATE_NORMAL:
141       WalkingBadguy::collision_solid(hit);
142       break;
143   }
144 }
145
146 HitResponse
147 Stumpy::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
148 {
149   switch (mystate) {
150     case STATE_INVINCIBLE:
151       if(hit.top || hit.bottom) {
152         physic.set_velocity_y(0);
153       }
154       if(hit.left || hit.right) {
155         physic.set_velocity_x(0);
156       }
157       return CONTINUE;
158       break;
159     case STATE_NORMAL:
160       return WalkingBadguy::collision_badguy(badguy, hit);
161       break;
162   }
163   return CONTINUE;
164 }
165
166 bool
167 Stumpy::is_freezable() const
168 {
169   return true;
170 }
171
172 /* EOF */