1 // SuperTux - "Will-O-Wisp" Badguy
2 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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/willowisp.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "audio/sound_source.hpp"
21 #include "object/lantern.hpp"
22 #include "object/path_walker.hpp"
23 #include "object/player.hpp"
24 #include "scripting/squirrel_util.hpp"
25 #include "sprite/sprite.hpp"
26 #include "supertux/game_session.hpp"
27 #include "supertux/object_factory.hpp"
28 #include "supertux/sector.hpp"
30 static const float FLYSPEED = 64; /**< speed in px per second */
31 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
32 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
33 static const std::string SOUNDFILE = "sounds/willowisp.wav";
35 WillOWisp::WillOWisp(const Reader& reader) :
36 BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS),
38 target_sector("main"),
39 target_spawnpoint("main"),
50 track_range = TRACK_RANGE;
51 vanish_range = VANISH_RANGE;
53 reader.get("sector", target_sector);
54 reader.get("spawnpoint", target_spawnpoint);
55 reader.get("name", name);
56 reader.get("flyspeed", flyspeed);
57 reader.get("track-range", track_range);
58 reader.get("vanish-range", vanish_range);
59 reader.get("hit-script", hit_script);
60 reader.get("running", running);
62 const lisp::Lisp* pathLisp = reader.get_lisp("path");
63 if(pathLisp != NULL) {
64 path.reset(new Path());
65 path->read(*pathLisp);
66 walker.reset(new PathWalker(path.get(), running));
68 mystate = STATE_PATHMOVING_TRACK;
72 sound_manager->preload(SOUNDFILE);
73 sound_manager->preload("sounds/warp.wav");
75 sprite->set_action("idle");
79 WillOWisp::draw(DrawingContext& context)
81 sprite->draw(context, get_pos(), layer);
83 context.push_target();
84 context.set_target(DrawingContext::LIGHTMAP);
86 sprite->draw(context, get_pos(), layer);
92 WillOWisp::active_update(float elapsed_time)
94 Player* player = get_nearest_player();
96 Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
97 Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
98 Vector dist = (p2 - p1);
105 if (dist.norm() <= track_range) {
106 mystate = STATE_TRACKING;
111 if (dist.norm() > vanish_range) {
113 } else if (dist.norm() >= 1) {
114 Vector dir = dist.unit();
115 movement = dir * elapsed_time * flyspeed;
117 /* We somehow landed right on top of the player without colliding.
118 * Sit tight and avoid a division by zero. */
120 sound_source->set_position(get_pos());
124 if(sprite->animation_done()) {
128 case STATE_VANISHING: {
129 Vector dir = dist.unit();
130 movement = dir * elapsed_time * flyspeed;
131 if(sprite->animation_done()) {
137 case STATE_PATHMOVING:
138 case STATE_PATHMOVING_TRACK:
139 if(walker.get() == NULL)
141 movement = walker->advance(elapsed_time) - get_pos();
142 if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
143 mystate = STATE_TRACKING;
153 WillOWisp::activate()
155 sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
156 sound_source->set_position(get_pos());
157 sound_source->set_looping(true);
158 sound_source->set_gain(2.0);
159 sound_source->set_reference_distance(32);
160 sound_source->play();
164 WillOWisp::deactivate()
166 sound_source.reset(NULL);
171 case STATE_PATHMOVING:
172 case STATE_PATHMOVING_TRACK:
175 mystate = STATE_IDLE;
178 case STATE_VANISHING:
187 mystate = STATE_VANISHING;
188 sprite->set_action("vanishing", 1);
189 set_colgroup_active(COLGROUP_DISABLED);
193 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
194 Lantern* lantern = dynamic_cast<Lantern*>(&other);
196 if (lantern && lantern->is_open())
199 if (dynamic_cast<Player*>(&other))
206 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
207 if(player.is_invincible())
210 if (mystate != STATE_TRACKING)
213 mystate = STATE_WARPING;
214 sprite->set_action("warping", 1);
216 if(hit_script != "") {
217 std::istringstream stream(hit_script);
218 Sector::current()->run_script(stream, "hit-script");
220 GameSession::current()->respawn(target_sector, target_spawnpoint);
222 sound_manager->play("sounds/warp.wav");
228 WillOWisp::goto_node(int node_no)
230 walker->goto_node(node_no);
231 if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
232 mystate = STATE_PATHMOVING;
237 WillOWisp::start_moving()
239 walker->start_moving();
243 WillOWisp::stop_moving()
245 walker->stop_moving();
249 WillOWisp::set_state(const std::string& new_state)
251 if(new_state == "stopped") {
252 mystate = STATE_STOPPED;
253 } else if(new_state == "idle") {
254 mystate = STATE_IDLE;
255 } else if(new_state == "move_path") {
256 mystate = STATE_PATHMOVING;
257 walker->start_moving();
258 } else if(new_state == "move_path_track") {
259 mystate = STATE_PATHMOVING_TRACK;
260 walker->start_moving();
261 } else if(new_state == "normal") {
262 mystate = STATE_IDLE;
263 } else if(new_state == "vanish") {
266 std::ostringstream msg;
267 msg << "Can't set unknown willowisp state '" << new_state << "', should "
268 "be stopped, move_path, move_path_track or normal";
269 throw new std::runtime_error(msg.str());
274 WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
279 std::cout << "Expose me '" << name << "'\n";
280 Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
281 expose_object(vm, table_idx, interface, name);
285 WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
290 std::cout << "UnExpose me '" << name << "'\n";
291 Scripting::unexpose_object(vm, table_idx, name);
294 IMPLEMENT_FACTORY(WillOWisp, "willowisp");