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"
25 #include "game_session.hpp"
26 #include "object/lantern.hpp"
27 #include "object/player.hpp"
28 #include "scripting/squirrel_util.hpp"
29 #include "object/path.hpp"
30 #include "object/path_walker.hpp"
31 #include "audio/sound_source.hpp"
32 #include "lisp/writer.hpp"
33 #include "object_factory.hpp"
34 #include "audio/sound_manager.hpp"
36 #include "sprite/sprite.hpp"
38 static const float FLYSPEED = 64; /**< speed in px per second */
39 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
40 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
41 static const std::string SOUNDFILE = "sounds/willowisp.wav";
43 WillOWisp::WillOWisp(const lisp::Lisp& reader)
44 : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main")
48 track_range = TRACK_RANGE;
49 vanish_range = VANISH_RANGE;
51 reader.get("sector", target_sector);
52 reader.get("spawnpoint", target_spawnpoint);
53 reader.get("name", name);
54 reader.get("flyspeed", flyspeed);
55 reader.get("track-range", track_range);
56 reader.get("vanish-range", vanish_range);
57 reader.get("hit-script", hit_script);
58 reader.get("running", running);
60 const lisp::Lisp* pathLisp = reader.get_lisp("path");
61 if(pathLisp != NULL) {
62 path.reset(new Path());
63 path->read(*pathLisp);
64 walker.reset(new PathWalker(path.get(), running));
66 mystate = STATE_PATHMOVING_TRACK;
70 sound_manager->preload(SOUNDFILE);
71 sound_manager->preload("sounds/warp.wav");
73 sprite->set_action("idle");
77 WillOWisp::draw(DrawingContext& context)
79 sprite->draw(context, get_pos(), layer);
81 context.push_target();
82 context.set_target(DrawingContext::LIGHTMAP);
84 sprite->draw(context, get_pos(), layer);
90 WillOWisp::active_update(float elapsed_time)
92 Player* player = get_nearest_player();
94 Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
95 Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
96 Vector dist = (p2 - p1);
103 if (dist.norm() <= track_range) {
104 mystate = STATE_TRACKING;
109 if (dist.norm() <= vanish_range) {
110 Vector dir = dist.unit();
111 movement = dir * elapsed_time * flyspeed;
115 sound_source->set_position(get_pos());
119 if(sprite->animation_done()) {
123 case STATE_VANISHING: {
124 Vector dir = dist.unit();
125 movement = dir * elapsed_time * flyspeed;
126 if(sprite->animation_done()) {
132 case STATE_PATHMOVING:
133 case STATE_PATHMOVING_TRACK:
134 if(walker.get() == NULL)
136 movement = walker->advance(elapsed_time) - get_pos();
137 if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
138 mystate = STATE_TRACKING;
148 WillOWisp::activate()
150 sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
151 sound_source->set_position(get_pos());
152 sound_source->set_looping(true);
153 sound_source->set_gain(2.0);
154 sound_source->set_reference_distance(32);
155 sound_source->play();
159 WillOWisp::deactivate()
161 sound_source.reset(NULL);
166 case STATE_PATHMOVING:
167 case STATE_PATHMOVING_TRACK:
170 mystate = STATE_IDLE;
173 case STATE_VANISHING:
182 mystate = STATE_VANISHING;
183 sprite->set_action("vanishing", 1);
184 set_colgroup_active(COLGROUP_DISABLED);
188 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
189 Lantern* lantern = dynamic_cast<Lantern*>(&other);
191 if (lantern && lantern->is_open())
194 if (dynamic_cast<Player*>(&other))
201 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
202 if(player.is_invincible())
205 if (mystate != STATE_TRACKING)
208 mystate = STATE_WARPING;
209 sprite->set_action("warping", 1);
211 if(hit_script != "") {
212 std::istringstream stream(hit_script);
213 Sector::current()->run_script(stream, "hit-script");
215 GameSession::current()->respawn(target_sector, target_spawnpoint);
217 sound_manager->play("sounds/warp.wav");
223 WillOWisp::goto_node(int node_no)
225 walker->goto_node(node_no);
226 if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
227 mystate = STATE_PATHMOVING;
232 WillOWisp::start_moving()
234 walker->start_moving();
238 WillOWisp::stop_moving()
240 walker->stop_moving();
244 WillOWisp::set_state(const std::string& new_state)
246 if(new_state == "stopped") {
247 mystate = STATE_STOPPED;
248 } else if(new_state == "idle") {
249 mystate = STATE_IDLE;
250 } else if(new_state == "move_path") {
251 mystate = STATE_PATHMOVING;
252 walker->start_moving();
253 } else if(new_state == "move_path_track") {
254 mystate = STATE_PATHMOVING_TRACK;
255 walker->start_moving();
256 } else if(new_state == "normal") {
257 mystate = STATE_IDLE;
258 } else if(new_state == "vanish") {
261 std::ostringstream msg;
262 msg << "Can't set unknown willowisp state '" << new_state << "', should "
263 "be stopped, move_path, move_path_track or normal";
264 throw new std::runtime_error(msg.str());
269 WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
274 std::cout << "Expose me '" << name << "'\n";
275 Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
276 expose_object(vm, table_idx, interface, name);
280 WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
285 std::cout << "UnExpose me '" << name << "'\n";
286 Scripting::unexpose_object(vm, table_idx, name);
289 IMPLEMENT_FACTORY(WillOWisp, "willowisp")