3 // SuperTux - Boss "GhostTree"
4 // Copyright (C) 2007 Matthias Braun <matze@braunis.de>
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.
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.
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.
22 #include "ghosttree.hpp"
23 #include "treewillowisp.hpp"
24 #include "sprite/sprite_manager.hpp"
26 #include "random_generator.hpp"
27 #include "object/lantern.hpp"
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;
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)
41 glow_sprite.reset(sprite_manager->create("images/creatures/ghosttree/ghosttree-glow.sprite"));
42 set_colgroup_active(COLGROUP_TOUCHABLE);
45 GhostTree::~GhostTree()
52 mystate = STATE_DYING;
53 sprite->set_action("dying", 1);
54 glow_sprite->set_action("dying", 1);
56 std::vector<TreeWillOWisp*>::iterator iter;
57 for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
58 TreeWillOWisp *willo = *iter;
66 willowisp_timer.start(1.0f, true);
67 colorchange_timer.start(13, true);
68 root_timer.start(5, true);
72 GhostTree::active_update(float elapsed_time)
76 if (mystate == STATE_IDLE) {
77 if(colorchange_timer.check()) {
78 sound_manager->play("sounds/tree_howling.ogg", get_pos());
80 treecolor = (treecolor + 1) % 3;
84 case 0: col = Color(1, 0, 0); break;
85 case 1: col = Color(0, 1, 0); break;
86 case 2: col = Color(0, 0, 1); break;
87 case 3: col = Color(1, 1, 0); break;
88 case 4: col = Color(1, 0, 1); break;
89 case 5: col = Color(0, 1, 1); break;
90 default: assert(false);
92 glow_sprite->set_color(col);
95 if(suck_timer.check()) {
96 Color col = glow_sprite->get_color();
97 sound_manager->play("sounds/tree_suck.ogg", get_pos());
98 std::vector<TreeWillOWisp*>::iterator iter;
99 for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
100 TreeWillOWisp *willo = *iter;
101 if(willo->get_color() == col) {
102 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 mystate = STATE_SUCKING;
108 if(willowisp_timer.check()) {
109 if(willowisps.size() < WILLOWISP_COUNT) {
110 Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
111 TreeWillOWisp *willowisp
112 = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
114 Sector::current()->add_object(willowisp);
115 willowisps.push_back(willowisp);
118 if(willo_spawn_y < -160)
122 if(willo_radius > 120)
125 if(willo_speed == 1.8f) {
132 willo_color = (willo_color + 1) % 3;
133 } while(willo_color == treecolor);
135 switch(willo_color) {
136 case 0: willowisp->set_color(Color(1, 0, 0)); break;
137 case 1: willowisp->set_color(Color(0, 1, 0)); break;
138 case 2: willowisp->set_color(Color(0, 0, 1)); break;
139 case 3: willowisp->set_color(Color(1, 1, 0)); break;
140 case 4: willowisp->set_color(Color(1, 0, 1)); break;
141 case 5: willowisp->set_color(Color(0, 1, 1)); break;
142 default: assert(false);
147 if(root_timer.check()) {
148 /* TODO indicate root with an animation */
149 Player* player = get_nearest_player();
150 Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
151 Sector::current()->add_object(root);
153 } else if (mystate == STATE_SWALLOWING) {
156 assert (suck_lantern);
157 Vector pos = suck_lantern->get_pos();
158 Vector delta = get_bbox().get_middle() + SUCK_TARGET_OFFSET - pos;
159 Vector dir = delta.unit();
160 if (delta.norm() < 1) {
162 suck_lantern->ungrab(*this, RIGHT);
163 suck_lantern->remove_me();
165 sprite->set_action("swallow", 1);
168 suck_lantern->grab(*this, pos, RIGHT);
171 // wait until lantern is swallowed
172 if (sprite->animation_done()) {
173 if (is_color_deadly(suck_lantern_color)) {
176 sprite->set_action("default");
177 mystate = STATE_IDLE;
186 GhostTree::is_color_deadly(Color color) const {
187 if (color == Color(0,0,0)) return false;
188 Color my_color = glow_sprite->get_color();
189 return ((my_color.red != color.red) || (my_color.green != color.green) || (my_color.blue != color.blue));
193 GhostTree::willowisp_died(TreeWillOWisp *willowisp)
195 if ((mystate == STATE_SUCKING) && (willowisp->was_sucked)) {
196 mystate = STATE_IDLE;
198 willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
202 GhostTree::draw(DrawingContext& context)
204 BadGuy::draw(context);
206 context.push_target();
207 context.push_transform();
208 context.set_target(DrawingContext::LIGHTMAP);
209 if (mystate == STATE_SUCKING) {
210 context.set_alpha(0.5 + fmodf(game_time, 0.5));
212 context.set_alpha(0.5);
214 glow_sprite->draw(context, get_pos(), layer);
215 context.pop_transform();
216 context.pop_target();
220 GhostTree::collides(GameObject& other, const CollisionHit& ) {
221 if (mystate != STATE_SUCKING) return false;
222 if (dynamic_cast<Lantern*>(&other)) return true;
223 if (dynamic_cast<Player*>(&other)) return true;
228 GhostTree::collision(GameObject& other, const CollisionHit& ) {
229 if(mystate != STATE_SUCKING) return ABORT_MOVE;
231 Player* player = dynamic_cast<Player*>(&other);
236 Lantern* lantern = dynamic_cast<Lantern*>(&other);
238 suck_lantern = lantern;
239 suck_lantern->grab(*this, suck_lantern->get_pos(), RIGHT);
240 suck_lantern_color = lantern->get_color();
241 mystate = STATE_SWALLOWING;
248 GhostTree::spawn_lantern() {
249 Lantern* lantern = new Lantern(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
250 Sector::current()->add_object(lantern);
253 IMPLEMENT_FACTORY(GhostTree, "ghosttree");