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