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