Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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 WALKSPEED = 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 = WALKSPEED;
37   max_drop_height = 16;
38   sound_manager->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 = WALKSPEED;
47   max_drop_height = 16;
48   sound_manager->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     sound_manager->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 = systemRandom.randf(-M_PI_2, M_PI_2);
106       float velocity = systemRandom.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(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
112     }
113
114     return true;
115
116   }
117
118   //TODO: exception?
119   return true;
120 }
121
122 void
123 Stumpy::collision_solid(const CollisionHit& hit)
124 {
125   update_on_ground_flag(hit);
126
127   switch (mystate) {
128     case STATE_INVINCIBLE:
129       if(hit.top || hit.bottom) {
130         physic.set_velocity_y(0);
131       }
132       if(hit.left || hit.right) {
133         physic.set_velocity_x(0);
134       }
135       break;
136     case STATE_NORMAL:
137       WalkingBadguy::collision_solid(hit);
138       break;
139   }
140 }
141
142 HitResponse
143 Stumpy::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
144 {
145   switch (mystate) {
146     case STATE_INVINCIBLE:
147       if(hit.top || hit.bottom) {
148         physic.set_velocity_y(0);
149       }
150       if(hit.left || hit.right) {
151         physic.set_velocity_x(0);
152       }
153       return CONTINUE;
154       break;
155     case STATE_NORMAL:
156       return WalkingBadguy::collision_badguy(badguy, hit);
157       break;
158   }
159   return CONTINUE;
160 }
161
162 IMPLEMENT_FACTORY(Stumpy, "stumpy");
163
164 /* EOF */