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