some new GhostTree stuff
[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
27 static const size_t WILLOWISP_COUNT = 10;
28 static const float ROOT_TOP_OFFSET = -64;
29 static const Vector SUCK_TARGET_OFFSET = Vector(-32,72);
30 static const float SUCK_TARGET_SPREAD = 16;
31
32 GhostTree::GhostTree(const lisp::Lisp& lisp)
33   : BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite",
34            LAYER_OBJECTS - 10),
35     willo_spawn_y(0), willo_radius(200), willo_speed(1.8f), willo_color(0),
36     treecolor(0)
37 {
38   glow_sprite.reset(sprite_manager->create("images/creatures/ghosttree/ghosttree-glow.sprite"));
39 }
40
41 GhostTree::~GhostTree()
42 {
43 }
44
45 void
46 GhostTree::die()
47 {
48   sprite->set_action("dying", 1); 
49   glow_sprite->set_action("dying", 1); 
50
51   std::vector<TreeWillOWisp*>::iterator iter;
52   for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
53     TreeWillOWisp *willo = *iter;
54     willo->vanish();
55   }
56 }
57
58 void
59 GhostTree::activate()
60 {
61   willowisp_timer.start(1.0f, true);
62   colorchange_timer.start(13, true);
63   root_timer.start(5, true);
64   set_group(COLGROUP_DISABLED);
65 }
66
67 void
68 GhostTree::active_update(float elapsed_time)
69 {
70   (void) elapsed_time;
71
72   if(colorchange_timer.check()) {
73     sound_manager->play("sounds/tree_howling.ogg", get_pos());
74     suck_timer.start(3);
75     treecolor = (treecolor + 1) % 3;
76
77     Color col;
78     switch(treecolor) {
79     case 0: col = Color(1, 0, 0); break;
80     case 1: col = Color(0, 1, 0); break;
81     case 2: col = Color(0, 0, 1); break;
82     case 3: col = Color(1, 1, 0); break;
83     case 4: col = Color(1, 0, 1); break;
84     case 5: col = Color(0, 1, 1); break;
85     default: assert(false);
86     }
87     glow_sprite->set_color(col);
88   }
89
90   if(suck_timer.check()) {
91     Color col = glow_sprite->get_color();
92     sound_manager->play("sounds/tree_suck.ogg", get_pos());
93     std::vector<TreeWillOWisp*>::iterator iter;
94     for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
95       TreeWillOWisp *willo = *iter;
96       if(willo->get_color() == col) {
97         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)));
98       }
99     }
100   }
101
102   if(willowisp_timer.check()) {
103     if(willowisps.size() < WILLOWISP_COUNT) {
104       Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y);
105       TreeWillOWisp *willowisp 
106         = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
107
108       Sector::current()->add_object(willowisp);
109       willowisps.push_back(willowisp);
110
111       willo_spawn_y -= 40;
112       if(willo_spawn_y < -160)
113         willo_spawn_y = 0;
114
115       willo_radius += 20;
116       if(willo_radius > 120)
117         willo_radius = 0;
118
119       if(willo_speed == 1.8f) {
120         willo_speed = 1.5f;
121       } else {
122         willo_speed = 1.8f;
123       }
124
125       do {
126         willo_color = (willo_color + 1) % 3;
127       } while(willo_color == treecolor);
128
129       switch(willo_color) {
130       case 0: willowisp->set_color(Color(1, 0, 0)); break;
131       case 1: willowisp->set_color(Color(0, 1, 0)); break;
132       case 2: willowisp->set_color(Color(0, 0, 1)); break;
133       case 3: willowisp->set_color(Color(1, 1, 0)); break;
134       case 4: willowisp->set_color(Color(1, 0, 1)); break;
135       case 5: willowisp->set_color(Color(0, 1, 1)); break;
136       default: assert(false);
137       }
138     }
139   }
140
141   if(root_timer.check()) {
142     /* TODO indicate root with an animation */
143     Player* player = get_nearest_player();
144     Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
145     Sector::current()->add_object(root);
146   }
147 }
148
149 void
150 GhostTree::willowisp_died(TreeWillOWisp *willowisp)
151 {
152   willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
153 }
154
155 void
156 GhostTree::draw(DrawingContext& context)
157 {
158   BadGuy::draw(context);
159
160   context.push_target();
161   context.set_target(DrawingContext::LIGHTMAP);
162   glow_sprite->draw(context, get_pos(), layer);
163   context.pop_target();
164 }
165
166
167 IMPLEMENT_FACTORY(GhostTree, "ghosttree");
168