22ec6171b82cf64175cbd14538a8906445cface7
[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 #include "log.hpp"
24 #include "game_session.hpp"
25 #include "object/lantern.hpp"
26 #include "object/player.hpp"
27 #include "scripting/squirrel_util.hpp"
28
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";
33
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")
36 {
37   flyspeed     = FLYSPEED;
38   track_range  = TRACK_RANGE;
39   vanish_range = VANISH_RANGE;
40
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);
48
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));
54   }
55
56   countMe = false;
57   sound_manager->preload(SOUNDFILE);
58 }
59
60 void
61 WillOWisp::draw(DrawingContext& context)
62 {
63   sprite->draw(context, get_pos(), layer);
64
65   context.push_target();
66   context.set_target(DrawingContext::LIGHTMAP);
67
68   sprite->draw(context, get_pos(), layer);
69
70   context.pop_target();
71 }
72
73 void
74 WillOWisp::active_update(float elapsed_time)
75 {
76   Player* player = get_nearest_player();
77   if (!player) return;
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);
81
82   switch(mystate) {
83   case STATE_STOPPED:
84     break;
85
86   case STATE_IDLE:
87     if (dist.norm() <= track_range) {
88       mystate = STATE_TRACKING;
89     }
90     break;
91
92   case STATE_TRACKING:
93     if (dist.norm() <= vanish_range) {
94       Vector dir = dist.unit();
95       movement = dir * elapsed_time * flyspeed;
96     } else {
97       vanish();
98     }
99     sound_source->set_position(get_pos());
100     break;
101
102   case STATE_WARPING:
103     if(sprite->animation_done()) {
104       remove_me();
105     }
106
107   case STATE_VANISHING: {
108     Vector dir = dist.unit();
109     movement = dir * elapsed_time * flyspeed;
110     if(sprite->animation_done()) {
111       remove_me();
112     }
113     break;
114   }
115
116   case STATE_PATHMOVING:
117   case STATE_PATHMOVING_TRACK:
118     if(walker.get() == NULL)
119       return;
120     movement = walker->advance(elapsed_time) - get_pos();
121     if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
122       mystate = STATE_TRACKING;
123     }
124     break;
125
126   default:
127     assert(false);
128   }
129 }
130
131 void
132 WillOWisp::activate()
133 {
134   sprite->set_action("idle");
135
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();
142 }
143
144 void
145 WillOWisp::deactivate()
146 {
147   sound_source.reset(NULL);
148
149   switch (mystate) {
150     case STATE_STOPPED:
151     case STATE_IDLE:
152     case STATE_PATHMOVING:
153     case STATE_PATHMOVING_TRACK:
154       break;
155     case STATE_TRACKING:
156       mystate = STATE_IDLE;
157       break;
158     case STATE_WARPING:
159     case STATE_VANISHING:
160       remove_me();
161       break;
162   }
163 }
164
165 void
166 WillOWisp::vanish()
167 {
168   mystate = STATE_VANISHING;
169   sprite->set_action("vanishing", 1);
170   set_group(COLGROUP_DISABLED);
171 }
172
173 bool
174 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
175   Lantern* lantern = dynamic_cast<Lantern*>(&other);
176
177   if (lantern && lantern->is_open())
178     return true;
179
180   if (dynamic_cast<Player*>(&other))
181     return true;
182
183   return false;
184 }
185
186 HitResponse
187 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
188   if(player.is_invincible())
189     return ABORT_MOVE;
190
191   if (mystate != STATE_TRACKING)
192     return ABORT_MOVE;
193
194   mystate = STATE_WARPING;
195   sprite->set_action("warping", 1);
196
197   if(hit_script != "") {
198     std::istringstream stream(hit_script);
199     Sector::current()->run_script(stream, "hit-script");
200   } else {
201     GameSession::current()->respawn(target_sector, target_spawnpoint);
202   }
203   sound_manager->play("sounds/warp.wav");
204
205   return CONTINUE;
206 }
207
208 void
209 WillOWisp::goto_node(int node_no)
210 {
211   walker->goto_node(node_no);
212   if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
213     mystate = STATE_PATHMOVING;
214     walker->start_moving();
215   }
216 }
217
218 void
219 WillOWisp::set_state(const std::string& new_state)
220 {
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;
233   } else {
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());
238   }
239 }
240
241 void
242 WillOWisp::expose(HSQUIRRELVM vm, SQInteger table_idx)
243 {
244   if (name.empty())
245     return;
246
247   std::cout << "Expose me '" << name << "'\n";
248   Scripting::WillOWisp* interface = static_cast<Scripting::WillOWisp*> (this);
249   expose_object(vm, table_idx, interface, name);
250 }
251   
252 void
253 WillOWisp::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
254 {
255   if (name.empty())
256     return;
257
258   std::cout << "UnExpose me '" << name << "'\n";
259   Scripting::unexpose_object(vm, table_idx, name);
260 }
261
262 IMPLEMENT_FACTORY(WillOWisp, "willowisp")