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")
38 track_range = TRACK_RANGE;
39 vanish_range = VANISH_RANGE;
41 reader.get("sector", target_sector);
42 reader.get("spawnpoint", target_spawnpoint);
43 reader.get("name", name);
44 reader.get("flyspeed", flyspeed);
45 reader.get("track-range", track_range);
46 reader.get("vanish-range", vanish_range);
47 reader.get("hit-script", hit_script);
49 const lisp::Lisp* pathLisp = reader.get_lisp("path");
50 if(pathLisp != NULL) {
51 path.reset(new Path());
52 path->read(*pathLisp);
53 walker.reset(new PathWalker(path.get(), false));
57 sound_manager->preload(SOUNDFILE);
61 WillOWisp::draw(DrawingContext& context)
63 sprite->draw(context, get_pos(), layer);
65 context.push_target();
66 context.set_target(DrawingContext::LIGHTMAP);
68 sprite->draw(context, get_pos(), layer);
74 WillOWisp::active_update(float elapsed_time)
76 Player* player = get_nearest_player();
78 Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
79 Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
80 Vector dist = (p2 - p1);
87 if (dist.norm() <= track_range) {
88 mystate = STATE_TRACKING;
93 if (dist.norm() <= vanish_range) {
94 Vector dir = dist.unit();
95 movement = dir * elapsed_time * flyspeed;
99 sound_source->set_position(get_pos());
103 if(sprite->animation_done()) {
107 case STATE_VANISHING: {
108 Vector dir = dist.unit();
109 movement = dir * elapsed_time * flyspeed;
110 if(sprite->animation_done()) {
116 case STATE_PATHMOVING:
117 case STATE_PATHMOVING_TRACK:
118 if(walker.get() == NULL)
120 movement = walker->advance(elapsed_time) - get_pos();
121 if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
122 mystate = STATE_TRACKING;
132 WillOWisp::activate()
134 sprite->set_action("idle");
136 sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
137 sound_source->set_position(get_pos());
138 sound_source->set_looping(true);
139 sound_source->set_gain(2.0);
140 sound_source->set_reference_distance(32);
141 sound_source->play();
145 WillOWisp::deactivate()
147 sound_source.reset(NULL);
152 case STATE_PATHMOVING:
153 case STATE_PATHMOVING_TRACK:
156 mystate = STATE_IDLE;
159 case STATE_VANISHING:
168 mystate = STATE_VANISHING;
169 sprite->set_action("vanishing", 1);
170 set_group(COLGROUP_DISABLED);
174 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
175 Lantern* lantern = dynamic_cast<Lantern*>(&other);
177 if (lantern && lantern->is_open())
180 if (dynamic_cast<Player*>(&other))
187 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
188 if(player.is_invincible())
191 if (mystate != STATE_TRACKING)
194 mystate = STATE_WARPING;
195 sprite->set_action("warping", 1);
197 if(hit_script != "") {
198 std::istringstream stream(hit_script);
199 Sector::current()->run_script(stream, "hit-script");
201 GameSession::current()->respawn(target_sector, target_spawnpoint);
203 sound_manager->play("sounds/warp.wav");
209 WillOWisp::goto_node(int node_no)
211 walker->goto_node(node_no);
212 if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
213 mystate = STATE_PATHMOVING;
214 walker->start_moving();
219 WillOWisp::set_state(const std::string& new_state)
221 if(new_state == "stopped") {
222 mystate = STATE_STOPPED;
223 } else if(new_state == "idle") {
224 mystate = STATE_IDLE;
225 } else if(new_state == "move_path") {
226 mystate = STATE_PATHMOVING;
227 walker->start_moving();
228 } else if(new_state == "move_path_track") {
229 mystate = STATE_PATHMOVING_TRACK;
230 walker->start_moving();
231 } else if(new_state == "normal") {
232 mystate = STATE_IDLE;
234 std::ostringstream msg;
235 msg << "Can't set unknown willowisp state '" << new_state << "', should "
236 "be stopped, move_path, move_path_track or normal";
237 throw new std::runtime_error(msg.str());
242 WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
247 std::cout << "Expose me '" << name << "'\n";
248 Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
249 expose_object(vm, table_idx, interface, name);
253 WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
258 std::cout << "UnExpose me '" << name << "'\n";
259 Scripting::unexpose_object(vm, table_idx, name);
262 IMPLEMENT_FACTORY(WillOWisp, "willowisp")