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"
29 #include "util/reader.hpp"
31 static const float FLYSPEED = 64; /**< speed in px per second */
32 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
33 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
34 static const std::string SOUNDFILE = "sounds/willowisp.wav";
36 WillOWisp::WillOWisp(const Reader& reader) :
37 BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS),
39 target_sector("main"),
40 target_spawnpoint("main"),
51 track_range = TRACK_RANGE;
52 vanish_range = VANISH_RANGE;
54 reader.get("sector", target_sector);
55 reader.get("spawnpoint", target_spawnpoint);
56 reader.get("name", name);
57 reader.get("flyspeed", flyspeed);
58 reader.get("track-range", track_range);
59 reader.get("vanish-range", vanish_range);
60 reader.get("hit-script", hit_script);
61 reader.get("running", running);
63 const lisp::Lisp* pathLisp = reader.get_lisp("path");
64 if(pathLisp != NULL) {
65 path.reset(new Path());
66 path->read(*pathLisp);
67 walker.reset(new PathWalker(path.get(), running));
69 mystate = STATE_PATHMOVING_TRACK;
73 sound_manager->preload(SOUNDFILE);
74 sound_manager->preload("sounds/warp.wav");
76 sprite->set_action("idle");
80 WillOWisp::draw(DrawingContext& context)
82 sprite->draw(context, get_pos(), layer);
84 context.push_target();
85 context.set_target(DrawingContext::LIGHTMAP);
87 sprite->draw(context, get_pos(), layer);
93 WillOWisp::active_update(float elapsed_time)
95 Player* player = get_nearest_player();
97 Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
98 Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
99 Vector dist = (p2 - p1);
106 if (dist.norm() <= track_range) {
107 mystate = STATE_TRACKING;
112 if (dist.norm() > vanish_range) {
114 } else if (dist.norm() >= 1) {
115 Vector dir = dist.unit();
116 movement = dir * elapsed_time * flyspeed;
118 /* We somehow landed right on top of the player without colliding.
119 * Sit tight and avoid a division by zero. */
121 sound_source->set_position(get_pos());
125 if(sprite->animation_done()) {
129 case STATE_VANISHING: {
130 Vector dir = dist.unit();
131 movement = dir * elapsed_time * flyspeed;
132 if(sprite->animation_done()) {
138 case STATE_PATHMOVING:
139 case STATE_PATHMOVING_TRACK:
140 if(walker.get() == NULL)
142 movement = walker->advance(elapsed_time) - get_pos();
143 if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
144 mystate = STATE_TRACKING;
154 WillOWisp::activate()
156 sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
157 sound_source->set_position(get_pos());
158 sound_source->set_looping(true);
159 sound_source->set_gain(2.0);
160 sound_source->set_reference_distance(32);
161 sound_source->play();
165 WillOWisp::deactivate()
167 sound_source.reset(NULL);
172 case STATE_PATHMOVING:
173 case STATE_PATHMOVING_TRACK:
176 mystate = STATE_IDLE;
179 case STATE_VANISHING:
188 mystate = STATE_VANISHING;
189 sprite->set_action("vanishing", 1);
190 set_colgroup_active(COLGROUP_DISABLED);
194 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
195 Lantern* lantern = dynamic_cast<Lantern*>(&other);
197 if (lantern && lantern->is_open())
200 if (dynamic_cast<Player*>(&other))
207 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
208 if(player.is_invincible())
211 if (mystate != STATE_TRACKING)
214 mystate = STATE_WARPING;
215 sprite->set_action("warping", 1);
217 if(hit_script != "") {
218 std::istringstream stream(hit_script);
219 Sector::current()->run_script(stream, "hit-script");
221 GameSession::current()->respawn(target_sector, target_spawnpoint);
223 sound_manager->play("sounds/warp.wav");
229 WillOWisp::goto_node(int node_no)
231 walker->goto_node(node_no);
232 if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
233 mystate = STATE_PATHMOVING;
238 WillOWisp::start_moving()
240 walker->start_moving();
244 WillOWisp::stop_moving()
246 walker->stop_moving();
250 WillOWisp::set_state(const std::string& new_state)
252 if(new_state == "stopped") {
253 mystate = STATE_STOPPED;
254 } else if(new_state == "idle") {
255 mystate = STATE_IDLE;
256 } else if(new_state == "move_path") {
257 mystate = STATE_PATHMOVING;
258 walker->start_moving();
259 } else if(new_state == "move_path_track") {
260 mystate = STATE_PATHMOVING_TRACK;
261 walker->start_moving();
262 } else if(new_state == "normal") {
263 mystate = STATE_IDLE;
264 } else if(new_state == "vanish") {
267 std::ostringstream msg;
268 msg << "Can't set unknown willowisp state '" << new_state << "', should "
269 "be stopped, move_path, move_path_track or normal";
270 throw new std::runtime_error(msg.str());
275 WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
280 std::cout << "Expose me '" << name << "'\n";
281 Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
282 expose_object(vm, table_idx, interface, name);
286 WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
291 std::cout << "UnExpose me '" << name << "'\n";
292 Scripting::unexpose_object(vm, table_idx, name);
295 IMPLEMENT_FACTORY(WillOWisp, "willowisp");