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