3 // SuperTux - "Will-O-Wisp" Badguy
4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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
22 #include "willowisp.hpp"
24 #include "game_session.hpp"
26 static const float FLYSPEED = 64; /**< speed in px per second */
27 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
28 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
29 static const std::string SOUNDFILE = "sounds/willowisp.wav";
31 WillOWisp::WillOWisp(const lisp::Lisp& reader)
32 : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main")
34 reader.get("sector", target_sector);
35 reader.get("spawnpoint", target_spawnpoint);
38 sound_manager->preload(SOUNDFILE);
42 WillOWisp::write(lisp::Writer& writer)
44 writer.start_list("willowisp");
46 writer.write_float("x", start_position.x);
47 writer.write_float("y", start_position.y);
48 writer.write_string("sector", target_sector);
49 writer.write_string("spawnpoint", target_spawnpoint);
51 writer.end_list("willowisp");
55 WillOWisp::draw(DrawingContext& context)
57 sprite->draw(context, get_pos(), layer);
59 context.push_target();
60 context.set_target(DrawingContext::LIGHTMAP);
62 sprite->draw(context, get_pos(), layer);
68 WillOWisp::active_update(float elapsed_time)
70 Player* player = get_nearest_player();
72 Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
73 Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
74 Vector dist = (p2 - p1);
76 if (mystate == STATE_IDLE) {
77 if (dist.norm() <= TRACK_RANGE) {
78 mystate = STATE_TRACKING;
82 if (mystate == STATE_TRACKING) {
83 if (dist.norm() <= VANISH_RANGE) {
84 Vector dir = dist.unit();
85 movement = dir*elapsed_time*FLYSPEED;
87 mystate = STATE_VANISHING;
88 sprite->set_action("vanishing", 1);
90 sound_source->set_position(get_pos());
93 if (mystate == STATE_WARPING) {
94 if(sprite->animation_done()) {
99 if (mystate == STATE_VANISHING) {
100 if(sprite->animation_done()) {
108 WillOWisp::activate()
110 sprite->set_action("idle");
112 sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
113 sound_source->set_position(get_pos());
114 sound_source->set_looping(true);
115 sound_source->set_gain(2.0);
116 sound_source->set_reference_distance(32);
117 sound_source->play();
121 WillOWisp::deactivate()
123 sound_source.reset(NULL);
129 mystate = STATE_IDLE;
132 case STATE_VANISHING:
139 WillOWisp::kill_fall()
144 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
145 if(player.is_invincible()) return ABORT_MOVE;
147 if (mystate != STATE_TRACKING) return ABORT_MOVE;
149 mystate = STATE_WARPING;
150 sprite->set_action("warping", 1);
152 GameSession::current()->respawn(target_sector, target_spawnpoint);
153 sound_manager->play("sounds/warp.wav");
158 IMPLEMENT_FACTORY(WillOWisp, "willowisp")