73dfc6b192591253172fcf337357a736948e0598
[supertux.git] / src / badguy / stumpy.cpp
1 //  $Id$
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_treehit.ogg");
36 }
37
38 Stumpy::Stumpy(const Vector& pos, Direction d)
39   : WalkingBadguy(pos, d, "images/creatures/mr_tree/stumpy.sprite","left","right"), mystate(STATE_INVINCIBLE)
40 {
41   walk_speed = WALKSPEED;
42   max_drop_height = 16;
43   sound_manager->preload("sounds/mr_treehit.ogg");
44   invincible_timer.start(INVINCIBLE_TIME);
45 }
46
47
48 void
49 Stumpy::write(lisp::Writer& writer)
50 {
51   writer.start_list("stumpy");
52   WalkingBadguy::write(writer);
53   writer.end_list("stumpy");
54 }
55
56 void
57 Stumpy::initialize()
58 {
59   switch (mystate) {
60     case STATE_INVINCIBLE:
61       sprite->set_action(dir == LEFT ? "dizzy-left" : "dizzy-right");
62       bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
63       physic.set_velocity_x(0);
64       break;
65     case STATE_NORMAL:
66       WalkingBadguy::initialize();
67       break;
68   }
69 }
70
71 void
72 Stumpy::active_update(float elapsed_time)
73 {
74   switch (mystate) {
75     case STATE_INVINCIBLE:
76       if (invincible_timer.check()) {
77         mystate = STATE_NORMAL;
78         WalkingBadguy::initialize();
79       }
80       BadGuy::active_update(elapsed_time);
81       break;
82     case STATE_NORMAL:
83       WalkingBadguy::active_update(elapsed_time);
84       break;
85   }
86 }
87
88 bool
89 Stumpy::collision_squished(GameObject& object)
90 {
91
92   // if we're still invincible, we ignore the hit
93   if (mystate == STATE_INVINCIBLE) {
94     sound_manager->play("sounds/mr_treehit.ogg", get_pos());
95     Player* player = dynamic_cast<Player*>(&object);
96     if (player) player->bounce(*this);
97     return true;
98   }
99
100   // if we can die, we do
101   if (mystate == STATE_NORMAL) {
102     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
103     set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
104     kill_squished(object);
105     // spawn some particles
106     // TODO: provide convenience function in MovingSprite or MovingObject?
107     for (int i = 0; i < 25; i++) {
108       Vector ppos = bbox.get_middle();
109       float angle = systemRandom.randf(-M_PI_2, M_PI_2);
110       float velocity = systemRandom.randf(45, 90);
111       float vx = sin(angle)*velocity;
112       float vy = -cos(angle)*velocity;
113       Vector pspeed = Vector(vx, vy);
114       Vector paccel = Vector(0, 100);
115       Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, 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 IMPLEMENT_FACTORY(Stumpy, "stumpy")