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"
25 #include "object/lantern.hpp"
26 #include "object/player.hpp"
27 #include "scripting/squirrel_util.hpp"
29 static const float FLYSPEED = 64; /**< speed in px per second */
30 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
31 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
32 static const std::string SOUNDFILE = "sounds/willowisp.wav";
34 WillOWisp::WillOWisp(const lisp::Lisp& reader)
35 : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main")
39 track_range = TRACK_RANGE;
40 vanish_range = VANISH_RANGE;
42 reader.get("sector", target_sector);
43 reader.get("spawnpoint", target_spawnpoint);
44 reader.get("name", name);
45 reader.get("flyspeed", flyspeed);
46 reader.get("track-range", track_range);
47 reader.get("vanish-range", vanish_range);
48 reader.get("hit-script", hit_script);
49 reader.get("running", running);
51 const lisp::Lisp* pathLisp = reader.get_lisp("path");
52 if(pathLisp != NULL) {
53 path.reset(new Path());
54 path->read(*pathLisp);
55 walker.reset(new PathWalker(path.get(), running));
57 mystate = STATE_PATHMOVING_TRACK;
61 sound_manager->preload(SOUNDFILE);
65 WillOWisp::draw(DrawingContext& context)
67 sprite->draw(context, get_pos(), layer);
69 context.push_target();
70 context.set_target(DrawingContext::LIGHTMAP);
72 sprite->draw(context, get_pos(), layer);
78 WillOWisp::active_update(float elapsed_time)
80 Player* player = get_nearest_player();
82 Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
83 Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
84 Vector dist = (p2 - p1);
91 if (dist.norm() <= track_range) {
92 mystate = STATE_TRACKING;
97 if (dist.norm() <= vanish_range) {
98 Vector dir = dist.unit();
99 movement = dir * elapsed_time * flyspeed;
103 sound_source->set_position(get_pos());
107 if(sprite->animation_done()) {
111 case STATE_VANISHING: {
112 Vector dir = dist.unit();
113 movement = dir * elapsed_time * flyspeed;
114 if(sprite->animation_done()) {
120 case STATE_PATHMOVING:
121 case STATE_PATHMOVING_TRACK:
122 if(walker.get() == NULL)
124 movement = walker->advance(elapsed_time) - get_pos();
125 if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
126 mystate = STATE_TRACKING;
136 WillOWisp::activate()
138 sprite->set_action("idle");
140 sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
141 sound_source->set_position(get_pos());
142 sound_source->set_looping(true);
143 sound_source->set_gain(2.0);
144 sound_source->set_reference_distance(32);
145 sound_source->play();
149 WillOWisp::deactivate()
151 sound_source.reset(NULL);
156 case STATE_PATHMOVING:
157 case STATE_PATHMOVING_TRACK:
160 mystate = STATE_IDLE;
163 case STATE_VANISHING:
172 mystate = STATE_VANISHING;
173 sprite->set_action("vanishing", 1);
174 set_group(COLGROUP_DISABLED);
178 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
179 Lantern* lantern = dynamic_cast<Lantern*>(&other);
181 if (lantern && lantern->is_open())
184 if (dynamic_cast<Player*>(&other))
191 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
192 if(player.is_invincible())
195 if (mystate != STATE_TRACKING)
198 mystate = STATE_WARPING;
199 sprite->set_action("warping", 1);
201 if(hit_script != "") {
202 std::istringstream stream(hit_script);
203 Sector::current()->run_script(stream, "hit-script");
205 GameSession::current()->respawn(target_sector, target_spawnpoint);
207 sound_manager->play("sounds/warp.wav");
213 WillOWisp::goto_node(int node_no)
215 walker->goto_node(node_no);
216 if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
217 mystate = STATE_PATHMOVING;
222 WillOWisp::start_moving()
224 walker->start_moving();
228 WillOWisp::stop_moving()
230 walker->stop_moving();
234 WillOWisp::set_state(const std::string& new_state)
236 if(new_state == "stopped") {
237 mystate = STATE_STOPPED;
238 } else if(new_state == "idle") {
239 mystate = STATE_IDLE;
240 } else if(new_state == "move_path") {
241 mystate = STATE_PATHMOVING;
242 walker->start_moving();
243 } else if(new_state == "move_path_track") {
244 mystate = STATE_PATHMOVING_TRACK;
245 walker->start_moving();
246 } else if(new_state == "normal") {
247 mystate = STATE_IDLE;
248 } else if(new_state == "vanish") {
251 std::ostringstream msg;
252 msg << "Can't set unknown willowisp state '" << new_state << "', should "
253 "be stopped, move_path, move_path_track or normal";
254 throw new std::runtime_error(msg.str());
259 WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
264 std::cout << "Expose me '" << name << "'\n";
265 Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
266 expose_object(vm, table_idx, interface, name);
270 WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
275 std::cout << "UnExpose me '" << name << "'\n";
276 Scripting::unexpose_object(vm, table_idx, name);
279 IMPLEMENT_FACTORY(WillOWisp, "willowisp")