(Tree)WillOWisps no longer collide with closed lamps.
[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
28 static const float FLYSPEED = 64; /**< speed in px per second */
29 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
30 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
31 static const std::string SOUNDFILE = "sounds/willowisp.wav";
32
33 WillOWisp::WillOWisp(const lisp::Lisp& reader)
34   : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main")
35 {
36   reader.get("sector", target_sector);
37   reader.get("spawnpoint", target_spawnpoint);
38
39   countMe = false;
40   sound_manager->preload(SOUNDFILE);
41 }
42
43 void
44 WillOWisp::write(lisp::Writer& writer)
45 {
46   writer.start_list("willowisp");
47
48   writer.write_float("x", start_position.x);
49   writer.write_float("y", start_position.y);
50   writer.write_string("sector", target_sector);
51   writer.write_string("spawnpoint", target_spawnpoint);
52
53   writer.end_list("willowisp");
54 }
55
56 void
57 WillOWisp::draw(DrawingContext& context)
58 {
59   sprite->draw(context, get_pos(), layer);
60
61   context.push_target();
62   context.set_target(DrawingContext::LIGHTMAP);
63
64   sprite->draw(context, get_pos(), layer);
65
66   context.pop_target();
67 }
68
69 void
70 WillOWisp::active_update(float elapsed_time)
71 {
72   Player* player = get_nearest_player();
73   if (!player) return;
74   Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
75   Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
76   Vector dist = (p2 - p1);
77
78   if (mystate == STATE_IDLE) {
79     if (dist.norm() <= TRACK_RANGE) {
80       mystate = STATE_TRACKING;
81     }
82   }
83
84   if (mystate == STATE_TRACKING) {
85     if (dist.norm() <= VANISH_RANGE) {
86       Vector dir = dist.unit();
87       movement = dir*elapsed_time*FLYSPEED;
88     } else {
89       vanish();
90     }
91     sound_source->set_position(get_pos());
92   }
93
94   if (mystate == STATE_WARPING) {
95     if(sprite->animation_done()) {
96       remove_me();
97     }
98   }
99
100   if (mystate == STATE_VANISHING) {
101     Vector dir = dist.unit();
102     movement = dir*elapsed_time*FLYSPEED;
103     if(sprite->animation_done()) {
104       remove_me();
105     }
106   }
107
108 }
109
110 void
111 WillOWisp::activate()
112 {
113   sprite->set_action("idle");
114
115   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
116   sound_source->set_position(get_pos());
117   sound_source->set_looping(true);
118   sound_source->set_gain(2.0);
119   sound_source->set_reference_distance(32);
120   sound_source->play();
121 }
122
123 void
124 WillOWisp::deactivate()
125 {
126   sound_source.reset(NULL);
127
128   switch (mystate) {
129     case STATE_IDLE:
130       break;
131     case STATE_TRACKING:
132       mystate = STATE_IDLE;
133       break;
134     case STATE_WARPING:
135     case STATE_VANISHING:
136       remove_me();
137       break;
138   }
139 }
140
141 void
142 WillOWisp::vanish()
143 {
144   mystate = STATE_VANISHING;
145   sprite->set_action("vanishing", 1);
146   set_group(COLGROUP_DISABLED);
147 }
148
149 bool
150 WillOWisp::collides(GameObject& other, const CollisionHit& ) {
151   Lantern* lantern = dynamic_cast<Lantern*>(&other);
152   if (lantern && lantern->is_open()) return true;
153   if (dynamic_cast<Player*>(&other)) return true;
154   return false;
155 }
156
157 HitResponse
158 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
159   if(player.is_invincible()) return ABORT_MOVE;
160
161   if (mystate != STATE_TRACKING) return ABORT_MOVE;
162
163   mystate = STATE_WARPING;
164   sprite->set_action("warping", 1);
165
166   GameSession::current()->respawn(target_sector, target_spawnpoint);
167   sound_manager->play("sounds/warp.wav");
168
169   return CONTINUE;
170 }
171
172 IMPLEMENT_FACTORY(WillOWisp, "willowisp")