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) {
111 } else if (dist.norm() >= 1) {
112 Vector dir = dist.unit();
113 movement = dir * elapsed_time * flyspeed;
115 /* We somehow landed right on top of the player without colliding.
116 * Sit tight and avoid a division by zero. */
118 sound_source->set_position(get_pos());
122 if(sprite->animation_done()) {
126 case STATE_VANISHING: {
127 Vector dir = dist.unit();
128 movement = dir * elapsed_time * flyspeed;
129 if(sprite->animation_done()) {
135 case STATE_PATHMOVING:
136 case STATE_PATHMOVING_TRACK:
137 if(walker.get() == NULL)
139 movement = walker->advance(elapsed_time) - get_pos();
140 if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
141 mystate = STATE_TRACKING;
151 WillOWisp::activate()
153 sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
154 sound_source->set_position(get_pos());
155 sound_source->set_looping(true);
156 sound_source->set_gain(2.0);
157 sound_source->set_reference_distance(32);
158 sound_source->play();
162 WillOWisp::deactivate()
164 sound_source.reset(NULL);
169 case STATE_PATHMOVING:
170 case STATE_PATHMOVING_TRACK:
173 mystate = STATE_IDLE;
176 case STATE_VANISHING:
185 mystate = STATE_VANISHING;
186 sprite->set_action("vanishing", 1);
187 set_colgroup_active(COLGROUP_DISABLED);
191 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
192 Lantern* lantern = dynamic_cast<Lantern*>(&other);
194 if (lantern && lantern->is_open())
197 if (dynamic_cast<Player*>(&other))
204 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
205 if(player.is_invincible())
208 if (mystate != STATE_TRACKING)
211 mystate = STATE_WARPING;
212 sprite->set_action("warping", 1);
214 if(hit_script != "") {
215 std::istringstream stream(hit_script);
216 Sector::current()->run_script(stream, "hit-script");
218 GameSession::current()->respawn(target_sector, target_spawnpoint);
220 sound_manager->play("sounds/warp.wav");
226 WillOWisp::goto_node(int node_no)
228 walker->goto_node(node_no);
229 if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
230 mystate = STATE_PATHMOVING;
235 WillOWisp::start_moving()
237 walker->start_moving();
241 WillOWisp::stop_moving()
243 walker->stop_moving();
247 WillOWisp::set_state(const std::string& new_state)
249 if(new_state == "stopped") {
250 mystate = STATE_STOPPED;
251 } else if(new_state == "idle") {
252 mystate = STATE_IDLE;
253 } else if(new_state == "move_path") {
254 mystate = STATE_PATHMOVING;
255 walker->start_moving();
256 } else if(new_state == "move_path_track") {
257 mystate = STATE_PATHMOVING_TRACK;
258 walker->start_moving();
259 } else if(new_state == "normal") {
260 mystate = STATE_IDLE;
261 } else if(new_state == "vanish") {
264 std::ostringstream msg;
265 msg << "Can't set unknown willowisp state '" << new_state << "', should "
266 "be stopped, move_path, move_path_track or normal";
267 throw new std::runtime_error(msg.str());
272 WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
277 std::cout << "Expose me '" << name << "'\n";
278 Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
279 expose_object(vm, table_idx, interface, name);
283 WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
288 std::cout << "UnExpose me '" << name << "'\n";
289 Scripting::unexpose_object(vm, table_idx, name);
292 IMPLEMENT_FACTORY(WillOWisp, "willowisp")