Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / ghosttree.cpp
1 //  SuperTux - Boss "GhostTree"
2 //  Copyright (C) 2007 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/ghosttree.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "badguy/root.hpp"
21 #include "badguy/treewillowisp.hpp"
22 #include "math/random_generator.hpp"
23 #include "object/lantern.hpp"
24 #include "object/player.hpp"
25 #include "sprite/sprite.hpp"
26 #include "sprite/sprite_manager.hpp"
27 #include "supertux/object_factory.hpp"
28 #include "supertux/sector.hpp"
29
30 #include <algorithm>
31 #include <math.h>
32
33 static const size_t WILLOWISP_COUNT = 10;
34 static const float ROOT_TOP_OFFSET = 64;
35 static const float WILLOWISP_TOP_OFFSET = -64;
36 static const Vector SUCK_TARGET_OFFSET = Vector(-16,-16);
37 static const float SUCK_TARGET_SPREAD = 8;
38
39 GhostTree::GhostTree(const Reader& lisp) :
40   BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite", LAYER_OBJECTS - 10), 
41   mystate(STATE_IDLE),
42   willowisp_timer(),
43   willo_spawn_y(0),
44   willo_radius(200), 
45   willo_speed(1.8f), 
46   willo_color(0),
47   glow_sprite(),
48   colorchange_timer(),
49   suck_timer(),
50   root_timer(),
51   treecolor(0), 
52   suck_lantern_color(),
53   suck_lantern(0),
54   willowisps()
55 {
56   glow_sprite = sprite_manager->create("images/creatures/ghosttree/ghosttree-glow.sprite");
57   set_colgroup_active(COLGROUP_TOUCHABLE);
58   sound_manager->preload("sounds/tree_howling.ogg");
59   sound_manager->preload("sounds/tree_suck.ogg");
60 }
61
62 GhostTree::~GhostTree()
63 {
64 }
65
66 void
67 GhostTree::die()
68 {
69   mystate = STATE_DYING;
70   sprite->set_action("dying", 1); 
71   glow_sprite->set_action("dying", 1); 
72
73   std::vector<TreeWillOWisp*>::iterator iter;
74   for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
75     TreeWillOWisp *willo = *iter;
76     willo->vanish();
77   }
78 }
79
80 void
81 GhostTree::activate()
82 {
83   willowisp_timer.start(1.0f, true);
84   colorchange_timer.start(13, true);
85   root_timer.start(5, true);
86 }
87
88 void
89 GhostTree::active_update(float elapsed_time)
90 {
91   (void) elapsed_time;
92
93   if (mystate == STATE_IDLE) {
94     if(colorchange_timer.check()) {
95       sound_manager->play("sounds/tree_howling.ogg", get_pos());
96       suck_timer.start(3);
97       treecolor = (treecolor + 1) % 3;
98
99       Color col;
100       switch(treecolor) {
101         case 0: col = Color(1, 0, 0); break;
102         case 1: col = Color(0, 1, 0); break;
103         case 2: col = Color(0, 0, 1); break;
104         case 3: col = Color(1, 1, 0); break;
105         case 4: col = Color(1, 0, 1); break;
106         case 5: col = Color(0, 1, 1); break;
107         default: assert(false);
108       }
109       glow_sprite->set_color(col);
110     }
111
112     if(suck_timer.check()) {
113       Color col = glow_sprite->get_color();
114       sound_manager->play("sounds/tree_suck.ogg", get_pos());
115       std::vector<TreeWillOWisp*>::iterator iter;
116       for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
117         TreeWillOWisp *willo = *iter;
118         if(willo->get_color() == col) {
119           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)));
120         }
121       }
122       mystate = STATE_SUCKING;
123     }
124
125     if(willowisp_timer.check()) {
126       if(willowisps.size() < WILLOWISP_COUNT) {
127         Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
128         TreeWillOWisp *willowisp 
129           = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
130
131         Sector::current()->add_object(willowisp);
132         willowisps.push_back(willowisp);
133
134         willo_spawn_y -= 40;
135         if(willo_spawn_y < -160)
136           willo_spawn_y = 0;
137
138         willo_radius += 20;
139         if(willo_radius > 120)
140           willo_radius = 0;
141
142         if(willo_speed == 1.8f) {
143           willo_speed = 1.5f;
144         } else {
145           willo_speed = 1.8f;
146         }
147
148         do {
149           willo_color = (willo_color + 1) % 3;
150         } while(willo_color == treecolor);
151
152         switch(willo_color) {
153           case 0: willowisp->set_color(Color(1, 0, 0)); break;
154           case 1: willowisp->set_color(Color(0, 1, 0)); break;
155           case 2: willowisp->set_color(Color(0, 0, 1)); break;
156           case 3: willowisp->set_color(Color(1, 1, 0)); break;
157           case 4: willowisp->set_color(Color(1, 0, 1)); break;
158           case 5: willowisp->set_color(Color(0, 1, 1)); break;
159           default: assert(false);
160         }
161       }
162     }
163
164     if(root_timer.check()) {
165       /* TODO indicate root with an animation */
166       Player* player = get_nearest_player();
167       if (player) {
168         Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
169         Sector::current()->add_object(root);
170       }
171     }
172   } else if (mystate == STATE_SWALLOWING) {
173     if (suck_lantern) {
174       // suck in lantern
175       assert (suck_lantern);
176       Vector pos = suck_lantern->get_pos();
177       Vector delta = get_bbox().get_middle() + SUCK_TARGET_OFFSET - pos;
178       Vector dir = delta.unit();
179       if (delta.norm() < 1) {
180         dir = delta;
181         suck_lantern->ungrab(*this, RIGHT);
182         suck_lantern->remove_me();
183         suck_lantern = 0;
184         sprite->set_action("swallow", 1); 
185       } else {
186         pos += dir;
187         suck_lantern->grab(*this, pos, RIGHT);
188       }
189     } else {
190       // wait until lantern is swallowed
191       if (sprite->animation_done()) {
192         if (is_color_deadly(suck_lantern_color)) {
193           die();
194         } else {
195           sprite->set_action("default");
196           mystate = STATE_IDLE;
197           spawn_lantern();
198         }
199       }
200     }
201   }
202 }
203
204 bool 
205 GhostTree::is_color_deadly(Color color) const {
206   if (color == Color(0,0,0)) return false;
207   Color my_color = glow_sprite->get_color();
208   return ((my_color.red != color.red) || (my_color.green != color.green) || (my_color.blue != color.blue));
209 }
210
211 void
212 GhostTree::willowisp_died(TreeWillOWisp *willowisp)
213 {
214   if ((mystate == STATE_SUCKING) && (willowisp->was_sucked)) {
215     mystate = STATE_IDLE;
216   }
217   willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
218 }
219
220 void
221 GhostTree::draw(DrawingContext& context)
222 {
223   BadGuy::draw(context);
224
225   context.push_target();
226   context.push_transform();
227   context.set_target(DrawingContext::LIGHTMAP);
228   if (mystate == STATE_SUCKING) {
229     context.set_alpha(0.5 + fmodf(game_time, 0.5));
230   } else {
231     context.set_alpha(0.5);
232   }
233   glow_sprite->draw(context, get_pos(), layer);
234   context.pop_transform();
235   context.pop_target();
236 }
237
238 bool
239 GhostTree::collides(GameObject& other, const CollisionHit& ) {
240   if (mystate != STATE_SUCKING) return false;
241   if (dynamic_cast<Lantern*>(&other)) return true;
242   if (dynamic_cast<Player*>(&other)) return true;
243   return false;
244 }
245
246 HitResponse
247 GhostTree::collision(GameObject& other, const CollisionHit& ) {
248   if(mystate != STATE_SUCKING) return ABORT_MOVE;
249
250   Player* player = dynamic_cast<Player*>(&other);
251   if (player) {
252     player->kill(false);
253   }
254
255   Lantern* lantern = dynamic_cast<Lantern*>(&other);
256   if (lantern) {
257     suck_lantern = lantern;
258     suck_lantern->grab(*this, suck_lantern->get_pos(), RIGHT);
259     suck_lantern_color = lantern->get_color();
260     mystate = STATE_SWALLOWING;
261   }
262
263   return ABORT_MOVE;
264 }
265
266 void
267 GhostTree::spawn_lantern() {
268   Lantern* lantern = new Lantern(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
269   Sector::current()->add_object(lantern);
270 }
271
272 IMPLEMENT_FACTORY(GhostTree, "ghosttree");
273
274 /* EOF */