[cppcheck] Part 1: Performance
[supertux.git] / src / badguy / ghosttree.cpp
1 //  SuperTux - Boss "GhostTree"
2 //  Copyright (C) 2007 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/ghosttree.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "badguy/root.hpp"
21 #include "badguy/treewillowisp.hpp"
22 #include "math/random_generator.hpp"
23 #include "object/lantern.hpp"
24 #include "object/player.hpp"
25 #include "sprite/sprite.hpp"
26 #include "sprite/sprite_manager.hpp"
27 #include "supertux/object_factory.hpp"
28 #include "supertux/sector.hpp"
29
30 #include <algorithm>
31 #include <math.h>
32
33 static const size_t WILLOWISP_COUNT = 10;
34 static const float ROOT_TOP_OFFSET = 64;
35 static const float WILLOWISP_TOP_OFFSET = -64;
36 static const Vector SUCK_TARGET_OFFSET = Vector(-16,-16);
37 static const float SUCK_TARGET_SPREAD = 8;
38
39 GhostTree::GhostTree(const Reader& lisp) :
40   BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite", LAYER_OBJECTS - 10),
41   mystate(STATE_IDLE),
42   willowisp_timer(),
43   willo_spawn_y(0),
44   willo_radius(200),
45   willo_speed(1.8f),
46   willo_color(0),
47   glow_sprite(SpriteManager::current()->create("images/creatures/ghosttree/ghosttree-glow.sprite")),
48   colorchange_timer(),
49   suck_timer(),
50   root_timer(),
51   treecolor(0),
52   suck_lantern_color(),
53   suck_lantern(0),
54   willowisps()
55 {
56   set_colgroup_active(COLGROUP_TOUCHABLE);
57   SoundManager::current()->preload("sounds/tree_howling.ogg");
58   SoundManager::current()->preload("sounds/tree_suck.ogg");
59 }
60
61 GhostTree::~GhostTree()
62 {
63 }
64
65 void
66 GhostTree::die()
67 {
68   mystate = STATE_DYING;
69   sprite->set_action("dying", 1);
70   glow_sprite->set_action("dying", 1);
71
72   for(auto iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
73     TreeWillOWisp& willo = **iter;
74     willo.vanish();
75   }
76   run_dead_script();
77 }
78
79 void
80 GhostTree::activate()
81 {
82   willowisp_timer.start(1.0f, true);
83   colorchange_timer.start(13, true);
84   root_timer.start(5, true);
85 }
86
87 void
88 GhostTree::active_update(float elapsed_time)
89 {
90   (void) elapsed_time;
91
92   if (mystate == STATE_IDLE) {
93     if(colorchange_timer.check()) {
94       SoundManager::current()->play("sounds/tree_howling.ogg", get_pos());
95       suck_timer.start(3);
96       treecolor = (treecolor + 1) % 3;
97
98       Color col;
99       switch(treecolor) {
100         case 0: col = Color(1, 0, 0); break;
101         case 1: col = Color(0, 1, 0); break;
102         case 2: col = Color(0, 0, 1); break;
103         case 3: col = Color(1, 1, 0); break;
104         case 4: col = Color(1, 0, 1); break;
105         case 5: col = Color(0, 1, 1); break;
106         default: assert(false);
107       }
108       glow_sprite->set_color(col);
109     }
110
111     if(suck_timer.check()) {
112       Color col = glow_sprite->get_color();
113       SoundManager::current()->play("sounds/tree_suck.ogg", get_pos());
114       for(auto iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
115         TreeWillOWisp& willo = **iter;
116         if(willo.get_color() == col) {
117           willo.start_sucking(get_bbox().get_middle() + SUCK_TARGET_OFFSET + Vector(gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD), gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD)));
118         }
119       }
120       mystate = STATE_SUCKING;
121     }
122
123     if(willowisp_timer.check()) {
124       if(willowisps.size() < WILLOWISP_COUNT) {
125         Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
126         auto willowisp = std::make_shared<TreeWillOWisp>(this, pos, 200 + willo_radius, willo_speed);
127
128         Sector::current()->add_object(willowisp);
129         willowisps.push_back(willowisp);
130
131         willo_spawn_y -= 40;
132         if(willo_spawn_y < -160)
133           willo_spawn_y = 0;
134
135         willo_radius += 20;
136         if(willo_radius > 120)
137           willo_radius = 0;
138
139         if(willo_speed == 1.8f) {
140           willo_speed = 1.5f;
141         } else {
142           willo_speed = 1.8f;
143         }
144
145         do {
146           willo_color = (willo_color + 1) % 3;
147         } while(willo_color == treecolor);
148
149         switch(willo_color) {
150           case 0: willowisp->set_color(Color(1, 0, 0)); break;
151           case 1: willowisp->set_color(Color(0, 1, 0)); break;
152           case 2: willowisp->set_color(Color(0, 0, 1)); break;
153           case 3: willowisp->set_color(Color(1, 1, 0)); break;
154           case 4: willowisp->set_color(Color(1, 0, 1)); break;
155           case 5: willowisp->set_color(Color(0, 1, 1)); break;
156           default: assert(false);
157         }
158       }
159     }
160
161     if(root_timer.check()) {
162       /* TODO indicate root with an animation */
163       Player* player = get_nearest_player();
164       if (player) {
165         auto root = std::make_shared<Root>(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
166         Sector::current()->add_object(root);
167       }
168     }
169   } else if (mystate == STATE_SWALLOWING) {
170     if (suck_lantern) {
171       // suck in lantern
172       assert (suck_lantern);
173       Vector pos = suck_lantern->get_pos();
174       Vector delta = get_bbox().get_middle() + SUCK_TARGET_OFFSET - pos;
175       Vector dir_ = delta.unit();
176       if (delta.norm() < 1) {
177         dir_ = delta;
178         suck_lantern->ungrab(*this, RIGHT);
179         suck_lantern->remove_me();
180         suck_lantern = 0;
181         sprite->set_action("swallow", 1);
182       } else {
183         pos += dir_;
184         suck_lantern->grab(*this, pos, RIGHT);
185       }
186     } else {
187       // wait until lantern is swallowed
188       if (sprite->animation_done()) {
189         if (is_color_deadly(suck_lantern_color)) {
190           die();
191         } else {
192           sprite->set_action("default");
193           mystate = STATE_IDLE;
194           spawn_lantern();
195         }
196       }
197     }
198   }
199 }
200
201 bool
202 GhostTree::is_color_deadly(Color color) const {
203   if (color == Color(0,0,0)) return false;
204   Color my_color = glow_sprite->get_color();
205   return ((my_color.red != color.red) || (my_color.green != color.green) || (my_color.blue != color.blue));
206 }
207
208 void
209 GhostTree::willowisp_died(TreeWillOWisp *willowisp)
210 {
211   if ((mystate == STATE_SUCKING) && (willowisp->was_sucked)) {
212     mystate = STATE_IDLE;
213   }
214   willowisps.erase(std::find_if(willowisps.begin(), willowisps.end(),
215                                 [willowisp](const std::shared_ptr<TreeWillOWisp>& lhs)
216                                 {
217                                   return lhs.get() == willowisp;
218                                 }));
219 }
220
221 void
222 GhostTree::draw(DrawingContext& context)
223 {
224   BadGuy::draw(context);
225
226   context.push_target();
227   context.push_transform();
228   context.set_target(DrawingContext::LIGHTMAP);
229   if (mystate == STATE_SUCKING) {
230     context.set_alpha(0.5 + fmodf(game_time, 0.5));
231   } else {
232     context.set_alpha(0.5);
233   }
234   glow_sprite->draw(context, get_pos(), layer);
235   context.pop_transform();
236   context.pop_target();
237 }
238
239 bool
240 GhostTree::collides(GameObject& other, const CollisionHit& ) {
241   if (mystate != STATE_SUCKING) return false;
242   if (dynamic_cast<Lantern*>(&other)) return true;
243   if (dynamic_cast<Player*>(&other)) return true;
244   return false;
245 }
246
247 HitResponse
248 GhostTree::collision(GameObject& other, const CollisionHit& ) {
249   if(mystate != STATE_SUCKING) return ABORT_MOVE;
250
251   Player* player = dynamic_cast<Player*>(&other);
252   if (player) {
253     player->kill(false);
254   }
255
256   Lantern* lantern = dynamic_cast<Lantern*>(&other);
257   if (lantern) {
258     suck_lantern = lantern;
259     suck_lantern->grab(*this, suck_lantern->get_pos(), RIGHT);
260     suck_lantern_color = lantern->get_color();
261     mystate = STATE_SWALLOWING;
262   }
263
264   return ABORT_MOVE;
265 }
266
267 void
268 GhostTree::spawn_lantern() {
269   auto lantern = std::make_shared<Lantern>(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
270   Sector::current()->add_object(lantern);
271 }
272
273 /* EOF */