7654292c2fa32da3f161c4edd4f5239526228ba6
[supertux.git] / src / badguy / stumpy.cpp
1 //  $Id: stumpy.cpp 3980 2006-07-10 19:55:56Z sommer $
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "stumpy.hpp"
23 #include "poisonivy.hpp"
24 #include "random_generator.hpp"
25 #include "object/sprite_particle.hpp"
26
27 static const float WALKSPEED = 120;
28 static const float INVINCIBLE_TIME = 1;
29
30 Stumpy::Stumpy(const lisp::Lisp& reader)
31   : WalkingBadguy(reader, "images/creatures/mr_tree/stumpy.sprite","left","right"), mystate(STATE_NORMAL)
32 {
33   walk_speed = WALKSPEED;
34   max_drop_height = 16;
35   sound_manager->preload("sounds/mr_tree.ogg");
36   sound_manager->preload("sounds/mr_treehit.ogg");
37 }
38
39 Stumpy::Stumpy(const Vector& pos, Direction d)
40   : WalkingBadguy(pos, d, "images/creatures/mr_tree/stumpy.sprite","left","right"), mystate(STATE_INVINCIBLE)
41 {
42   walk_speed = WALKSPEED;
43   max_drop_height = 16;
44   sound_manager->preload("sounds/mr_treehit.ogg");
45   invincible_timer.start(INVINCIBLE_TIME);
46 }
47
48
49 void
50 Stumpy::write(lisp::Writer& writer)
51 {
52   writer.start_list("stumpy");
53   WalkingBadguy::write(writer);
54   writer.end_list("stumpy");
55 }
56
57 void
58 Stumpy::activate()
59 {
60   switch (mystate) {
61     case STATE_INVINCIBLE:
62       sprite->set_action(dir == LEFT ? "dizzy-left" : "dizzy-right");
63       bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
64       physic.vx = 0;
65       break;
66     case STATE_NORMAL:
67       WalkingBadguy::activate();
68       break;
69   }
70 }
71
72 void
73 Stumpy::active_update(float elapsed_time)
74 {
75   switch (mystate) {
76     case STATE_INVINCIBLE:
77       if (invincible_timer.check()) {
78         mystate = STATE_NORMAL;
79         WalkingBadguy::activate();
80       }
81       BadGuy::active_update(elapsed_time);
82       break;
83     case STATE_NORMAL:
84       WalkingBadguy::active_update(elapsed_time);
85       break;
86   }
87 }
88
89 bool
90 Stumpy::collision_squished(GameObject& object)
91 {
92
93   // if we're still invincible, we ignore the hit
94   if (mystate == STATE_INVINCIBLE) {
95     sound_manager->play("sounds/mr_treehit.ogg", get_pos());
96     Player* player = dynamic_cast<Player*>(&object);
97     if (player) player->bounce(*this);
98     return true;
99   }
100
101   // if we can die, we do
102   if (mystate == STATE_NORMAL) {
103     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
104     set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
105     kill_squished(object);
106     // spawn some particles
107     // TODO: provide convenience function in MovingSprite or MovingObject?
108     for (int i = 0; i < 25; i++) {
109       Vector ppos = bbox.get_middle();
110       float angle = systemRandom.randf(-M_PI_2, M_PI_2);
111       float velocity = systemRandom.randf(45, 90);
112       float vx = sin(angle)*velocity;
113       float vy = -cos(angle)*velocity;
114       Vector pspeed = Vector(vx, vy);
115       Vector paccel = Vector(0, 100);
116       Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
117     }
118
119     return true;
120
121   }
122
123   //TODO: exception?
124   return true;
125 }
126
127 void
128 Stumpy::collision_solid(const CollisionHit& hit)
129 {
130   update_on_ground_flag(hit);
131
132   switch (mystate) {
133     case STATE_INVINCIBLE:
134       if(hit.top || hit.bottom) {
135         physic.vy = 0;
136       }
137       if(hit.left || hit.right) {
138         physic.vx = 0;
139       }
140       break;
141     case STATE_NORMAL:
142       WalkingBadguy::collision_solid(hit);
143       break;
144   }
145 }
146
147 HitResponse
148 Stumpy::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
149 {
150   switch (mystate) {
151     case STATE_INVINCIBLE:
152       if(hit.top || hit.bottom) {
153         physic.vy = 0;
154       }
155       if(hit.left || hit.right) {
156         physic.vx = 0;
157       }
158       return CONTINUE;
159       break;
160     case STATE_NORMAL:
161       return WalkingBadguy::collision_badguy(badguy, hit);
162       break;
163   }
164   return CONTINUE;
165 }
166
167 IMPLEMENT_FACTORY(Stumpy, "stumpy")