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