00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "badguy/ghosttree.hpp"
00018
00019 #include "audio/sound_manager.hpp"
00020 #include "badguy/root.hpp"
00021 #include "badguy/treewillowisp.hpp"
00022 #include "math/random_generator.hpp"
00023 #include "object/lantern.hpp"
00024 #include "object/player.hpp"
00025 #include "sprite/sprite.hpp"
00026 #include "sprite/sprite_manager.hpp"
00027 #include "supertux/object_factory.hpp"
00028 #include "supertux/sector.hpp"
00029
00030 #include <algorithm>
00031 #include <math.h>
00032
00033 static const size_t WILLOWISP_COUNT = 10;
00034 static const float ROOT_TOP_OFFSET = 64;
00035 static const float WILLOWISP_TOP_OFFSET = -64;
00036 static const Vector SUCK_TARGET_OFFSET = Vector(-16,-16);
00037 static const float SUCK_TARGET_SPREAD = 8;
00038
00039 GhostTree::GhostTree(const Reader& lisp) :
00040 BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite", LAYER_OBJECTS - 10),
00041 mystate(STATE_IDLE),
00042 willowisp_timer(),
00043 willo_spawn_y(0),
00044 willo_radius(200),
00045 willo_speed(1.8f),
00046 willo_color(0),
00047 glow_sprite(),
00048 colorchange_timer(),
00049 suck_timer(),
00050 root_timer(),
00051 treecolor(0),
00052 suck_lantern_color(),
00053 suck_lantern(0),
00054 willowisps()
00055 {
00056 glow_sprite = sprite_manager->create("images/creatures/ghosttree/ghosttree-glow.sprite");
00057 set_colgroup_active(COLGROUP_TOUCHABLE);
00058 sound_manager->preload("sounds/tree_howling.ogg");
00059 sound_manager->preload("sounds/tree_suck.ogg");
00060 }
00061
00062 GhostTree::~GhostTree()
00063 {
00064 }
00065
00066 void
00067 GhostTree::die()
00068 {
00069 mystate = STATE_DYING;
00070 sprite->set_action("dying", 1);
00071 glow_sprite->set_action("dying", 1);
00072
00073 std::vector<TreeWillOWisp*>::iterator iter;
00074 for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
00075 TreeWillOWisp *willo = *iter;
00076 willo->vanish();
00077 }
00078 }
00079
00080 void
00081 GhostTree::activate()
00082 {
00083 willowisp_timer.start(1.0f, true);
00084 colorchange_timer.start(13, true);
00085 root_timer.start(5, true);
00086 }
00087
00088 void
00089 GhostTree::active_update(float elapsed_time)
00090 {
00091 (void) elapsed_time;
00092
00093 if (mystate == STATE_IDLE) {
00094 if(colorchange_timer.check()) {
00095 sound_manager->play("sounds/tree_howling.ogg", get_pos());
00096 suck_timer.start(3);
00097 treecolor = (treecolor + 1) % 3;
00098
00099 Color col;
00100 switch(treecolor) {
00101 case 0: col = Color(1, 0, 0); break;
00102 case 1: col = Color(0, 1, 0); break;
00103 case 2: col = Color(0, 0, 1); break;
00104 case 3: col = Color(1, 1, 0); break;
00105 case 4: col = Color(1, 0, 1); break;
00106 case 5: col = Color(0, 1, 1); break;
00107 default: assert(false);
00108 }
00109 glow_sprite->set_color(col);
00110 }
00111
00112 if(suck_timer.check()) {
00113 Color col = glow_sprite->get_color();
00114 sound_manager->play("sounds/tree_suck.ogg", get_pos());
00115 std::vector<TreeWillOWisp*>::iterator iter;
00116 for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
00117 TreeWillOWisp *willo = *iter;
00118 if(willo->get_color() == col) {
00119 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)));
00120 }
00121 }
00122 mystate = STATE_SUCKING;
00123 }
00124
00125 if(willowisp_timer.check()) {
00126 if(willowisps.size() < WILLOWISP_COUNT) {
00127 Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
00128 TreeWillOWisp *willowisp
00129 = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
00130
00131 Sector::current()->add_object(willowisp);
00132 willowisps.push_back(willowisp);
00133
00134 willo_spawn_y -= 40;
00135 if(willo_spawn_y < -160)
00136 willo_spawn_y = 0;
00137
00138 willo_radius += 20;
00139 if(willo_radius > 120)
00140 willo_radius = 0;
00141
00142 if(willo_speed == 1.8f) {
00143 willo_speed = 1.5f;
00144 } else {
00145 willo_speed = 1.8f;
00146 }
00147
00148 do {
00149 willo_color = (willo_color + 1) % 3;
00150 } while(willo_color == treecolor);
00151
00152 switch(willo_color) {
00153 case 0: willowisp->set_color(Color(1, 0, 0)); break;
00154 case 1: willowisp->set_color(Color(0, 1, 0)); break;
00155 case 2: willowisp->set_color(Color(0, 0, 1)); break;
00156 case 3: willowisp->set_color(Color(1, 1, 0)); break;
00157 case 4: willowisp->set_color(Color(1, 0, 1)); break;
00158 case 5: willowisp->set_color(Color(0, 1, 1)); break;
00159 default: assert(false);
00160 }
00161 }
00162 }
00163
00164 if(root_timer.check()) {
00165
00166 Player* player = get_nearest_player();
00167 if (player) {
00168 Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
00169 Sector::current()->add_object(root);
00170 }
00171 }
00172 } else if (mystate == STATE_SWALLOWING) {
00173 if (suck_lantern) {
00174
00175 assert (suck_lantern);
00176 Vector pos = suck_lantern->get_pos();
00177 Vector delta = get_bbox().get_middle() + SUCK_TARGET_OFFSET - pos;
00178 Vector dir = delta.unit();
00179 if (delta.norm() < 1) {
00180 dir = delta;
00181 suck_lantern->ungrab(*this, RIGHT);
00182 suck_lantern->remove_me();
00183 suck_lantern = 0;
00184 sprite->set_action("swallow", 1);
00185 } else {
00186 pos += dir;
00187 suck_lantern->grab(*this, pos, RIGHT);
00188 }
00189 } else {
00190
00191 if (sprite->animation_done()) {
00192 if (is_color_deadly(suck_lantern_color)) {
00193 die();
00194 } else {
00195 sprite->set_action("default");
00196 mystate = STATE_IDLE;
00197 spawn_lantern();
00198 }
00199 }
00200 }
00201 }
00202 }
00203
00204 bool
00205 GhostTree::is_color_deadly(Color color) const {
00206 if (color == Color(0,0,0)) return false;
00207 Color my_color = glow_sprite->get_color();
00208 return ((my_color.red != color.red) || (my_color.green != color.green) || (my_color.blue != color.blue));
00209 }
00210
00211 void
00212 GhostTree::willowisp_died(TreeWillOWisp *willowisp)
00213 {
00214 if ((mystate == STATE_SUCKING) && (willowisp->was_sucked)) {
00215 mystate = STATE_IDLE;
00216 }
00217 willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
00218 }
00219
00220 void
00221 GhostTree::draw(DrawingContext& context)
00222 {
00223 BadGuy::draw(context);
00224
00225 context.push_target();
00226 context.push_transform();
00227 context.set_target(DrawingContext::LIGHTMAP);
00228 if (mystate == STATE_SUCKING) {
00229 context.set_alpha(0.5 + fmodf(game_time, 0.5));
00230 } else {
00231 context.set_alpha(0.5);
00232 }
00233 glow_sprite->draw(context, get_pos(), layer);
00234 context.pop_transform();
00235 context.pop_target();
00236 }
00237
00238 bool
00239 GhostTree::collides(GameObject& other, const CollisionHit& ) {
00240 if (mystate != STATE_SUCKING) return false;
00241 if (dynamic_cast<Lantern*>(&other)) return true;
00242 if (dynamic_cast<Player*>(&other)) return true;
00243 return false;
00244 }
00245
00246 HitResponse
00247 GhostTree::collision(GameObject& other, const CollisionHit& ) {
00248 if(mystate != STATE_SUCKING) return ABORT_MOVE;
00249
00250 Player* player = dynamic_cast<Player*>(&other);
00251 if (player) {
00252 player->kill(false);
00253 }
00254
00255 Lantern* lantern = dynamic_cast<Lantern*>(&other);
00256 if (lantern) {
00257 suck_lantern = lantern;
00258 suck_lantern->grab(*this, suck_lantern->get_pos(), RIGHT);
00259 suck_lantern_color = lantern->get_color();
00260 mystate = STATE_SWALLOWING;
00261 }
00262
00263 return ABORT_MOVE;
00264 }
00265
00266 void
00267 GhostTree::spawn_lantern() {
00268 Lantern* lantern = new Lantern(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
00269 Sector::current()->add_object(lantern);
00270 }
00271
00272