Random stuff that I should have committed ages ago.
[supertux.git] / src / badguy / willowisp.cpp
1 //  $Id$
2 //
3 //  SuperTux - "Will-O-Wisp" Badguy
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
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.
10 //
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.
15 //
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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "willowisp.hpp"
23
24 #include "log.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"
35 #include "sector.hpp"
36 #include "sprite/sprite.hpp"
37
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";
42
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")
45 {
46   bool running = false;
47   flyspeed     = FLYSPEED;
48   track_range  = TRACK_RANGE;
49   vanish_range = VANISH_RANGE;
50
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);
59
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));
65     if(running)
66       mystate = STATE_PATHMOVING_TRACK;
67   }
68
69   countMe = false;
70   sound_manager->preload(SOUNDFILE);
71   sound_manager->preload("sounds/warp.wav");
72
73   sprite->set_action("idle");
74 }
75
76 void
77 WillOWisp::draw(DrawingContext& context)
78 {
79   sprite->draw(context, get_pos(), layer);
80
81   context.push_target();
82   context.set_target(DrawingContext::LIGHTMAP);
83
84   sprite->draw(context, get_pos(), layer);
85
86   context.pop_target();
87 }
88
89 void
90 WillOWisp::active_update(float elapsed_time)
91 {
92   Player* player = get_nearest_player();
93   if (!player) return;
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);
97
98   switch(mystate) {
99   case STATE_STOPPED:
100     break;
101
102   case STATE_IDLE:
103     if (dist.norm() <= track_range) {
104       mystate = STATE_TRACKING;
105     }
106     break;
107
108   case STATE_TRACKING:
109     if (dist.norm() <= vanish_range) {
110       Vector dir = dist.unit();
111       movement = dir * elapsed_time * flyspeed;
112     } else {
113       vanish();
114     }
115     sound_source->set_position(get_pos());
116     break;
117
118   case STATE_WARPING:
119     if(sprite->animation_done()) {
120       remove_me();
121     }
122
123   case STATE_VANISHING: {
124     Vector dir = dist.unit();
125     movement = dir * elapsed_time * flyspeed;
126     if(sprite->animation_done()) {
127       remove_me();
128     }
129     break;
130   }
131
132   case STATE_PATHMOVING:
133   case STATE_PATHMOVING_TRACK:
134     if(walker.get() == NULL)
135       return;
136     movement = walker->advance(elapsed_time) - get_pos();
137     if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
138       mystate = STATE_TRACKING;
139     }
140     break;
141
142   default:
143     assert(false);
144   }
145 }
146
147 void
148 WillOWisp::activate()
149 {
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();
156 }
157
158 void
159 WillOWisp::deactivate()
160 {
161   sound_source.reset(NULL);
162
163   switch (mystate) {
164     case STATE_STOPPED:
165     case STATE_IDLE:
166     case STATE_PATHMOVING:
167     case STATE_PATHMOVING_TRACK:
168       break;
169     case STATE_TRACKING:
170       mystate = STATE_IDLE;
171       break;
172     case STATE_WARPING:
173     case STATE_VANISHING:
174       remove_me();
175       break;
176   }
177 }
178
179 void
180 WillOWisp::vanish()
181 {
182   mystate = STATE_VANISHING;
183   sprite->set_action("vanishing", 1);
184   set_colgroup_active(COLGROUP_DISABLED);
185 }
186
187 bool
188 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
189   Lantern* lantern = dynamic_cast<Lantern*>(&other);
190
191   if (lantern && lantern->is_open())
192     return true;
193
194   if (dynamic_cast<Player*>(&other))
195     return true;
196
197   return false;
198 }
199
200 HitResponse
201 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
202   if(player.is_invincible())
203     return ABORT_MOVE;
204
205   if (mystate != STATE_TRACKING)
206     return ABORT_MOVE;
207
208   mystate = STATE_WARPING;
209   sprite->set_action("warping", 1);
210
211   if(hit_script != "") {
212     std::istringstream stream(hit_script);
213     Sector::current()->run_script(stream, "hit-script");
214   } else {
215     GameSession::current()->respawn(target_sector, target_spawnpoint);
216   }
217   sound_manager->play("sounds/warp.wav");
218
219   return CONTINUE;
220 }
221
222 void
223 WillOWisp::goto_node(int node_no)
224 {
225   walker->goto_node(node_no);
226   if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
227     mystate = STATE_PATHMOVING;
228   }
229 }
230
231 void
232 WillOWisp::start_moving()
233 {
234   walker->start_moving();
235 }
236
237 void
238 WillOWisp::stop_moving()
239 {
240   walker->stop_moving();
241 }
242
243 void
244 WillOWisp::set_state(const std::string& new_state)
245 {
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") {
259     vanish();
260   } else {
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());
265   }
266 }
267
268 void
269 WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
270 {
271   if (name.empty())
272     return;
273
274   std::cout << "Expose me '" << name << "'\n";
275   Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
276   expose_object(vm, table_idx, interface, name);
277 }
278   
279 void
280 WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
281 {
282   if (name.empty())
283     return;
284
285   std::cout << "UnExpose me '" << name << "'\n";
286   Scripting::unexpose_object(vm, table_idx, name);
287 }
288
289 IMPLEMENT_FACTORY(WillOWisp, "willowisp")