Add some more sound_manager->preloads into object constructors
[supertux.git] / src / badguy / mrtree.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 "mrtree.hpp"
23 #include "poisonivy.hpp"
24
25 static const float WALKSPEED = 100;
26 static const float WALKSPEED_SMALL = 120;
27 static const float INVINCIBLE_TIME = 1;
28
29 static const float POISONIVY_WIDTH = 32;
30 static const float POISONIVY_HEIGHT = 32;
31 static const float POISONIVY_Y_OFFSET = 24;
32
33
34 MrTree::MrTree(const lisp::Lisp& reader)
35   : BadGuy(reader, "images/creatures/mr_tree/mr_tree.sprite"), mystate(STATE_BIG)
36 {
37   sprite->set_action(dir == LEFT ? "large-left" : "large-right");
38   sound_manager->preload("sounds/mr_tree.ogg");
39   sound_manager->preload("sounds/mr_treehit.ogg");
40 }
41
42 void
43 MrTree::write(lisp::Writer& writer)
44 {
45   writer.start_list("mrtree");
46
47   writer.write_float("x", start_position.x);
48   writer.write_float("y", start_position.y);
49
50   writer.end_list("mrtree");
51 }
52
53 void
54 MrTree::activate()
55 {
56   if (mystate == STATE_BIG) {
57     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
58     sprite->set_action(dir == LEFT ? "large-left" : "large-right");
59     return;
60   }
61   if (mystate == STATE_INVINCIBLE) {
62     physic.set_velocity_x(0);
63     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
64     return;
65   }
66   if (mystate == STATE_NORMAL) {
67     physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
68     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
69     return;
70   }
71 }
72
73 void
74 MrTree::active_update(float elapsed_time)
75 {
76   if ((mystate == STATE_INVINCIBLE) && (invincible_timer.check())) {
77     mystate = STATE_NORMAL;
78     activate();
79   }
80
81   if (might_fall())
82   {
83     dir = (dir == LEFT ? RIGHT : LEFT);
84     activate();
85   }
86
87   BadGuy::active_update(elapsed_time);
88 }
89
90 bool
91 MrTree::collision_squished(Player& player)
92 {
93   // if we're big, we shrink
94   if(mystate == STATE_BIG) {
95     mystate = STATE_INVINCIBLE;
96     invincible_timer.start(INVINCIBLE_TIME);
97
98     float old_x_offset = sprite->get_current_hitbox_x_offset();
99     float old_y_offset = sprite->get_current_hitbox_y_offset();
100     activate();
101
102     // shrink bounding box and adjust sprite position to where the stump once was
103     bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
104     Vector pos = get_pos();
105     pos.x += sprite->get_current_hitbox_x_offset() - old_x_offset;
106     pos.y += sprite->get_current_hitbox_y_offset() - old_y_offset;
107     set_pos(pos);
108
109     sound_manager->play("sounds/mr_tree.ogg", get_pos());
110     player.bounce(*this);
111
112     Vector leaf1_pos = Vector(pos.x - POISONIVY_WIDTH - 1, pos.y - POISONIVY_Y_OFFSET);
113     Rect leaf1_bbox = Rect(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT);
114     if (Sector::current()->is_free_space(leaf1_bbox)) {
115       PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1, LEFT);
116       Sector::current()->add_object(leaf1);
117     }
118     Vector leaf2_pos = Vector(pos.x + sprite->get_current_hitbox_width() + 1, pos.y - POISONIVY_Y_OFFSET);
119     Rect leaf2_bbox = Rect(leaf2_pos.x, leaf2_pos.y, leaf2_pos.x + POISONIVY_WIDTH, leaf2_pos.y + POISONIVY_HEIGHT);
120     if (Sector::current()->is_free_space(leaf2_bbox)) {
121       PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1, RIGHT);
122       Sector::current()->add_object(leaf2);
123     }
124
125     return true;
126   }
127
128   // if we're still invincible, we ignore the hit
129   if (mystate == STATE_INVINCIBLE) {
130     sound_manager->play("sounds/mr_treehit.ogg", get_pos());
131     player.bounce(*this);
132     return true;
133   }
134
135   // if we're small, we die
136   if (mystate == STATE_NORMAL) {
137     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
138     bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
139     kill_squished(player);
140     return true;
141   }
142
143   //TODO: exception?
144   return true;
145 }
146
147 HitResponse
148 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
149 {
150   if(fabsf(hit.normal.y) > .5) {
151     physic.set_velocity_y(0);
152   } else {
153     dir = dir == LEFT ? RIGHT : LEFT;
154     activate();
155   }
156
157   return CONTINUE;
158 }
159
160 HitResponse
161 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
162 {
163   if(fabsf(hit.normal.x) > .8) { // left or right hit
164     dir = dir == LEFT ? RIGHT : LEFT;
165     activate();
166   }
167
168   return CONTINUE;
169 }
170
171 IMPLEMENT_FACTORY(MrTree, "mrtree")
172