willowisp position relative to ghosttree now, ghosttree is notified from vanished...
[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 "root.hpp"
24
25 static const size_t WILLOWISP_COUNT = 10;
26
27 GhostTree::GhostTree(const lisp::Lisp& lisp)
28   : BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite",
29            LAYER_OBJECTS - 10),
30     willo_spawn_y(0), willo_radius(200), willo_speed(1.8f), willo_color(0),
31     treecolor(0)
32 {
33 }
34
35 GhostTree::~GhostTree()
36 {
37 }
38
39 void
40 GhostTree::activate()
41 {
42   willowisp_timer.start(1.0f, true);
43   colorchange_timer.start(13, true);
44   root_timer.start(5, true);
45   set_group(COLGROUP_DISABLED);
46 }
47
48 void
49 GhostTree::active_update(float elapsed_time)
50 {
51   (void) elapsed_time;
52
53   if(colorchange_timer.check()) {
54     treecolor++;
55     if(treecolor > 5)
56         treecolor = 0;
57
58     switch(treecolor) {
59     case 0: sprite->set_color(Color(1, 0, 0)); break;
60     case 1: sprite->set_color(Color(0, 1, 0)); break;
61     case 2: sprite->set_color(Color(0, 0, 1)); break;
62     case 3: sprite->set_color(Color(1, 1, 0)); break;
63     case 4: sprite->set_color(Color(1, 0, 1)); break;
64     case 5: sprite->set_color(Color(0, 1, 1)); break;
65     default: assert(false);
66     }
67   }
68
69   if(willowisp_timer.check()) {
70     if(willowisps.size() < WILLOWISP_COUNT) {
71       Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y);
72       TreeWillOWisp *willowisp 
73         = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
74
75       Sector::current()->add_object(willowisp);
76       willowisps.push_back(willowisp);
77
78       willo_spawn_y -= 40;
79       if(willo_spawn_y < -160)
80         willo_spawn_y = 0;
81
82       willo_radius += 20;
83       if(willo_radius > 120)
84         willo_radius = 0;
85
86       if(willo_speed == 1.8f) {
87         willo_speed = 1.5f;
88       } else {
89         willo_speed = 1.8f;
90       }
91
92       willo_color++;
93       if(willo_color > 5)
94         willo_color = 0;
95       switch(willo_color) {
96       case 0: willowisp->set_color(Color(1, 0, 0)); break;
97       case 1: willowisp->set_color(Color(0, 1, 0)); break;
98       case 2: willowisp->set_color(Color(0, 0, 1)); break;
99       case 3: willowisp->set_color(Color(1, 1, 0)); break;
100       case 4: willowisp->set_color(Color(1, 0, 1)); break;
101       case 5: willowisp->set_color(Color(0, 1, 1)); break;
102       default: assert(false);
103       }
104     }
105   }
106
107   if(root_timer.check()) {
108     /* TODO indicate root with an animation */
109     Player* player = get_nearest_player();
110     Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()));
111     Sector::current()->add_object(root);
112   }
113 }
114
115 void
116 GhostTree::willowisp_died(TreeWillOWisp *willowisp)
117 {
118   willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
119 }
120
121 void
122 GhostTree::start_sucking()
123 {
124   /* TODO create a particle system to indicate the sucking... */
125 }
126
127 IMPLEMENT_FACTORY(GhostTree, "ghosttree");
128