More includes and some braces to build with gcc 4.3.0
[supertux.git] / src / badguy / ghosttree.cpp
1 //  $Id$
2 //
3 //  SuperTux - Boss "GhostTree"
4 //  Copyright (C) 2007 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 #include <config.h>
20 #include <algorithm>
21
22 #include "ghosttree.hpp"
23 #include "treewillowisp.hpp"
24 #include "sprite/sprite_manager.hpp"
25 #include "root.hpp"
26 #include "random_generator.hpp"
27 #include "object/lantern.hpp"
28
29 static const size_t WILLOWISP_COUNT = 10;
30 static const float ROOT_TOP_OFFSET = 64;
31 static const float WILLOWISP_TOP_OFFSET = -64;
32 static const Vector SUCK_TARGET_OFFSET = Vector(-16,-16);
33 static const float SUCK_TARGET_SPREAD = 8;
34
35 GhostTree::GhostTree(const lisp::Lisp& lisp)
36   : BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite",
37            LAYER_OBJECTS - 10), mystate(STATE_IDLE),
38     willo_spawn_y(0), willo_radius(200), willo_speed(1.8f), willo_color(0),
39     treecolor(0), suck_lantern(0)
40 {
41   glow_sprite.reset(sprite_manager->create("images/creatures/ghosttree/ghosttree-glow.sprite"));
42   set_colgroup_active(COLGROUP_TOUCHABLE);
43 }
44
45 GhostTree::~GhostTree()
46 {
47 }
48
49 void
50 GhostTree::die()
51 {
52   mystate = STATE_DYING;
53   sprite->set_action("dying", 1); 
54   glow_sprite->set_action("dying", 1); 
55
56   std::vector<TreeWillOWisp*>::iterator iter;
57   for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
58     TreeWillOWisp *willo = *iter;
59     willo->vanish();
60   }
61 }
62
63 void
64 GhostTree::activate()
65 {
66   willowisp_timer.start(1.0f, true);
67   colorchange_timer.start(13, true);
68   root_timer.start(5, true);
69 }
70
71 void
72 GhostTree::active_update(float elapsed_time)
73 {
74   (void) elapsed_time;
75
76   if (mystate == STATE_IDLE) {
77     if(colorchange_timer.check()) {
78       sound_manager->play("sounds/tree_howling.ogg", get_pos());
79       suck_timer.start(3);
80       treecolor = (treecolor + 1) % 3;
81
82       Color col;
83       switch(treecolor) {
84         case 0: col = Color(1, 0, 0); break;
85         case 1: col = Color(0, 1, 0); break;
86         case 2: col = Color(0, 0, 1); break;
87         case 3: col = Color(1, 1, 0); break;
88         case 4: col = Color(1, 0, 1); break;
89         case 5: col = Color(0, 1, 1); break;
90         default: assert(false);
91       }
92       glow_sprite->set_color(col);
93     }
94
95     if(suck_timer.check()) {
96       Color col = glow_sprite->get_color();
97       sound_manager->play("sounds/tree_suck.ogg", get_pos());
98       std::vector<TreeWillOWisp*>::iterator iter;
99       for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
100         TreeWillOWisp *willo = *iter;
101         if(willo->get_color() == col) {
102           willo->start_sucking(get_bbox().get_middle() + SUCK_TARGET_OFFSET + Vector(systemRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD), systemRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD)));
103         }
104       }
105       mystate = STATE_SUCKING;
106     }
107
108     if(willowisp_timer.check()) {
109       if(willowisps.size() < WILLOWISP_COUNT) {
110         Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
111         TreeWillOWisp *willowisp 
112                 = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
113
114         Sector::current()->add_object(willowisp);
115         willowisps.push_back(willowisp);
116
117         willo_spawn_y -= 40;
118         if(willo_spawn_y < -160)
119           willo_spawn_y = 0;
120
121         willo_radius += 20;
122         if(willo_radius > 120)
123           willo_radius = 0;
124
125         if(willo_speed == 1.8f) {
126           willo_speed = 1.5f;
127         } else {
128           willo_speed = 1.8f;
129         }
130
131         do {
132           willo_color = (willo_color + 1) % 3;
133         } while(willo_color == treecolor);
134
135         switch(willo_color) {
136           case 0: willowisp->set_color(Color(1, 0, 0)); break;
137           case 1: willowisp->set_color(Color(0, 1, 0)); break;
138           case 2: willowisp->set_color(Color(0, 0, 1)); break;
139           case 3: willowisp->set_color(Color(1, 1, 0)); break;
140           case 4: willowisp->set_color(Color(1, 0, 1)); break;
141           case 5: willowisp->set_color(Color(0, 1, 1)); break;
142           default: assert(false);
143         }
144       }
145     }
146
147     if(root_timer.check()) {
148       /* TODO indicate root with an animation */
149       Player* player = get_nearest_player();
150       Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
151       Sector::current()->add_object(root);
152     }
153   } else if (mystate == STATE_SWALLOWING) {
154     if (suck_lantern) {
155       // suck in lantern
156       assert (suck_lantern);
157       Vector pos = suck_lantern->get_pos();
158       Vector delta = get_bbox().get_middle() + SUCK_TARGET_OFFSET - pos;
159       Vector dir = delta.unit();
160       if (delta.norm() < 1) {
161         dir = delta;
162         suck_lantern->ungrab(*this, RIGHT);
163         suck_lantern->remove_me();
164         suck_lantern = 0;
165         sprite->set_action("swallow", 1); 
166       } else {
167         pos += dir;
168         suck_lantern->grab(*this, pos, RIGHT);
169       }
170     } else {
171       // wait until lantern is swallowed
172       if (sprite->animation_done()) {
173         if (is_color_deadly(suck_lantern_color)) {
174           die();
175         } else {
176           sprite->set_action("default");
177           mystate = STATE_IDLE;
178           spawn_lantern();
179         }
180       }
181     }
182   }
183 }
184
185 bool 
186 GhostTree::is_color_deadly(Color color) const {
187   if (color == Color(0,0,0)) return false;
188   Color my_color = glow_sprite->get_color();
189   return ((my_color.red != color.red) || (my_color.green != color.green) || (my_color.blue != color.blue));
190 }
191
192 void
193 GhostTree::willowisp_died(TreeWillOWisp *willowisp)
194 {
195   if ((mystate == STATE_SUCKING) && (willowisp->was_sucked)) {
196     mystate = STATE_IDLE;
197   }
198   willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
199 }
200
201 void
202 GhostTree::draw(DrawingContext& context)
203 {
204   BadGuy::draw(context);
205
206   context.push_target();
207   context.push_transform();
208   context.set_target(DrawingContext::LIGHTMAP);
209   if (mystate == STATE_SUCKING) {
210     context.set_alpha(0.5 + fmodf(game_time, 0.5));
211   } else {
212     context.set_alpha(0.5);
213   }
214   glow_sprite->draw(context, get_pos(), layer);
215   context.pop_transform();
216   context.pop_target();
217 }
218
219 bool
220 GhostTree::collides(GameObject& other, const CollisionHit& ) {
221   if (mystate != STATE_SUCKING) return false;
222   if (dynamic_cast<Lantern*>(&other)) return true;
223   if (dynamic_cast<Player*>(&other)) return true;
224   return false;
225 }
226
227 HitResponse
228 GhostTree::collision(GameObject& other, const CollisionHit& ) {
229   if(mystate != STATE_SUCKING) return ABORT_MOVE;
230
231   Player* player = dynamic_cast<Player*>(&other);
232   if (player) {
233     player->kill(false);
234   }
235
236   Lantern* lantern = dynamic_cast<Lantern*>(&other);
237   if (lantern) {
238     suck_lantern = lantern;
239     suck_lantern->grab(*this, suck_lantern->get_pos(), RIGHT);
240     suck_lantern_color = lantern->get_color();
241     mystate = STATE_SWALLOWING;
242   }
243
244   return ABORT_MOVE;
245 }
246
247 void
248 GhostTree::spawn_lantern() {
249   Lantern* lantern = new Lantern(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
250   Sector::current()->add_object(lantern);
251 }
252
253 IMPLEMENT_FACTORY(GhostTree, "ghosttree");
254