1 // SuperTux - Boss "GhostTree"
2 // Copyright (C) 2007 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "badguy/ghosttree.hpp"
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"
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;
39 GhostTree::GhostTree(const Reader& lisp) :
40 BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite", LAYER_OBJECTS - 10),
56 glow_sprite = SpriteManager::current()->create("images/creatures/ghosttree/ghosttree-glow.sprite");
57 set_colgroup_active(COLGROUP_TOUCHABLE);
58 SoundManager::current()->preload("sounds/tree_howling.ogg");
59 SoundManager::current()->preload("sounds/tree_suck.ogg");
62 GhostTree::~GhostTree()
69 mystate = STATE_DYING;
70 sprite->set_action("dying", 1);
71 glow_sprite->set_action("dying", 1);
73 for(auto iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
74 TreeWillOWisp& willo = **iter;
83 willowisp_timer.start(1.0f, true);
84 colorchange_timer.start(13, true);
85 root_timer.start(5, true);
89 GhostTree::active_update(float elapsed_time)
93 if (mystate == STATE_IDLE) {
94 if(colorchange_timer.check()) {
95 SoundManager::current()->play("sounds/tree_howling.ogg", get_pos());
97 treecolor = (treecolor + 1) % 3;
101 case 0: col = Color(1, 0, 0); break;
102 case 1: col = Color(0, 1, 0); break;
103 case 2: col = Color(0, 0, 1); break;
104 case 3: col = Color(1, 1, 0); break;
105 case 4: col = Color(1, 0, 1); break;
106 case 5: col = Color(0, 1, 1); break;
107 default: assert(false);
109 glow_sprite->set_color(col);
112 if(suck_timer.check()) {
113 Color col = glow_sprite->get_color();
114 SoundManager::current()->play("sounds/tree_suck.ogg", get_pos());
115 for(auto iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
116 TreeWillOWisp& willo = **iter;
117 if(willo.get_color() == col) {
118 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)));
121 mystate = STATE_SUCKING;
124 if(willowisp_timer.check()) {
125 if(willowisps.size() < WILLOWISP_COUNT) {
126 Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
127 auto willowisp = std::make_shared<TreeWillOWisp>(this, pos, 200 + willo_radius, willo_speed);
129 Sector::current()->add_object(willowisp);
130 willowisps.push_back(willowisp);
133 if(willo_spawn_y < -160)
137 if(willo_radius > 120)
140 if(willo_speed == 1.8f) {
147 willo_color = (willo_color + 1) % 3;
148 } while(willo_color == treecolor);
150 switch(willo_color) {
151 case 0: willowisp->set_color(Color(1, 0, 0)); break;
152 case 1: willowisp->set_color(Color(0, 1, 0)); break;
153 case 2: willowisp->set_color(Color(0, 0, 1)); break;
154 case 3: willowisp->set_color(Color(1, 1, 0)); break;
155 case 4: willowisp->set_color(Color(1, 0, 1)); break;
156 case 5: willowisp->set_color(Color(0, 1, 1)); break;
157 default: assert(false);
162 if(root_timer.check()) {
163 /* TODO indicate root with an animation */
164 Player* player = get_nearest_player();
166 auto root = std::make_shared<Root>(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
167 Sector::current()->add_object(root);
170 } else if (mystate == STATE_SWALLOWING) {
173 assert (suck_lantern);
174 Vector pos = suck_lantern->get_pos();
175 Vector delta = get_bbox().get_middle() + SUCK_TARGET_OFFSET - pos;
176 Vector dir_ = delta.unit();
177 if (delta.norm() < 1) {
179 suck_lantern->ungrab(*this, RIGHT);
180 suck_lantern->remove_me();
182 sprite->set_action("swallow", 1);
185 suck_lantern->grab(*this, pos, RIGHT);
188 // wait until lantern is swallowed
189 if (sprite->animation_done()) {
190 if (is_color_deadly(suck_lantern_color)) {
193 sprite->set_action("default");
194 mystate = STATE_IDLE;
203 GhostTree::is_color_deadly(Color color) const {
204 if (color == Color(0,0,0)) return false;
205 Color my_color = glow_sprite->get_color();
206 return ((my_color.red != color.red) || (my_color.green != color.green) || (my_color.blue != color.blue));
210 GhostTree::willowisp_died(TreeWillOWisp *willowisp)
212 if ((mystate == STATE_SUCKING) && (willowisp->was_sucked)) {
213 mystate = STATE_IDLE;
215 willowisps.erase(std::find_if(willowisps.begin(), willowisps.end(),
216 [willowisp](const std::shared_ptr<TreeWillOWisp>& lhs)
218 return lhs.get() == willowisp;
223 GhostTree::draw(DrawingContext& context)
225 BadGuy::draw(context);
227 context.push_target();
228 context.push_transform();
229 context.set_target(DrawingContext::LIGHTMAP);
230 if (mystate == STATE_SUCKING) {
231 context.set_alpha(0.5 + fmodf(game_time, 0.5));
233 context.set_alpha(0.5);
235 glow_sprite->draw(context, get_pos(), layer);
236 context.pop_transform();
237 context.pop_target();
241 GhostTree::collides(GameObject& other, const CollisionHit& ) {
242 if (mystate != STATE_SUCKING) return false;
243 if (dynamic_cast<Lantern*>(&other)) return true;
244 if (dynamic_cast<Player*>(&other)) return true;
249 GhostTree::collision(GameObject& other, const CollisionHit& ) {
250 if(mystate != STATE_SUCKING) return ABORT_MOVE;
252 Player* player = dynamic_cast<Player*>(&other);
257 Lantern* lantern = dynamic_cast<Lantern*>(&other);
259 suck_lantern = lantern;
260 suck_lantern->grab(*this, suck_lantern->get_pos(), RIGHT);
261 suck_lantern_color = lantern->get_color();
262 mystate = STATE_SWALLOWING;
269 GhostTree::spawn_lantern() {
270 auto lantern = std::make_shared<Lantern>(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
271 Sector::current()->add_object(lantern);